Coverage Report

Created: 2026-07-29 06:31

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
299k
                                                       BlockType&& block) {
31
299k
    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
299k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
86.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
86.6k
        block.first->record_channel_id(channel_id);
43
212k
    } else {
44
212k
        block->record_channel_id(channel_id);
45
212k
    }
46
47
299k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
299k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
299k
    }
50
299k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
59.4k
                                                       BlockType&& block) {
31
59.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
59.4k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
59.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
59.4k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
59.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
59.3k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
59.3k
    }
50
59.4k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
212k
                                                       BlockType&& block) {
31
212k
    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
212k
    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
212k
    } else {
44
212k
        block->record_channel_id(channel_id);
45
212k
    }
46
47
213k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
213k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
213k
    }
50
212k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
27.2k
                                                       BlockType&& block) {
31
27.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
27.2k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
27.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
27.2k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
27.2k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
27.2k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
27.2k
    }
50
27.2k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
2.11M
                                         int channel_id) {
56
2.11M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
2.11M
    bool all_finished = _running_sink_operators == 0;
60
2.11M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
86.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
86.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
213k
        } else {
65
213k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
213k
            data_block->swap(block->_data_block);
67
213k
        }
68
299k
        return true;
69
1.81M
    } else if (all_finished) {
70
1.63M
        *eos = true;
71
1.63M
    } else {
72
178k
        std::unique_lock l(*_m[channel_id]);
73
178k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
18
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
18
                local_state->_shared_state->sub_mem_usage(channel_id,
77
18
                                                          block.first->_allocated_bytes);
78
18
            } else {
79
18
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
18
                data_block->swap(block->_data_block);
81
18
            }
82
36
            return true;
83
36
        }
84
178k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
178k
        local_state->_dependency->block();
86
178k
    }
87
1.81M
    return false;
88
2.11M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
406k
                                         int channel_id) {
56
406k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
406k
    bool all_finished = _running_sink_operators == 0;
60
406k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
59.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
59.3k
            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
59.3k
        return true;
69
347k
    } else if (all_finished) {
70
314k
        *eos = true;
71
314k
    } else {
72
33.2k
        std::unique_lock l(*_m[channel_id]);
73
33.2k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
11
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
11
                local_state->_shared_state->sub_mem_usage(channel_id,
77
11
                                                          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
11
            return true;
83
11
        }
84
33.2k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
33.2k
        local_state->_dependency->block();
86
33.2k
    }
87
347k
    return false;
88
406k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.65M
                                         int channel_id) {
56
1.65M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.65M
    bool all_finished = _running_sink_operators == 0;
60
1.65M
    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
213k
        } else {
65
213k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
213k
            data_block->swap(block->_data_block);
67
213k
        }
68
213k
        return true;
69
1.43M
    } else if (all_finished) {
70
1.30M
        *eos = true;
71
1.30M
    } else {
72
137k
        std::unique_lock l(*_m[channel_id]);
73
137k
        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
18
            } else {
79
18
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
18
                data_block->swap(block->_data_block);
81
18
            }
82
18
            return true;
83
18
        }
84
137k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
137k
        local_state->_dependency->block();
86
137k
    }
87
1.43M
    return false;
88
1.65M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
58.6k
                                         int channel_id) {
56
58.6k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
58.6k
    bool all_finished = _running_sink_operators == 0;
60
58.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
27.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
27.2k
            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
27.2k
        return true;
69
31.4k
    } else if (all_finished) {
70
23.5k
        *eos = true;
71
23.5k
    } else {
72
7.89k
        std::unique_lock l(*_m[channel_id]);
73
7.89k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
7
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
7
                local_state->_shared_state->sub_mem_usage(channel_id,
77
7
                                                          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
7
            return true;
83
7
        }
84
7.88k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
7.88k
        local_state->_dependency->block();
86
7.88k
    }
87
31.3k
    return false;
88
58.6k
}
89
90
template <typename BlockType>
91
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
92
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
93
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
94
0
        block.first->record_channel_id(channel_id);
95
0
    } else {
96
0
        block->record_channel_id(channel_id);
97
0
    }
98
0
    _data_queue[channel_id].enqueue(std::move(block));
