Coverage Report

Created: 2026-05-18 13:00

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