Coverage Report

Created: 2026-03-16 04:49

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
#include "common/compile_check_begin.h"
28
template <typename BlockType>
29
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
30
                                                       LocalExchangeSinkLocalState* local_state,
31
252k
                                                       BlockType&& block) {
32
252k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
252k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
54.7k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
54.7k
        block.first->record_channel_id(channel_id);
44
197k
    } else {
45
197k
        block->record_channel_id(channel_id);
46
197k
    }
47
48
252k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
252k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
252k
    }
51
252k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
38.2k
                                                       BlockType&& block) {
32
38.2k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
38.2k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
38.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
38.2k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
38.2k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
38.2k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
38.2k
    }
51
38.2k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
197k
                                                       BlockType&& block) {
32
197k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
197k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
        block.first->record_channel_id(channel_id);
44
197k
    } else {
45
197k
        block->record_channel_id(channel_id);
46
197k
    }
47
48
197k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
197k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
197k
    }
51
197k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
16.4k
                                                       BlockType&& block) {
32
16.4k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
16.4k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
16.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
16.4k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
16.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
16.4k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
16.4k
    }
51
16.4k
}
52
53
template <typename BlockType>
54
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
55
                                         BlockType& block, bool* eos, Block* data_block,
56
1.99M
                                         int channel_id) {
57
1.99M
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
1.99M
    bool all_finished = _running_sink_operators == 0;
61
1.99M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
54.6k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
54.6k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
197k
        } else {
66
197k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
197k
            data_block->swap(block->_data_block);
68
197k
        }
69
252k
        return true;
70
1.74M
    } else if (all_finished) {
71
1.60M
        *eos = true;
72
1.60M
    } else {
73
137k
        std::unique_lock l(*_m[channel_id]);
74
137k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
15
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
15
                local_state->_shared_state->sub_mem_usage(channel_id,
78
15
                                                          block.first->_allocated_bytes);
79
15
            } else {
80
6
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
6
                data_block->swap(block->_data_block);
82
6
            }
83
21
            return true;
84
21
        }
85
137k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
137k
        local_state->_dependency->block();
87
137k
    }
88
1.74M
    return false;
89
1.99M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
383k
                                         int channel_id) {
57
383k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
383k
    bool all_finished = _running_sink_operators == 0;
61
383k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
38.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
38.2k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
        } else {
66
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
            data_block->swap(block->_data_block);
68
        }
69
38.2k
        return true;
70
344k
    } else if (all_finished) {
71
322k
        *eos = true;
72
322k
    } else {
73
22.8k
        std::unique_lock l(*_m[channel_id]);
74
22.8k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
13
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
13
                local_state->_shared_state->sub_mem_usage(channel_id,
78
13
                                                          block.first->_allocated_bytes);
79
            } else {
80
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
                data_block->swap(block->_data_block);
82
            }
83
13
            return true;
84
13
        }
85
22.8k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
22.8k
        local_state->_dependency->block();
87
22.8k
    }
88
344k
    return false;
89
383k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
56
1.58M
                                         int channel_id) {
57
1.58M
    if (local_state == nullptr) {
58
12
        return _dequeue_data(block, eos, data_block, channel_id);
59
12
    }
60
1.58M
    bool all_finished = _running_sink_operators == 0;
61
1.58M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
197k
        } else {
66
197k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
197k
            data_block->swap(block->_data_block);
68
197k
        }
69
197k
        return true;
70
1.38M
    } else if (all_finished) {
71
1.27M
        *eos = true;
72
1.27M
    } else {
73
111k
        std::unique_lock l(*_m[channel_id]);
74
111k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
                local_state->_shared_state->sub_mem_usage(channel_id,
78
                                                          block.first->_allocated_bytes);
79
6
            } else {
80
6
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
6
                data_block->swap(block->_data_block);
82
6
            }
83
6
            return true;
84
6
        }
85
111k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
111k
        local_state->_dependency->block();
87
111k
    }
88
1.38M
    return false;
89
1.58M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
35.4k
                                         int channel_id) {
57
35.4k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
35.4k
    bool all_finished = _running_sink_operators == 0;
61
35.4k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
16.4k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
16.4k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
        } else {
66
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
            data_block->swap(block->_data_block);
68
        }
