Coverage Report

Created: 2026-06-27 12:32

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
74.5k
                                                       BlockType&& block) {
31
74.5k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
74.5k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
24.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
24.0k
        block.first->record_channel_id(channel_id);
43
50.5k
    } else {
44
50.5k
        block->record_channel_id(channel_id);
45
50.5k
    }
46
47
74.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
74.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
74.5k
    }
50
74.5k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
22.9k
                                                       BlockType&& block) {
31
22.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
22.9k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
22.9k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
22.9k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
22.9k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
22.9k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
22.9k
    }
50
22.9k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
50.5k
                                                       BlockType&& block) {
31
50.5k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
50.5k
    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
50.5k
    } else {
44
50.5k
        block->record_channel_id(channel_id);
45
50.5k
    }
46
47
50.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
50.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
50.5k
    }
50
50.5k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
1.10k
                                                       BlockType&& block) {
31
1.10k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
1.10k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
1.10k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
1.10k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
1.10k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
1.08k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
1.08k
    }
50
1.10k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
417k
                                         int channel_id) {
56
417k
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
417k
    bool all_finished = _running_sink_operators == 0;
60
417k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
23.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
23.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
50.5k
        } else {
65
50.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
50.5k
            data_block->swap(block->_data_block);
67
50.5k
        }
68
74.5k
        return true;
69
342k
    } else if (all_finished) {
70
298k
        *eos = true;
71
298k
    } else {
72
44.6k
        std::unique_lock l(*_m[channel_id]);
73
44.6k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
2
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
2
                local_state->_shared_state->sub_mem_usage(channel_id,
77
2
                                                          block.first->_allocated_bytes);
78
2
            } else {
79
0
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
0
                data_block->swap(block->_data_block);
81
0
            }
82
2
            return true;
83
2
        }
84
44.6k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
44.6k
        local_state->_dependency->block();
86
44.6k
    }
87
342k
    return false;
88
417k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
42.2k
                                         int channel_id) {
56
42.2k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
42.2k
    bool all_finished = _running_sink_operators == 0;
60
42.2k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
22.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
22.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
22.9k
        return true;
69
22.9k
    } else if (all_finished) {
70
11.8k
        *eos = true;
71
11.8k
    } else {
72
7.51k
        std::unique_lock l(*_m[channel_id]);
73
7.51k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
2
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
2
                local_state->_shared_state->sub_mem_usage(channel_id,
77
2
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
2
            return true;
83
2
        }
84
7.50k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
7.50k
        local_state->_dependency->block();
86
7.50k
    }
87
19.3k
    return false;
88
42.2k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
372k
                                         int channel_id) {
56
372k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
372k
    bool all_finished = _running_sink_operators == 0;
60
372k
    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
50.5k
        } else {
65
50.5k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
50.5k
            data_block->swap(block->_data_block);
67
50.5k
        }
68
50.5k
        return true;
69
322k
    } else if (all_finished) {
70
285k
        *eos = true;
71
285k
    } else {
72
36.7k
        std::unique_lock l(*_m[channel_id]);
73
36.7k
        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
0
            } else {
79
0
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
0
                data_block->swap(block->_data_block);
81
0
            }
82
0
            return true;
83
0
        }
84
36.7k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
36.7k
        local_state->_dependency->block();
86
36.7k
    }
87
322k
    return false;
88
372k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
2.45k
                                         int channel_id) {
56
2.45k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
2.44k
    bool all_finished = _running_sink_operators == 0;
60
2.44k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
1.08k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
1.08k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
1.08k
        return true;
69
1.36k
    } else if (all_finished) {
70
932
        *eos = true;
71
932
    } else {
72
428
        std::unique_lock l(*_m[channel_id]);
73
428
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
0
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
0
                local_state->_shared_state->sub_mem_usage(channel_id,
77
0
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
0
            return true;
83
0
        }
84
428
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
428
        local_state->_dependency->block();
86
428
    }
87
1.36k
    return false;
