Coverage Report

Created: 2026-05-09 12:54

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
245k
                                                       BlockType&& block) {
31
245k
    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
245k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
66.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
66.0k
        block.first->record_channel_id(channel_id);
43
178k
    } else {
44
178k
        block->record_channel_id(channel_id);
45
178k
    }
46
47
245k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
245k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
245k
    }
50
245k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
47.0k
                                                       BlockType&& block) {
31
47.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
47.0k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
47.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
47.0k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
47.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
47.0k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
47.0k
    }
50
47.0k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
178k
                                                       BlockType&& block) {
31
178k
    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
178k
    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
178k
    } else {
44
178k
        block->record_channel_id(channel_id);
45
178k
    }
46
47
178k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
178k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
178k
    }
50
178k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
19.0k
                                                       BlockType&& block) {
31
19.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
19.0k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
19.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
19.0k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
19.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
18.9k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
18.9k
    }
50
19.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.66M
                                         int channel_id) {
56
1.66M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.66M
    bool all_finished = _running_sink_operators == 0;
60
1.66M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
65.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
65.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
179k
        } else {
65
179k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
179k
            data_block->swap(block->_data_block);
67
179k
        }
68
244k
        return true;
69
1.41M
    } else if (all_finished) {
70
1.27M
        *eos = true;
71
1.27M
    } else {
72
145k
        std::unique_lock l(*_m[channel_id]);
73
145k
        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
12
            } else {
79
12
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
12
                data_block->swap(block->_data_block);
81
12
            }
82
18
            return true;
83
18
        }
84
145k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
145k
        local_state->_dependency->block();
86
145k
    }
87
1.41M
    return false;
88
1.66M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
242k
                                         int channel_id) {
56
242k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
242k
    bool all_finished = _running_sink_operators == 0;
60
242k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
47.0k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
47.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
47.0k
        return true;
69
195k
    } else if (all_finished) {
70
168k
        *eos = true;
71
168k
    } else {
72
26.6k
        std::unique_lock l(*_m[channel_id]);
73
26.6k
        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
26.6k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
26.6k
        local_state->_dependency->block();
86
26.6k
    }
87
195k
    return false;
88
242k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.37M
                                         int channel_id) {
56
1.37M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.37M
    bool all_finished = _running_sink_operators == 0;
60
1.37M
    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
179k
        } else {
65
179k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
179k
            data_block->swap(block->_data_block);
67
179k
        }
68
179k
        return true;
69
1.19M
    } else if (all_finished) {
70
1.08M
        *eos = true;
71
1.08M
    } else {
72
114k
        std::unique_lock l(*_m[channel_id]);
73
114k
        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
12
            } else {
79
12
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
12
                data_block->swap(block->_data_block);
81
12
            }
82
12
            return true;
83
12
        }
84
114k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
114k
        local_state->_dependency->block();
86
114k
    }
87
1.19M
    return false;
88
1.37M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
45.0k
                                         int channel_id) {
56
45.0k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
45.0k
    bool all_finished = _running_sink_operators == 0;
60
45.0k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
18.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
18.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
18.9k
        return true;
69
26.0k
    } else if (all_finished) {
70
21.3k
        *eos = true;
71
21.3k
    } else {
72
4.70k
        std::unique_lock l(*_m[channel_id]);
73
4.70k
        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
4.69k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
4.69k
        local_state->_dependency->block();
86
4.69k
    }
87
26.0k
    return false;
88
45.0k
}
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
96.6k
                              SinkInfo& sink_info) {
116
96.6k
    if (in_block->empty()) {
117
78.2k
        return Status::OK();
118
78.2k
    }
119
18.3k
    {
120
18.3k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
18.3k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
18.3k
    }
123
18.3k
    {
124
18.3k
        SCOPED_TIMER(profile.distribute_timer);
125
18.3k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
18.3k
                                    *sink_info.channel_id, sink_info.local_state,
127
18.3k
                                    sink_info.shuffle_idx_to_instance_idx));
128
18.3k
    }
129
130
18.3k
    sink_info.local_state->_memory_used_counter->set(
131
18.3k
            sink_info.local_state->_shared_state->mem_usage);
132
18.3k
    return Status::OK();
133
18.3k
}
134
135
78.9k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
78.9k
    PartitionedBlock partitioned_block;
137
78.9k
    bool eos;