69
16.4k
        return true;
70
19.0k
    } else if (all_finished) {
71
15.8k
        *eos = true;
72
15.8k
    } else {
73
3.21k
        std::unique_lock l(*_m[channel_id]);
74
3.21k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
2
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
2
                local_state->_shared_state->sub_mem_usage(channel_id,
78
2
                                                          block.first->_allocated_bytes);
79
            } else {
80
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
                data_block->swap(block->_data_block);
82
            }
83
2
            return true;
84
2
        }
85
3.21k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
3.21k
        local_state->_dependency->block();
87
3.21k
    }
88
19.0k
    return false;
89
35.4k
}
90
91
template <typename BlockType>
92
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
93
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
94
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
95
0
        block.first->record_channel_id(channel_id);
96
0
    } else {
97
0
        block->record_channel_id(channel_id);
98
0
    }
99
0
    _data_queue[channel_id].enqueue(std::move(block));
100
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_
101
102
template <typename BlockType>
103
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
104
20
                                         int channel_id) {
105
20
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
0
            data_block->swap(block->_data_block);
109
0
        }
110
0
        return true;
111
0
    }
112
20
    return false;
113
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
104
4
                                         int channel_id) {
105
4
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
            data_block->swap(block->_data_block);
109
        }
110
0
        return true;
111
0
    }
112
4
    return false;
113
4
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
104
12
                                         int channel_id) {
105
12
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
0
            data_block->swap(block->_data_block);
109
0
        }
110
0
        return true;
111
0
    }
112
12
    return false;
113
12
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
104
4
                                         int channel_id) {
105
4
    if (_data_queue[channel_id].try_dequeue(block)) {
106
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
107
                      !std::is_same_v<BroadcastBlock, BlockType>) {
108
            data_block->swap(block->_data_block);
109
        }
110
0
        return true;
111
0
    }
112
4
    return false;
113
4
}
114
115
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
116
181k
                              SinkInfo& sink_info) {
117
181k
    if (in_block->empty()) {
118
160k
        return Status::OK();
119
160k
    }
120
21.1k
    {
121
21.1k
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
21.1k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
21.1k
    }
124
21.1k
    {
125
21.1k
        SCOPED_TIMER(profile.distribute_timer);
126
21.1k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
21.1k
                                    *sink_info.channel_id, sink_info.local_state,
128
21.1k
                                    sink_info.shuffle_idx_to_instance_idx));
129
21.1k
    }
130
131
21.1k
    sink_info.local_state->_memory_used_counter->set(
132
21.1k
            sink_info.local_state->_shared_state->mem_usage);
133
21.1k
    return Status::OK();
134
21.1k
}
135
136
161k
void ShuffleExchanger::close(SourceInfo&& source_info) {
137
161k
    PartitionedBlock partitioned_block;
138
161k
    bool eos;
139
161k
    Block block;
140
161k
    _data_queue[source_info.channel_id].set_eos();
141
161k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
161k
                         source_info.channel_id)) {
143
        // do nothing
144
40
    }
145
161k
}
146
147
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
148
183k
                                   SourceInfo&& source_info) {
149
183k
    PartitionedBlock partitioned_block;
150
183k
    MutableBlock mutable_block;
151
152
183k
    auto get_data = [&]() -> Status {
153
38.1k
        do {
154
38.1k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
38.1k
                                       partitioned_block.second.offset_start;
156
38.1k
            auto block_wrapper = partitioned_block.first;
157
38.1k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
38.1k
                                                   offset_start + partitioned_block.second.length));
159
38.1k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
38.1k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
37.2k
                               source_info.channel_id));
162
30.6k
        return Status::OK();
163
30.6k
    };
164
165
183k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
183k
                      source_info.channel_id)) {
167
30.6k
        SCOPED_TIMER(profile.copy_data_timer);
168
30.6k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
30.6k
                block, partitioned_block.first->_data_block);
170
30.6k
        RETURN_IF_ERROR(get_data());
171
30.6k
    }
172
183k
    return Status::OK();
