Coverage Report

Created: 2026-07-29 17:59

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
203k
                                                       BlockType&& block) {
31
203k
    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
203k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
60.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
60.3k
        block.first->record_channel_id(channel_id);
43
142k
    } else {
44
142k
        block->record_channel_id(channel_id);
45
142k
    }
46
47
203k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
203k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
203k
    }
50
203k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
45.6k
                                                       BlockType&& block) {
31
45.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
45.6k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
45.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
45.6k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
45.6k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
45.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
45.5k
    }
50
45.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
142k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
142k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
142k
    }
50
142k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
14.7k
                                                       BlockType&& block) {
31
14.7k
    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
14.7k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
14.7k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
14.7k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
14.7k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
14.6k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
14.6k
    }
50
14.7k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.38M
                                         int channel_id) {
56
1.38M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.38M
    bool all_finished = _running_sink_operators == 0;
60
1.38M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
60.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
60.2k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
142k
        } else {
65
142k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
142k
            data_block->swap(block->_data_block);
67
142k
        }
68
203k
        return true;
69
1.18M
    } else if (all_finished) {
70
1.06M
        *eos = true;
71
1.06M
    } else {
72
118k
        std::unique_lock l(*_m[channel_id]);
73
118k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
19
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
19
                local_state->_shared_state->sub_mem_usage(channel_id,
77
19
                                                          block.first->_allocated_bytes);
78
19
            } else {
79
12
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
12
                data_block->swap(block->_data_block);
81
12
            }
82
31
            return true;
83
31
        }
84
118k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
118k
        local_state->_dependency->block();
86
118k
    }
87
1.18M
    return false;
88
1.38M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
332k
                                         int channel_id) {
56
332k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
332k
    bool all_finished = _running_sink_operators == 0;
60
332k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
45.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
45.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
45.5k
        return true;
69
286k
    } else if (all_finished) {
70
261k
        *eos = true;
71
261k
    } else {
72
25.5k
        std::unique_lock l(*_m[channel_id]);
73
25.5k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
17
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
17
                local_state->_shared_state->sub_mem_usage(channel_id,
77
17
                                                          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
17
            return true;
83
17
        }
84
25.5k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
25.5k
        local_state->_dependency->block();
86
25.5k
    }
87
286k
    return false;
88
332k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.01M
                                         int channel_id) {
56
1.01M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.01M
    bool all_finished = _running_sink_operators == 0;
60
1.01M
    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
142k
        } else {
65
142k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
142k
            data_block->swap(block->_data_block);
67
142k
        }
68
142k
        return true;
69
876k
    } else if (all_finished) {
70
786k
        *eos = true;
71
786k
    } else {
72
90.3k
        std::unique_lock l(*_m[channel_id]);
73
90.3k
        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
12
            } else {
79
12
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
12
                data_block->swap(block->_data_block);
81
12
            }
82
12
            return true;
83
12
        }
84
90.3k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
90.3k
        local_state->_dependency->block();
86
90.3k
    }
87
876k
    return false;
88
1.01M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
37.6k
                                         int channel_id) {
56
37.6k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
37.6k
    bool all_finished = _running_sink_operators == 0;
60
37.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
14.6k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
14.6k
            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
14.6k
        return true;
69
22.9k
    } else if (all_finished) {
70
20.0k
        *eos = true;
71
20.0k
    } else {
72
2.92k
        std::unique_lock l(*_m[channel_id]);
73
2.92k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
2
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
2
                local_state->_shared_state->sub_mem_usage(channel_id,
77
2
                                                          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
2
            return true;
83
2
        }
84
2.92k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
2.92k
        local_state->_dependency->block();
86
2.92k
    }
87
22.9k
    return false;
