Coverage Report

Created: 2026-05-20 07:42

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
260k
                                                       BlockType&& block) {
31
260k
    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
260k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
62.6k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
62.6k
        block.first->record_channel_id(channel_id);
43
197k
    } else {
44
197k
        block->record_channel_id(channel_id);
45
197k
    }
46
47
260k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
260k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
260k
    }
50
260k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
45.5k
                                                       BlockType&& block) {
31
45.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
45.5k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
45.5k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
45.5k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
45.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
45.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
45.5k
    }
50
45.5k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
197k
                                                       BlockType&& block) {
31
197k
    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
197k
    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
197k
    } else {
44
197k
        block->record_channel_id(channel_id);
45
197k
    }
46
47
197k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
197k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
197k
    }
50
197k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
17.1k
                                                       BlockType&& block) {
31
17.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
17.1k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
17.1k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
17.1k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
17.1k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
17.0k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
17.0k
    }
50
17.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.69M
                                         int channel_id) {
56
1.69M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.69M
    bool all_finished = _running_sink_operators == 0;
60
1.69M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
62.6k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
62.6k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
197k
        } else {
65
197k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
197k
            data_block->swap(block->_data_block);
67
197k
        }
68
260k
        return true;
69
1.43M
    } else if (all_finished) {
70
1.27M
        *eos = true;
71
1.27M
    } else {
72
165k
        std::unique_lock l(*_m[channel_id]);
73
165k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
8
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
8
                local_state->_shared_state->sub_mem_usage(channel_id,
77
8
                                                          block.first->_allocated_bytes);
78
19
            } else {
79
19
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
19
                data_block->swap(block->_data_block);
81
19
            }
82
27
            return true;
83
27
        }
84
165k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
165k
        local_state->_dependency->block();
86
165k
    }
87
1.43M
    return false;
88
1.69M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
232k
                                         int channel_id) {
56
232k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
231k
    bool all_finished = _running_sink_operators == 0;
60
231k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
45.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
45.5k
            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
45.5k
        return true;
69
186k
    } else if (all_finished) {
70
160k
        *eos = true;
71
160k
    } else {
72
25.6k
        std::unique_lock l(*_m[channel_id]);
73
25.6k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
6
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
6
                local_state->_shared_state->sub_mem_usage(channel_id,
77
6
                                                          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
6
            return true;
83
6
        }
84
25.6k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
25.6k
        local_state->_dependency->block();
86
25.6k
    }
87
186k
    return false;
88
231k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.42M
                                         int channel_id) {
56
1.42M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.42M
    bool all_finished = _running_sink_operators == 0;
60
1.42M
    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
197k
        } else {
65
197k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
197k
            data_block->swap(block->_data_block);
67
197k
        }
68
197k
        return true;
69
1.23M
    } else if (all_finished) {
70
1.09M
        *eos = true;
71
1.09M
    } else {
72
134k
        std::unique_lock l(*_m[channel_id]);
73
134k
        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
19
            } else {
79
19
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
19
                data_block->swap(block->_data_block);
81
19
            }
82
19
            return true;
83
19
        }
84
134k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
134k
        local_state->_dependency->block();
86
134k
    }
87
1.23M
    return false;
88
1.42M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
38.5k
                                         int channel_id) {
56
38.5k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
38.5k
    bool all_finished = _running_sink_operators == 0;
60
38.5k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
17.0k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
17.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
17.0k
        return true;
69
21.4k
    } else if (all_finished) {
70
16.1k
        *eos = true;
71
16.1k
    } else {
72
5.31k
        std::unique_lock l(*_m[channel_id]);
73
5.31k
        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
5.31k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
5.31k
        local_state->_dependency->block();
86
5.31k
    }
87
21.4k
    return false;
