Coverage Report

Created: 2026-06-03 15: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
template <typename BlockType>
28
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
29
                                                       LocalExchangeSinkLocalState* local_state,
30
191k
                                                       BlockType&& block) {
31
191k
    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
191k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
46.7k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
46.7k
        block.first->record_channel_id(channel_id);
43
145k
    } else {
44
145k
        block->record_channel_id(channel_id);
45
145k
    }
46
47
191k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
191k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
191k
    }
50
191k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
33.6k
                                                       BlockType&& block) {
31
33.6k
    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
33.6k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
33.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
33.6k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
33.6k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
33.6k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
33.6k
    }
50
33.6k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
145k
                                                       BlockType&& block) {
31
145k
    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
145k
    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
145k
    } else {
44
145k
        block->record_channel_id(channel_id);
45
145k
    }
46
47
145k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
145k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
145k
    }
50
145k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
13.1k
                                                       BlockType&& block) {
31
13.1k
    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
13.1k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
13.1k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
13.1k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
13.1k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
13.1k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
13.1k
    }
50
13.1k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.40M
                                         int channel_id) {
56
1.40M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.40M
    bool all_finished = _running_sink_operators == 0;
60
1.40M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
46.7k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
46.7k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
145k
        } else {
65
145k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
145k
            data_block->swap(block->_data_block);
67
145k
        }
68
191k
        return true;
69
1.21M
    } else if (all_finished) {
70
1.10M
        *eos = true;
71
1.10M
    } else {
72
111k
        std::unique_lock l(*_m[channel_id]);
73
111k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
15
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
15
                local_state->_shared_state->sub_mem_usage(channel_id,
77
15
                                                          block.first->_allocated_bytes);
78
18
            } else {
79
18
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
18
                data_block->swap(block->_data_block);
81
18
            }
82
33
            return true;
83
33
        }
84
111k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
111k
        local_state->_dependency->block();
86
111k
    }
87
1.21M
    return false;
88
1.40M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
257k
                                         int channel_id) {
56
257k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
257k
    bool all_finished = _running_sink_operators == 0;
60
257k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
33.6k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
33.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
33.6k
        return true;
69
224k
    } else if (all_finished) {
70
206k
        *eos = true;
71
206k
    } else {
72
18.1k
        std::unique_lock l(*_m[channel_id]);
73
18.1k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
10
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
10
                local_state->_shared_state->sub_mem_usage(channel_id,
77
10
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
10
            return true;
83
10
        }
84
18.1k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
18.1k
        local_state->_dependency->block();
86
18.1k
    }
87
224k
    return false;
88
257k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.11M
                                         int channel_id) {
56
1.11M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.11M
    bool all_finished = _running_sink_operators == 0;
60
1.11M
    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
145k
        } else {
65
145k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
145k
            data_block->swap(block->_data_block);
67
145k
        }
68
145k
        return true;
69
972k
    } else if (all_finished) {
70
881k
        *eos = true;
71
881k
    } else {
72
90.6k
        std::unique_lock l(*_m[channel_id]);
73
90.6k
        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
18
            } else {
79
18
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
18
                data_block->swap(block->_data_block);
81
18
            }
82
18
            return true;
83
18
        }
84
90.6k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
90.6k
        local_state->_dependency->block();
86
90.6k
    }
87
972k
    return false;
88
1.11M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
29.2k
                                         int channel_id) {
56
29.2k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
29.2k
    bool all_finished = _running_sink_operators == 0;
60
29.2k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
13.1k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
13.1k
            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
13.1k
        return true;
69
16.1k
    } else if (all_finished) {
70
13.5k
        *eos = true;
71
13.5k
    } else {
72
2.53k
        std::unique_lock l(*_m[channel_id]);
73
2.53k
        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
2.53k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
2.53k
        local_state->_dependency->block();
86
2.53k
    }
87
16.1k
    return false;
88
29.2k
}
89
90
template <typename BlockType>
91
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
92
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
93
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
94
0
        block.first->record_channel_id(channel_id);