88
2.44k
}
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
12.5k
                              SinkInfo& sink_info) {
116
12.5k
    if (in_block->empty()) {
117
5.30k
        return Status::OK();
118
5.30k
    }
119
7.21k
    {
120
7.21k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
7.21k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
7.21k
    }
123
7.21k
    {
124
7.21k
        SCOPED_TIMER(profile.distribute_timer);
125
7.21k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
7.21k
                                    *sink_info.channel_id, sink_info.local_state,
127
7.21k
                                    sink_info.shuffle_idx_to_instance_idx));
128
7.21k
    }
129
130
7.21k
    sink_info.local_state->_memory_used_counter->set(
131
7.21k
            sink_info.local_state->_shared_state->mem_usage);
132
7.21k
    return Status::OK();
133
7.21k
}
134
135
5.50k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
5.50k
    PartitionedBlock partitioned_block;
137
5.50k
    bool eos;
138
5.50k
    Block block;
139
5.50k
    _data_queue[source_info.channel_id].set_eos();
140
5.50k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
5.50k
                         source_info.channel_id)) {
142
        // do nothing
143
0
    }
144
5.50k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
15.8k
                                   SourceInfo&& source_info) {
148
15.8k
    PartitionedBlock partitioned_block;
149
15.8k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
15.8k
                      source_info.channel_id)) {
151
11.1k
        SCOPED_TIMER(profile.copy_data_timer);
152
11.1k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
11.1k
                block, partitioned_block.first->_data_block);
154
11.1k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
22.3k
        do {
156
22.3k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
22.3k
                                       partitioned_block.second.offset_start;
158
22.3k
            auto block_wrapper = partitioned_block.first;
159
22.3k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
22.3k
                                                   offset_start + partitioned_block.second.length));
161
22.3k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
22.3k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
19.4k
                               source_info.channel_id));
164
11.1k
    }
165
15.8k
    return Status::OK();
166
15.8k
}
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
7.21k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
7.21k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
7.21k
    const auto rows = cast_set<int32_t>(block->rows());
176
7.21k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
7.21k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
7.21k
    {
179
7.21k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
36.3M
        for (int32_t i = 0; i < rows; ++i) {
181
36.3M
            partition_rows_histogram[channel_ids[i]]++;
182
36.3M
        }
183
64.7k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
57.5k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
57.5k
        }
186
36.2M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
36.2M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
36.2M
            partition_rows_histogram[channel_ids[i]]--;
189
36.2M
        }
190
7.21k
    }
191
192
7.21k
    Block data_block;
193
7.21k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
7.21k
    if (!_free_blocks.try_dequeue(data_block)) {
195
2.49k
        data_block = block->clone_empty();
196
2.49k
    }
197
7.21k
    data_block.swap(*block);
198
7.21k
    new_block_wrapper =
199
7.21k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
7.21k
    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
7.21k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
7.21k
    const auto& map = *shuffle_idx_to_instance_idx;
211
7.21k
    int32_t enqueue_rows = 0;
212
57.5k
    for (const auto& it : map) {
213
57.5k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
0
                << it.first << " : " << it.second << " " << _num_partitions;
215
57.5k
        uint32_t start = partition_rows_histogram[it.first];
216
57.5k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
57.5k
        if (size > 0) {
218
22.4k
            enqueue_rows += size;
219
22.4k
            _enqueue_data_and_set_ready(
220
22.4k
                    it.second, local_state,
221
22.4k
                    {new_block_wrapper,
222
22.4k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
22.4k
        }
224
57.5k
    }
225
7.21k
    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
7.21k
    return Status::OK();
239
7.21k
}
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
69.0k
                                  SinkInfo& sink_info) {
285
69.0k
    if (in_block->empty()) {
286
19.6k
        return Status::OK();
287
19.6k
    }
288
49.4k
    Block new_block;
289
49.4k
    if (!_free_blocks.try_dequeue(new_block)) {
290
17.9k
        new_block = {in_block->clone_empty()};
291
17.9k
    }
292
49.4k
    new_block.swap(*in_block);
293
49.4k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
49.4k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
49.4k
            std::move(new_block),
296
49.4k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
49.4k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
49.4k
    sink_info.local_state->_memory_used_counter->set(
301
49.4k
            sink_info.local_state->_shared_state->mem_usage);
302
303
49.4k
    return Status::OK();
304
69.0k
}
305
306
139k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
139k
    Block next_block;
