Coverage Report

Created: 2026-06-04 01:48

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
204k
                                                       BlockType&& block) {
31
204k
    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
204k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
62.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
62.3k
        block.first->record_channel_id(channel_id);
43
142k
    } else {
44
142k
        block->record_channel_id(channel_id);
45
142k
    }
46
47
204k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
204k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
204k
    }
50
204k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
44.2k
                                                       BlockType&& block) {
31
44.2k
    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
44.2k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
44.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
44.2k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
44.2k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
44.1k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
44.1k
    }
50
44.2k
}
_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
18.0k
                                                       BlockType&& block) {
31
18.0k
    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
18.0k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
18.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
18.0k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
18.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
18.0k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
18.0k
    }
50
18.0k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.64M
                                         int channel_id) {
56
1.64M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.64M
    bool all_finished = _running_sink_operators == 0;
60
1.64M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
62.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
62.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
204k
        return true;
69
1.44M
    } else if (all_finished) {
70
1.31M
        *eos = true;
71
1.31M
    } else {
72
123k
        std::unique_lock l(*_m[channel_id]);
73
123k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
16
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
16
                local_state->_shared_state->sub_mem_usage(channel_id,
77
16
                                                          block.first->_allocated_bytes);
78
16
            } else {
79
11
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
11
                data_block->swap(block->_data_block);
81
11
            }
82
27
            return true;
83
27
        }
84
123k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
123k
        local_state->_dependency->block();
86
123k
    }
87
1.44M
    return false;
88
1.64M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
439k
                                         int channel_id) {
56
439k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
439k
    bool all_finished = _running_sink_operators == 0;
60
439k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
44.1k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
44.1k
            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
44.1k
        return true;
69
395k
    } else if (all_finished) {
70
370k
        *eos = true;
71
370k
    } else {
72
25.2k
        std::unique_lock l(*_m[channel_id]);
73
25.2k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
16
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
16
                local_state->_shared_state->sub_mem_usage(channel_id,
77
16
                                                          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
16
            return true;
83
16
        }
84
25.2k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
25.2k
        local_state->_dependency->block();
86
25.2k
    }
87
395k
    return false;
88
439k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.16M
                                         int channel_id) {
56
1.16M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.16M
    bool all_finished = _running_sink_operators == 0;
60
1.16M
    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
1.02M
    } else if (all_finished) {
70
929k
        *eos = true;
71
929k
    } else {
72
93.1k
        std::unique_lock l(*_m[channel_id]);
73
93.1k
        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
11
            } else {
79
11
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
11
                data_block->swap(block->_data_block);
81
11
            }
82
11
            return true;
83
11
        }
84
93.1k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
93.1k
        local_state->_dependency->block();
86
93.1k
    }
87
1.02M
    return false;
88
1.16M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
40.1k
                                         int channel_id) {
56
40.1k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
40.1k
    bool all_finished = _running_sink_operators == 0;
60
40.1k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
18.0k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
18.0k
            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
18.0k
        return true;
69
22.0k
    } else if (all_finished) {
70
17.3k
        *eos = true;
71
17.3k
    } else {
72
4.67k
        std::unique_lock l(*_m[channel_id]);
73
4.67k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
0
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
0
                local_state->_shared_state->sub_mem_usage(channel_id,
77
0
                                                          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
0
            return true;
83
0
        }
84
4.67k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
4.67k
        local_state->_dependency->block();
86
4.67k
    }
87
22.0k
    return false;
88
40.1k
}
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
201k
                              SinkInfo& sink_info) {
116
201k
    if (in_block->empty()) {
117
180k
        return Status::OK();
118
180k
    }
119
20.8k
    {
120
20.8k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
20.8k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
20.8k
    }
123
20.8k
    {
124
20.8k
        SCOPED_TIMER(profile.distribute_timer);
125
20.8k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
20.8k
                                    *sink_info.channel_id, sink_info.local_state,
127
20.8k
                                    sink_info.shuffle_idx_to_instance_idx));
128
20.8k
    }
129
130
20.8k
    sink_info.local_state->_memory_used_counter->set(
131
20.8k
            sink_info.local_state->_shared_state->mem_usage);
132
20.8k
    return Status::OK();
