Coverage Report

Created: 2026-03-19 17:45

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
286k
                                                       BlockType&& block) {
32
286k
    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
286k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
67.8k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
67.8k
        block.first->record_channel_id(channel_id);
44
219k
    } else {
45
219k
        block->record_channel_id(channel_id);
46
219k
    }
47
48
286k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
286k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
286k
    }
51
286k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
40.3k
                                                       BlockType&& block) {
32
40.3k
    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
40.3k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
40.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
40.3k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
40.3k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
40.3k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
40.3k
    }
51
40.3k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
219k
                                                       BlockType&& block) {
32
219k
    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
219k
    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
219k
    } else {
45
219k
        block->record_channel_id(channel_id);
46
219k
    }
47
48
219k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
218k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
218k
    }
51
219k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
27.4k
                                                       BlockType&& block) {
32
27.4k
    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
27.4k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
27.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
27.4k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
27.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
27.4k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
27.4k
    }
51
27.4k
}
52
53
template <typename BlockType>
54
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
55
                                         BlockType& block, bool* eos, Block* data_block,
56
2.02M
                                         int channel_id) {
57
2.02M
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
2.02M
    bool all_finished = _running_sink_operators == 0;
61
2.02M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
67.7k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
67.7k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
218k
        } else {
66
218k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
218k
            data_block->swap(block->_data_block);
68
218k
        }
69
286k
        return true;
70
1.73M
    } else if (all_finished) {
71
1.58M
        *eos = true;
72
1.58M
    } else {
73
144k
        std::unique_lock l(*_m[channel_id]);
74
144k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
20
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
20
                local_state->_shared_state->sub_mem_usage(channel_id,
78
20
                                                          block.first->_allocated_bytes);
79
20
            } else {
80
11
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
11
                data_block->swap(block->_data_block);
82
11
            }
83
31
            return true;
84
31
        }
85
144k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
144k
        local_state->_dependency->block();
87
144k
    }
88
1.73M
    return false;
89
2.02M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
294k
                                         int channel_id) {
57
294k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
294k
    bool all_finished = _running_sink_operators == 0;
61
294k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
40.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
40.3k
            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
40.3k
        return true;
70
253k
    } else if (all_finished) {
71
230k
        *eos = true;
72
230k
    } else {
73
23.0k
        std::unique_lock l(*_m[channel_id]);
74
23.0k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
9
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
9
                local_state->_shared_state->sub_mem_usage(channel_id,
78
9
                                                          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
9
            return true;
84
9
        }
85
23.0k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
23.0k
        local_state->_dependency->block();
87
23.0k
    }
88
253k
    return false;
89
294k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
56
1.66M
                                         int channel_id) {
57
1.66M
    if (local_state == nullptr) {
58
12
        return _dequeue_data(block, eos, data_block, channel_id);
59
12
    }
60
1.66M
    bool all_finished = _running_sink_operators == 0;
61
1.66M
    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
218k
        } else {
66
218k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
218k
            data_block->swap(block->_data_block);
68
218k
        }
69
218k
        return true;
70
1.44M
    } else if (all_finished) {
71
1.33M
        *eos = true;
72
1.33M
    } else {
73
115k
        std::unique_lock l(*_m[channel_id]);
74
115k
        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
11
            } else {
80
11
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
11
                data_block->swap(block->_data_block);
82
11
            }
83
11
            return true;
84
11
        }
85
115k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
115k
        local_state->_dependency->block();
87
115k
    }
88
1.44M
    return false;
89
1.66M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
58.6k
                                         int channel_id) {
57
58.6k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
58.6k
    bool all_finished = _running_sink_operators == 0;
61
58.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
27.4k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
27.4k
            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
27.4k
        return true;
70
31.2k
    } else if (all_finished) {
71
24.8k
        *eos = true;
72
24.8k
    } else {
73
6.34k
        std::unique_lock l(*_m[channel_id]);
74
6.34k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
11
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
11
                local_state->_shared_state->sub_mem_usage(channel_id,
78
11
                                                          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
11
            return true;
84
11
        }
85
6.33k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
6.33k
        local_state->_dependency->block();
87
6.33k
    }
88
31.1k
    return false;
