Coverage Report

Created: 2026-08-14 04:50

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
301k
                                                       BlockType&& block) {
31
301k
    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
301k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
90.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
90.6k
        block.first->record_channel_id(channel_id);
43
210k
    } else {
44
210k
        block->record_channel_id(channel_id);
45
210k
    }
46
47
301k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
301k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
301k
    }
50
301k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
68.9k
                                                       BlockType&& block) {
31
68.9k
    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
68.9k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
68.9k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
68.9k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
68.9k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
68.8k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
68.8k
    }
50
68.9k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
210k
                                                       BlockType&& block) {
31
210k
    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
210k
    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
210k
    } else {
44
210k
        block->record_channel_id(channel_id);
45
210k
    }
46
47
210k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
210k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
210k
    }
50
210k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
21.6k
                                                       BlockType&& block) {
31
21.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
21.6k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
21.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
21.6k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
21.6k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
21.6k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
21.6k
    }
50
21.6k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
2.18M
                                         int channel_id) {
56
2.18M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
2.18M
    bool all_finished = _running_sink_operators == 0;
60
2.18M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
90.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
90.3k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
210k
        } else {
65
210k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
210k
            data_block->swap(block->_data_block);
67
210k
        }
68
301k
        return true;
69
1.88M
    } else if (all_finished) {
70
1.69M
        *eos = true;
71
1.69M
    } else {
72
190k
        std::unique_lock l(*_m[channel_id]);
73
190k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
34
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
34
                local_state->_shared_state->sub_mem_usage(channel_id,
77
34
                                                          block.first->_allocated_bytes);
78
34
            } else {
79
9
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
9
                data_block->swap(block->_data_block);
81
9
            }
82
43
            return true;
83
43
        }
84
190k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
190k
        local_state->_dependency->block();
86
190k
    }
87
1.88M
    return false;
88
2.18M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
443k
                                         int channel_id) {
56
443k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
443k
    bool all_finished = _running_sink_operators == 0;
60
443k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
68.7k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
68.7k
            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
68.7k
        return true;
69
374k
    } else if (all_finished) {
70
341k
        *eos = true;
71
341k
    } else {
72
33.3k
        std::unique_lock l(*_m[channel_id]);
73
33.3k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
32
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
32
                local_state->_shared_state->sub_mem_usage(channel_id,
77
32
                                                          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
32
            return true;
83
32
        }
84
33.3k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
33.3k
        local_state->_dependency->block();
86
33.3k
    }
87
374k
    return false;
88
443k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.68M
                                         int channel_id) {
56
1.68M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.68M
    bool all_finished = _running_sink_operators == 0;
60
1.68M
    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
210k
        } else {
65
210k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
210k
            data_block->swap(block->_data_block);
67
210k
        }
68
210k
        return true;
69
1.47M
    } else if (all_finished) {
70
1.32M
        *eos = true;
71
1.32M
    } else {
72
153k
        std::unique_lock l(*_m[channel_id]);
73
153k
        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
9
            } else {
79
9
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
9
                data_block->swap(block->_data_block);
81
9
            }
82
9
            return true;
83
9
        }
84
153k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
153k
        local_state->_dependency->block();
86
153k
    }
87
1.47M
    return false;
88
1.68M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
51.7k
                                         int channel_id) {
56
51.7k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
51.6k
    bool all_finished = _running_sink_operators == 0;
60
51.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
21.6k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
21.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
21.6k
        return true;
69
30.0k
    } else if (all_finished) {
70
25.9k
        *eos = true;
71
25.9k
    } else {
72
4.12k
        std::unique_lock l(*_m[channel_id]);
73
4.12k
        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
4.11k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
4.11k
        local_state->_dependency->block();
86
4.11k
    }
87
30.0k
    return false;
88
51.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
192k
                              SinkInfo& sink_info) {
116
192k
    if (in_block->empty()) {
117
165k
        return Status::OK();
118
165k
    }
119
26.6k
    {
120
26.6k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
26.6k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
26.6k
    }
123
26.6k
    {
124
26.6k
        SCOPED_TIMER(profile.distribute_timer);
125
26.6k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
26.6k
                                    *sink_info.channel_id, sink_info.local_state,
127
26.6k
                                    sink_info.shuffle_idx_to_instance_idx));
128
26.6k
    }