88
38.5k
}
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
93.4k
                              SinkInfo& sink_info) {
116
93.4k
    if (in_block->empty()) {
117
76.7k
        return Status::OK();
118
76.7k
    }
119
16.6k
    {
120
16.6k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
16.6k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
16.6k
    }
123
16.6k
    {
124
16.6k
        SCOPED_TIMER(profile.distribute_timer);
125
16.6k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
16.6k
                                    *sink_info.channel_id, sink_info.local_state,
127
16.6k
                                    sink_info.shuffle_idx_to_instance_idx));
128
16.6k
    }
129
130
16.6k
    sink_info.local_state->_memory_used_counter->set(
131
16.6k
            sink_info.local_state->_shared_state->mem_usage);
132
16.6k
    return Status::OK();
133
16.6k
}
134
135
77.5k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
77.5k
    PartitionedBlock partitioned_block;
137
77.5k
    bool eos;
138
77.5k
    Block block;
139
77.5k
    _data_queue[source_info.channel_id].set_eos();
140
77.5k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
77.5k
                         source_info.channel_id)) {
142
        // do nothing
143
45
    }
144
77.5k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
101k
                                   SourceInfo&& source_info) {
148
101k
    PartitionedBlock partitioned_block;
149
101k
    MutableBlock mutable_block;
150
151
101k
    auto get_data = [&]() -> Status {
152
40.4k
        do {
153
40.4k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
40.4k
                                       partitioned_block.second.offset_start;
155
40.4k
            auto block_wrapper = partitioned_block.first;
156
40.4k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
40.4k
                                                   offset_start + partitioned_block.second.length));
158
40.4k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
40.4k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
39.2k
                               source_info.channel_id));
161
29.8k
        return Status::OK();
162
29.8k
    };
163
164
101k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
101k
                      source_info.channel_id)) {
166
29.9k
        SCOPED_TIMER(profile.copy_data_timer);
167
29.9k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
29.9k
                block, partitioned_block.first->_data_block);
169
29.9k
        RETURN_IF_ERROR(get_data());
170
29.9k
    }
171
101k
    return Status::OK();
172
101k
}
173
174
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
175
                                     Block* block, int channel_id,
176
                                     LocalExchangeSinkLocalState* local_state,
177
16.6k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
16.6k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
16.6k
    const auto rows = cast_set<int32_t>(block->rows());
182
16.6k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
16.6k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
16.6k
    {
185
16.6k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
14.6M
        for (int32_t i = 0; i < rows; ++i) {
187
14.5M
            partition_rows_histogram[channel_ids[i]]++;
188
14.5M
        }
189
152k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
135k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
135k
        }
192
13.7M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
13.7M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
13.7M
            partition_rows_histogram[channel_ids[i]]--;
195
13.7M
        }
196
16.6k
    }
197
198
16.6k
    Block data_block;
199
16.6k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
16.6k
    if (!_free_blocks.try_dequeue(data_block)) {
201
8.57k
        data_block = block->clone_empty();
202
8.57k
    }
203
16.6k
    data_block.swap(*block);
204
16.6k
    new_block_wrapper =
205
16.6k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
16.6k
    if (new_block_wrapper->_data_block.empty()) {
207
0
        return Status::OK();
208
0
    }
209
    /**
210
     * Data are hash-shuffled and distributed to all instances of
211
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
212
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
213
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
214
     */
215
16.6k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
16.6k
    const auto& map = *shuffle_idx_to_instance_idx;
217
16.6k
    int32_t enqueue_rows = 0;