88
37.6k
}
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
146k
                              SinkInfo& sink_info) {
116
146k
    if (in_block->empty()) {
117
127k
        return Status::OK();
118
127k
    }
119
19.5k
    {
120
19.5k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
19.5k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
19.5k
    }
123
19.5k
    {
124
19.5k
        SCOPED_TIMER(profile.distribute_timer);
125
19.5k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
19.5k
                                    *sink_info.channel_id, sink_info.local_state,
127
19.5k
                                    sink_info.shuffle_idx_to_instance_idx));
128
19.5k
    }
129
130
19.5k
    sink_info.local_state->_memory_used_counter->set(
131
19.5k
            sink_info.local_state->_shared_state->mem_usage);
132
19.5k
    return Status::OK();
133
19.5k
}
134
135
128k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
128k
    PartitionedBlock partitioned_block;
137
128k
    bool eos;
138
128k
    Block block;
139
128k
    _data_queue[source_info.channel_id].set_eos();
140
128k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
128k
                         source_info.channel_id)) {
142
        // do nothing
143
6
    }
144
128k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
149k
                                   SourceInfo&& source_info) {
148
149k
    PartitionedBlock partitioned_block;
149
149k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
149k
                      source_info.channel_id)) {
151
29.9k
        SCOPED_TIMER(profile.copy_data_timer);
152
29.9k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
29.9k
                block, partitioned_block.first->_data_block);
154
29.9k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
37.6k
        do {
156
37.6k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
37.6k
                                       partitioned_block.second.offset_start;
158
37.6k
            auto block_wrapper = partitioned_block.first;
159
37.6k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
37.6k
                                                   offset_start + partitioned_block.second.length));
161
37.6k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
37.6k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
36.0k
                               source_info.channel_id));
164
29.9k
    }
165
149k
    return Status::OK();
166
149k
}
167
168
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
169
                                     Block* block, int channel_id,
170
                                     LocalExchangeSinkLocalState* local_state,
171
19.7k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
19.7k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
19.7k
    const auto rows = cast_set<int32_t>(block->rows());
176
19.7k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
19.7k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
19.7k
    {
179
19.7k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
29.8M
        for (int32_t i = 0; i < rows; ++i) {
181
29.8M
            partition_rows_histogram[channel_ids[i]]++;
182
29.8M
        }
183
172k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
153k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
153k
        }
186
29.8M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
29.8M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
29.8M
            partition_rows_histogram[channel_ids[i]]--;
189
29.8M
        }
190
19.7k
    }
191
192
19.7k
    Block data_block;
193
19.7k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
19.7k
    if (!_free_blocks.try_dequeue(data_block)) {
195
10.3k
        data_block = block->clone_empty();
196
10.3k
    }
197
19.7k
    data_block.swap(*block);
198
19.7k
    new_block_wrapper =
199
19.7k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
19.7k
    if (new_block_wrapper->_data_block.empty()) {
201
0
        return Status::OK();
202
0
    }
203
    /**
204
     * Data are hash-shuffled and distributed to all instances of
205
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
206
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
207
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
208
     */
209
19.7k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
19.7k
    const auto& map = *shuffle_idx_to_instance_idx;
211
19.7k
    int32_t enqueue_rows = 0;