89
58.6k
}
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
135k
                              SinkInfo& sink_info) {
117
135k
    if (in_block->empty()) {
118
116k
        return Status::OK();
119
116k
    }
120
19.1k
    {
121
19.1k
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
19.1k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
19.1k
    }
124
19.1k
    {
125
19.1k
        SCOPED_TIMER(profile.distribute_timer);
126
19.1k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
19.1k
                                    *sink_info.channel_id, sink_info.local_state,
128
19.1k
                                    sink_info.shuffle_idx_to_instance_idx));
129
19.1k
    }
130
131
19.1k
    sink_info.local_state->_memory_used_counter->set(
132
19.1k
            sink_info.local_state->_shared_state->mem_usage);
133
19.1k
    return Status::OK();
134
19.1k
}
135
136
117k
void ShuffleExchanger::close(SourceInfo&& source_info) {
137
117k
    PartitionedBlock partitioned_block;
138
117k
    bool eos;
139
117k
    Block block;
140
117k
    _data_queue[source_info.channel_id].set_eos();
141
117k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
117k
                         source_info.channel_id)) {
143
        // do nothing
144
5
    }
145
117k
}
146
147
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
148
138k
                                   SourceInfo&& source_info) {
149
138k
    PartitionedBlock partitioned_block;
150
138k
    MutableBlock mutable_block;
151
152
138k
    auto get_data = [&]() -> Status {
153
40.2k
        do {
154
40.2k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
40.2k
                                       partitioned_block.second.offset_start;
156
40.2k
            auto block_wrapper = partitioned_block.first;
157
40.2k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
40.2k
                                                   offset_start + partitioned_block.second.length));
159
40.2k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
40.2k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
38.8k
                               source_info.channel_id));
162
31.5k
        return Status::OK();
163
31.5k
    };
164
165
138k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
138k
                      source_info.channel_id)) {
167
31.5k
        SCOPED_TIMER(profile.copy_data_timer);
168
31.5k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
31.5k
                block, partitioned_block.first->_data_block);
170
31.5k
        RETURN_IF_ERROR(get_data());
171
31.5k
    }
172
138k
    return Status::OK();
173
138k
}
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.9k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
18.9k
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
18.9k
    const auto rows = cast_set<int32_t>(block->rows());
183
18.9k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
18.9k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
18.9k
    {
186
18.9k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
16.2M
        for (int32_t i = 0; i < rows; ++i) {
188
16.2M
            partition_rows_histogram[channel_ids[i]]++;
189
16.2M
        }
190
172k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
153k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
153k
        }
193
15.3M
        for (int32_t i = rows - 1; i >= 0; --i) {
194
15.3M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
15.3M
            partition_rows_histogram[channel_ids[i]]--;
196
15.3M
        }
197
18.9k
    }
198
199
18.9k
    Block data_block;
200
18.9k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
18.9k
    if (!_free_blocks.try_dequeue(data_block)) {
202
11.5k
        data_block = block->clone_empty();
203
11.5k
    }
204
18.9k
    data_block.swap(*block);
205
18.9k
    new_block_wrapper =
206
18.9k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
18.9k
    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.9k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
18.9k
    const auto& map = *shuffle_idx_to_instance_idx;
218
18.9k
    int32_t enqueue_rows = 0;
219
151k
    for (const auto& it : map) {
220
151k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
221
2
                << it.first << " : " << it.second << " " << _num_partitions;
222
151k
        uint32_t start = partition_rows_histogram[it.first];
223
151k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
151k
        if (size > 0) {
225
40.3k
            enqueue_rows += size;
226
40.3k
            _enqueue_data_and_set_ready(
227
40.3k
                    it.second, local_state,
228
40.3k
                    {new_block_wrapper,
229
40.3k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
40.3k
        }
231
151k
    }
232
18.9k
    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.9k
    return Status::OK();
246
18.9k
}
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
341k
                                  SinkInfo& sink_info) {
292
341k
    if (in_block->empty()) {
293
131k
        return Status::OK();
294
131k
    }
295
210k
    Block new_block;
296
210k
    if (!_free_blocks.try_dequeue(new_block)) {
297
96.4k
        new_block = {in_block->clone_empty()};
298
96.4k
    }
299
210k
    new_block.swap(*in_block);
300
210k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
210k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
210k
            std::move(new_block),
303
210k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
210k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
210k
    sink_info.local_state->_memory_used_counter->set(
308
210k
            sink_info.local_state->_shared_state->mem_usage);
309
310
210k
    return Status::OK();
311
341k
}
312
313
653k
void PassthroughExchanger::close(SourceInfo&& source_info) {
314
653k
    Block next_block;
315
653k
    BlockWrapperSPtr wrapper;
316
653k
    bool eos;
317
653k
    _data_queue[source_info.channel_id].set_eos();
318
655k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
319
655k
                         source_info.channel_id)) {
320
        // do nothing
321
2.05k
    }