308
139k
    BlockWrapperSPtr wrapper;
309
139k
    bool eos;
310
139k
    _data_queue[source_info.channel_id].set_eos();
311
139k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
139k
                         source_info.channel_id)) {
313
        // do nothing
314
0
    }
315
139k
}
316
317
5.37k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
5.37k
    Block next_block;
319
5.37k
    BlockWrapperSPtr wrapper;
320
5.37k
    bool eos;
321
5.37k
    _data_queue[source_info.channel_id].set_eos();
322
5.37k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
5.37k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
5.37k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
225k
                                       Profile&& profile, SourceInfo&& source_info) {
330
225k
    BlockWrapperSPtr next_block;
331
225k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
225k
    return Status::OK();
333
225k
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
1.79k
                                SinkInfo& sink_info) {
337
1.79k
    if (in_block->empty()) {
338
672
        return Status::OK();
339
672
    }
340
1.12k
    Block new_block;
341
1.12k
    if (!_free_blocks.try_dequeue(new_block)) {
342
711
        new_block = {in_block->clone_empty()};
343
711
    }
344
1.12k
    new_block.swap(*in_block);
345
346
1.12k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
1.12k
            std::move(new_block),
348
1.12k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
1.12k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
1.12k
    sink_info.local_state->_memory_used_counter->set(
352
1.12k
            sink_info.local_state->_shared_state->mem_usage);
353
354
1.12k
    return Status::OK();
355
1.79k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
2.16k
                                     Profile&& profile, SourceInfo&& source_info) {
359
2.16k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
2.16k
    BlockWrapperSPtr next_block;
364
2.16k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
2.16k
    return Status::OK();
366
2.16k
}
367
368
18.9k
void ExchangerBase::finalize() {
369
18.9k
    DCHECK(_running_source_operators == 0);
370
18.9k
    Block block;
371
39.1k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
20.1k
    }
374
18.9k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
204
                                SinkInfo& sink_info) {
378
204
    if (in_block->empty()) {
379
58
        return Status::OK();
380
58
    }
381
146
    Block new_block;
382
146
    if (!_free_blocks.try_dequeue(new_block)) {
383
81
        new_block = {in_block->clone_empty()};
384
81
    }
385
146
    new_block.swap(*in_block);
386
146
    auto wrapper = BlockWrapper::create_shared(
387
146
            std::move(new_block),
388
146
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
1.25k
    for (int i = 0; i < _num_partitions; i++) {
390
1.10k
        _enqueue_data_and_set_ready(
391
1.10k
                i, sink_info.local_state,
392
1.10k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
1.10k
    }
394
395
146
    return Status::OK();
396
204
}
397
398
468
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
468
    BroadcastBlock partitioned_block;
400
468
    bool eos;
401
468
    Block block;
402
468
    _data_queue[source_info.channel_id].set_eos();
403
468
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
468
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
468
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
1.98k
                                     Profile&& profile, SourceInfo&& source_info) {
411
1.98k
    BroadcastBlock partitioned_block;
412
413
1.98k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
1.98k
                      source_info.channel_id)) {
415
1.08k
        SCOPED_TIMER(profile.copy_data_timer);
416
1.08k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
1.08k
                block, partitioned_block.first->_data_block);
418
1.08k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
1.08k
        auto block_wrapper = partitioned_block.first;
420
1.08k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
1.08k
                                               partitioned_block.second.offset_start,
422
1.08k
                                               partitioned_block.second.length));
423
1.08k
    }
424
425
1.98k
    return Status::OK();