95
0
    } else {
96
0
        block->record_channel_id(channel_id);
97
0
    }
98
0
    _data_queue[channel_id].enqueue(std::move(block));
99
0
}
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_
Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_
100
101
template <typename BlockType>
102
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
103
20
                                         int channel_id) {
104
20
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
20
    return false;
112
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
8
    return false;
112
8
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
113
114
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
115
114k
                              SinkInfo& sink_info) {
116
114k
    if (in_block->empty()) {
117
99.8k
        return Status::OK();
118
99.8k
    }
119
15.0k
    {
120
15.0k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
15.0k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
15.0k
    }
123
15.0k
    {
124
15.0k
        SCOPED_TIMER(profile.distribute_timer);
125
15.0k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
15.0k
                                    *sink_info.channel_id, sink_info.local_state,
127
15.0k
                                    sink_info.shuffle_idx_to_instance_idx));
128
15.0k
    }
129
130
15.0k
    sink_info.local_state->_memory_used_counter->set(
131
15.0k
            sink_info.local_state->_shared_state->mem_usage);
132
15.0k
    return Status::OK();
133
15.0k
}
134
135
100k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
100k
    PartitionedBlock partitioned_block;
137
100k
    bool eos;
138
100k
    Block block;
139
100k
    _data_queue[source_info.channel_id].set_eos();
140
100k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
100k
                         source_info.channel_id)) {
142
        // do nothing
143
35
    }
144
100k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
117k
                                   SourceInfo&& source_info) {
148
117k
    PartitionedBlock partitioned_block;
149
117k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
150
117k
                      source_info.channel_id)) {
151
23.4k
        SCOPED_TIMER(profile.copy_data_timer);
152
23.4k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
153
23.4k
                block, partitioned_block.first->_data_block);
154
23.4k
        auto& mutable_block = scoped_mutable_block.mutable_block();
155
30.0k
        do {
156
30.0k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
157
30.0k
                                       partitioned_block.second.offset_start;
158
30.0k
            auto block_wrapper = partitioned_block.first;
159
30.0k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
160
30.0k
                                                   offset_start + partitioned_block.second.length));
161
30.0k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
162
30.0k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
163
29.5k
                               source_info.channel_id));
164
23.4k
    }
165
117k
    return Status::OK();
166
117k
}
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
15.0k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
172
15.0k
    if (local_state == nullptr) {
173
0
        return _split_rows(state, channel_ids, block, channel_id);
174
0
    }
175
15.0k
    const auto rows = cast_set<int32_t>(block->rows());
176
15.0k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
177
15.0k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
178
15.0k
    {
179
15.0k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
180
11.2M
        for (int32_t i = 0; i < rows; ++i) {
181
11.2M
            partition_rows_histogram[channel_ids[i]]++;
182
11.2M
        }
183
141k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
184
126k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
185
126k
        }
186
10.9M
        for (int32_t i = rows - 1; i >= 0; --i) {
187
10.9M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
188
10.9M
            partition_rows_histogram[channel_ids[i]]--;
189
10.9M
        }
190
15.0k
    }
191
192
15.0k
    Block data_block;
193
15.0k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
194
15.0k
    if (!_free_blocks.try_dequeue(data_block)) {
195
8.32k
        data_block = block->clone_empty();
196
8.32k
    }
197
15.0k
    data_block.swap(*block);
198
15.0k
    new_block_wrapper =
199
15.0k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
200
15.0k
    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
15.0k
    DCHECK(shuffle_idx_to_instance_idx && !shuffle_idx_to_instance_idx->empty());
210
15.0k
    const auto& map = *shuffle_idx_to_instance_idx;
211
15.0k
    int32_t enqueue_rows = 0;
