Coverage Report

Created: 2026-03-12 14:02

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
#include "common/compile_check_begin.h"
28
template <typename BlockType>
29
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
30
                                                       LocalExchangeSinkLocalState* local_state,
31
53.0k
                                                       BlockType&& block) {
32
53.0k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
53.0k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
492
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
492
        block.first->record_channel_id(channel_id);
44
52.5k
    } else {
45
52.5k
        block->record_channel_id(channel_id);
46
52.5k
    }
47
48
53.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
53.0k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
53.0k
    }
51
53.0k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
44
                                                       BlockType&& block) {
32
44
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
44
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
44
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
44
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
44
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
40
        local_state->_shared_state->set_ready_to_read(channel_id);
50
40
    }
51
44
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
52.5k
                                                       BlockType&& block) {
32
52.5k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
52.5k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
        block.first->record_channel_id(channel_id);
44
52.5k
    } else {
45
52.5k
        block->record_channel_id(channel_id);
46
52.5k
    }
47
48
52.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
52.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
52.5k
    }
51
52.5k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
448
                                                       BlockType&& block) {
32
448
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
448
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
448
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
448
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
448
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
432
        local_state->_shared_state->set_ready_to_read(channel_id);
50
432
    }
51
448
}
52
53
template <typename BlockType>
54
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
55
                                         BlockType& block, bool* eos, Block* data_block,
56
349k
                                         int channel_id) {
57
349k
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
349k
    bool all_finished = _running_sink_operators == 0;
61
349k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
468
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
468
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
52.5k
        } else {
66
52.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
52.5k
            data_block->swap(block->_data_block);
68
52.5k
        }
69
53.0k
        return true;
70
296k
    } else if (all_finished) {
71
264k
        *eos = true;
72
264k
    } else {
73
31.9k
        std::unique_lock l(*_m[channel_id]);
74
31.9k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
0
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
0
                local_state->_shared_state->sub_mem_usage(channel_id,
78
0
                                                          block.first->_allocated_bytes);
79
0
            } else {
80
0
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
0
                data_block->swap(block->_data_block);
82
0
            }
83
0
            return true;
84
0
        }
85
31.9k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
31.9k
        local_state->_dependency->block();
87
31.9k
    }
88
296k
    return false;
89
349k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
230
                                         int channel_id) {
57
230
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
226
    bool all_finished = _running_sink_operators == 0;
61
226
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
36
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
36
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
        } else {
66
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
            data_block->swap(block->_data_block);
68
        }
69
36
        return true;
70
190
    } else if (all_finished) {
71
172
        *eos = true;
72
172
    } else {
73
18
        std::unique_lock l(*_m[channel_id]);
74
18
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
0
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
0
                local_state->_shared_state->sub_mem_usage(channel_id,
78
0
                                                          block.first->_allocated_bytes);
79
            } else {
80
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
                data_block->swap(block->_data_block);
82
            }
83
0
            return true;
84
0
        }
85
18
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
18
        local_state->_dependency->block();
87
18
    }
88
190
    return false;
89
226
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
56
348k
                                         int channel_id) {
57
348k
    if (local_state == nullptr) {
58
12
        return _dequeue_data(block, eos, data_block, channel_id);
59
12
    }
60
348k
    bool all_finished = _running_sink_operators == 0;
61
348k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
52.5k
        } else {
66
52.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
52.5k
            data_block->swap(block->_data_block);
68
52.5k
        }
69
52.5k
        return true;
70
295k
    } else if (all_finished) {
71
263k
        *eos = true;
72
263k
    } else {
73
31.8k
        std::unique_lock l(*_m[channel_id]);
74
31.8k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
                local_state->_shared_state->sub_mem_usage(channel_id,
78
                                                          block.first->_allocated_bytes);
79
0
            } else {
80
0
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
0
                data_block->swap(block->_data_block);
82
0
            }
83
0
            return true;
84
0
        }
85
31.8k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
31.8k
        local_state->_dependency->block();
87
31.8k
    }
88
295k
    return false;
89
348k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
1.30k
                                         int channel_id) {
57
1.30k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
1.30k
    bool all_finished = _running_sink_operators == 0;
61
1.30k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
432
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
432
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
        } else {
66
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
            data_block->swap(block->_data_block);
68
        }
69
432
        return true;
70
868
    } else if (all_finished) {
71
804
        *eos = true;
72
804
    } else {
73
64
        std::unique_lock l(*_m[channel_id]);
74
64
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
0
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
0
                local_state->_shared_state->sub_mem_usage(channel_id,
78
0
                                                          block.first->_allocated_bytes);
79
            } else {
80
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
                data_block->swap(block->_data_block);
82
            }
83
0
            return true;
84
0
        }
85
64
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
64
        local_state->_dependency->block();
87
64
    }
88
868
    return false;
