Coverage Report

Created: 2026-05-22 12:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/local_exchanger.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exec/exchange/local_exchanger.h"
19
20
#include "common/cast_set.h"
21
#include "common/status.h"
22
#include "exec/exchange/local_exchange_sink_operator.h"
23
#include "exec/exchange/local_exchange_source_operator.h"
24
#include "exec/partitioner/partitioner.h"
25
26
namespace doris {
27
template <typename BlockType>
28
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
29
                                                       LocalExchangeSinkLocalState* local_state,
30
272k
                                                       BlockType&& block) {
31
272k
    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
272k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
69.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
69.3k
        block.first->record_channel_id(channel_id);
43
202k
    } else {
44
202k
        block->record_channel_id(channel_id);
45
202k
    }
46
47
272k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
272k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
272k
    }
50
272k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
48.9k
                                                       BlockType&& block) {
31
48.9k
    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
48.9k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
48.9k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
48.9k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
48.9k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
48.8k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
48.8k
    }
50
48.9k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
202k
                                                       BlockType&& block) {
31
202k
    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
202k
    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
202k
    } else {
44
202k
        block->record_channel_id(channel_id);
45
202k
    }
46
47
202k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
202k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
202k
    }
50
202k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
20.4k
                                                       BlockType&& block) {
31
20.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
20.4k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
20.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
20.4k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
20.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
20.3k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
20.3k
    }
50
20.4k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.91M
                                         int channel_id) {
56
1.91M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.91M
    bool all_finished = _running_sink_operators == 0;
60
1.91M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
69.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
69.2k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
202k
        } else {
65
202k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
202k
            data_block->swap(block->_data_block);
67
202k
        }
68
271k
        return true;
69
1.64M
    } else if (all_finished) {
70
1.47M
        *eos = true;
71
1.47M
    } else {
72
170k
        std::unique_lock l(*_m[channel_id]);
73
170k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
15
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
15
                local_state->_shared_state->sub_mem_usage(channel_id,
77
15
                                                          block.first->_allocated_bytes);
78
15
            } else {
79
8
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
8
                data_block->swap(block->_data_block);
81
8
            }
82
23
            return true;
83
23
        }
84
170k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
170k
        local_state->_dependency->block();
86
170k
    }
87
1.64M
    return false;
88
1.91M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
284k
                                         int channel_id) {
56
284k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
284k
    bool all_finished = _running_sink_operators == 0;
60
284k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
48.8k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
48.8k
            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
48.8k
        return true;
69
235k
    } else if (all_finished) {
70
207k
        *eos = true;
71
207k
    } else {
72
28.3k
        std::unique_lock l(*_m[channel_id]);
73
28.3k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
13
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
13
                local_state->_shared_state->sub_mem_usage(channel_id,
77
13
                                                          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
13
            return true;
83
13
        }
84
28.3k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
28.3k
        local_state->_dependency->block();
86
28.3k
    }
87
235k
    return false;
88
284k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.58M
                                         int channel_id) {
56
1.58M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.58M
    bool all_finished = _running_sink_operators == 0;
60
1.58M
    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
202k
        } else {
65
202k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
202k
            data_block->swap(block->_data_block);
67
202k
        }
68
202k
        return true;
69
1.38M
    } else if (all_finished) {
70
1.24M
        *eos = true;
71
1.24M
    } else {
72
138k
        std::unique_lock l(*_m[channel_id]);
73
138k
        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
8
            } else {
79
8
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
8
                data_block->swap(block->_data_block);
81
8
            }
82
8
            return true;
83
8
        }
84
138k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
138k
        local_state->_dependency->block();
86
138k
    }
87
1.38M
    return false;
88
1.58M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
44.6k
                                         int channel_id) {
56
44.6k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
44.6k
    bool all_finished = _running_sink_operators == 0;
60
44.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
20.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
20.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
20.3k
        return true;
69
24.2k
    } else if (all_finished) {
70
20.0k
        *eos = true;
71
20.0k
    } else {
72
4.19k
        std::unique_lock l(*_m[channel_id]);
73
4.19k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
2
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
2
                local_state->_shared_state->sub_mem_usage(channel_id,
77
2
                                                          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
2
            return true;
83
2
        }
84
4.18k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
4.18k
        local_state->_dependency->block();
86
4.18k
    }
87
24.2k
    return false;
