Coverage Report

Created: 2026-06-02 13:57

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