99
0
}
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_
Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_
100
101
template <typename BlockType>
102
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
103
20
                                         int channel_id) {
104
20
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
20
    return false;
112
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
113
114
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
115
174k
                              SinkInfo& sink_info) {
116
174k
    if (in_block->empty()) {
117
149k
        return Status::OK();
118
149k
    }
119
25.0k
    {
120
25.0k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
25.0k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
25.0k
    }
123
25.0k
    {
124
25.0k
        SCOPED_TIMER(profile.distribute_timer);
125
25.0k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
25.0k
                                    *sink_info.channel_id, sink_info.local_state,
127
25.0k
                                    sink_info.shuffle_idx_to_instance_idx));
128
25.0k
    }
129
130
25.0k
    sink_info.local_state->_memory_used_counter->set(
131
25.0k
            sink_info.local_state->_shared_state->mem_usage);
132
25.0k
    return Status::OK();
133
25.0k
}
134
135
151k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
151k
    PartitionedBlock partitioned_block;
137
151k
    bool eos;
138
151k
    Block block;
139
151k
    _data_queue[source_info.channel_id].set_eos();
140
151k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
151k
                         source_info.channel_id)) {
142
        // do nothing
143
21
    }
144
151k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
182k
                                   SourceInfo&& source_info) {
148
182k
    PartitionedBlock partitioned_block;
149
182k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
182k
                      source_info.channel_id)) {
151
41.1k
        SCOPED_TIMER(profile.copy_data_timer);
152
41.1k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
41.1k
                block, partitioned_block.first->_data_block);
154
41.1k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
52.2k
        do {
156
52.2k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
52.2k
                                       partitioned_block.second.offset_start;
158
52.2k
            auto block_wrapper = partitioned_block.first;
159
52.2k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
52.2k
                                                   offset_start + partitioned_block.second.length));
161
52.2k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
52.2k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
50.4k
                               source_info.channel_id));
164
41.1k
    }
165
182k
    return Status::OK();
166
182k
}
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
24.9k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
24.9k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
24.9k
    const auto rows = cast_set<int32_t>(block->rows());
176
24.9k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
24.9k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
24.9k
    {
179
24.9k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
26.2M
        for (int32_t i = 0; i < rows; ++i) {
181
26.1M
            partition_rows_histogram[channel_ids[i]]++;
182
26.1M
        }
183
200k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
175k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
175k
        }
186
25.9M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
25.9M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
25.9M
            partition_rows_histogram[channel_ids[i]]--;
189
25.9M
        }
190
24.9k
    }
191
192
24.9k
    Block data_block;
193
24.9k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
24.9k
    if (!_free_blocks.try_dequeue(data_block)) {
195
13.3k
        data_block = block->clone_empty();
196
13.3k
    }
197
24.9k
    data_block.swap(*block);
198
24.9k
    new_block_wrapper =
199
24.9k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
24.9k
    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
24.9k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
24.9k
    const auto& map = *shuffle_idx_to_instance_idx;
211
24.9k
    int32_t enqueue_rows = 0;
212
175k
    for (const auto& it : map) {
213
175k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
2
                << it.first << " : " << it.second << " " << _num_partitions;
215
175k
        uint32_t start = partition_rows_histogram[it.first];
216
175k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
175k
        if (size > 0) {
218
52.3k
            enqueue_rows += size;
219
52.3k
            _enqueue_data_and_set_ready(
220
52.3k
                    it.second, local_state,
221
52.3k
                    {new_block_wrapper,
222
52.3k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
52.3k
        }
224
175k
    }
225
24.9k
    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
24.9k
    return Status::OK();
239
24.9k
}
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
303k
                                  SinkInfo& sink_info) {
285
303k
    if (in_block->empty()) {
286
94.9k
        return Status::OK();
287
94.9k
    }
288
208k
    Block new_block;
289
208k
    if (!_free_blocks.try_dequeue(new_block)) {
290
87.1k
        new_block = {in_block->clone_empty()};
291
87.1k
    }
292
208k
    new_block.swap(*in_block);
293
208k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
208k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
208k
            std::move(new_block),
296
208k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
208k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
208k
    sink_info.local_state->_memory_used_counter->set(
301
208k
            sink_info.local_state->_shared_state->mem_usage);
302
303
208k
    return Status::OK();
304
303k
}
305
306
638k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
638k
    Block next_block;
308
638k
    BlockWrapperSPtr wrapper;
309
638k
    bool eos;
310
638k
    _data_queue[source_info.channel_id].set_eos();
311
639k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
639k
                         source_info.channel_id)) {
313
        // do nothing
314
1.59k
    }