88
44.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
119k
                              SinkInfo& sink_info) {
116
119k
    if (in_block->empty()) {
117
99.5k
        return Status::OK();
118
99.5k
    }
119
20.1k
    {
120
20.1k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
20.1k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
20.1k
    }
123
20.1k
    {
124
20.1k
        SCOPED_TIMER(profile.distribute_timer);
125
20.1k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
20.1k
                                    *sink_info.channel_id, sink_info.local_state,
127
20.1k
                                    sink_info.shuffle_idx_to_instance_idx));
128
20.1k
    }
129
130
20.1k
    sink_info.local_state->_memory_used_counter->set(
131
20.1k
            sink_info.local_state->_shared_state->mem_usage);
132
20.1k
    return Status::OK();
133
20.1k
}
134
135
100k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
100k
    PartitionedBlock partitioned_block;
137
100k
    bool eos;
138
100k
    Block block;
139
100k
    _data_queue[source_info.channel_id].set_eos();
140
100k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
100k
                         source_info.channel_id)) {
142
        // do nothing
143
268
    }
144
100k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
126k
                                   SourceInfo&& source_info) {
148
126k
    PartitionedBlock partitioned_block;
149
126k
    MutableBlock mutable_block;
150
151
126k
    auto get_data = [&]() -> Status {
152
41.8k
        do {
153
41.8k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
41.8k
                                       partitioned_block.second.offset_start;
155
41.8k
            auto block_wrapper = partitioned_block.first;
156
41.8k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
41.8k
                                                   offset_start + partitioned_block.second.length));
158
41.8k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
41.8k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
41.2k
                               source_info.channel_id));
161
33.4k
        return Status::OK();
162
33.4k
    };
163
164
126k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
126k
                      source_info.channel_id)) {
166
33.5k
        SCOPED_TIMER(profile.copy_data_timer);
167
33.5k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
33.5k
                block, partitioned_block.first->_data_block);
169
33.5k
        RETURN_IF_ERROR(get_data());
170
33.5k
    }
171
126k
    return Status::OK();
172
126k
}
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
20.1k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
20.1k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
20.1k
    const auto rows = cast_set<int32_t>(block->rows());
182
20.1k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
20.1k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
20.1k
    {
185
20.1k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
9.33M
        for (int32_t i = 0; i < rows; ++i) {
187
9.31M
            partition_rows_histogram[channel_ids[i]]++;
188
9.31M
        }
189
204k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
184k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
184k
        }
192
8.64M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
8.62M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
8.62M
            partition_rows_histogram[channel_ids[i]]--;
195
8.62M
        }
196
20.1k
    }
197
198
20.1k
    Block data_block;
199
20.1k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
20.1k
    if (!_free_blocks.try_dequeue(data_block)) {
201
11.1k
        data_block = block->clone_empty();
202
11.1k
    }
203
20.1k
    data_block.swap(*block);
204
20.1k
    new_block_wrapper =
205
20.1k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
20.1k
    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
20.1k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
20.1k
    const auto& map = *shuffle_idx_to_instance_idx;
217
20.1k
    int32_t enqueue_rows = 0;
218
184k
    for (const auto& it : map) {
219
18.4E
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
18.4E
                << it.first << " : " << it.second << " " << _num_partitions;
221
184k
        uint32_t start = partition_rows_histogram[it.first];
222
184k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
184k
        if (size > 0) {
224
42.3k
            enqueue_rows += size;
225
42.3k
            _enqueue_data_and_set_ready(
226
42.3k
                    it.second, local_state,
227
42.3k
                    {new_block_wrapper,
228
42.3k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
42.3k
        }
230
184k
    }
231
20.1k
    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
20.1k
    return Status::OK();
245
20.1k
}
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
302k
                                  SinkInfo& sink_info) {
291
302k
    if (in_block->empty()) {
292
102k
        return Status::OK();
293
102k
    }
294
199k
    Block new_block;
295
199k
    if (!_free_blocks.try_dequeue(new_block)) {
296
79.4k
        new_block = {in_block->clone_empty()};
297
79.4k
    }
298
199k
    new_block.swap(*in_block);
299
199k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
199k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
199k
            std::move(new_block),
302
199k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
199k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
199k
    sink_info.local_state->_memory_used_counter->set(
307
199k
            sink_info.local_state->_shared_state->mem_usage);
308
309
199k
    return Status::OK();
310
302k
}
311
312
615k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
615k
    Block next_block;
314
615k
    BlockWrapperSPtr wrapper;
315
615k
    bool eos;
316
615k
    _data_queue[source_info.channel_id].set_eos();
317
616k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
616k
                         source_info.channel_id)) {
319
        // do nothing
320
1.17k
    }
321
615k
}
322
323
10.6k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
10.6k
    Block next_block;
