Coverage Report

Created: 2026-05-28 03:46

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