89
1.30k
}
90
91
template <typename BlockType>
92
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
93
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
94
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
95
0
        block.first->record_channel_id(channel_id);
96
0
    } else {
97
0
        block->record_channel_id(channel_id);
98
0
    }
99
0
    _data_queue[channel_id].enqueue(std::move(block));
100
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_
101
102
template <typename BlockType>
103
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
104
20
                                         int channel_id) {
105
20
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
0
            data_block->swap(block->_data_block);
109
0
        }
110
0
        return true;
111
0
    }
112
20
    return false;
113
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
104
4
                                         int channel_id) {
105
4
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
            data_block->swap(block->_data_block);
109
        }
110
0
        return true;
111
0
    }
112
4
    return false;
113
4
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
104
12
                                         int channel_id) {
105
12
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
0
            data_block->swap(block->_data_block);
109
0
        }
110
0
        return true;
111
0
    }
112
12
    return false;
113
12
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
104
4
                                         int channel_id) {
105
4
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
            data_block->swap(block->_data_block);
109
        }
110
0
        return true;
111
0
    }
112
4
    return false;
113
4
}
114
115
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
116
139
                              SinkInfo& sink_info) {
117
139
    if (in_block->empty()) {
118
94
        return Status::OK();
119
94
    }
120
45
    {
121
45
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
45
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
45
    }
124
45
    {
125
45
        SCOPED_TIMER(profile.distribute_timer);
126
45
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
45
                                    *sink_info.channel_id, sink_info.local_state,
128
45
                                    sink_info.shuffle_idx_to_instance_idx));
129
45
    }
130
131
44
    sink_info.local_state->_memory_used_counter->set(
132
44
            sink_info.local_state->_shared_state->mem_usage);
133
44
    return Status::OK();
134
45
}
135
136
100
void ShuffleExchanger::close(SourceInfo&& source_info) {
137
100
    PartitionedBlock partitioned_block;
138
100
    bool eos;
139
100
    Block block;
140
100
    _data_queue[source_info.channel_id].set_eos();
141
100
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
100
                         source_info.channel_id)) {
143
        // do nothing
144
0
    }
145
100
}
146
147
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
148
94
                                   SourceInfo&& source_info) {
149
94
    PartitionedBlock partitioned_block;
150
94
    MutableBlock mutable_block;
151
152
94
    auto get_data = [&]() -> Status {
153
36
        do {
154
36
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
36
                                       partitioned_block.second.offset_start;
156
36
            auto block_wrapper = partitioned_block.first;
157
36
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
36
                                                   offset_start + partitioned_block.second.length));
159
36
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
36
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
36
                               source_info.channel_id));
162
32
        return Status::OK();
163
32
    };
164
165
94
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
94
                      source_info.channel_id)) {
167
32
        SCOPED_TIMER(profile.copy_data_timer);
168
32
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
32
                block, partitioned_block.first->_data_block);
170
32
        RETURN_IF_ERROR(get_data());
171
32
    }
172
94
    return Status::OK();
173
94
}
174
175
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
176
                                     Block* block, int channel_id,
177
                                     LocalExchangeSinkLocalState* local_state,
178
45
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
45
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
45
    const auto rows = cast_set<int32_t>(block->rows());
183
45
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
45
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
45
    {
186
45
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
303
        for (int32_t i = 0; i < rows; ++i) {
188
258
            partition_rows_histogram[channel_ids[i]]++;
189
258
        }
190
225
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
180
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
180
        }
193
303
        for (int32_t i = rows - 1; i >= 0; --i) {
194
258
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
258
            partition_rows_histogram[channel_ids[i]]--;
196
258
        }
197
45
    }
198
199
45
    Block data_block;
200
45
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
45
    if (!_free_blocks.try_dequeue(data_block)) {
202
30
        data_block = block->clone_empty();
203
30
    }
204
45
    data_block.swap(*block);
205
45
    new_block_wrapper =
206
45
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
45
    if (new_block_wrapper->_data_block.empty()) {
208
0
        return Status::OK();
209
0
    }
210
    /**
211
     * Data are hash-shuffled and distributed to all instances of
212
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
213
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
214
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
215
     */
216
45
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
45
    const auto& map = *shuffle_idx_to_instance_idx;
218
45
    int32_t enqueue_rows = 0;