325
10.6k
    BlockWrapperSPtr wrapper;
326
10.6k
    bool eos;
327
10.6k
    _data_queue[source_info.channel_id].set_eos();
328
10.6k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
10.6k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
10.6k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
949k
                                       Profile&& profile, SourceInfo&& source_info) {
336
949k
    BlockWrapperSPtr next_block;
337
949k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
949k
    return Status::OK();
339
949k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
4.19k
                                SinkInfo& sink_info) {
343
4.19k
    if (in_block->empty()) {
344
1.36k
        return Status::OK();
345
1.36k
    }
346
2.83k
    Block new_block;
347
2.83k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.59k
        new_block = {in_block->clone_empty()};
349
1.59k
    }
350
2.83k
    new_block.swap(*in_block);
351
352
2.83k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
2.83k
            std::move(new_block),
354
2.83k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
2.83k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
2.83k
    sink_info.local_state->_memory_used_counter->set(
358
2.83k
            sink_info.local_state->_shared_state->mem_usage);
359
360
2.83k
    return Status::OK();
361
4.19k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
5.20k
                                     Profile&& profile, SourceInfo&& source_info) {
365
5.20k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
5.19k
    BlockWrapperSPtr next_block;
370
5.19k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
5.19k
    return Status::OK();
372
5.20k
}
373
374
115k
void ExchangerBase::finalize() {
375
115k
    DCHECK(_running_source_operators == 0);
376
115k
    Block block;
377
209k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
94.5k
    }
380
115k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
4.25k
                                SinkInfo& sink_info) {
384
4.25k
    if (in_block->empty()) {
385
1.50k
        return Status::OK();
386
1.50k
    }
387
2.75k
    Block new_block;
388
2.75k
    if (!_free_blocks.try_dequeue(new_block)) {
389
2.06k
        new_block = {in_block->clone_empty()};
390
2.06k
    }
391
2.75k
    new_block.swap(*in_block);
392
2.75k
    auto wrapper = BlockWrapper::create_shared(
393
2.75k
            std::move(new_block),
394
2.75k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
23.1k
    for (int i = 0; i < _num_partitions; i++) {
396
20.4k
        _enqueue_data_and_set_ready(
397
20.4k
                i, sink_info.local_state,
398
20.4k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
20.4k
    }
400
401
2.75k
    return Status::OK();
402
4.25k
}
403
404
10.0k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
10.0k
    BroadcastBlock partitioned_block;
406
10.0k
    bool eos;
407
10.0k
    Block block;
408
10.0k
    _data_queue[source_info.channel_id].set_eos();
409
10.0k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
10.0k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
10.0k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
34.5k
                                     Profile&& profile, SourceInfo&& source_info) {
417
34.5k
    BroadcastBlock partitioned_block;
418
419
34.5k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
34.5k
                      source_info.channel_id)) {
421
20.3k
        SCOPED_TIMER(profile.copy_data_timer);
422
20.3k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
20.3k
                block, partitioned_block.first->_data_block);
424
20.3k
        auto block_wrapper = partitioned_block.first;
425
20.3k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
20.3k
                                               partitioned_block.second.offset_start,
427
20.3k
                                               partitioned_block.second.length));
428
20.3k
    }
429
430
34.5k
    return Status::OK();
