Coverage Report

Created: 2026-06-09 07:29

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