322
653k
}
323
324
11.9k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
11.9k
    Block next_block;
326
11.9k
    BlockWrapperSPtr wrapper;
327
11.9k
    bool eos;
328
11.9k
    _data_queue[source_info.channel_id].set_eos();
329
11.9k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
11.9k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
11.9k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
974k
                                       Profile&& profile, SourceInfo&& source_info) {
337
974k
    BlockWrapperSPtr next_block;
338
974k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
974k
    return Status::OK();
340
974k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
4.65k
                                SinkInfo& sink_info) {
344
4.65k
    if (in_block->empty()) {
345
1.71k
        return Status::OK();
346
1.71k
    }
347
2.94k
    Block new_block;
348
2.94k
    if (!_free_blocks.try_dequeue(new_block)) {
349
2.00k
        new_block = {in_block->clone_empty()};
350
2.00k
    }
351
2.94k
    new_block.swap(*in_block);
352
353
2.94k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
2.94k
            std::move(new_block),
355
2.94k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
2.94k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
2.94k
    sink_info.local_state->_memory_used_counter->set(
359
2.94k
            sink_info.local_state->_shared_state->mem_usage);
360
361
2.94k
    return Status::OK();
362
4.65k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
5.34k
                                     Profile&& profile, SourceInfo&& source_info) {
366
5.34k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
5.34k
    BlockWrapperSPtr next_block;
371
5.34k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
5.34k
    return Status::OK();
373
5.34k
}
374
375
128k
void ExchangerBase::finalize() {
376
128k
    DCHECK(_running_source_operators == 0);
377
128k
    Block block;
378
241k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
112k
    }
381
128k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
5.14k
                                SinkInfo& sink_info) {
385
5.14k
    if (in_block->empty()) {
386
1.68k
        return Status::OK();
387
1.68k
    }
388
3.45k
    Block new_block;
389
3.45k
    if (!_free_blocks.try_dequeue(new_block)) {
390
2.28k
        new_block = {in_block->clone_empty()};
391
2.28k
    }
392
3.45k
    new_block.swap(*in_block);
393
3.45k
    auto wrapper = BlockWrapper::create_shared(
394
3.45k
            std::move(new_block),
395
3.45k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
30.9k
    for (int i = 0; i < _num_partitions; i++) {
397
27.4k
        _enqueue_data_and_set_ready(
398
27.4k
                i, sink_info.local_state,
399
27.4k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
27.4k
    }
401
402
3.45k
    return Status::OK();
403
5.14k
}
404
405
12.4k
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
12.4k
    BroadcastBlock partitioned_block;
407
12.4k
    bool eos;
408
12.4k
    Block block;
409
12.4k
    _data_queue[source_info.channel_id].set_eos();
410
12.4k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
12.4k
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
12.4k
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
46.1k
                                     Profile&& profile, SourceInfo&& source_info) {
418
46.1k
    BroadcastBlock partitioned_block;
419
420
46.1k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
46.1k
                      source_info.channel_id)) {
422
27.4k
        SCOPED_TIMER(profile.copy_data_timer);
423
27.4k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
27.4k
                block, partitioned_block.first->_data_block);
425
27.4k
        auto block_wrapper = partitioned_block.first;
426
27.4k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
27.4k
                                               partitioned_block.second.offset_start,
428
27.4k
                                               partitioned_block.second.length));
429
27.4k
    }
430
431
46.1k
    return Status::OK();