431
34.5k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
499
                                                       SinkInfo& sink_info) {
435
499
    Block new_block;
436
499
    if (!_free_blocks.try_dequeue(new_block)) {
437
270
        new_block = {in_block->clone_empty()};
438
270
    }
439
499
    new_block.swap(*in_block);
440
499
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
499
    _enqueue_data_and_set_ready(
442
499
            channel_id, sink_info.local_state,
443
499
            {BlockWrapper::create_shared(
444
499
                     std::move(new_block),
445
499
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
446
499
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
447
448
499
    sink_info.local_state->_memory_used_counter->set(
449
499
            sink_info.local_state->_shared_state->mem_usage);
450
499
    return Status::OK();
451
499
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
2.15k
                                                   SinkInfo& sink_info) {
455
2.15k
    std::vector<uint32_t> channel_ids;
456
2.15k
    const auto num_rows = block->rows();
457
2.15k
    channel_ids.resize(num_rows, 0);
458
2.15k
    if (num_rows <= _num_partitions) {
459
1.59k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.59k
    } else {
461
568
        size_t i = 0;
462
18.0k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
17.4k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
17.4k
        }
465
568
        if (i < num_rows - 1) {
466
480
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
480
        }
468
568
    }
469
470
2.15k
    sink_info.local_state->_memory_used_counter->set(
471
2.15k
            sink_info.local_state->_shared_state->mem_usage);
472
2.15k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
2.15k
    return Status::OK();
474
2.15k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
2.15k
                                                 Block* block, SinkInfo& sink_info) {
479
2.15k
    const auto rows = cast_set<int32_t>(block->rows());
480
2.15k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
481
2.15k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
482
2.15k
    {
483
2.15k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
65.6k
        for (int32_t i = 0; i < rows; ++i) {
485
63.5k
            partition_rows_histogram[channel_ids[i]]++;
486
63.5k
        }
487
13.1k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
10.9k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
10.9k
        }
490
491
65.0k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
62.8k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
62.8k
            partition_rows_histogram[channel_ids[i]]--;
494
62.8k
        }
495
2.15k
    }
496
2.15k
    Block data_block;
497
2.15k
    if (!_free_blocks.try_dequeue(data_block)) {
498
1.95k
        data_block = block->clone_empty();
499
1.95k
    }
500
2.15k
    data_block.swap(*block);
501
2.15k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
502
2.15k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
503
2.15k
    if (new_block_wrapper->_data_block.empty()) {
504
0
        return Status::OK();
505
0
    }
506
13.1k
    for (int32_t i = 0; i < _num_partitions; i++) {
507
10.9k
        const uint32_t start = partition_rows_histogram[i];
508
10.9k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
509
10.9k
        if (size > 0) {
510
6.16k
            _enqueue_data_and_set_ready(
511
6.16k
                    i, sink_info.local_state,
512
6.16k
                    {new_block_wrapper,
513
6.16k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
514
6.16k
        }
515
10.9k
    }
516
2.15k
    return Status::OK();
517
2.15k
}
518
519
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
520
5.62k
                                          Profile&& profile, SinkInfo& sink_info) {
521
5.62k
    if (in_block->empty()) {
522
2.96k
        return Status::OK();
523
2.96k
    }
524
2.66k
    if (_is_pass_through) {
525
499
        return _passthrough_sink(state, in_block, sink_info);
526
2.16k
    } else {
527
2.16k
        if (++_total_block >= _num_partitions) {
528
311
            _is_pass_through = true;
529
311
        }
530
2.16k
        return _shuffle_sink(state, in_block, sink_info);
531
2.16k
    }
532
2.66k
}
533
534
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
535
6.13k
                                               Profile&& profile, SourceInfo&& source_info) {
536
6.13k
    if (!_tmp_block[source_info.channel_id].empty()) {
537
175
        *block = std::move(_tmp_block[source_info.channel_id]);
538
175
        *eos = _tmp_eos[source_info.channel_id];
539
175
        _tmp_block[source_info.channel_id] = {};
540
175
        return Status::OK();
541
175
    }
542
5.95k
    PartitionedBlock partitioned_block;
543
5.95k
    MutableBlock mutable_block;
544
545
5.95k
    auto get_data = [&]() -> Status {
546
6.65k
        do {
547
6.65k
            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
495
                if (mutable_block.rows() > 0) {
550
175
                    _tmp_block[source_info.channel_id] =
551
175
                            std::move(partitioned_block.first->_data_block);
552
175
                    _tmp_eos[source_info.channel_id] = *eos;
553
175
                    *eos = false;
554
320
                } else {
555
320
                    *block = std::move(partitioned_block.first->_data_block);
556
320
                }
557
495
                break;
558
495
            }
559
6.15k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
560
6.15k
                                       partitioned_block.second.offset_start;
561
6.15k
            auto block_wrapper = partitioned_block.first;
562
6.15k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
563
6.15k
                                                   offset_start + partitioned_block.second.length));
564
6.15k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
565
6.15k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
566
6.15k
                               source_info.channel_id));
567
3.28k
        return Status::OK();
568
3.28k
    };
569
570
5.95k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
571
5.95k
                      source_info.channel_id)) {
572
3.29k
        SCOPED_TIMER(profile.copy_data_timer);
573
3.29k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
574
3.29k
                block, partitioned_block.first->_data_block);
575
3.29k
        RETURN_IF_ERROR(get_data());
576
3.29k
    }
577
5.95k
    return Status::OK();
578
5.95k
}
579
580
4.09k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
581
4.09k
    PartitionedBlock partitioned_block;
582
4.09k
    bool eos;
583
4.09k
    Block block;
584
4.09k
    _data_queue[source_info.channel_id].set_eos();
585
4.09k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
586
4.09k
                         source_info.channel_id)) {
587
        // do nothing
588
2
    }
589
4.09k
}
590
591
} // namespace doris