219
179
    for (const auto& it : map) {
220
179
        DCHECK(it.second >= 0 && it.second < _num_partitions)
221
0
                << it.first << " : " << it.second << " " << _num_partitions;
222
179
        uint32_t start = partition_rows_histogram[it.first];
223
179
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
179
        if (size > 0) {
225
44
            enqueue_rows += size;
226
44
            _enqueue_data_and_set_ready(
227
44
                    it.second, local_state,
228
44
                    {new_block_wrapper,
229
44
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
44
        }
231
179
    }
232
45
    if (enqueue_rows != rows) [[unlikely]] {
233
1
        fmt::memory_buffer debug_string_buffer;
234
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
235
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
236
3
        for (const auto& it : map) {
237
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
238
3
        }
239
1
        return Status::InternalError(
240
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
241
1
                "rows={}, Detail: {}]",
242
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
243
1
    }
244
245
44
    return Status::OK();
246
45
}
247
248
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
249
0
                                     Block* block, int channel_id) {
250
0
    const auto rows = cast_set<int32_t>(block->rows());
251
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
252
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
253
0
    {
254
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
255
0
        for (int32_t i = 0; i < rows; ++i) {
256
0
            partition_rows_histogram[channel_ids[i]]++;
257
0
        }
258
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
259
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
260
0
        }
261
0
        for (int32_t i = rows - 1; i >= 0; --i) {
262
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
263
0
            partition_rows_histogram[channel_ids[i]]--;
264
0
        }
265
0
    }
266
267
0
    Block data_block;
268
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
269
0
    if (!_free_blocks.try_dequeue(data_block)) {
270
0
        data_block = block->clone_empty();
271
0
    }
272
0
    data_block.swap(*block);
273
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
274
0
    if (new_block_wrapper->_data_block.empty()) {
275
0
        return Status::OK();
276
0
    }
277
0
    for (int i = 0; i < _num_partitions; i++) {
278
0
        uint32_t start = partition_rows_histogram[i];
279
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
280
0
        if (size > 0) {
281
0
            _enqueue_data_and_set_ready(
282
0
                    i, {new_block_wrapper,
283
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
284
0
        }
285
0
    }
286
287
0
    return Status::OK();
288
0
}
289
290
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
291
60.9k
                                  SinkInfo& sink_info) {
292
60.9k
    if (in_block->empty()) {
293
10.1k
        return Status::OK();
294
10.1k
    }
295
50.8k
    Block new_block;
296
50.8k
    if (!_free_blocks.try_dequeue(new_block)) {
297
19.7k
        new_block = {in_block->clone_empty()};
298
19.7k
    }
299
50.8k
    new_block.swap(*in_block);
300
50.8k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
50.8k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
50.8k
            std::move(new_block),
303
50.8k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
50.8k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
50.8k
    sink_info.local_state->_memory_used_counter->set(
308
50.8k
            sink_info.local_state->_shared_state->mem_usage);
309
310
50.8k
    return Status::OK();
311
60.9k
}
312
313
127k
void PassthroughExchanger::close(SourceInfo&& source_info) {
314
127k
    Block next_block;
315
127k
    BlockWrapperSPtr wrapper;
316
127k
    bool eos;
317
127k
    _data_queue[source_info.channel_id].set_eos();
318
127k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
319
127k
                         source_info.channel_id)) {
320
        // do nothing
321
0
    }
322
127k
}
323
324
6.01k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
6.01k
    Block next_block;
326
6.01k
    BlockWrapperSPtr wrapper;
327
6.01k
    bool eos;
328
6.01k
    _data_queue[source_info.channel_id].set_eos();
329
6.01k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
6.01k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
6.01k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
210k
                                       Profile&& profile, SourceInfo&& source_info) {
337
210k
    BlockWrapperSPtr next_block;
338
210k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
210k
    return Status::OK();
340
210k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
1.97k
                                SinkInfo& sink_info) {
344
1.97k
    if (in_block->empty()) {
345
754
        return Status::OK();
346
754
    }
347
1.22k
    Block new_block;
348
1.22k
    if (!_free_blocks.try_dequeue(new_block)) {
349
835
        new_block = {in_block->clone_empty()};
350
835
    }
351
1.22k
    new_block.swap(*in_block);
352
353
1.22k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
1.22k
            std::move(new_block),
355
1.22k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
1.22k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
1.22k
    sink_info.local_state->_memory_used_counter->set(
359
1.22k
            sink_info.local_state->_shared_state->mem_usage);
360
361
1.22k
    return Status::OK();
362
1.97k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
2.28k
                                     Profile&& profile, SourceInfo&& source_info) {
366
2.28k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
2.28k
    BlockWrapperSPtr next_block;
371
2.28k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
2.28k
    return Status::OK();
373
2.28k
}
374
375
16.9k
void ExchangerBase::finalize() {
376
16.9k
    DCHECK(_running_source_operators == 0);
377
16.9k
    Block block;
378
37.9k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
21.0k
    }