173
183k
}
174
175
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
176
                                     Block* block, int channel_id,
177
                                     LocalExchangeSinkLocalState* local_state,
178
21.0k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
21.0k
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
21.0k
    const auto rows = cast_set<int32_t>(block->rows());
183
21.0k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
21.0k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
21.0k
    {
186
21.0k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
10.8M
        for (int32_t i = 0; i < rows; ++i) {
188
10.8M
            partition_rows_histogram[channel_ids[i]]++;
189
10.8M
        }
190
243k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
222k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
222k
        }
193
10.0M
        for (int32_t i = rows - 1; i >= 0; --i) {
194
9.98M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
9.98M
            partition_rows_histogram[channel_ids[i]]--;
196
9.98M
        }
197
21.0k
    }
198
199
21.0k
    Block data_block;
200
21.0k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
21.0k
    if (!_free_blocks.try_dequeue(data_block)) {
202
12.5k
        data_block = block->clone_empty();
203
12.5k
    }
204
21.0k
    data_block.swap(*block);
205
21.0k
    new_block_wrapper =
206
21.0k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
21.0k
    if (new_block_wrapper->_data_block.empty()) {
208
0
        return Status::OK();
209
0
    }
210
    /**
211
     * Data are hash-shuffled and distributed to all instances of
212
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
213
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
214
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
215
     */
216
21.0k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
21.0k
    const auto& map = *shuffle_idx_to_instance_idx;
218
21.0k
    int32_t enqueue_rows = 0;
219
220k
    for (const auto& it : map) {
220
220k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
221
8
                << it.first << " : " << it.second << " " << _num_partitions;
222
220k
        uint32_t start = partition_rows_histogram[it.first];
223
220k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
220k
        if (size > 0) {
225
38.2k
            enqueue_rows += size;
226
38.2k
            _enqueue_data_and_set_ready(
227
38.2k
                    it.second, local_state,
228
38.2k
                    {new_block_wrapper,
229
38.2k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
38.2k
        }
231
220k
    }
232
21.0k
    if (enqueue_rows != rows) [[unlikely]] {
233
1
        fmt::memory_buffer debug_string_buffer;
234
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
235
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
236
3
        for (const auto& it : map) {
237
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
238
3
        }
239
1
        return Status::InternalError(
240
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
241
1
                "rows={}, Detail: {}]",
242
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
243
1
    }
244
245
21.0k
    return Status::OK();
246
21.0k
}
247
248
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
249
0
                                     Block* block, int channel_id) {
250
0
    const auto rows = cast_set<int32_t>(block->rows());
251
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
252
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
253
0
    {
254
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
255
0
        for (int32_t i = 0; i < rows; ++i) {
256
0
            partition_rows_histogram[channel_ids[i]]++;
257
0
        }
258
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
259
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
260
0
        }
261
0
        for (int32_t i = rows - 1; i >= 0; --i) {
262
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
263
0
            partition_rows_histogram[channel_ids[i]]--;
264
0
        }
265
0
    }
266
267
0
    Block data_block;
268
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
269
0
    if (!_free_blocks.try_dequeue(data_block)) {
270
0
        data_block = block->clone_empty();
271
0
    }
272
0
    data_block.swap(*block);
273
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
274
0
    if (new_block_wrapper->_data_block.empty()) {
275
0
        return Status::OK();
276
0
    }
277
0
    for (int i = 0; i < _num_partitions; i++) {
278
0
        uint32_t start = partition_rows_histogram[i];
279
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
280
0
        if (size > 0) {
281
0
            _enqueue_data_and_set_ready(
282
0
                    i, {new_block_wrapper,
283
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
284
0
        }
285
0
    }
286
287
0
    return Status::OK();
288
0
}
289
290
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
291
301k
                                  SinkInfo& sink_info) {
292
301k
    if (in_block->empty()) {
293
110k
        return Status::OK();
294
110k
    }
295
190k
    Block new_block;
296
190k
    if (!_free_blocks.try_dequeue(new_block)) {
297
85.9k
        new_block = {in_block->clone_empty()};
298
85.9k
    }
299
190k
    new_block.swap(*in_block);
300
190k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
190k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
190k
            std::move(new_block),
303
190k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
190k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
190k
    sink_info.local_state->_memory_used_counter->set(
308
190k
            sink_info.local_state->_shared_state->mem_usage);
309
310
190k
    return Status::OK();
311
301k
}
312
313
621k
void PassthroughExchanger::close(SourceInfo&& source_info) {
314
621k
    Block next_block;
315
621k
    BlockWrapperSPtr wrapper;
316
621k
    bool eos;
317
621k
    _data_queue[source_info.channel_id].set_eos();
318
623k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
319
623k
                         source_info.channel_id)) {
320
        // do nothing
321
2.13k
    }