212
153k
    for (const auto& it : map) {
213
153k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
8
                << it.first << " : " << it.second << " " << _num_partitions;
215
153k
        uint32_t start = partition_rows_histogram[it.first];
216
153k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
153k
        if (size > 0) {
218
37.6k
            enqueue_rows += size;
219
37.6k
            _enqueue_data_and_set_ready(
220
37.6k
                    it.second, local_state,
221
37.6k
                    {new_block_wrapper,
222
37.6k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
37.6k
        }
224
153k
    }
225
19.7k
    if (enqueue_rows != rows) [[unlikely]] {
226
1
        fmt::memory_buffer debug_string_buffer;
227
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
228
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
229
3
        for (const auto& it : map) {
230
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
231
3
        }
232
1
        return Status::InternalError(
233
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
234
1
                "rows={}, Detail: {}]",
235
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
236
1
    }
237
238
19.7k
    return Status::OK();
239
19.7k
}
240
241
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
242
0
                                     Block* block, int channel_id) {
243
0
    const auto rows = cast_set<int32_t>(block->rows());
244
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
245
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
246
0
    {
247
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
248
0
        for (int32_t i = 0; i < rows; ++i) {
249
0
            partition_rows_histogram[channel_ids[i]]++;
250
0
        }
251
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
252
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
253
0
        }
254
0
        for (int32_t i = rows - 1; i >= 0; --i) {
255
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
256
0
            partition_rows_histogram[channel_ids[i]]--;
257
0
        }
258
0
    }
259
260
0
    Block data_block;
261
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
262
0
    if (!_free_blocks.try_dequeue(data_block)) {
263
0
        data_block = block->clone_empty();
264
0
    }
265
0
    data_block.swap(*block);
266
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
267
0
    if (new_block_wrapper->_data_block.empty()) {
268
0
        return Status::OK();
269
0
    }
270
0
    for (int i = 0; i < _num_partitions; i++) {
271
0
        uint32_t start = partition_rows_histogram[i];
272
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
273
0
        if (size > 0) {
274
0
            _enqueue_data_and_set_ready(
275
0
                    i, {new_block_wrapper,
276
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
277
0
        }
278
0
    }
279
280
0
    return Status::OK();
281
0
}
282
283
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
284
205k
                                  SinkInfo& sink_info) {
285
205k
    if (in_block->empty()) {
286
64.9k
        return Status::OK();
287
64.9k
    }
288
141k
    Block new_block;
289
141k
    if (!_free_blocks.try_dequeue(new_block)) {
290
57.9k
        new_block = {in_block->clone_empty()};
291
57.9k
    }
292
141k
    new_block.swap(*in_block);
293
141k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
141k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
141k
            std::move(new_block),
296
141k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
141k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
141k
    sink_info.local_state->_memory_used_counter->set(
301
141k
            sink_info.local_state->_shared_state->mem_usage);
302
303
141k
    return Status::OK();
304
205k
}
305
306
389k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
389k
    Block next_block;
308
389k
    BlockWrapperSPtr wrapper;
309
389k
    bool eos;
310
389k
    _data_queue[source_info.channel_id].set_eos();
311
391k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
391k
                         source_info.channel_id)) {
313
        // do nothing
314
2.07k
    }
315
389k
}
316
317
5.50k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
5.50k
    Block next_block;
319
5.50k
    BlockWrapperSPtr wrapper;
320
5.50k
    bool eos;
321
5.50k
    _data_queue[source_info.channel_id].set_eos();
322
5.50k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
5.50k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
5.50k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
618k
                                       Profile&& profile, SourceInfo&& source_info) {
330
618k
    BlockWrapperSPtr next_block;
331
618k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
618k
    return Status::OK();
333
618k
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
2.69k
                                SinkInfo& sink_info) {
337
2.69k
    if (in_block->empty()) {
338
816
        return Status::OK();
339
816
    }
340
1.87k
    Block new_block;
341
1.87k
    if (!_free_blocks.try_dequeue(new_block)) {
342
962
        new_block = {in_block->clone_empty()};
343
962
    }
344
1.87k
    new_block.swap(*in_block);
345
346
1.87k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
1.87k
            std::move(new_block),
348
1.87k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
1.87k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
1.87k
    sink_info.local_state->_memory_used_counter->set(
352
1.87k
            sink_info.local_state->_shared_state->mem_usage);
353
354
1.87k
    return Status::OK();
355
2.69k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
3.40k
                                     Profile&& profile, SourceInfo&& source_info) {
359
3.40k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
3.40k
    BlockWrapperSPtr next_block;
364
3.40k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
3.40k
    return Status::OK();
366
3.40k
}
367
368
90.9k
void ExchangerBase::finalize() {
369
90.9k
    DCHECK(_running_source_operators == 0);
370
90.9k
    Block block;
371
160k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
70.0k
    }