133
20.8k
}
134
135
181k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
181k
    PartitionedBlock partitioned_block;
137
181k
    bool eos;
138
181k
    Block block;
139
181k
    _data_queue[source_info.channel_id].set_eos();
140
181k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
181k
                         source_info.channel_id)) {
142
        // do nothing
143
4
    }
144
181k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
205k
                                   SourceInfo&& source_info) {
148
205k
    PartitionedBlock partitioned_block;
149
205k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
205k
                      source_info.channel_id)) {
151
31.0k
        SCOPED_TIMER(profile.copy_data_timer);
152
31.0k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
31.0k
                block, partitioned_block.first->_data_block);
154
31.0k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
38.8k
        do {
156
38.8k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
38.8k
                                       partitioned_block.second.offset_start;
158
38.8k
            auto block_wrapper = partitioned_block.first;
159
38.8k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
38.8k
                                                   offset_start + partitioned_block.second.length));
161
38.8k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
38.8k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
37.4k
                               source_info.channel_id));
164
31.0k
    }
165
205k
    return Status::OK();
166
205k
}
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
20.7k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
20.7k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
20.7k
    const auto rows = cast_set<int32_t>(block->rows());
176
20.7k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
20.7k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
20.7k
    {
179
20.7k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
20.2M
        for (int32_t i = 0; i < rows; ++i) {
181
20.2M
            partition_rows_histogram[channel_ids[i]]++;
182
20.2M
        }
183
192k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
172k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
172k
        }
186
19.1M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
19.0M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
19.0M
            partition_rows_histogram[channel_ids[i]]--;
189
19.0M
        }
190
20.7k
    }
191
192
20.7k
    Block data_block;
193
20.7k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
20.7k
    if (!_free_blocks.try_dequeue(data_block)) {
195
11.8k
        data_block = block->clone_empty();
196
11.8k
    }
197
20.7k
    data_block.swap(*block);
198
20.7k
    new_block_wrapper =
199
20.7k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
20.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
20.7k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
20.7k
    const auto& map = *shuffle_idx_to_instance_idx;
211
20.7k
    int32_t enqueue_rows = 0;
212
172k
    for (const auto& it : map) {
213
172k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
12
                << it.first << " : " << it.second << " " << _num_partitions;
215
172k
        uint32_t start = partition_rows_histogram[it.first];
216
172k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
172k
        if (size > 0) {
218
38.8k
            enqueue_rows += size;
219
38.8k
            _enqueue_data_and_set_ready(
220
38.8k
                    it.second, local_state,
221
38.8k
                    {new_block_wrapper,
222
38.8k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
38.8k
        }
224
172k
    }
225
20.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
20.7k
    return Status::OK();
239
20.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
216k
                                  SinkInfo& sink_info) {
285
216k
    if (in_block->empty()) {
286
76.0k
        return Status::OK();
287
76.0k
    }
288
140k
    Block new_block;
289
140k
    if (!_free_blocks.try_dequeue(new_block)) {
290
60.0k
        new_block = {in_block->clone_empty()};
291
60.0k
    }
292
140k
    new_block.swap(*in_block);
293
140k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
140k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
140k
            std::move(new_block),
296
140k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
140k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
140k
    sink_info.local_state->_memory_used_counter->set(
301
140k
            sink_info.local_state->_shared_state->mem_usage);
302
303
140k
    return Status::OK();
304
216k
}
305
306
459k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
459k
    Block next_block;
308
459k
    BlockWrapperSPtr wrapper;
309
459k
    bool eos;
310
459k
    _data_queue[source_info.channel_id].set_eos();
311
459k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
459k
                         source_info.channel_id)) {
313
        // do nothing
314
701
    }
315
459k
}
316
317
6.97k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
6.97k
    Block next_block;
319
6.97k
    BlockWrapperSPtr wrapper;
320
6.97k
    bool eos;
321
6.97k
    _data_queue[source_info.channel_id].set_eos();
