Coverage Report

Created: 2026-05-19 18:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/local_exchanger.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exec/exchange/local_exchanger.h"
19
20
#include "common/cast_set.h"
21
#include "common/status.h"
22
#include "exec/exchange/local_exchange_sink_operator.h"
23
#include "exec/exchange/local_exchange_source_operator.h"
24
#include "exec/partitioner/partitioner.h"
25
26
namespace doris {
27
template <typename BlockType>
28
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
29
                                                       LocalExchangeSinkLocalState* local_state,
30
188k
                                                       BlockType&& block) {
31
188k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
188k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
45.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
45.2k
        block.first->record_channel_id(channel_id);
43
142k
    } else {
44
142k
        block->record_channel_id(channel_id);
45
142k
    }
46
47
188k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
188k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
188k
    }
50
188k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
29.6k
                                                       BlockType&& block) {
31
29.6k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
29.6k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
29.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
29.6k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
29.6k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
29.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
29.5k
    }
50
29.6k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
142k
                                                       BlockType&& block) {
31
142k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
142k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
        block.first->record_channel_id(channel_id);
43
142k
    } else {
44
142k
        block->record_channel_id(channel_id);
45
142k
    }
46
47
143k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
143k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
143k
    }
50
142k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
15.6k
                                                       BlockType&& block) {
31
15.6k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
15.6k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
15.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
15.6k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
15.6k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
15.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
15.5k
    }
50
15.6k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.77M
                                         int channel_id) {
56
1.77M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.77M
    bool all_finished = _running_sink_operators == 0;
60
1.77M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
45.1k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
45.1k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
143k
        } else {
65
143k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
143k
            data_block->swap(block->_data_block);
67
143k
        }
68
188k
        return true;
69
1.58M
    } else if (all_finished) {
70
1.47M
        *eos = true;
71
1.47M
    } else {
72
109k
        std::unique_lock l(*_m[channel_id]);
73
109k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
7
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
7
                local_state->_shared_state->sub_mem_usage(channel_id,
77
7
                                                          block.first->_allocated_bytes);
78
16
            } else {
79
16
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
16
                data_block->swap(block->_data_block);
81
16
            }
82
23
            return true;
83
23
        }
84
109k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
109k
        local_state->_dependency->block();
86
109k
    }
87
1.58M
    return false;
88
1.77M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
398k
                                         int channel_id) {
56
398k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
398k
    bool all_finished = _running_sink_operators == 0;
60
398k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
29.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
29.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
29.5k
        return true;
69
369k
    } else if (all_finished) {
70
352k
        *eos = true;
71
352k
    } else {
72
16.4k
        std::unique_lock l(*_m[channel_id]);
73
16.4k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
6
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
6
                local_state->_shared_state->sub_mem_usage(channel_id,
77
6
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
6
            return true;
83
6
        }
84
16.4k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
16.4k
        local_state->_dependency->block();
86
16.4k
    }
87
369k
    return false;
88
398k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.33M
                                         int channel_id) {
56
1.33M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.33M
    bool all_finished = _running_sink_operators == 0;
60
1.33M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
143k
        } else {
65
143k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
143k
            data_block->swap(block->_data_block);
67
143k
        }
68
143k
        return true;
69
1.19M
    } else if (all_finished) {
70
1.10M
        *eos = true;
71
1.10M
    } else {
72
90.2k
        std::unique_lock l(*_m[channel_id]);
73
90.2k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
                local_state->_shared_state->sub_mem_usage(channel_id,
77
                                                          block.first->_allocated_bytes);
78
16
            } else {
79
16
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
16
                data_block->swap(block->_data_block);
81
16
            }
82
16
            return true;
83
16
        }
84
90.1k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
90.1k
        local_state->_dependency->block();
86
90.1k
    }
87
1.19M
    return false;
88
1.33M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
34.9k
                                         int channel_id) {
56
34.9k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
34.9k
    bool all_finished = _running_sink_operators == 0;
60
34.9k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
15.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
15.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
15.5k
        return true;
69
19.3k
    } else if (all_finished) {
70
16.1k
        *eos = true;
71
16.1k
    } else {
72
3.26k
        std::unique_lock l(*_m[channel_id]);
73
3.26k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
1
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
1
                local_state->_shared_state->sub_mem_usage(channel_id,
77
1
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
1
            return true;
83
1
        }
84
3.26k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
3.26k
        local_state->_dependency->block();
86
3.26k
    }
