Coverage Report

Created: 2026-03-13 21:11

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