218
135k
    for (const auto& it : map) {
219
135k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
1
                << it.first << " : " << it.second << " " << _num_partitions;
221
135k
        uint32_t start = partition_rows_histogram[it.first];
222
135k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
135k
        if (size > 0) {
224
40.5k
            enqueue_rows += size;
225
40.5k
            _enqueue_data_and_set_ready(
226
40.5k
                    it.second, local_state,
227
40.5k
                    {new_block_wrapper,
228
40.5k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
40.5k
        }
230
135k
    }
231
16.6k
    if (enqueue_rows != rows) [[unlikely]] {
232
1
        fmt::memory_buffer debug_string_buffer;
233
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
234
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
235
3
        for (const auto& it : map) {
236
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
237
3
        }
238
1
        return Status::InternalError(
239
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
240
1
                "rows={}, Detail: {}]",
241
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
242
1
    }
243
244
16.6k
    return Status::OK();
245
16.6k
}
246
247
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
248
0
                                     Block* block, int channel_id) {
249
0
    const auto rows = cast_set<int32_t>(block->rows());
250
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
251
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
252
0
    {
253
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
254
0
        for (int32_t i = 0; i < rows; ++i) {
255
0
            partition_rows_histogram[channel_ids[i]]++;
256
0
        }
257
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
258
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
259
0
        }
260
0
        for (int32_t i = rows - 1; i >= 0; --i) {
261
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
262
0
            partition_rows_histogram[channel_ids[i]]--;
263
0
        }
264
0
    }
265
266
0
    Block data_block;
267
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
268
0
    if (!_free_blocks.try_dequeue(data_block)) {
269
0
        data_block = block->clone_empty();
270
0
    }
271
0
    data_block.swap(*block);
272
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
273
0
    if (new_block_wrapper->_data_block.empty()) {
274
0
        return Status::OK();
275
0
    }
276
0
    for (int i = 0; i < _num_partitions; i++) {
277
0
        uint32_t start = partition_rows_histogram[i];
278
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
279
0
        if (size > 0) {
280
0
            _enqueue_data_and_set_ready(
281
0
                    i, {new_block_wrapper,
282
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
283
0
        }
284
0
    }
285
286
0
    return Status::OK();
287
0
}
288
289
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
290
290k
                                  SinkInfo& sink_info) {
291
290k
    if (in_block->empty()) {
292
96.2k
        return Status::OK();
293
96.2k
    }
294
194k
    Block new_block;
295
194k
    if (!_free_blocks.try_dequeue(new_block)) {
296
76.6k
        new_block = {in_block->clone_empty()};
297
76.6k
    }
298
194k
    new_block.swap(*in_block);
299
194k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
194k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
194k
            std::move(new_block),
302
194k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
194k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
194k
    sink_info.local_state->_memory_used_counter->set(
307
194k
            sink_info.local_state->_shared_state->mem_usage);
308
309
194k
    return Status::OK();
310
290k
}
311
312
541k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
541k
    Block next_block;
314
541k
    BlockWrapperSPtr wrapper;
315
541k
    bool eos;
316
541k
    _data_queue[source_info.channel_id].set_eos();
317
541k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
541k
                         source_info.channel_id)) {
319
        // do nothing
320
628
    }
321
541k
}
322
323
11.2k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
11.2k
    Block next_block;
325
11.2k
    BlockWrapperSPtr wrapper;
326
11.2k
    bool eos;
327
11.2k
    _data_queue[source_info.channel_id].set_eos();
328
11.2k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
11.2k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
11.2k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
867k
                                       Profile&& profile, SourceInfo&& source_info) {
336
867k
    BlockWrapperSPtr next_block;
337
867k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
867k
    return Status::OK();
339
867k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
4.86k
                                SinkInfo& sink_info) {
343
4.86k
    if (in_block->empty()) {
344
1.55k
        return Status::OK();
345
1.55k
    }
346
3.30k
    Block new_block;
347
3.30k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.84k
        new_block = {in_block->clone_empty()};
349
1.84k
    }
350
3.30k
    new_block.swap(*in_block);
351
352
3.30k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
3.30k
            std::move(new_block),
354
3.30k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
3.30k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
3.30k
    sink_info.local_state->_memory_used_counter->set(
358
3.30k
            sink_info.local_state->_shared_state->mem_usage);
359
360
3.30k
    return Status::OK();
361
4.86k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
5.99k
                                     Profile&& profile, SourceInfo&& source_info) {
365
5.99k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
5.99k
    BlockWrapperSPtr next_block;
370
5.99k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
5.99k
    return Status::OK();
372
5.99k
}
373
374
109k
void ExchangerBase::finalize() {
375
109k
    DCHECK(_running_source_operators == 0);
376
109k
    Block block;
377
197k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
88.1k
    }