212
126k
    for (const auto& it : map) {
213
126k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
214
8
                << it.first << " : " << it.second << " " << _num_partitions;
215
126k
        uint32_t start = partition_rows_histogram[it.first];
216
126k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
217
126k
        if (size > 0) {
218
30.0k
            enqueue_rows += size;
219
30.0k
            _enqueue_data_and_set_ready(
220
30.0k
                    it.second, local_state,
221
30.0k
                    {new_block_wrapper,
222
30.0k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
223
30.0k
        }
224
126k
    }
225
15.0k
    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
15.0k
    return Status::OK();
239
15.0k
}
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
220k
                                  SinkInfo& sink_info) {
285
220k
    if (in_block->empty()) {
286
76.5k
        return Status::OK();
287
76.5k
    }
288
143k
    Block new_block;
289
143k
    if (!_free_blocks.try_dequeue(new_block)) {
290
59.8k
        new_block = {in_block->clone_empty()};
291
59.8k
    }
292
143k
    new_block.swap(*in_block);
293
143k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
294
143k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
295
143k
            std::move(new_block),
296
143k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
297
298
143k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
299
300
143k
    sink_info.local_state->_memory_used_counter->set(
301
143k
            sink_info.local_state->_shared_state->mem_usage);
302
303
143k
    return Status::OK();
304
220k
}
305
306
436k
void PassthroughExchanger::close(SourceInfo&& source_info) {
307
436k
    Block next_block;
308
436k
    BlockWrapperSPtr wrapper;
309
436k
    bool eos;
310
436k
    _data_queue[source_info.channel_id].set_eos();
311
438k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
312
438k
                         source_info.channel_id)) {
313
        // do nothing
314
1.84k
    }
315
436k
}
316
317
6.37k
void PassToOneExchanger::close(SourceInfo&& source_info) {
318
6.37k
    Block next_block;
319
6.37k
    BlockWrapperSPtr wrapper;
320
6.37k
    bool eos;
321
6.37k
    _data_queue[source_info.channel_id].set_eos();
322
6.37k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
323
6.37k
                         source_info.channel_id)) {
324
        // do nothing
325
0
    }
326
6.37k
}
327
328
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
329
667k
                                       Profile&& profile, SourceInfo&& source_info) {
330
667k
    BlockWrapperSPtr next_block;
331
667k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
332
667k
    return Status::OK();
333
667k
}
334
335
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
336
2.29k
                                SinkInfo& sink_info) {
337
2.29k
    if (in_block->empty()) {
338
794
        return Status::OK();
339
794
    }
340
1.49k
    Block new_block;
341
1.49k
    if (!_free_blocks.try_dequeue(new_block)) {
342
884
        new_block = {in_block->clone_empty()};
343
884
    }
344
1.49k
    new_block.swap(*in_block);
345
346
1.49k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
347
1.49k
            std::move(new_block),
348
18.4E
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
349
1.49k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
350
351
1.49k
    sink_info.local_state->_memory_used_counter->set(
352
1.49k
            sink_info.local_state->_shared_state->mem_usage);
353
354
1.49k
    return Status::OK();
355
2.29k
}
356
357
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
358
2.70k
                                     Profile&& profile, SourceInfo&& source_info) {
359
2.70k
    if (source_info.channel_id != 0) {
360
3
        *eos = true;
361
3
        return Status::OK();
362
3
    }
363
2.70k
    BlockWrapperSPtr next_block;
364
2.70k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
365
2.70k
    return Status::OK();
366
2.70k
}
367
368
98.9k
void ExchangerBase::finalize() {
369
98.9k
    DCHECK(_running_source_operators == 0);
370
98.9k
    Block block;
371
168k
    while (_free_blocks.try_dequeue(block)) {
372
        // do nothing
373
69.4k
    }
374
98.9k
}
375
376
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
377
2.33k
                                SinkInfo& sink_info) {
378
2.33k
    if (in_block->empty()) {
379
859
        return Status::OK();
380
859
    }
381
1.47k
    Block new_block;
382
1.47k
    if (!_free_blocks.try_dequeue(new_block)) {
383
1.18k
        new_block = {in_block->clone_empty()};
384
1.18k
    }
385
1.47k
    new_block.swap(*in_block);
386
1.47k
    auto wrapper = BlockWrapper::create_shared(
387
1.47k
            std::move(new_block),
388
1.47k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
389
14.6k
    for (int i = 0; i < _num_partitions; i++) {
390
13.1k
        _enqueue_data_and_set_ready(
391
13.1k
                i, sink_info.local_state,
392
13.1k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
393
13.1k
    }
394
395
1.47k
    return Status::OK();
396
2.33k
}
397
398
6.79k
void BroadcastExchanger::close(SourceInfo&& source_info) {
399
6.79k
    BroadcastBlock partitioned_block;
400
6.79k
    bool eos;
401
6.79k
    Block block;
402
6.79k
    _data_queue[source_info.channel_id].set_eos();
403
6.79k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
404
6.79k
                         source_info.channel_id)) {
405
        // do nothing
406
0
    }
407
6.79k
}
408
409
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
410
22.4k
                                     Profile&& profile, SourceInfo&& source_info) {
411
22.4k
    BroadcastBlock partitioned_block;
412
413
22.4k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
414
22.4k
                      source_info.channel_id)) {
415
13.1k
        SCOPED_TIMER(profile.copy_data_timer);
416
13.1k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
417
13.1k
                block, partitioned_block.first->_data_block);