426
1.98k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
20
                                                       SinkInfo& sink_info) {
430
20
    Block new_block;
431
20
    if (!_free_blocks.try_dequeue(new_block)) {
432
13
        new_block = {in_block->clone_empty()};
433
13
    }
434
20
    new_block.swap(*in_block);
435
20
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
20
    _enqueue_data_and_set_ready(
437
20
            channel_id, sink_info.local_state,
438
20
            {BlockWrapper::create_shared(
439
20
                     std::move(new_block),
440
20
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
20
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
20
    sink_info.local_state->_memory_used_counter->set(
444
20
            sink_info.local_state->_shared_state->mem_usage);
445
20
    return Status::OK();
446
20
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
182
                                                   SinkInfo& sink_info) {
450
182
    std::vector<uint32_t> channel_ids;
451
182
    const auto num_rows = block->rows();
452
182
    channel_ids.resize(num_rows, 0);
453
182
    if (num_rows <= _num_partitions) {
454
178
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
178
    } else {
456
4
        size_t i = 0;
457
12
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
8
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
8
        }
460
4
        if (i < num_rows - 1) {
461
4
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
4
        }
463
4
    }
464
465
182
    sink_info.local_state->_memory_used_counter->set(
466
182
            sink_info.local_state->_shared_state->mem_usage);
467
182
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
182
    return Status::OK();
469
182
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
182
                                                 Block* block, SinkInfo& sink_info) {
474
182
    const auto rows = cast_set<int32_t>(block->rows());
475
182
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
182
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
182
    {
478
182
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
710
        for (int32_t i = 0; i < rows; ++i) {
480
528
            partition_rows_histogram[channel_ids[i]]++;
481
528
        }
482
1.62k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
1.44k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
1.44k
        }
485
486
710
        for (int32_t i = rows - 1; i >= 0; --i) {
487
528
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
488
528
            partition_rows_histogram[channel_ids[i]]--;
489
528
        }
490
182
    }
491
182
    Block data_block;
492
182
    if (!_free_blocks.try_dequeue(data_block)) {
493
178
        data_block = block->clone_empty();
494
178
    }
495
182
    data_block.swap(*block);
496
182
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
182
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
182
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
1.62k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
1.44k
        const uint32_t start = partition_rows_histogram[i];
503
1.44k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
1.44k
        if (size > 0) {
505
496
            _enqueue_data_and_set_ready(
506
496
                    i, sink_info.local_state,
507
496
                    {new_block_wrapper,
508
496
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
496
        }
510
1.44k
    }
511
182
    return Status::OK();
512
182
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
666
                                          Profile&& profile, SinkInfo& sink_info) {
516
666
    if (in_block->empty()) {
517
462
        return Status::OK();
518
462
    }
519
204
    if (_is_pass_through) {
520
20
        return _passthrough_sink(state, in_block, sink_info);
521
184
    } else {
522
184
        if (++_total_block >= _num_partitions) {
523
1
            _is_pass_through = true;
524
1
        }
525
184
        return _shuffle_sink(state, in_block, sink_info);
526
184
    }
527
204
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
528
                                               Profile&& profile, SourceInfo&& source_info) {
531
528
    if (!_tmp_block[source_info.channel_id].empty()) {
532
4
        *block = std::move(_tmp_block[source_info.channel_id]);
533
4
        *eos = _tmp_eos[source_info.channel_id];
534
4
        _tmp_block[source_info.channel_id] = {};
535
4
        return Status::OK();
536
4
    }
537
524
    PartitionedBlock partitioned_block;
538
524
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
524
                      source_info.channel_id)) {
540
148
        SCOPED_TIMER(profile.copy_data_timer);
541
148
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
148
                block, partitioned_block.first->_data_block);
543
148
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
512
        do {
545
512
            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
16
                if (mutable_block.rows() > 0) {
548
4
                    _tmp_block[source_info.channel_id] =
549
4
                            std::move(partitioned_block.first->_data_block);
550
4
                    _tmp_eos[source_info.channel_id] = *eos;
551
4
                    *eos = false;
552
12
                } else {
553
12
                    scoped_mutable_block.restore();
554
12
                    *block = std::move(partitioned_block.first->_data_block);
555
12
                }
556
16
                break;
557
16
            }
558
496
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
496
                                       partitioned_block.second.offset_start;
560
496
            auto block_wrapper = partitioned_block.first;
561
496
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
496
                                                   offset_start + partitioned_block.second.length));
563
496
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
496
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
496
                               source_info.channel_id));
566
148
    }
567
524
    return Status::OK();
568
524
}
569
570
468
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
468
    PartitionedBlock partitioned_block;
572
468
    bool eos;
573
468
    Block block;
574
468
    _data_queue[source_info.channel_id].set_eos();
575
468
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
468
                         source_info.channel_id)) {
577
        // do nothing
578
0
    }
579
468
}
580
581
} // namespace doris