87
19.3k
    return false;
88
34.9k
}
89
90
template <typename BlockType>
91
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
92
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
93
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
94
0
        block.first->record_channel_id(channel_id);
95
0
    } else {
96
0
        block->record_channel_id(channel_id);
97
0
    }
98
0
    _data_queue[channel_id].enqueue(std::move(block));
99
0
}
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_
Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_
100
101
template <typename BlockType>
102
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
103
20
                                         int channel_id) {
104
20
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
20
    return false;
112
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
113
114
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
115
187k
                              SinkInfo& sink_info) {
116
187k
    if (in_block->empty()) {
117
171k
        return Status::OK();
118
171k
    }
119
15.8k
    {
120
15.8k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
15.8k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
15.8k
    }
123
15.8k
    {
124
15.8k
        SCOPED_TIMER(profile.distribute_timer);
125
15.8k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
15.8k
                                    *sink_info.channel_id, sink_info.local_state,
127
15.8k
                                    sink_info.shuffle_idx_to_instance_idx));
128
15.8k
    }
129
130
15.8k
    sink_info.local_state->_memory_used_counter->set(
131
15.8k
            sink_info.local_state->_shared_state->mem_usage);
132
15.8k
    return Status::OK();
133
15.8k
}
134
135
173k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
173k
    PartitionedBlock partitioned_block;
137
173k
    bool eos;
138
173k
    Block block;
139
173k
    _data_queue[source_info.channel_id].set_eos();
140
173k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
173k
                         source_info.channel_id)) {
142
        // do nothing
143
63
    }
144
173k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
187k
                                   SourceInfo&& source_info) {
148
187k
    PartitionedBlock partitioned_block;
149
187k
    MutableBlock mutable_block;
150
151
187k
    auto get_data = [&]() -> Status {
152
25.7k
        do {
153
25.7k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
25.7k
                                       partitioned_block.second.offset_start;
155
25.7k
            auto block_wrapper = partitioned_block.first;
156
25.7k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
25.7k
                                                   offset_start + partitioned_block.second.length));
158
25.7k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
25.7k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
25.1k
                               source_info.channel_id));
161
21.5k
        return Status::OK();
162
21.5k
    };
163
164
187k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
187k
                      source_info.channel_id)) {
166
21.5k
        SCOPED_TIMER(profile.copy_data_timer);
167
21.5k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
21.5k
                block, partitioned_block.first->_data_block);
169
21.5k
        RETURN_IF_ERROR(get_data());
170
21.5k
    }
171
187k
    return Status::OK();
172
187k
}
173
174
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
175
                                     Block* block, int channel_id,
176
                                     LocalExchangeSinkLocalState* local_state,
177
15.7k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
15.7k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
15.7k
    const auto rows = cast_set<int32_t>(block->rows());
182
15.7k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
15.7k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
15.7k
    {
185
15.7k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
10.7M
        for (int32_t i = 0; i < rows; ++i) {
187
10.7M
            partition_rows_histogram[channel_ids[i]]++;
188
10.7M
        }
189
195k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
179k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
179k
        }
192
10.5M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
10.5M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
10.5M
            partition_rows_histogram[channel_ids[i]]--;
195
10.5M
        }
196
15.7k
    }
197
198
15.7k
    Block data_block;
199
15.7k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
15.7k
    if (!_free_blocks.try_dequeue(data_block)) {
201
9.52k
        data_block = block->clone_empty();
202
9.52k
    }
203
15.7k
    data_block.swap(*block);
204
15.7k
    new_block_wrapper =
205
15.7k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
15.7k
    if (new_block_wrapper->_data_block.empty()) {
207
0
        return Status::OK();
208
0
    }
209
    /**
210
     * Data are hash-shuffled and distributed to all instances of
211
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
212
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
213
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
214
     */
215
15.7k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
15.7k
    const auto& map = *shuffle_idx_to_instance_idx;
217
15.7k
    int32_t enqueue_rows = 0;