418
13.1k
        auto& mutable_block = scoped_mutable_block.mutable_block();
419
13.1k
        auto block_wrapper = partitioned_block.first;
420
13.1k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
421
13.1k
                                               partitioned_block.second.offset_start,
422
13.1k
                                               partitioned_block.second.length));
423
13.1k
    }
424
425
22.4k
    return Status::OK();
426
22.4k
}
427
428
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
429
608
                                                       SinkInfo& sink_info) {
430
608
    Block new_block;
431
608
    if (!_free_blocks.try_dequeue(new_block)) {
432
362
        new_block = {in_block->clone_empty()};
433
362
    }
434
608
    new_block.swap(*in_block);
435
608
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
436
608
    _enqueue_data_and_set_ready(
437
608
            channel_id, sink_info.local_state,
438
608
            {BlockWrapper::create_shared(
439
608
                     std::move(new_block),
440
608
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
441
608
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
442
443
608
    sink_info.local_state->_memory_used_counter->set(
444
608
            sink_info.local_state->_shared_state->mem_usage);
445
608
    return Status::OK();
446
608
}
447
448
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
449
1.25k
                                                   SinkInfo& sink_info) {
450
1.25k
    std::vector<uint32_t> channel_ids;
451
1.25k
    const auto num_rows = block->rows();
452
1.25k
    channel_ids.resize(num_rows, 0);
453
1.25k
    if (num_rows <= _num_partitions) {
454
966
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
455
966
    } else {
456
293
        size_t i = 0;
457
27.5k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
458
27.2k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
459
27.2k
        }
460
293
        if (i < num_rows - 1) {
461
185
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
462
185
        }
463
293
    }
464
465
1.25k
    sink_info.local_state->_memory_used_counter->set(
466
1.25k
            sink_info.local_state->_shared_state->mem_usage);
467
1.25k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
468
1.25k
    return Status::OK();
469
1.25k
}
470
471
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
472
                                                 const std::vector<uint32_t>& channel_ids,
473
1.25k
                                                 Block* block, SinkInfo& sink_info) {
474
1.25k
    const auto rows = cast_set<int32_t>(block->rows());
475
1.25k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
476
1.25k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
477
1.25k
    {
478
1.25k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
479
81.0k
        for (int32_t i = 0; i < rows; ++i) {
480
79.8k
            partition_rows_histogram[channel_ids[i]]++;
481
79.8k
        }
482
7.95k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
483
6.69k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
484
6.69k
        }
485
486
80.3k
        for (int32_t i = rows - 1; i >= 0; --i) {
487
79.1k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
488
79.1k
            partition_rows_histogram[channel_ids[i]]--;
489
79.1k
        }
490
1.25k
    }