322
621k
}
323
324
11.9k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
11.9k
    Block next_block;
326
11.9k
    BlockWrapperSPtr wrapper;
327
11.9k
    bool eos;
328
11.9k
    _data_queue[source_info.channel_id].set_eos();
329
11.9k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
11.9k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
11.9k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
920k
                                       Profile&& profile, SourceInfo&& source_info) {
337
920k
    BlockWrapperSPtr next_block;
338
920k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
920k
    return Status::OK();
340
920k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
4.27k
                                SinkInfo& sink_info) {
344
4.27k
    if (in_block->empty()) {
345
1.57k
        return Status::OK();
346
1.57k
    }
347
2.70k
    Block new_block;
348
2.70k
    if (!_free_blocks.try_dequeue(new_block)) {
349
1.74k
        new_block = {in_block->clone_empty()};
350
1.74k
    }
351
2.70k
    new_block.swap(*in_block);
352
353
2.70k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
2.70k
            std::move(new_block),
355
2.70k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
2.70k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
2.70k
    sink_info.local_state->_memory_used_counter->set(
359
2.70k
            sink_info.local_state->_shared_state->mem_usage);
360
361
2.70k
    return Status::OK();
362
4.27k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
5.14k
                                     Profile&& profile, SourceInfo&& source_info) {
366
5.14k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
5.14k
    BlockWrapperSPtr next_block;
371
5.14k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
5.14k
    return Status::OK();
373
5.14k
}
374
375
115k
void ExchangerBase::finalize() {
376
115k
    DCHECK(_running_source_operators == 0);
377
115k
    Block block;
378
216k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
101k
    }
381
115k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
2.92k
                                SinkInfo& sink_info) {
385
2.92k
    if (in_block->empty()) {
386
995
        return Status::OK();
387
995
    }
388
1.92k
    Block new_block;
389
1.92k
    if (!_free_blocks.try_dequeue(new_block)) {
390
1.51k
        new_block = {in_block->clone_empty()};
391
1.51k
    }
392
1.92k
    new_block.swap(*in_block);
393
1.92k
    auto wrapper = BlockWrapper::create_shared(
394
1.92k
            std::move(new_block),
395
1.92k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
18.3k
    for (int i = 0; i < _num_partitions; i++) {
397
16.4k
        _enqueue_data_and_set_ready(
398
16.4k
                i, sink_info.local_state,
399
16.4k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
16.4k
    }
401
402
1.92k
    return Status::OK();
403
2.92k
}
404
405
7.90k
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
7.90k
    BroadcastBlock partitioned_block;
407
7.90k
    bool eos;
408
7.90k
    Block block;
409
7.90k
    _data_queue[source_info.channel_id].set_eos();
410
7.90k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
7.90k
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
7.90k
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
27.5k
                                     Profile&& profile, SourceInfo&& source_info) {
418
27.5k
    BroadcastBlock partitioned_block;
419
420
27.5k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
27.5k
                      source_info.channel_id)) {
422
16.4k
        SCOPED_TIMER(profile.copy_data_timer);
423
16.4k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
16.4k
                block, partitioned_block.first->_data_block);
425
16.4k
        auto block_wrapper = partitioned_block.first;
426
16.4k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
16.4k
                                               partitioned_block.second.offset_start,
428
16.4k
                                               partitioned_block.second.length));
429
16.4k
    }
430
431
27.5k
    return Status::OK();