218
179k
    for (const auto& it : map) {
219
18.4E
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
18.4E
                << it.first << " : " << it.second << " " << _num_partitions;
221
179k
        uint32_t start = partition_rows_histogram[it.first];
222
179k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
179k
        if (size > 0) {
224
25.8k
            enqueue_rows += size;
225
25.8k
            _enqueue_data_and_set_ready(
226
25.8k
                    it.second, local_state,
227
25.8k
                    {new_block_wrapper,
228
25.8k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
25.8k
        }
230
179k
    }
231
15.7k
    if (enqueue_rows != rows) [[unlikely]] {
232
1
        fmt::memory_buffer debug_string_buffer;
233
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
234
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
235
3
        for (const auto& it : map) {
236
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
237
3
        }
238
1
        return Status::InternalError(
239
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
240
1
                "rows={}, Detail: {}]",
241
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
242
1
    }
243
244
15.7k
    return Status::OK();
245
15.7k
}
246
247
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
248
0
                                     Block* block, int channel_id) {
249
0
    const auto rows = cast_set<int32_t>(block->rows());
250
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
251
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
252
0
    {
253
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
254
0
        for (int32_t i = 0; i < rows; ++i) {
255
0
            partition_rows_histogram[channel_ids[i]]++;
256
0
        }
257
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
258
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
259
0
        }
260
0
        for (int32_t i = rows - 1; i >= 0; --i) {
261
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
262
0
            partition_rows_histogram[channel_ids[i]]--;
263
0
        }
264
0
    }
265
266
0
    Block data_block;
267
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
268
0
    if (!_free_blocks.try_dequeue(data_block)) {
269
0
        data_block = block->clone_empty();
270
0
    }
271
0
    data_block.swap(*block);
272
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
273
0
    if (new_block_wrapper->_data_block.empty()) {
274
0
        return Status::OK();
275
0
    }
276
0
    for (int i = 0; i < _num_partitions; i++) {
277
0
        uint32_t start = partition_rows_histogram[i];
278
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
279
0
        if (size > 0) {
280
0
            _enqueue_data_and_set_ready(
281
0
                    i, {new_block_wrapper,
282
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
283
0
        }
284
0
    }
285
286
0
    return Status::OK();
287
0
}
288
289
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
290
220k
                                  SinkInfo& sink_info) {
291
220k
    if (in_block->empty()) {
292
79.0k
        return Status::OK();
293
79.0k
    }
294
141k
    Block new_block;
295
141k
    if (!_free_blocks.try_dequeue(new_block)) {
296
61.8k
        new_block = {in_block->clone_empty()};
297
61.8k
    }
298
141k
    new_block.swap(*in_block);
299
141k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
141k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
141k
            std::move(new_block),
302
141k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
141k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
141k
    sink_info.local_state->_memory_used_counter->set(
307
141k
            sink_info.local_state->_shared_state->mem_usage);
308
309
141k
    return Status::OK();
310
220k
}
311
312
545k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
545k
    Block next_block;
314
545k
    BlockWrapperSPtr wrapper;
315
545k
    bool eos;
316
545k
    _data_queue[source_info.channel_id].set_eos();
317
547k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
547k
                         source_info.channel_id)) {
319
        // do nothing
320
1.89k
    }
321
545k
}
322
323
7.44k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
7.44k
    Block next_block;
325
7.44k
    BlockWrapperSPtr wrapper;
326
7.44k
    bool eos;
327
7.44k
    _data_queue[source_info.channel_id].set_eos();
328
7.44k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
7.44k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
7.44k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
774k
                                       Profile&& profile, SourceInfo&& source_info) {
336
774k
    BlockWrapperSPtr next_block;
337
774k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
774k
    return Status::OK();
339
774k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
3.04k
                                SinkInfo& sink_info) {
343
3.04k
    if (in_block->empty()) {
344
1.12k
        return Status::OK();
345
1.12k
    }
346
1.92k
    Block new_block;
347
1.92k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.28k
        new_block = {in_block->clone_empty()};
349
1.28k
    }
350
1.92k
    new_block.swap(*in_block);
351
352
1.92k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
1.92k
            std::move(new_block),