322
6.97k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
6.97k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
6.97k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
692k
                                       Profile&& profile, SourceInfo&& source_info) {
330
692k
    BlockWrapperSPtr next_block;
331
692k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
692k
    return Status::OK();
333
692k
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
2.64k
                                SinkInfo& sink_info) {
337
2.64k
    if (in_block->empty()) {
338
891
        return Status::OK();
339
891
    }
340
1.75k
    Block new_block;
341
1.75k
    if (!_free_blocks.try_dequeue(new_block)) {
342
1.00k
        new_block = {in_block->clone_empty()};
343
1.00k
    }
344
1.75k
    new_block.swap(*in_block);
345
346
1.75k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
1.75k
            std::move(new_block),
348
18.4E
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
1.75k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
1.75k
    sink_info.local_state->_memory_used_counter->set(
352
1.75k
            sink_info.local_state->_shared_state->mem_usage);
353
354
1.75k
    return Status::OK();
355
2.64k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
3.18k
                                     Profile&& profile, SourceInfo&& source_info) {
359
3.18k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
3.18k
    BlockWrapperSPtr next_block;
364
3.18k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
3.18k
    return Status::OK();
366
3.18k
}
367
368
106k
void ExchangerBase::finalize() {
369
106k
    DCHECK(_running_source_operators == 0);
370
106k
    Block block;
371
181k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
74.5k
    }
374
106k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
4.08k
                                SinkInfo& sink_info) {
378
4.08k
    if (in_block->empty()) {
379
1.51k
        return Status::OK();
380
1.51k
    }
381
2.56k
    Block new_block;
382
2.56k
    if (!_free_blocks.try_dequeue(new_block)) {
383
1.92k
        new_block = {in_block->clone_empty()};
384
1.92k
    }
385
2.56k
    new_block.swap(*in_block);
386
2.56k
    auto wrapper = BlockWrapper::create_shared(
387
2.56k
            std::move(new_block),
388
2.56k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
20.6k
    for (int i = 0; i < _num_partitions; i++) {
390
18.0k
        _enqueue_data_and_set_ready(
391
18.0k
                i, sink_info.local_state,
392
18.0k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
18.0k
    }
394
395
2.56k
    return Status::OK();
396
4.08k
}
397
398
8.68k
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
8.68k
    BroadcastBlock partitioned_block;
400
8.68k
    bool eos;
401
8.68k
    Block block;
402
8.68k
    _data_queue[source_info.channel_id].set_eos();
403
8.68k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
8.68k
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
8.68k
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
31.3k
                                     Profile&& profile, SourceInfo&& source_info) {
411
31.3k
    BroadcastBlock partitioned_block;
412
413
31.3k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
31.3k
                      source_info.channel_id)) {
415
18.0k
        SCOPED_TIMER(profile.copy_data_timer);
416
18.0k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
18.0k
                block, partitioned_block.first->_data_block);
418
18.0k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
18.0k
        auto block_wrapper = partitioned_block.first;
420
18.0k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
18.0k
                                               partitioned_block.second.offset_start,
422
18.0k
                                               partitioned_block.second.length));
423
18.0k
    }
424
425
31.3k
    return Status::OK();