129
130
26.6k
    sink_info.local_state->_memory_used_counter->set(
131
26.6k
            sink_info.local_state->_shared_state->mem_usage);
132
26.6k
    return Status::OK();
133
26.6k
}
134
135
166k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
166k
    PartitionedBlock partitioned_block;
137
166k
    bool eos;
138
166k
    Block block;
139
166k
    _data_queue[source_info.channel_id].set_eos();
140
166k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
166k
                         source_info.channel_id)) {
142
        // do nothing
143
31
    }
144
166k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
200k
                                   SourceInfo&& source_info) {
148
200k
    PartitionedBlock partitioned_block;
149
200k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
200k
                      source_info.channel_id)) {
151
42.5k
        SCOPED_TIMER(profile.copy_data_timer);
152
42.5k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
42.5k
                block, partitioned_block.first->_data_block);
154
42.5k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
61.9k
        do {
156
61.9k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
61.9k
                                       partitioned_block.second.offset_start;
158
61.9k
            auto block_wrapper = partitioned_block.first;
159
61.9k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
61.9k
                                                   offset_start + partitioned_block.second.length));
161
61.9k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
61.9k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
57.6k
                               source_info.channel_id));
164
42.5k
    }
165
200k
    return Status::OK();
166
200k
}
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
26.8k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
26.8k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
26.8k
    const auto rows = cast_set<int32_t>(block->rows());
176
26.8k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
26.8k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
26.8k
    {
179
26.8k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
56.0M
        for (int32_t i = 0; i < rows; ++i) {
181
55.9M
            partition_rows_histogram[channel_ids[i]]++;
182
55.9M
        }
183
245k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
218k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
218k
        }
186
54.7M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
54.7M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
54.7M
            partition_rows_histogram[channel_ids[i]]--;
189
54.7M
        }
190
26.8k
    }
191
192
26.8k
    Block data_block;
193
26.8k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
26.8k
    if (!_free_blocks.try_dequeue(data_block)) {
195
14.5k
        data_block = block->clone_empty();
196
14.5k
    }
197
26.8k
    data_block.swap(*block);
198
26.8k
    new_block_wrapper =
199
26.8k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
26.8k
    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
26.8k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
26.8k
    const auto& map = *shuffle_idx_to_instance_idx;
211
26.8k
    int32_t enqueue_rows = 0;
212
218k
    for (const auto& it : map) {
213
18.4E
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
18.4E
                << it.first << " : " << it.second << " " << _num_partitions;
215
218k
        uint32_t start = partition_rows_histogram[it.first];
216
218k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
218k
        if (size > 0) {
218
62.1k
            enqueue_rows += size;
219
62.1k
            _enqueue_data_and_set_ready(
220
62.1k
                    it.second, local_state,
221
62.1k
                    {new_block_wrapper,
222
62.1k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
62.1k
        }
224
218k
    }
225
26.8k
    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
26.8k
    return Status::OK();
239
26.8k
}
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
302k
                                  SinkInfo& sink_info) {
285
302k
    if (in_block->empty()) {
286
96.1k
        return Status::OK();
287
96.1k
    }
288
206k
    Block new_block;
289
206k
    if (!_free_blocks.try_dequeue(new_block)) {
290
84.0k
        new_block = {in_block->clone_empty()};
291
84.0k
    }
292
206k
    new_block.swap(*in_block);
293
206k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
206k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
206k
            std::move(new_block),
296
18.4E
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
206k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
206k
    sink_info.local_state->_memory_used_counter->set(
301
206k
            sink_info.local_state->_shared_state->mem_usage);
302
303
206k
    return Status::OK();
304
302k
}
305
306
652k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
652k
    Block next_block;
308
652k
    BlockWrapperSPtr wrapper;
309
652k
    bool eos;
310
652k
    _data_queue[source_info.channel_id].set_eos();
311
652k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
652k
                         source_info.channel_id)) {
313
        // do nothing
314
38
    }
315
652k
}
316
317
13.9k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
13.9k
    Block next_block;
319
13.9k
    BlockWrapperSPtr wrapper;
320
13.9k
    bool eos;
321
13.9k
    _data_queue[source_info.channel_id].set_eos();