380
109k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
3.85k
                                SinkInfo& sink_info) {
384
3.85k
    if (in_block->empty()) {
385
1.27k
        return Status::OK();
386
1.27k
    }
387
2.58k
    Block new_block;
388
2.58k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.63k
        new_block = {in_block->clone_empty()};
390
1.63k
    }
391
2.58k
    new_block.swap(*in_block);
392
2.58k
    auto wrapper = BlockWrapper::create_shared(
393
2.58k
            std::move(new_block),
394
2.58k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
19.6k
    for (int i = 0; i < _num_partitions; i++) {
396
17.1k
        _enqueue_data_and_set_ready(
397
17.1k
                i, sink_info.local_state,
398
17.1k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
17.1k
    }
400
401
2.58k
    return Status::OK();
402
3.85k
}
403
404
8.06k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
8.06k
    BroadcastBlock partitioned_block;
406
8.06k
    bool eos;
407
8.06k
    Block block;
408
8.06k
    _data_queue[source_info.channel_id].set_eos();
409
8.06k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
8.06k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
8.06k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
30.4k
                                     Profile&& profile, SourceInfo&& source_info) {
417
30.4k
    BroadcastBlock partitioned_block;
418
419
30.4k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
30.4k
                      source_info.channel_id)) {
421
17.0k
        SCOPED_TIMER(profile.copy_data_timer);
422
17.0k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
17.0k
                block, partitioned_block.first->_data_block);
424
17.0k
        auto block_wrapper = partitioned_block.first;
425
17.0k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
17.0k
                                               partitioned_block.second.offset_start,
427
17.0k
                                               partitioned_block.second.length));
428
17.0k
    }
429
430
30.4k
    return Status::OK();