315
638k
}
316
317
19.1k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
19.1k
    Block next_block;
319
19.1k
    BlockWrapperSPtr wrapper;
320
19.1k
    bool eos;
321
19.1k
    _data_queue[source_info.channel_id].set_eos();
322
19.1k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
19.1k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
19.1k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
981k
                                       Profile&& profile, SourceInfo&& source_info) {
330
981k
    BlockWrapperSPtr next_block;
331
981k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
981k
    return Status::OK();
333
981k
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
6.49k
                                SinkInfo& sink_info) {
337
6.49k
    if (in_block->empty()) {
338
2.18k
        return Status::OK();
339
2.18k
    }
340
4.31k
    Block new_block;
341
4.31k
    if (!_free_blocks.try_dequeue(new_block)) {
342
2.44k
        new_block = {in_block->clone_empty()};
343
2.44k
    }
344
4.31k
    new_block.swap(*in_block);
345
346
4.31k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
4.31k
            std::move(new_block),
348
4.31k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
4.31k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
4.31k
    sink_info.local_state->_memory_used_counter->set(
352
4.31k
            sink_info.local_state->_shared_state->mem_usage);
353
354
4.31k
    return Status::OK();
355
6.49k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
7.58k
                                     Profile&& profile, SourceInfo&& source_info) {
359
7.58k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
7.58k
    BlockWrapperSPtr next_block;
364
7.58k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
7.58k
    return Status::OK();
366
7.58k
}
367
368
128k
void ExchangerBase::finalize() {
369
128k
    DCHECK(_running_source_operators == 0);
370
128k
    Block block;
371
233k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
105k
    }
374
128k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
4.09k
                                SinkInfo& sink_info) {
378
4.09k
    if (in_block->empty()) {
379
1.35k
        return Status::OK();
380
1.35k
    }
381
2.74k
    Block new_block;
382
2.74k
    if (!_free_blocks.try_dequeue(new_block)) {
383
1.88k
        new_block = {in_block->clone_empty()};
384
1.88k
    }
385
2.74k
    new_block.swap(*in_block);
386
2.74k
    auto wrapper = BlockWrapper::create_shared(
387
2.74k
            std::move(new_block),
388
2.74k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
29.9k
    for (int i = 0; i < _num_partitions; i++) {
390
27.2k
        _enqueue_data_and_set_ready(
391
27.2k
                i, sink_info.local_state,
392
27.2k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
27.2k
    }
394
395
2.74k
    return Status::OK();
396
4.09k
}
397
398
11.7k
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
11.7k
    BroadcastBlock partitioned_block;
400
11.7k
    bool eos;
401
11.7k
    Block block;
402
11.7k
    _data_queue[source_info.channel_id].set_eos();
403
11.7k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
11.7k
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
11.7k
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
46.8k
                                     Profile&& profile, SourceInfo&& source_info) {
411
46.8k
    BroadcastBlock partitioned_block;
412
413
46.8k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
46.8k
                      source_info.channel_id)) {
415
27.2k
        SCOPED_TIMER(profile.copy_data_timer);
416
27.2k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
27.2k
                block, partitioned_block.first->_data_block);
418
27.2k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
27.2k
        auto block_wrapper = partitioned_block.first;
420
27.2k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
27.2k
                                               partitioned_block.second.offset_start,
422
27.2k
                                               partitioned_block.second.length));
423
27.2k
    }
424
425
46.8k
    return Status::OK();