138
78.9k
    Block block;
139
78.9k
    _data_queue[source_info.channel_id].set_eos();
140
78.9k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
78.9k
                         source_info.channel_id)) {
142
        // do nothing
143
25
    }
144
78.9k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
103k
                                   SourceInfo&& source_info) {
148
103k
    PartitionedBlock partitioned_block;
149
103k
    MutableBlock mutable_block;
150
151
103k
    auto get_data = [&]() -> Status {
152
40.6k
        do {
153
40.6k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
40.6k
                                       partitioned_block.second.offset_start;
155
40.6k
            auto block_wrapper = partitioned_block.first;
156
40.6k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
40.6k
                                                   offset_start + partitioned_block.second.length));
158
40.6k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
40.6k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
39.8k
                               source_info.channel_id));
161
30.6k
        return Status::OK();
162
30.6k
    };
163
164
103k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
103k
                      source_info.channel_id)) {
166
30.6k
        SCOPED_TIMER(profile.copy_data_timer);
167
30.6k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
30.6k
                block, partitioned_block.first->_data_block);
169
30.6k
        RETURN_IF_ERROR(get_data());
170
30.6k
    }
171
103k
    return Status::OK();
172
103k
}
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
18.3k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
18.3k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
18.3k
    const auto rows = cast_set<int32_t>(block->rows());
182
18.3k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
18.3k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
18.3k
    {
185
18.3k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
11.9M
        for (int32_t i = 0; i < rows; ++i) {
187
11.9M
            partition_rows_histogram[channel_ids[i]]++;
188
11.9M
        }
189
170k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
152k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
152k
        }
192
10.9M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
10.9M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
10.9M
            partition_rows_histogram[channel_ids[i]]--;
195
10.9M
        }
196
18.3k
    }
197
198
18.3k
    Block data_block;
199
18.3k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
18.3k
    if (!_free_blocks.try_dequeue(data_block)) {
201
9.90k
        data_block = block->clone_empty();
202
9.90k
    }
203
18.3k
    data_block.swap(*block);
204
18.3k
    new_block_wrapper =
205
18.3k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
18.3k
    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
18.3k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
18.3k
    const auto& map = *shuffle_idx_to_instance_idx;
217
18.3k
    int32_t enqueue_rows = 0;
218
152k
    for (const auto& it : map) {
219
152k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
1
                << it.first << " : " << it.second << " " << _num_partitions;
221
152k
        uint32_t start = partition_rows_histogram[it.first];
222
152k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
152k
        if (size > 0) {
224
40.7k
            enqueue_rows += size;
225
40.7k
            _enqueue_data_and_set_ready(
226
40.7k
                    it.second, local_state,
227
40.7k
                    {new_block_wrapper,
228
40.7k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
40.7k
        }
230
152k
    }
231
18.3k
    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
18.3k
    return Status::OK();
245
18.3k
}
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
282k
                                  SinkInfo& sink_info) {
291
282k
    if (in_block->empty()) {
292
105k
        return Status::OK();
293
105k
    }
294
176k
    Block new_block;
295
176k
    if (!_free_blocks.try_dequeue(new_block)) {
296
72.5k
        new_block = {in_block->clone_empty()};
297
72.5k
    }
298
176k
    new_block.swap(*in_block);
299
176k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
176k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
176k
            std::move(new_block),
302
176k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
176k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
176k
    sink_info.local_state->_memory_used_counter->set(
307
176k
            sink_info.local_state->_shared_state->mem_usage);
308
309
176k
    return Status::OK();
310
282k
}
311
312
536k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
536k
    Block next_block;
314
536k
    BlockWrapperSPtr wrapper;
315
536k
    bool eos;
316
536k
    _data_queue[source_info.channel_id].set_eos();
317
538k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
538k
                         source_info.channel_id)) {
319
        // do nothing
320
1.68k
    }
321
536k
}
322
323
8.70k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
8.70k
    Block next_block;
325
8.70k
    BlockWrapperSPtr wrapper;
326
8.70k
    bool eos;
327
8.70k
    _data_queue[source_info.channel_id].set_eos();