322
13.9k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
13.9k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
13.9k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
1.00M
                                       Profile&& profile, SourceInfo&& source_info) {
330
1.00M
    BlockWrapperSPtr next_block;
331
1.00M
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
1.00M
    return Status::OK();
333
1.00M
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
5.58k
                                SinkInfo& sink_info) {
337
5.58k
    if (in_block->empty()) {
338
1.74k
        return Status::OK();
339
1.74k
    }
340
3.83k
    Block new_block;
341
3.83k
    if (!_free_blocks.try_dequeue(new_block)) {
342
2.01k
        new_block = {in_block->clone_empty()};
343
2.01k
    }
344
3.83k
    new_block.swap(*in_block);
345
346
3.83k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
3.83k
            std::move(new_block),
348
3.83k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
3.83k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
3.83k
    sink_info.local_state->_memory_used_counter->set(
352
3.83k
            sink_info.local_state->_shared_state->mem_usage);
353
354
3.83k
    return Status::OK();
355
5.58k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
6.98k
                                     Profile&& profile, SourceInfo&& source_info) {
359
6.98k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
6.98k
    BlockWrapperSPtr next_block;
364
6.98k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
6.98k
    return Status::OK();
366
6.98k
}
367
368
129k
void ExchangerBase::finalize() {
369
129k
    DCHECK(_running_source_operators == 0);
370
129k
    Block block;
371
232k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
102k
    }
374
129k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
4.26k
                                SinkInfo& sink_info) {
378
4.26k
    if (in_block->empty()) {
379
1.57k
        return Status::OK();
380
1.57k
    }
381
2.69k
    Block new_block;
382
2.69k
    if (!_free_blocks.try_dequeue(new_block)) {
383
1.94k
        new_block = {in_block->clone_empty()};
384
1.94k
    }
385
2.69k
    new_block.swap(*in_block);
386
2.69k
    auto wrapper = BlockWrapper::create_shared(
387
2.69k
            std::move(new_block),
388
2.69k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
24.3k
    for (int i = 0; i < _num_partitions; i++) {
390
21.6k
        _enqueue_data_and_set_ready(
391
21.6k
                i, sink_info.local_state,
392
21.6k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
21.6k
    }
394
395
2.69k
    return Status::OK();
396
4.26k
}
397
398
12.9k
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
12.9k
    BroadcastBlock partitioned_block;
400
12.9k
    bool eos;
401
12.9k
    Block block;
402
12.9k
    _data_queue[source_info.channel_id].set_eos();
403
12.9k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
12.9k
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
12.9k
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
38.6k
                                     Profile&& profile, SourceInfo&& source_info) {
411
38.6k
    BroadcastBlock partitioned_block;
412
413
38.6k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
38.6k
                      source_info.channel_id)) {
415
21.6k
        SCOPED_TIMER(profile.copy_data_timer);
416
21.6k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
21.6k
                block, partitioned_block.first->_data_block);
418
21.6k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
21.6k
        auto block_wrapper = partitioned_block.first;
420
21.6k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
21.6k
                                               partitioned_block.second.offset_start,
422
21.6k
                                               partitioned_block.second.length));
423
21.6k
    }
424
425
38.6k
    return Status::OK();