354
1.92k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
1.92k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
1.92k
    sink_info.local_state->_memory_used_counter->set(
358
1.92k
            sink_info.local_state->_shared_state->mem_usage);
359
360
1.92k
    return Status::OK();
361
3.04k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
3.49k
                                     Profile&& profile, SourceInfo&& source_info) {
365
3.49k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
3.49k
    BlockWrapperSPtr next_block;
370
3.49k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
3.49k
    return Status::OK();
372
3.49k
}
373
374
101k
void ExchangerBase::finalize() {
375
101k
    DCHECK(_running_source_operators == 0);
376
101k
    Block block;
377
175k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
73.4k
    }
380
101k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
3.39k
                                SinkInfo& sink_info) {
384
3.39k
    if (in_block->empty()) {
385
1.21k
        return Status::OK();
386
1.21k
    }
387
2.18k
    Block new_block;
388
2.18k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.53k
        new_block = {in_block->clone_empty()};
390
1.53k
    }
391
2.18k
    new_block.swap(*in_block);
392
2.18k
    auto wrapper = BlockWrapper::create_shared(
393
2.18k
            std::move(new_block),
394
2.18k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
17.7k
    for (int i = 0; i < _num_partitions; i++) {
396
15.6k
        _enqueue_data_and_set_ready(
397
15.6k
                i, sink_info.local_state,
398
15.6k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
15.6k
    }
400
401
2.18k
    return Status::OK();
402
3.39k
}
403
404
8.04k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
8.04k
    BroadcastBlock partitioned_block;
406
8.04k
    bool eos;
407
8.04k
    Block block;
408
8.04k
    _data_queue[source_info.channel_id].set_eos();
409
8.04k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
8.04k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
8.04k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
26.8k
                                     Profile&& profile, SourceInfo&& source_info) {
417
26.8k
    BroadcastBlock partitioned_block;
418
419
26.8k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
26.8k
                      source_info.channel_id)) {
421
15.5k
        SCOPED_TIMER(profile.copy_data_timer);
422
15.5k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
15.5k
                block, partitioned_block.first->_data_block);
424
15.5k
        auto block_wrapper = partitioned_block.first;
425
15.5k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
15.5k
                                               partitioned_block.second.offset_start,
427
15.5k
                                               partitioned_block.second.length));
428
15.5k
    }
429
430
26.8k
    return Status::OK();