426
31.3k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
862
                                                       SinkInfo& sink_info) {
430
862
    Block new_block;
431
862
    if (!_free_blocks.try_dequeue(new_block)) {
432
536
        new_block = {in_block->clone_empty()};
433
536
    }
434
862
    new_block.swap(*in_block);
435
862
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
862
    _enqueue_data_and_set_ready(
437
862
            channel_id, sink_info.local_state,
438
862
            {BlockWrapper::create_shared(
439
862
                     std::move(new_block),
440
862
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
862
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
862
    sink_info.local_state->_memory_used_counter->set(
444
862
            sink_info.local_state->_shared_state->mem_usage);
445
862
    return Status::OK();
446
862
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
1.97k
                                                   SinkInfo& sink_info) {
450
1.97k
    std::vector<uint32_t> channel_ids;
451
1.97k
    const auto num_rows = block->rows();
452
1.97k
    channel_ids.resize(num_rows, 0);
453
1.97k
    if (num_rows <= _num_partitions) {
454
1.33k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
1.33k
    } else {
456
634
        size_t i = 0;
457
36.1k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
35.5k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
35.5k
        }
460
634
        if (i < num_rows - 1) {
461
370
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
370
        }
463
634
    }
464
465
1.97k
    sink_info.local_state->_memory_used_counter->set(
466
1.97k
            sink_info.local_state->_shared_state->mem_usage);
467
1.97k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
1.97k
    return Status::OK();
469
1.97k
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
1.97k
                                                 Block* block, SinkInfo& sink_info) {
474
1.97k
    const auto rows = cast_set<int32_t>(block->rows());
475
1.97k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
1.97k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
1.97k
    {
478
1.97k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
110k
        for (int32_t i = 0; i < rows; ++i) {
480
108k
            partition_rows_histogram[channel_ids[i]]++;
481
108k
        }
482
10.6k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
8.65k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
8.65k
        }
485
486
110k
        for (int32_t i = rows - 1; i >= 0; --i) {
487
108k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
488
108k
            partition_rows_histogram[channel_ids[i]]--;
489
108k
        }
490
1.97k
    }
491
1.97k
    Block data_block;
492
1.97k
    if (!_free_blocks.try_dequeue(data_block)) {
493
1.72k
        data_block = block->clone_empty();
494
1.72k
    }
495
1.97k
    data_block.swap(*block);
496
1.97k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
1.97k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
1.97k
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
10.6k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
8.65k
        const uint32_t start = partition_rows_histogram[i];
503
8.65k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
8.65k
        if (size > 0) {
505
4.46k
            _enqueue_data_and_set_ready(
506
4.46k
                    i, sink_info.local_state,
507
4.46k
                    {new_block_wrapper,
508
4.46k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
4.46k
        }
510
8.65k
    }
511
1.97k
    return Status::OK();
512
1.97k
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
5.59k
                                          Profile&& profile, SinkInfo& sink_info) {
516
5.59k
    if (in_block->empty()) {
517
2.76k
        return Status::OK();
518
2.76k
    }
519
2.83k
    if (_is_pass_through) {
520
862
        return _passthrough_sink(state, in_block, sink_info);
521
1.97k
    } else {
522
1.97k
        if (++_total_block >= _num_partitions) {
523
486
            _is_pass_through = true;
524
486
        }
525
1.97k
        return _shuffle_sink(state, in_block, sink_info);
526
1.97k
    }
527
2.83k
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
6.46k
                                               Profile&& profile, SourceInfo&& source_info) {
531
6.46k
    if (!_tmp_block[source_info.channel_id].empty()) {
532
375
        *block = std::move(_tmp_block[source_info.channel_id]);
533
375
        *eos = _tmp_eos[source_info.channel_id];
534
375
        _tmp_block[source_info.channel_id] = {};
535
375
        return Status::OK();
536
375
    }
537
6.08k
    PartitionedBlock partitioned_block;
538
6.08k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
6.08k
                      source_info.channel_id)) {
540
3.27k
        SCOPED_TIMER(profile.copy_data_timer);
541
3.27k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
3.27k
                block, partitioned_block.first->_data_block);
543
3.27k
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
5.31k
        do {
545
5.31k
            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
859
                if (mutable_block.rows() > 0) {
548
375
                    _tmp_block[source_info.channel_id] =
549
375
                            std::move(partitioned_block.first->_data_block);
550
375
                    _tmp_eos[source_info.channel_id] = *eos;
551
375
                    *eos = false;
552
484
                } else {
553
484
                    scoped_mutable_block.restore();
554
484
                    *block = std::move(partitioned_block.first->_data_block);
555
484
                }
556
859
                break;
557
859
            }
558
4.45k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
4.45k
                                       partitioned_block.second.offset_start;
560
4.45k
            auto block_wrapper = partitioned_block.first;
561
4.45k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
4.45k
                                                   offset_start + partitioned_block.second.length));
563
4.45k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
4.45k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
4.45k
                               source_info.channel_id));
566
3.27k
    }
567
6.08k
    return Status::OK();
568
6.08k
}
569
570
3.84k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
3.84k
    PartitionedBlock partitioned_block;
572
3.84k
    bool eos;
573
3.84k
    Block block;
574
3.84k
    _data_queue[source_info.channel_id].set_eos();
575
3.85k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
3.85k
                         source_info.channel_id)) {
577
        // do nothing
578
5
    }
579
3.84k
}
580
581
} // namespace doris