426
38.6k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
889
                                                       SinkInfo& sink_info) {
430
889
    Block new_block;
431
889
    if (!_free_blocks.try_dequeue(new_block)) {
432
277
        new_block = {in_block->clone_empty()};
433
277
    }
434
889
    new_block.swap(*in_block);
435
889
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
889
    _enqueue_data_and_set_ready(
437
889
            channel_id, sink_info.local_state,
438
889
            {BlockWrapper::create_shared(
439
889
                     std::move(new_block),
440
889
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
889
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
889
    sink_info.local_state->_memory_used_counter->set(
444
889
            sink_info.local_state->_shared_state->mem_usage);
445
889
    return Status::OK();
446
889
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
2.20k
                                                   SinkInfo& sink_info) {
450
2.20k
    std::vector<uint32_t> channel_ids;
451
2.20k
    const auto num_rows = block->rows();
452
2.20k
    channel_ids.resize(num_rows, 0);
453
2.20k
    if (num_rows <= _num_partitions) {
454
1.61k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
1.61k
    } else {
456
594
        size_t i = 0;
457
28.2k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
27.6k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
27.6k
        }
460
594
        if (i < num_rows - 1) {
461
390
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
390
        }
463
594
    }
464
465
2.20k
    sink_info.local_state->_memory_used_counter->set(
466
2.20k
            sink_info.local_state->_shared_state->mem_usage);
467
2.20k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
2.20k
    return Status::OK();
469
2.20k
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
2.20k
                                                 Block* block, SinkInfo& sink_info) {
474
2.20k
    const auto rows = cast_set<int32_t>(block->rows());
475
2.20k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
2.20k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
2.20k
    {
478
2.20k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
86.3k
        for (int32_t i = 0; i < rows; ++i) {
480
84.1k
            partition_rows_histogram[channel_ids[i]]++;
481
84.1k
        }
482
13.2k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
11.0k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
11.0k
        }
485
486
86.1k
        for (int32_t i = rows - 1; i >= 0; --i) {
487
83.9k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
488
83.9k
            partition_rows_histogram[channel_ids[i]]--;
489
83.9k
        }
490
2.20k
    }
491
2.20k
    Block data_block;
492
2.20k
    if (!_free_blocks.try_dequeue(data_block)) {
493
1.96k
        data_block = block->clone_empty();
494
1.96k
    }
495
2.20k
    data_block.swap(*block);
496
2.20k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
2.20k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
2.20k
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
13.2k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
11.0k
        const uint32_t start = partition_rows_histogram[i];
503
11.0k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
11.0k
        if (size > 0) {
505
5.91k
            _enqueue_data_and_set_ready(
506
5.91k
                    i, sink_info.local_state,
507
5.91k
                    {new_block_wrapper,
508
5.91k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
5.91k
        }
510
11.0k
    }
511
2.20k
    return Status::OK();
512
2.20k
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
6.68k
                                          Profile&& profile, SinkInfo& sink_info) {
516
6.68k
    if (in_block->empty()) {
517
3.58k
        return Status::OK();
518
3.58k
    }
519
3.10k
    if (_is_pass_through) {
520
889
        return _passthrough_sink(state, in_block, sink_info);
521
2.21k
    } else {
522
2.21k
        if (++_total_block >= _num_partitions) {
523
479
            _is_pass_through = true;
524
479
        }
525
2.21k
        return _shuffle_sink(state, in_block, sink_info);
526
2.21k
    }
527
3.10k
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
7.67k
                                               Profile&& profile, SourceInfo&& source_info) {
531
7.67k
    if (!_tmp_block[source_info.channel_id].empty()) {
532
161
        *block = std::move(_tmp_block[source_info.channel_id]);
533
161
        *eos = _tmp_eos[source_info.channel_id];
534
161
        _tmp_block[source_info.channel_id] = {};
535
161
        return Status::OK();
536
161
    }
537
7.51k
    PartitionedBlock partitioned_block;
538
7.51k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
7.51k
                      source_info.channel_id)) {
540
4.08k
        SCOPED_TIMER(profile.copy_data_timer);
541
4.08k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
4.08k
                block, partitioned_block.first->_data_block);
543
4.08k
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
6.79k
        do {
545
6.79k
            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
885
                if (mutable_block.rows() > 0) {
548
161
                    _tmp_block[source_info.channel_id] =
549
161
                            std::move(partitioned_block.first->_data_block);
550
161
                    _tmp_eos[source_info.channel_id] = *eos;
551
161
                    *eos = false;
552
724
                } else {
553
724
                    scoped_mutable_block.restore();
554
724
                    *block = std::move(partitioned_block.first->_data_block);
555
724
                }
556
885
                break;
557
885
            }
558
5.90k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
5.90k
                                       partitioned_block.second.offset_start;
560
5.90k
            auto block_wrapper = partitioned_block.first;
561
5.90k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
5.90k
                                                   offset_start + partitioned_block.second.length));
563
5.90k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
5.90k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
5.90k
                               source_info.channel_id));
566
4.08k
    }
567
7.51k
    return Status::OK();
568
7.51k
}
569
570
4.65k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
4.65k
    PartitionedBlock partitioned_block;
572
4.65k
    bool eos;
573
4.65k
    Block block;
574
4.65k
    _data_queue[source_info.channel_id].set_eos();
575
4.65k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
4.65k
                         source_info.channel_id)) {
577
        // do nothing
578
5
    }
579
4.65k
}
580
581
} // namespace doris