431
26.8k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
597
                                                       SinkInfo& sink_info) {
435
597
    Block new_block;
436
597
    if (!_free_blocks.try_dequeue(new_block)) {
437
346
        new_block = {in_block->clone_empty()};
438
346
    }
439
597
    new_block.swap(*in_block);
440
597
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
597
    _enqueue_data_and_set_ready(
442
597
            channel_id, sink_info.local_state,
443
597
            {BlockWrapper::create_shared(
444
597
                     std::move(new_block),
445
597
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
446
597
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
447
448
597
    sink_info.local_state->_memory_used_counter->set(
449
597
            sink_info.local_state->_shared_state->mem_usage);
450
597
    return Status::OK();
451
597
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
1.58k
                                                   SinkInfo& sink_info) {
455
1.58k
    std::vector<uint32_t> channel_ids;
456
1.58k
    const auto num_rows = block->rows();
457
1.58k
    channel_ids.resize(num_rows, 0);
458
1.58k
    if (num_rows <= _num_partitions) {
459
1.30k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.30k
    } else {
461
280
        size_t i = 0;
462
15.6k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
15.3k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
15.3k
        }
465
280
        if (i < num_rows - 1) {
466
175
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
175
        }
468
280
    }
469
470
1.58k
    sink_info.local_state->_memory_used_counter->set(
471
1.58k
            sink_info.local_state->_shared_state->mem_usage);
472
1.58k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
1.58k
    return Status::OK();
474
1.58k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
1.58k
                                                 Block* block, SinkInfo& sink_info) {
479
1.58k
    const auto rows = cast_set<int32_t>(block->rows());
480
1.58k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
481
1.58k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
482
1.58k
    {
483
1.58k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
60.2k
        for (int32_t i = 0; i < rows; ++i) {
485
58.6k
            partition_rows_histogram[channel_ids[i]]++;
486
58.6k
        }
487
8.94k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
7.35k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
7.35k
        }
490
491
60.2k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
58.6k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
58.6k
            partition_rows_histogram[channel_ids[i]]--;
494
58.6k
        }
495
1.58k
    }
496
1.58k
    Block data_block;
497
1.58k
    if (!_free_blocks.try_dequeue(data_block)) {
498
1.41k
        data_block = block->clone_empty();
499
1.41k
    }
500
1.58k
    data_block.swap(*block);
501
1.58k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
502
1.58k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
503
1.58k
    if (new_block_wrapper->_data_block.empty()) {
504
0
        return Status::OK();
505
0
    }
506
8.94k
    for (int32_t i = 0; i < _num_partitions; i++) {
507
7.35k
        const uint32_t start = partition_rows_histogram[i];
508
7.35k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
509
7.35k
        if (size > 0) {
510
3.17k
            _enqueue_data_and_set_ready(
511
3.17k
                    i, sink_info.local_state,
512
3.17k
                    {new_block_wrapper,
513
3.17k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
514
3.17k
        }
515
7.35k
    }
516
1.58k
    return Status::OK();
517
1.58k
}
518
519
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
520
4.84k
                                          Profile&& profile, SinkInfo& sink_info) {
521
4.84k
    if (in_block->empty()) {
522
2.65k
        return Status::OK();
523
2.65k
    }
524
2.18k
    if (_is_pass_through) {
525
597
        return _passthrough_sink(state, in_block, sink_info);
526
1.58k
    } else {
527
1.58k
        if (++_total_block >= _num_partitions) {
528
326
            _is_pass_through = true;
529
326
        }
530
1.58k
        return _shuffle_sink(state, in_block, sink_info);
531
1.58k
    }
532
2.18k
}
533
534
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
535
5.41k
                                               Profile&& profile, SourceInfo&& source_info) {
536
5.41k
    if (!_tmp_block[source_info.channel_id].empty()) {
537
256
        *block = std::move(_tmp_block[source_info.channel_id]);
538
256
        *eos = _tmp_eos[source_info.channel_id];
539
256
        _tmp_block[source_info.channel_id] = {};
540
256
        return Status::OK();
541
256
    }
542
5.16k
    PartitionedBlock partitioned_block;
543
5.16k
    MutableBlock mutable_block;
544
545
5.16k
    auto get_data = [&]() -> Status {
546
3.75k
        do {
547
3.75k
            if (partitioned_block.second.row_idxs == nullptr) {
548
                // The passthrough path which means the block is not partitioned, we can directly move the block without copying.
549
592
                if (mutable_block.rows() > 0) {
550
256
                    _tmp_block[source_info.channel_id] =
551
256
                            std::move(partitioned_block.first->_data_block);
552
256
                    _tmp_eos[source_info.channel_id] = *eos;
553
256
                    *eos = false;
554
336
                } else {
555
336
                    *block = std::move(partitioned_block.first->_data_block);
556
336
                }
557
592
                break;
558
592
            }
559
3.16k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
560
3.16k
                                       partitioned_block.second.offset_start;
561
3.16k
            auto block_wrapper = partitioned_block.first;
562
3.16k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
563
3.16k
                                                   offset_start + partitioned_block.second.length));
564
3.17k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
565
3.17k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
566
3.17k
                               source_info.channel_id));
567
2.39k
        return Status::OK();
568
2.39k
    };
569
570
5.16k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
571
5.16k
                      source_info.channel_id)) {
572
2.40k
        SCOPED_TIMER(profile.copy_data_timer);
573
2.40k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
574
2.40k
                block, partitioned_block.first->_data_block);
575
2.40k
        RETURN_IF_ERROR(get_data());
576
2.40k
    }
577
5.16k
    return Status::OK();
578
5.16k
}
579
580
3.59k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
581
3.59k
    PartitionedBlock partitioned_block;
582
3.59k
    bool eos;
583
3.59k
    Block block;
584
3.59k
    _data_queue[source_info.channel_id].set_eos();
585
3.59k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
586
3.59k
                         source_info.channel_id)) {
587
        // do nothing
588
5
    }
589
3.59k
}
590
591
} // namespace doris