Coverage Report

Created: 2026-06-23 14:43

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