328
8.70k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
8.70k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
8.70k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
823k
                                       Profile&& profile, SourceInfo&& source_info) {
336
823k
    BlockWrapperSPtr next_block;
337
823k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
823k
    return Status::OK();
339
823k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
3.54k
                                SinkInfo& sink_info) {
343
3.54k
    if (in_block->empty()) {
344
1.22k
        return Status::OK();
345
1.22k
    }
346
2.31k
    Block new_block;
347
2.31k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.41k
        new_block = {in_block->clone_empty()};
349
1.41k
    }
350
2.31k
    new_block.swap(*in_block);
351
352
2.31k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
2.31k
            std::move(new_block),
354
2.31k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
2.31k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
2.31k
    sink_info.local_state->_memory_used_counter->set(
358
2.31k
            sink_info.local_state->_shared_state->mem_usage);
359
360
2.31k
    return Status::OK();
361
3.54k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
4.20k
                                     Profile&& profile, SourceInfo&& source_info) {
365
4.20k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
4.20k
    BlockWrapperSPtr next_block;
370
4.20k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
4.20k
    return Status::OK();
372
4.20k
}
373
374
101k
void ExchangerBase::finalize() {
375
101k
    DCHECK(_running_source_operators == 0);
376
101k
    Block block;
377
186k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
85.1k
    }
380
101k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
3.97k
                                SinkInfo& sink_info) {
384
3.97k
    if (in_block->empty()) {
385
1.49k
        return Status::OK();
386
1.49k
    }
387
2.48k
    Block new_block;
388
2.48k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.82k
        new_block = {in_block->clone_empty()};
390
1.82k
    }
391
2.48k
    new_block.swap(*in_block);
392
2.48k
    auto wrapper = BlockWrapper::create_shared(
393
2.48k
            std::move(new_block),
394
18.4E
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
21.4k
    for (int i = 0; i < _num_partitions; i++) {
396
19.0k
        _enqueue_data_and_set_ready(
397
19.0k
                i, sink_info.local_state,
398
19.0k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
19.0k
    }
400
401
2.48k
    return Status::OK();
402
3.97k
}
403
404
10.6k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
10.6k
    BroadcastBlock partitioned_block;
406
10.6k
    bool eos;
407
10.6k
    Block block;
408
10.6k
    _data_queue[source_info.channel_id].set_eos();
409
10.6k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
10.6k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
10.6k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
34.3k
                                     Profile&& profile, SourceInfo&& source_info) {
417
34.3k
    BroadcastBlock partitioned_block;
418
419
34.3k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
34.3k
                      source_info.channel_id)) {
421
18.9k
        SCOPED_TIMER(profile.copy_data_timer);
422
18.9k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
18.9k
                block, partitioned_block.first->_data_block);
424
18.9k
        auto block_wrapper = partitioned_block.first;
425
18.9k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
18.9k
                                               partitioned_block.second.offset_start,
427
18.9k
                                               partitioned_block.second.length));
428
18.9k
    }
429
430
34.3k
    return Status::OK();