374
90.9k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
3.40k
                                SinkInfo& sink_info) {
378
3.40k
    if (in_block->empty()) {
379
1.43k
        return Status::OK();
380
1.43k
    }
381
1.96k
    Block new_block;
382
1.96k
    if (!_free_blocks.try_dequeue(new_block)) {
383
1.62k
        new_block = {in_block->clone_empty()};
384
1.62k
    }
385
1.96k
    new_block.swap(*in_block);
386
1.96k
    auto wrapper = BlockWrapper::create_shared(
387
1.96k
            std::move(new_block),
388
1.96k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
16.6k
    for (int i = 0; i < _num_partitions; i++) {
390
14.7k
        _enqueue_data_and_set_ready(
391
14.7k
                i, sink_info.local_state,
392
14.7k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
14.7k
    }
394
395
1.96k
    return Status::OK();
396
3.40k
}
397
398
10.0k
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
10.0k
    BroadcastBlock partitioned_block;
400
10.0k
    bool eos;
401
10.0k
    Block block;
402
10.0k
    _data_queue[source_info.channel_id].set_eos();
403
10.0k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
10.0k
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
10.0k
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
27.5k
                                     Profile&& profile, SourceInfo&& source_info) {
411
27.5k
    BroadcastBlock partitioned_block;
412
413
27.5k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
27.5k
                      source_info.channel_id)) {
415
14.6k
        SCOPED_TIMER(profile.copy_data_timer);
416
14.6k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
14.6k
                block, partitioned_block.first->_data_block);
418
14.6k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
14.6k
        auto block_wrapper = partitioned_block.first;
420
14.6k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
14.6k
                                               partitioned_block.second.offset_start,
422
14.6k
                                               partitioned_block.second.length));
423
14.6k
    }
424
425
27.5k
    return Status::OK();