431
30.4k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
899
                                                       SinkInfo& sink_info) {
435
899
    Block new_block;
436
899
    if (!_free_blocks.try_dequeue(new_block)) {
437
254
        new_block = {in_block->clone_empty()};
438
254
    }
439
899
    new_block.swap(*in_block);
440
899
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
899
    _enqueue_data_and_set_ready(
442
899
            channel_id, sink_info.local_state,
443
899
            {BlockWrapper::create_shared(
444
899
                     std::move(new_block),
445
899
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
446
899
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
447
448
899
    sink_info.local_state->_memory_used_counter->set(
449
899
            sink_info.local_state->_shared_state->mem_usage);
450
899
    return Status::OK();
451
899
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
1.51k
                                                   SinkInfo& sink_info) {
455
1.51k
    std::vector<uint32_t> channel_ids;
456
1.51k
    const auto num_rows = block->rows();
457
1.51k
    channel_ids.resize(num_rows, 0);
458
1.51k
    if (num_rows <= _num_partitions) {
459
1.02k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.02k
    } else {
461
489
        size_t i = 0;
462
11.0k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
10.5k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
10.5k
        }
465
489
        if (i < num_rows - 1) {
466
381
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
381
        }
468
489
    }
469
470
1.51k
    sink_info.local_state->_memory_used_counter->set(
471
1.51k
            sink_info.local_state->_shared_state->mem_usage);
472
1.51k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
1.51k
    return Status::OK();
474
1.51k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
1.51k
                                                 Block* block, SinkInfo& sink_info) {
479
1.51k
    const auto rows = cast_set<int32_t>(block->rows());
480
1.51k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
481
1.51k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
482
1.51k
    {
483
1.51k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
53.5k
        for (int32_t i = 0; i < rows; ++i) {
485
52.0k
            partition_rows_histogram[channel_ids[i]]++;
486
52.0k
        }
487
9.56k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
8.04k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
8.04k
        }
490
491
53.5k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
52.0k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
52.0k
            partition_rows_histogram[channel_ids[i]]--;
494
52.0k
        }
495
1.51k
    }
496
1.51k
    Block data_block;
497
1.51k
    if (!_free_blocks.try_dequeue(data_block)) {
498
1.32k
        data_block = block->clone_empty();
499
1.32k
    }
500
1.51k
    data_block.swap(*block);
501
1.51k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
502
1.51k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
503
1.51k
    if (new_block_wrapper->_data_block.empty()) {
504
0
        return Status::OK();
505
0
    }
506
9.56k
    for (int32_t i = 0; i < _num_partitions; i++) {
507
8.04k
        const uint32_t start = partition_rows_histogram[i];
508
8.04k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
509
8.04k
        if (size > 0) {
510
4.09k
            _enqueue_data_and_set_ready(
511
4.09k
                    i, sink_info.local_state,
512
4.09k
                    {new_block_wrapper,
513
4.09k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
514
4.09k
        }
515
8.04k
    }
516
1.51k
    return Status::OK();
517
1.51k
}
518
519
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
520
5.32k
                                          Profile&& profile, SinkInfo& sink_info) {
521
5.32k
    if (in_block->empty()) {
522
2.90k
        return Status::OK();
523
2.90k
    }
524
2.41k
    if (_is_pass_through) {
525
899
        return _passthrough_sink(state, in_block, sink_info);
526
1.51k
    } else {
527
1.51k
        if (++_total_block >= _num_partitions) {
528
295
            _is_pass_through = true;
529
295
        }
530
1.51k
        return _shuffle_sink(state, in_block, sink_info);
531
1.51k
    }
532
2.41k
}
533
534
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
535
6.08k
                                               Profile&& profile, SourceInfo&& source_info) {
536
6.08k
    if (!_tmp_block[source_info.channel_id].empty()) {
537
148
        *block = std::move(_tmp_block[source_info.channel_id]);
538
148
        *eos = _tmp_eos[source_info.channel_id];
539
148
        _tmp_block[source_info.channel_id] = {};
540
148
        return Status::OK();
541
148
    }
542
5.94k
    PartitionedBlock partitioned_block;
543
5.94k
    MutableBlock mutable_block;
544
545
5.94k
    auto get_data = [&]() -> Status {
546
4.98k
        do {
547
4.98k
            if (partitioned_block.second.row_idxs == nullptr) {
548
                // The passthrough path which means the block is not partitioned, we can directly move the block without copying.
549
894
                if (mutable_block.rows() > 0) {
550
148
                    _tmp_block[source_info.channel_id] =
551
148
                            std::move(partitioned_block.first->_data_block);
552
148
                    _tmp_eos[source_info.channel_id] = *eos;
553
148
                    *eos = false;
554
746
                } else {
555
746
                    *block = std::move(partitioned_block.first->_data_block);
556
746
                }
557
894
                break;
558
894
            }
559
4.08k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
560
4.08k
                                       partitioned_block.second.offset_start;
561
4.08k
            auto block_wrapper = partitioned_block.first;
562
4.08k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
563
4.08k
                                                   offset_start + partitioned_block.second.length));
564
4.08k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
565
4.08k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
566
4.08k
                               source_info.channel_id));
567
2.87k
        return Status::OK();
568
2.87k
    };
569
570
5.94k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
571
5.94k
                      source_info.channel_id)) {
572
2.87k
        SCOPED_TIMER(profile.copy_data_timer);
573
2.87k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
574
2.87k
                block, partitioned_block.first->_data_block);
575
2.87k
        RETURN_IF_ERROR(get_data());
576
2.87k
    }
577
5.94k
    return Status::OK();
578
5.94k
}
579
580
3.45k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
581
3.45k
    PartitionedBlock partitioned_block;
582
3.45k
    bool eos;
583
3.45k
    Block block;
584
3.45k
    _data_queue[source_info.channel_id].set_eos();
585
3.46k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
586
3.46k
                         source_info.channel_id)) {
587
        // do nothing
588
5
    }
589
3.45k
}
590
591
} // namespace doris