381
16.9k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
114
                                SinkInfo& sink_info) {
385
114
    if (in_block->empty()) {
386
50
        return Status::OK();
387
50
    }
388
64
    Block new_block;
389
64
    if (!_free_blocks.try_dequeue(new_block)) {
390
57
        new_block = {in_block->clone_empty()};
391
57
    }
392
64
    new_block.swap(*in_block);
393
64
    auto wrapper = BlockWrapper::create_shared(
394
64
            std::move(new_block),
395
64
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
512
    for (int i = 0; i < _num_partitions; i++) {
397
448
        _enqueue_data_and_set_ready(
398
448
                i, sink_info.local_state,
399
448
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
448
    }
401
402
64
    return Status::OK();
403
114
}
404
405
404
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
404
    BroadcastBlock partitioned_block;
407
404
    bool eos;
408
404
    Block block;
409
404
    _data_queue[source_info.channel_id].set_eos();
410
404
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
404
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
404
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
900
                                     Profile&& profile, SourceInfo&& source_info) {
418
900
    BroadcastBlock partitioned_block;
419
420
900
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
900
                      source_info.channel_id)) {
422
432
        SCOPED_TIMER(profile.copy_data_timer);
423
432
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
432
                block, partitioned_block.first->_data_block);
425
432
        auto block_wrapper = partitioned_block.first;
426
432
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
432
                                               partitioned_block.second.offset_start,
428
432
                                               partitioned_block.second.length));
429
432
    }
430
431
900
    return Status::OK();
432
900
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
20
                                                       SinkInfo& sink_info) {
436
20
    Block new_block;
437
20
    if (!_free_blocks.try_dequeue(new_block)) {
438
13
        new_block = {in_block->clone_empty()};
439
13
    }
440
20
    new_block.swap(*in_block);
441
20
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
20
    _enqueue_data_and_set_ready(
443
20
            channel_id, sink_info.local_state,
444
20
            BlockWrapper::create_shared(
445
20
                    std::move(new_block),
446
20
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
20
                    channel_id));
448
449
20
    sink_info.local_state->_memory_used_counter->set(
450
20
            sink_info.local_state->_shared_state->mem_usage);
451
20
    return Status::OK();
452
20
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
108
                                                   SinkInfo& sink_info) {
456
108
    std::vector<uint32_t> channel_ids;
457
108
    const auto num_rows = block->rows();
458
108
    channel_ids.resize(num_rows, 0);
459
108
    if (num_rows <= _num_partitions) {
460
52
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
56
    } else {
462
56
        size_t i = 0;
463
624
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
568
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
568
        }
466
56
        if (i < num_rows - 1) {
467
6
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
6
        }
469
56
    }
470
471
108
    sink_info.local_state->_memory_used_counter->set(
472
108
            sink_info.local_state->_shared_state->mem_usage);
473
108
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
108
    return Status::OK();
475
108
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
108
                                                 Block* block, SinkInfo& sink_info) {
480
108
    const auto rows = cast_set<int32_t>(block->rows());
481
108
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
108
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
108
    {
484
108
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
4.77k
        for (int32_t i = 0; i < rows; ++i) {
486
4.67k
            partition_rows_histogram[channel_ids[i]]++;
487
4.67k
        }
488
956
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
848
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
848
        }
491
492
4.77k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
4.67k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
4.67k
            partition_rows_histogram[channel_ids[i]]--;
495
4.67k
        }
496
108
    }
497
956
    for (int32_t i = 0; i < _num_partitions; i++) {
498
848
        const size_t start = partition_rows_histogram[i];
499
848
        const size_t size = partition_rows_histogram[i + 1] - start;
500
848
        if (size > 0) {
501
520
            std::unique_ptr<MutableBlock> mutable_block =
502
520
                    MutableBlock::create_unique(block->clone_empty());
503
520
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
520
            auto new_block = mutable_block->to_block();
505
506
520
            _enqueue_data_and_set_ready(
507
520
                    i, sink_info.local_state,
508
520
                    BlockWrapper::create_shared(
509
520
                            std::move(new_block),
510
520
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
520
                            i));
512
520
        }
513
848
    }
514
108
    return Status::OK();
515
108
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
604
                                          Profile&& profile, SinkInfo& sink_info) {
519
604
    if (in_block->empty()) {
520
476
        return Status::OK();
521
476
    }
522
128
    if (_is_pass_through) {
523
20
        return _passthrough_sink(state, in_block, sink_info);
524
108
    } else {
525
108
        if (++_total_block >= _num_partitions) {
526
3
            _is_pass_through = true;
527
3
        }
528
108
        return _shuffle_sink(state, in_block, sink_info);
529
108
    }
530
128
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
1.03k
                                               Profile&& profile, SourceInfo&& source_info) {
534
1.03k
    BlockWrapperSPtr next_block;
535
1.03k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
1.03k
    return Status::OK();
537
1.03k
}
538
539
484
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
484
    Block next_block;
541
484
    bool eos;
542
484
    BlockWrapperSPtr wrapper;
543
484
    _data_queue[source_info.channel_id].set_eos();
544
484
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
484
                         source_info.channel_id)) {
546
        // do nothing
547
0
    }
548
484
}
549
550
} // namespace doris