426
27.5k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
1.01k
                                                       SinkInfo& sink_info) {
430
1.01k
    Block new_block;
431
1.01k
    if (!_free_blocks.try_dequeue(new_block)) {
432
391
        new_block = {in_block->clone_empty()};
433
391
    }
434
1.01k
    new_block.swap(*in_block);
435
1.01k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
1.01k
    _enqueue_data_and_set_ready(
437
1.01k
            channel_id, sink_info.local_state,
438
1.01k
            {BlockWrapper::create_shared(
439
1.01k
                     std::move(new_block),
440
18.4E
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
1.01k
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
1.01k
    sink_info.local_state->_memory_used_counter->set(
444
1.01k
            sink_info.local_state->_shared_state->mem_usage);
445
1.01k
    return Status::OK();
446
1.01k
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
1.60k
                                                   SinkInfo& sink_info) {
450
1.60k
    std::vector<uint32_t> channel_ids;
451
1.60k
    const auto num_rows = block->rows();
452
1.60k
    channel_ids.resize(num_rows, 0);
453
1.60k
    if (num_rows <= _num_partitions) {
454
1.04k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
1.04k
    } else {
456
562
        size_t i = 0;
457
25.9k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
25.3k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
25.3k
        }
460
562
        if (i < num_rows - 1) {
461
452
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
452
        }
463
562
    }
464
465
1.60k
    sink_info.local_state->_memory_used_counter->set(
466
1.60k
            sink_info.local_state->_shared_state->mem_usage);
467
1.60k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
1.60k
    return Status::OK();
469
1.60k
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
1.60k
                                                 Block* block, SinkInfo& sink_info) {
474
1.60k
    const auto rows = cast_set<int32_t>(block->rows());
475
1.60k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
1.60k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
1.60k
    {
478
1.60k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
77.5k
        for (int32_t i = 0; i < rows; ++i) {
480
75.9k
            partition_rows_histogram[channel_ids[i]]++;
481
75.9k
        }
482
11.6k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
10.0k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
10.0k
        }
485
486
77.5k
        for (int32_t i = rows - 1; i >= 0; --i) {
487
75.9k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
488
75.9k
            partition_rows_histogram[channel_ids[i]]--;
489
75.9k
        }
490
1.60k
    }
491
1.60k
    Block data_block;
492
1.60k
    if (!_free_blocks.try_dequeue(data_block)) {
493
1.38k
        data_block = block->clone_empty();
494
1.38k
    }
495
1.60k
    data_block.swap(*block);
496
1.60k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
1.60k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
1.60k
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
11.6k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
10.0k
        const uint32_t start = partition_rows_histogram[i];
503
10.0k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
10.0k
        if (size > 0) {
505
6.91k
            _enqueue_data_and_set_ready(
506
6.91k
                    i, sink_info.local_state,
507
6.91k
                    {new_block_wrapper,
508
6.91k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
6.91k
        }
510
10.0k
    }
511
1.60k
    return Status::OK();
512
1.60k
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
5.40k
                                          Profile&& profile, SinkInfo& sink_info) {
516
5.40k
    if (in_block->empty()) {
517
2.77k
        return Status::OK();
518
2.77k
    }
519
2.62k
    if (_is_pass_through) {
520
1.01k
        return _passthrough_sink(state, in_block, sink_info);
521
1.60k
    } else {
522
1.60k
        if (++_total_block >= _num_partitions) {
523
342
            _is_pass_through = true;
524
342
        }
525
1.60k
        return _shuffle_sink(state, in_block, sink_info);
526
1.60k
    }
527
2.62k
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
8.10k
                                               Profile&& profile, SourceInfo&& source_info) {
531
8.10k
    if (!_tmp_block[source_info.channel_id].empty()) {
532
162
        *block = std::move(_tmp_block[source_info.channel_id]);
533
162
        *eos = _tmp_eos[source_info.channel_id];
534
162
        _tmp_block[source_info.channel_id] = {};
535
162
        return Status::OK();
536
162
    }
537
7.93k
    PartitionedBlock partitioned_block;
538
7.93k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
7.93k
                      source_info.channel_id)) {
540
5.22k
        SCOPED_TIMER(profile.copy_data_timer);
541
5.22k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
5.22k
                block, partitioned_block.first->_data_block);
543
5.22k
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
7.92k
        do {
545
7.92k
            if (partitioned_block.second.row_idxs == nullptr) {
546
                // The passthrough path which means the block is not partitioned, we can directly move the block without copying.
547
1.01k
                if (mutable_block.rows() > 0) {
548
162
                    _tmp_block[source_info.channel_id] =
549
162
                            std::move(partitioned_block.first->_data_block);
550
162
                    _tmp_eos[source_info.channel_id] = *eos;
551
162
                    *eos = false;
552
850
                } else {
553
850
                    scoped_mutable_block.restore();
554
850
                    *block = std::move(partitioned_block.first->_data_block);
555
850
                }
556
1.01k
                break;
557
1.01k
            }
558
6.91k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
6.91k
                                       partitioned_block.second.offset_start;
560
6.91k
            auto block_wrapper = partitioned_block.first;
561
6.91k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
6.91k
                                                   offset_start + partitioned_block.second.length));
563
6.91k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
6.91k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
6.91k
                               source_info.channel_id));
566
5.22k
    }
567
7.93k
    return Status::OK();
568
7.93k
}
569
570
3.53k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
3.53k
    PartitionedBlock partitioned_block;
572
3.53k
    bool eos;
573
3.53k
    Block block;
574
3.53k
    _data_queue[source_info.channel_id].set_eos();
575
3.53k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
3.53k
                         source_info.channel_id)) {
577
        // do nothing
578
5
    }
579
3.53k
}
580
581
} // namespace doris