432
27.5k
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
448
                                                       SinkInfo& sink_info) {
436
448
    Block new_block;
437
448
    if (!_free_blocks.try_dequeue(new_block)) {
438
230
        new_block = {in_block->clone_empty()};
439
230
    }
440
448
    new_block.swap(*in_block);
441
448
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
448
    _enqueue_data_and_set_ready(
443
448
            channel_id, sink_info.local_state,
444
448
            BlockWrapper::create_shared(
445
448
                    std::move(new_block),
446
18.4E
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
448
                    channel_id));
448
449
448
    sink_info.local_state->_memory_used_counter->set(
450
448
            sink_info.local_state->_shared_state->mem_usage);
451
448
    return Status::OK();
452
448
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
1.31k
                                                   SinkInfo& sink_info) {
456
1.31k
    std::vector<uint32_t> channel_ids;
457
1.31k
    const auto num_rows = block->rows();
458
1.31k
    channel_ids.resize(num_rows, 0);
459
1.31k
    if (num_rows <= _num_partitions) {
460
1.08k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
1.08k
    } else {
462
233
        size_t i = 0;
463
10.5k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
10.2k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
10.2k
        }
466
233
        if (i < num_rows - 1) {
467
137
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
137
        }
469
233
    }
470
471
1.31k
    sink_info.local_state->_memory_used_counter->set(
472
1.31k
            sink_info.local_state->_shared_state->mem_usage);
473
1.31k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
1.31k
    return Status::OK();
475
1.31k
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
1.31k
                                                 Block* block, SinkInfo& sink_info) {
480
1.31k
    const auto rows = cast_set<int32_t>(block->rows());
481
1.31k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
1.31k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
1.31k
    {
484
1.31k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
47.5k
        for (int32_t i = 0; i < rows; ++i) {
486
46.2k
            partition_rows_histogram[channel_ids[i]]++;
487
46.2k
        }
488
9.26k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
7.94k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
7.94k
        }
491
492
47.4k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
46.1k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
46.1k
            partition_rows_histogram[channel_ids[i]]--;
495
46.1k
        }
496
1.31k
    }
497
9.27k
    for (int32_t i = 0; i < _num_partitions; i++) {
498
7.95k
        const size_t start = partition_rows_histogram[i];
499
7.95k
        const size_t size = partition_rows_histogram[i + 1] - start;
500
7.95k
        if (size > 0) {
501
4.22k
            std::unique_ptr<MutableBlock> mutable_block =
502
4.22k
                    MutableBlock::create_unique(block->clone_empty());
503
4.22k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
4.22k
            auto new_block = mutable_block->to_block();
505
506
4.22k
            _enqueue_data_and_set_ready(
507
4.22k
                    i, sink_info.local_state,
508
4.22k
                    BlockWrapper::create_shared(
509
4.22k
                            std::move(new_block),
510
18.4E
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
4.22k
                            i));
512
4.22k
        }
513
7.95k
    }
514
1.31k
    return Status::OK();
515
1.31k
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
5.65k
                                          Profile&& profile, SinkInfo& sink_info) {
519
5.65k
    if (in_block->empty()) {
520
3.88k
        return Status::OK();
521
3.88k
    }
522
1.76k
    if (_is_pass_through) {
523
449
        return _passthrough_sink(state, in_block, sink_info);
524
1.31k
    } else {
525
1.31k
        if (++_total_block >= _num_partitions) {
526
235
            _is_pass_through = true;
527
235
        }
528
1.31k
        return _shuffle_sink(state, in_block, sink_info);
529
1.31k
    }
530
1.76k
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
9.76k
                                               Profile&& profile, SourceInfo&& source_info) {
534
9.76k
    BlockWrapperSPtr next_block;
535
9.76k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
9.76k
    return Status::OK();
537
9.76k
}
538
539
4.52k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
4.52k
    Block next_block;
541
4.52k
    bool eos;
542
4.52k
    BlockWrapperSPtr wrapper;
543
4.52k
    _data_queue[source_info.channel_id].set_eos();
544
4.53k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
4.53k
                         source_info.channel_id)) {
546
        // do nothing
547
5
    }
548
4.52k
}
549
550
} // namespace doris