432
46.1k
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
597
                                                       SinkInfo& sink_info) {
436
597
    Block new_block;
437
597
    if (!_free_blocks.try_dequeue(new_block)) {
438
294
        new_block = {in_block->clone_empty()};
439
294
    }
440
597
    new_block.swap(*in_block);
441
597
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
597
    _enqueue_data_and_set_ready(
443
597
            channel_id, sink_info.local_state,
444
597
            BlockWrapper::create_shared(
445
597
                    std::move(new_block),
446
597
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
597
                    channel_id));
448
449
597
    sink_info.local_state->_memory_used_counter->set(
450
597
            sink_info.local_state->_shared_state->mem_usage);
451
597
    return Status::OK();
452
597
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
2.02k
                                                   SinkInfo& sink_info) {
456
2.02k
    std::vector<uint32_t> channel_ids;
457
2.02k
    const auto num_rows = block->rows();
458
2.02k
    channel_ids.resize(num_rows, 0);
459
2.02k
    if (num_rows <= _num_partitions) {
460
1.71k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
1.71k
    } else {
462
312
        size_t i = 0;
463
19.3k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
19.0k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
19.0k
        }
466
312
        if (i < num_rows - 1) {
467
161
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
161
        }
469
312
    }
470
471
2.02k
    sink_info.local_state->_memory_used_counter->set(
472
2.02k
            sink_info.local_state->_shared_state->mem_usage);
473
2.02k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
2.02k
    return Status::OK();
475
2.02k
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
2.02k
                                                 Block* block, SinkInfo& sink_info) {
480
2.02k
    const auto rows = cast_set<int32_t>(block->rows());
481
2.02k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
2.02k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
2.02k
    {
484
2.02k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
65.3k
        for (int32_t i = 0; i < rows; ++i) {
486
63.3k
            partition_rows_histogram[channel_ids[i]]++;
487
63.3k
        }
488
14.3k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
12.3k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
12.3k
        }
491
492
65.5k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
63.5k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
63.5k
            partition_rows_histogram[channel_ids[i]]--;
495
63.5k
        }
496
2.02k
    }
497
14.3k
    for (int32_t i = 0; i < _num_partitions; i++) {
498
12.3k
        const size_t start = partition_rows_histogram[i];
499
12.3k
        const size_t size = partition_rows_histogram[i + 1] - start;
500
12.3k
        if (size > 0) {
501
5.33k
            std::unique_ptr<MutableBlock> mutable_block =
502
5.33k
                    MutableBlock::create_unique(block->clone_empty());
503
5.33k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
5.33k
            auto new_block = mutable_block->to_block();
505
506
5.33k
            _enqueue_data_and_set_ready(
507
5.33k
                    i, sink_info.local_state,
508
5.33k
                    BlockWrapper::create_shared(
509
5.33k
                            std::move(new_block),
510
5.33k
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
5.33k
                            i));
512
5.33k
        }
513
12.3k
    }
514
2.02k
    return Status::OK();
515
2.02k
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
7.61k
                                          Profile&& profile, SinkInfo& sink_info) {
519
7.61k
    if (in_block->empty()) {
520
4.98k
        return Status::OK();
521
4.98k
    }
522
2.62k
    if (_is_pass_through) {
523
597
        return _passthrough_sink(state, in_block, sink_info);
524
2.03k
    } else {
525
2.03k
        if (++_total_block >= _num_partitions) {
526
282
            _is_pass_through = true;
527
282
        }
528
2.03k
        return _shuffle_sink(state, in_block, sink_info);
529
2.03k
    }
530
2.62k
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
12.1k
                                               Profile&& profile, SourceInfo&& source_info) {
534
12.1k
    BlockWrapperSPtr next_block;
535
12.1k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
12.1k
    return Status::OK();
537
12.1k
}
538
539
5.71k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
5.71k
    Block next_block;
541
5.71k
    bool eos;
542
5.71k
    BlockWrapperSPtr wrapper;
543
5.71k
    _data_queue[source_info.channel_id].set_eos();
544
5.71k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
5.71k
                         source_info.channel_id)) {
546
        // do nothing
547
2
    }
548
5.71k
}
549
550
} // namespace doris