426
46.8k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
627
                                                       SinkInfo& sink_info) {
430
627
    Block new_block;
431
627
    if (!_free_blocks.try_dequeue(new_block)) {
432
330
        new_block = {in_block->clone_empty()};
433
330
    }
434
627
    new_block.swap(*in_block);
435
627
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
627
    _enqueue_data_and_set_ready(
437
627
            channel_id, sink_info.local_state,
438
627
            {BlockWrapper::create_shared(
439
627
                     std::move(new_block),
440
627
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
627
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
627
    sink_info.local_state->_memory_used_counter->set(
444
627
            sink_info.local_state->_shared_state->mem_usage);
445
627
    return Status::OK();
446
627
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
2.41k
                                                   SinkInfo& sink_info) {
450
2.41k
    std::vector<uint32_t> channel_ids;
451
2.41k
    const auto num_rows = block->rows();
452
2.41k
    channel_ids.resize(num_rows, 0);
453
2.41k
    if (num_rows <= _num_partitions) {
454
1.99k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
1.99k
    } else {
456
427
        size_t i = 0;
457
31.4k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
31.0k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
31.0k
        }
460
427
        if (i < num_rows - 1) {
461
265
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
265
        }
463
427
    }
464
465
2.41k
    sink_info.local_state->_memory_used_counter->set(
466
2.41k
            sink_info.local_state->_shared_state->mem_usage);
467
2.41k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
2.41k
    return Status::OK();
469
2.41k
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
2.41k
                                                 Block* block, SinkInfo& sink_info) {
474
2.41k
    const auto rows = cast_set<int32_t>(block->rows());
475
2.41k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
2.41k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
2.41k
    {
478
2.41k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
102k
        for (int32_t i = 0; i < rows; ++i) {
480
99.9k
            partition_rows_histogram[channel_ids[i]]++;
481
99.9k
        }
482
23.2k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
20.7k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
20.7k
        }
485
486
102k
        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
2.41k
    }
491
2.41k
    Block data_block;
492
2.41k
    if (!_free_blocks.try_dequeue(data_block)) {
493
2.15k
        data_block = block->clone_empty();
494
2.15k
    }
495
2.41k
    data_block.swap(*block);
496
2.41k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
2.41k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
2.41k
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
23.2k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
20.7k
        const uint32_t start = partition_rows_histogram[i];
503
20.7k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
20.7k
        if (size > 0) {
505
6.40k
            _enqueue_data_and_set_ready(
506
6.40k
                    i, sink_info.local_state,
507
6.40k
                    {new_block_wrapper,
508
6.40k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
6.40k
        }
510
20.7k
    }
511
2.41k
    return Status::OK();
512
2.41k
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
9.00k
                                          Profile&& profile, SinkInfo& sink_info) {
516
9.00k
    if (in_block->empty()) {
517
5.95k
        return Status::OK();
518
5.95k
    }
519
3.05k
    if (_is_pass_through) {
520
627
        return _passthrough_sink(state, in_block, sink_info);
521
2.42k
    } else {
522
2.42k
        if (++_total_block >= _num_partitions) {
523
324
            _is_pass_through = true;
524
324
        }
525
2.42k
        return _shuffle_sink(state, in_block, sink_info);
526
2.42k
    }
527
3.05k
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
9.10k
                                               Profile&& profile, SourceInfo&& source_info) {
531
9.10k
    if (!_tmp_block[source_info.channel_id].empty()) {
532
153
        *block = std::move(_tmp_block[source_info.channel_id]);
533
153
        *eos = _tmp_eos[source_info.channel_id];
534
153
        _tmp_block[source_info.channel_id] = {};
535
153
        return Status::OK();
536
153
    }
537
8.95k
    PartitionedBlock partitioned_block;
538
8.95k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
8.95k
                      source_info.channel_id)) {
540
4.02k
        SCOPED_TIMER(profile.copy_data_timer);
541
4.02k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
4.02k
                block, partitioned_block.first->_data_block);
543
4.02k
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
7.01k
        do {
545
7.01k
            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
622
                if (mutable_block.rows() > 0) {
548
153
                    _tmp_block[source_info.channel_id] =
549
153
                            std::move(partitioned_block.first->_data_block);
550
153
                    _tmp_eos[source_info.channel_id] = *eos;
551
153
                    *eos = false;
552
469
                } else {
553
469
                    scoped_mutable_block.restore();
554
469
                    *block = std::move(partitioned_block.first->_data_block);
555
469
                }
556
622
                break;
557
622
            }
558
6.39k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
6.39k
                                       partitioned_block.second.offset_start;
560
6.39k
            auto block_wrapper = partitioned_block.first;
561
6.39k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
6.39k
                                                   offset_start + partitioned_block.second.length));
563
6.39k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
6.39k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
6.39k
                               source_info.channel_id));
566
4.02k
    }
567
8.95k
    return Status::OK();
568
8.95k
}
569
570
6.74k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
6.74k
    PartitionedBlock partitioned_block;
572
6.74k
    bool eos;
573
6.74k
    Block block;
574
6.74k
    _data_queue[source_info.channel_id].set_eos();
575
6.75k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
6.75k
                         source_info.channel_id)) {
577
        // do nothing
578
5
    }
579
6.74k
}
580
581
} // namespace doris