491
1.25k
    Block data_block;
492
1.25k
    if (!_free_blocks.try_dequeue(data_block)) {
493
1.11k
        data_block = block->clone_empty();
494
1.11k
    }
495
1.25k
    data_block.swap(*block);
496
1.25k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
497
1.25k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
498
1.25k
    if (new_block_wrapper->_data_block.empty()) {
499
0
        return Status::OK();
500
0
    }
501
7.95k
    for (int32_t i = 0; i < _num_partitions; i++) {
502
6.69k
        const uint32_t start = partition_rows_histogram[i];
503
6.69k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
504
6.69k
        if (size > 0) {
505
2.95k
            _enqueue_data_and_set_ready(
506
2.95k
                    i, sink_info.local_state,
507
2.95k
                    {new_block_wrapper,
508
2.95k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
509
2.95k
        }
510
6.69k
    }
511
1.25k
    return Status::OK();
512
1.25k
}
513
514
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
515
4.18k
                                          Profile&& profile, SinkInfo& sink_info) {
516
4.18k
    if (in_block->empty()) {
517
2.31k
        return Status::OK();
518
2.31k
    }
519
1.87k
    if (_is_pass_through) {
520
608
        return _passthrough_sink(state, in_block, sink_info);
521
1.26k
    } else {
522
1.26k
        if (++_total_block >= _num_partitions) {
523
289
            _is_pass_through = true;
524
289
        }
525
1.26k
        return _shuffle_sink(state, in_block, sink_info);
526
1.26k
    }
527
1.87k
}
528
529
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
530
4.56k
                                               Profile&& profile, SourceInfo&& source_info) {
531
4.56k
    if (!_tmp_block[source_info.channel_id].empty()) {
532
203
        *block = std::move(_tmp_block[source_info.channel_id]);
533
203
        *eos = _tmp_eos[source_info.channel_id];
534
203
        _tmp_block[source_info.channel_id] = {};
535
203
        return Status::OK();
536
203
    }
537
4.35k
    PartitionedBlock partitioned_block;
538
4.35k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
539
4.35k
                      source_info.channel_id)) {
540
2.31k
        SCOPED_TIMER(profile.copy_data_timer);
541
2.31k
        auto scoped_mutable_block = VectorizedUtils::build_scoped_mutable_mem_reuse_block(
542
2.31k
                block, partitioned_block.first->_data_block);
543
2.31k
        auto& mutable_block = scoped_mutable_block.mutable_block();
544
3.55k
        do {
545
3.55k
            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
604
                if (mutable_block.rows() > 0) {
548
203
                    _tmp_block[source_info.channel_id] =
549
203
                            std::move(partitioned_block.first->_data_block);
550
203
                    _tmp_eos[source_info.channel_id] = *eos;
551
203
                    *eos = false;
552
401
                } else {
553
401
                    scoped_mutable_block.restore();
554
401
                    *block = std::move(partitioned_block.first->_data_block);
555
401
                }
556
604
                break;
557
604
            }
558
2.94k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
559
2.94k
                                       partitioned_block.second.offset_start;
560
2.94k
            auto block_wrapper = partitioned_block.first;
561
2.94k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
562
2.94k
                                                   offset_start + partitioned_block.second.length));
563
2.94k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
564
2.94k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
565
2.94k
                               source_info.channel_id));
566
2.31k
    }
567
4.35k
    return Status::OK();
568
4.35k
}
569
570
2.96k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
571
2.96k
    PartitionedBlock partitioned_block;
572
2.96k
    bool eos;
573
2.96k
    Block block;
574
2.96k
    _data_queue[source_info.channel_id].set_eos();
575
2.96k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
576
2.96k
                         source_info.channel_id)) {
577
        // do nothing
578
3
    }
579
2.96k
}
580
581
} // namespace doris