431
34.3k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
951
                                                       SinkInfo& sink_info) {
435
951
    Block new_block;
436
951
    if (!_free_blocks.try_dequeue(new_block)) {
437
283
        new_block = {in_block->clone_empty()};
438
283
    }
439
951
    new_block.swap(*in_block);
440
951
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
951
    _enqueue_data_and_set_ready(
442
951
            channel_id, sink_info.local_state,
443
951
            {BlockWrapper::create_shared(
444
951
                     std::move(new_block),
445
951
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
446
951
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
447
448
951
    sink_info.local_state->_memory_used_counter->set(
449
951
            sink_info.local_state->_shared_state->mem_usage);
450
951
    return Status::OK();
451
951
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
2.07k
                                                   SinkInfo& sink_info) {
455
2.07k
    std::vector<uint32_t> channel_ids;
456
2.07k
    const auto num_rows = block->rows();
457
2.07k
    channel_ids.resize(num_rows, 0);
458
2.07k
    if (num_rows <= _num_partitions) {
459
1.64k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.64k
    } else {
461
422
        size_t i = 0;
462
13.9k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
13.5k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
13.5k
        }
465
422
        if (i < num_rows - 1) {
466
275
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
275
        }
468
422
    }
469
470
2.07k
    sink_info.local_state->_memory_used_counter->set(
471
2.07k
            sink_info.local_state->_shared_state->mem_usage);
472
2.07k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
2.07k
    return Status::OK();
474
2.07k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
2.07k
                                                 Block* block, SinkInfo& sink_info) {
479
2.07k
    const auto rows = cast_set<int32_t>(block->rows());
480
2.07k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
481
2.07k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
482
2.07k
    {
483
2.07k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
65.1k
        for (int32_t i = 0; i < rows; ++i) {
485
63.0k
            partition_rows_histogram[channel_ids[i]]++;
486
63.0k
        }
487
15.0k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
13.0k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
13.0k
        }
490
491
64.4k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
62.3k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
62.3k
            partition_rows_histogram[channel_ids[i]]--;
494
62.3k
        }
495
2.07k
    }
496
2.07k
    Block data_block;
497
2.07k
    if (!_free_blocks.try_dequeue(data_block)) {
498
1.79k
        data_block = block->clone_empty();
499
1.79k
    }
500
2.07k
    data_block.swap(*block);
501
2.07k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
502
2.07k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
503
2.07k
    if (new_block_wrapper->_data_block.empty()) {
504
0
        return Status::OK();
505
0
    }
506
15.0k
    for (int32_t i = 0; i < _num_partitions; i++) {
507
13.0k
        const uint32_t start = partition_rows_histogram[i];
508
13.0k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
509
13.0k
        if (size > 0) {
510
5.35k
            _enqueue_data_and_set_ready(
511
5.35k
                    i, sink_info.local_state,
512
5.35k
                    {new_block_wrapper,
513
5.35k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
514
5.35k
        }
515
13.0k
    }
516
2.07k
    return Status::OK();
517
2.07k
}
518
519
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
520
7.93k
                                          Profile&& profile, SinkInfo& sink_info) {
521
7.93k
    if (in_block->empty()) {
522
4.91k
        return Status::OK();
523
4.91k
    }
524
3.02k
    if (_is_pass_through) {
525
951
        return _passthrough_sink(state, in_block, sink_info);
526
2.07k
    } else {
527
2.07k
        if (++_total_block >= _num_partitions) {
528
309
            _is_pass_through = true;
529
309
        }
530
2.07k
        return _shuffle_sink(state, in_block, sink_info);
531
2.07k
    }
532
3.02k
}
533
534
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
535
8.94k
                                               Profile&& profile, SourceInfo&& source_info) {
536
8.94k
    if (!_tmp_block[source_info.channel_id].empty()) {
537
134
        *block = std::move(_tmp_block[source_info.channel_id]);
538
134
        *eos = _tmp_eos[source_info.channel_id];
539
134
        _tmp_block[source_info.channel_id] = {};
540
134
        return Status::OK();
541
134
    }
542
8.81k
    PartitionedBlock partitioned_block;
543
8.81k
    MutableBlock mutable_block;
544
545
8.81k
    auto get_data = [&]() -> Status {
546
6.28k
        do {
547
6.28k
            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
947
                if (mutable_block.rows() > 0) {
550
135
                    _tmp_block[source_info.channel_id] =
551
135
                            std::move(partitioned_block.first->_data_block);
552
135
                    _tmp_eos[source_info.channel_id] = *eos;
553
135
                    *eos = false;
554
812
                } else {
555
812
                    *block = std::move(partitioned_block.first->_data_block);
556
812
                }
557
947
                break;
558
947
            }
559
5.33k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
560
5.33k
                                       partitioned_block.second.offset_start;
561
5.33k
            auto block_wrapper = partitioned_block.first;
562
5.33k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
563
5.33k
                                                   offset_start + partitioned_block.second.length));
564
5.34k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
565
5.34k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
566
5.34k
                               source_info.channel_id));
567
4.09k
        return Status::OK();
568
4.09k
    };
569
570
8.81k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
571
8.81k
                      source_info.channel_id)) {
572
4.10k
        SCOPED_TIMER(profile.copy_data_timer);
573
4.10k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
574
4.10k
                block, partitioned_block.first->_data_block);
575
4.10k
        RETURN_IF_ERROR(get_data());
576
4.10k
    }
577
8.81k
    return Status::OK();
578
8.81k
}
579
580
6.04k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
581
6.04k
    PartitionedBlock partitioned_block;
582
6.04k
    bool eos;
583
6.04k
    Block block;
584
6.04k
    _data_queue[source_info.channel_id].set_eos();
585
6.04k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
586
6.04k
                         source_info.channel_id)) {
587
        // do nothing
588
3
    }
589
6.04k
}
590
591
} // namespace doris