Coverage Report

Created: 2026-03-31 13:11

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
302k
                                                       BlockType&& block) {
32
302k
    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
302k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
83.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
83.2k
        block.first->record_channel_id(channel_id);
44
218k
    } else {
45
218k
        block->record_channel_id(channel_id);
46
218k
    }
47
48
302k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
301k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
301k
    }
51
302k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
47.0k
                                                       BlockType&& block) {
32
47.0k
    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
47.0k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
47.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
47.0k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
47.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
46.8k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
46.8k
    }
51
47.0k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
218k
                                                       BlockType&& block) {
32
218k
    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
218k
    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
218k
    } else {
45
218k
        block->record_channel_id(channel_id);
46
218k
    }
47
48
218k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
218k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
218k
    }
51
218k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
36.2k
                                                       BlockType&& block) {
32
36.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
36.2k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
36.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
36.2k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
36.2k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
36.2k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
36.2k
    }
51
36.2k
}
52
53
template <typename BlockType>
54
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
55
                                         BlockType& block, bool* eos, Block* data_block,
56
2.16M
                                         int channel_id) {
57
2.16M
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
2.16M
    bool all_finished = _running_sink_operators == 0;
61
2.16M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
82.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
82.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
218k
        } else {
66
218k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
218k
            data_block->swap(block->_data_block);
68
218k
        }
69
301k
        return true;
70
1.86M
    } else if (all_finished) {
71
1.71M
        *eos = true;
72
1.71M
    } else {
73
150k
        std::unique_lock l(*_m[channel_id]);
74
150k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
18
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
18
                local_state->_shared_state->sub_mem_usage(channel_id,
78
18
                                                          block.first->_allocated_bytes);
79
18
            } else {
80
5
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
5
                data_block->swap(block->_data_block);
82
5
            }
83
23
            return true;
84
23
        }
85
150k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
150k
        local_state->_dependency->block();
87
150k
    }
88
1.86M
    return false;
89
2.16M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
389k
                                         int channel_id) {
57
389k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
389k
    bool all_finished = _running_sink_operators == 0;
61
389k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
46.7k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
46.7k
            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
46.7k
        return true;
70
342k
    } else if (all_finished) {
71
317k
        *eos = true;
72
317k
    } else {
73
25.2k
        std::unique_lock l(*_m[channel_id]);
74
25.2k
        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
25.2k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
25.2k
        local_state->_dependency->block();
87
25.2k
    }
88
342k
    return false;
89
389k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
56
1.69M
                                         int channel_id) {
57
1.69M
    if (local_state == nullptr) {
58
12
        return _dequeue_data(block, eos, data_block, channel_id);
59
12
    }
60
1.69M
    bool all_finished = _running_sink_operators == 0;
61
1.69M
    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
218k
        } else {
66
218k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
218k
            data_block->swap(block->_data_block);
68
218k
        }
69
218k
        return true;
70
1.47M
    } else if (all_finished) {
71
1.36M
        *eos = true;
72
1.36M
    } else {
73
118k
        std::unique_lock l(*_m[channel_id]);
74
118k
        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
5
            } else {
80
5
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
5
                data_block->swap(block->_data_block);
82
5
            }
83
5
            return true;
84
5
        }
85
118k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
118k
        local_state->_dependency->block();
87
118k
    }
88
1.47M
    return false;
89
1.69M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
78.0k
                                         int channel_id) {
57
78.0k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
78.0k
    bool all_finished = _running_sink_operators == 0;
61
78.0k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
36.1k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
36.1k
            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
36.1k
        return true;
70
41.8k
    } else if (all_finished) {
71
34.9k
        *eos = true;
72
34.9k
    } else {
73
6.91k
        std::unique_lock l(*_m[channel_id]);
74
6.91k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
5
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
5
                local_state->_shared_state->sub_mem_usage(channel_id,
78
5
                                                          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
5
            return true;
84
5
        }
85
6.90k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
6.90k
        local_state->_dependency->block();
87
6.90k
    }
88
41.8k
    return false;
89
78.0k
}
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
184k
                              SinkInfo& sink_info) {
117
184k
    if (in_block->empty()) {
118
160k
        return Status::OK();
119
160k
    }
120
24.4k
    {
121
24.4k
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
24.4k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
24.4k
    }
124
24.4k
    {
125
24.4k
        SCOPED_TIMER(profile.distribute_timer);
126
24.4k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
24.4k
                                    *sink_info.channel_id, sink_info.local_state,
128
24.4k
                                    sink_info.shuffle_idx_to_instance_idx));
129
24.4k
    }
130
131
24.4k
    sink_info.local_state->_memory_used_counter->set(
132
24.4k
            sink_info.local_state->_shared_state->mem_usage);
133
24.4k
    return Status::OK();
134
24.4k
}
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
162k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
162k
                         source_info.channel_id)) {
143
        // do nothing
144
962
    }
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
45.7k
        do {
154
45.7k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
45.7k
                                       partitioned_block.second.offset_start;
156
45.7k
            auto block_wrapper = partitioned_block.first;
157
45.7k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
45.7k
                                                   offset_start + partitioned_block.second.length));
159
45.7k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
45.7k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
43.9k
                               source_info.channel_id));
162
35.3k
        return Status::OK();
163
35.3k
    };
164
165
183k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
183k
                      source_info.channel_id)) {
167
35.4k
        SCOPED_TIMER(profile.copy_data_timer);
168
35.4k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
35.4k
                block, partitioned_block.first->_data_block);
170
35.4k
        RETURN_IF_ERROR(get_data());
171
35.4k
    }
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
24.2k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
24.2k
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
24.2k
    const auto rows = cast_set<int32_t>(block->rows());
183
24.2k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
24.2k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
24.2k
    {
186
24.2k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
21.9M
        for (int32_t i = 0; i < rows; ++i) {
188
21.8M
            partition_rows_histogram[channel_ids[i]]++;
189
21.8M
        }
190
213k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
188k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
188k
        }
193
20.8M
        for (int32_t i = rows - 1; i >= 0; --i) {
194
20.7M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
20.7M
            partition_rows_histogram[channel_ids[i]]--;
196
20.7M
        }
197
24.2k
    }
198
199
24.2k
    Block data_block;
200
24.2k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
24.2k
    if (!_free_blocks.try_dequeue(data_block)) {
202
15.8k
        data_block = block->clone_empty();
203
15.8k
    }
204
24.2k
    data_block.swap(*block);
205
24.2k
    new_block_wrapper =
206
24.2k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
24.2k
    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
24.2k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
24.2k
    const auto& map = *shuffle_idx_to_instance_idx;
218
24.2k
    int32_t enqueue_rows = 0;
219
188k
    for (const auto& it : map) {
220
18.4E
        DCHECK(it.second >= 0 && it.second < _num_partitions)
221
18.4E
                << it.first << " : " << it.second << " " << _num_partitions;
222
188k
        uint32_t start = partition_rows_histogram[it.first];
223
188k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
188k
        if (size > 0) {
225
47.0k
            enqueue_rows += size;
226
47.0k
            _enqueue_data_and_set_ready(
227
47.0k
                    it.second, local_state,
228
47.0k
                    {new_block_wrapper,
229
47.0k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
47.0k
        }
231
188k
    }
232
24.2k
    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
24.2k
    return Status::OK();
246
24.2k
}
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
318k
                                  SinkInfo& sink_info) {
292
318k
    if (in_block->empty()) {
293
108k
        return Status::OK();
294
108k
    }
295
209k
    Block new_block;
296
209k
    if (!_free_blocks.try_dequeue(new_block)) {
297
93.5k
        new_block = {in_block->clone_empty()};
298
93.5k
    }
299
209k
    new_block.swap(*in_block);
300
209k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
209k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
209k
            std::move(new_block),
303
209k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
209k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
209k
    sink_info.local_state->_memory_used_counter->set(
308
209k
            sink_info.local_state->_shared_state->mem_usage);
309
310
209k
    return Status::OK();
311
318k
}
312
313
666k
void PassthroughExchanger::close(SourceInfo&& source_info) {
314
666k
    Block next_block;
315
666k
    BlockWrapperSPtr wrapper;
316
666k
    bool eos;
317
666k
    _data_queue[source_info.channel_id].set_eos();
318
666k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
319
666k
                         source_info.channel_id)) {
320
        // do nothing
321
48
    }
322
666k
}
323
324
10.8k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
10.8k
    Block next_block;
326
10.8k
    BlockWrapperSPtr wrapper;
327
10.8k
    bool eos;
328
10.8k
    _data_queue[source_info.channel_id].set_eos();
329
10.8k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
10.8k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
10.8k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
991k
                                       Profile&& profile, SourceInfo&& source_info) {
337
991k
    BlockWrapperSPtr next_block;
338
991k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
991k
    return Status::OK();
340
991k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
4.55k
                                SinkInfo& sink_info) {
344
4.55k
    if (in_block->empty()) {
345
1.54k
        return Status::OK();
346
1.54k
    }
347
3.00k
    Block new_block;
348
3.00k
    if (!_free_blocks.try_dequeue(new_block)) {
349
1.85k
        new_block = {in_block->clone_empty()};
350
1.85k
    }
351
3.00k
    new_block.swap(*in_block);
352
353
3.00k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
3.00k
            std::move(new_block),
355
3.00k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
3.00k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
3.00k
    sink_info.local_state->_memory_used_counter->set(
359
3.00k
            sink_info.local_state->_shared_state->mem_usage);
360
361
3.00k
    return Status::OK();
362
4.55k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
5.39k
                                     Profile&& profile, SourceInfo&& source_info) {
366
5.39k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
5.39k
    BlockWrapperSPtr next_block;
371
5.39k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
5.39k
    return Status::OK();
373
5.39k
}
374
375
135k
void ExchangerBase::finalize() {
376
135k
    DCHECK(_running_source_operators == 0);
377
135k
    Block block;
378
250k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
115k
    }
381
135k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
5.16k
                                SinkInfo& sink_info) {
385
5.16k
    if (in_block->empty()) {
386
1.84k
        return Status::OK();
387
1.84k
    }
388
3.32k
    Block new_block;
389
3.32k
    if (!_free_blocks.try_dequeue(new_block)) {
390
2.48k
        new_block = {in_block->clone_empty()};
391
2.48k
    }
392
3.32k
    new_block.swap(*in_block);
393
3.32k
    auto wrapper = BlockWrapper::create_shared(
394
3.32k
            std::move(new_block),
395
3.32k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
39.5k
    for (int i = 0; i < _num_partitions; i++) {
397
36.2k
        _enqueue_data_and_set_ready(
398
36.2k
                i, sink_info.local_state,
399
36.2k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
36.2k
    }
401
402
3.32k
    return Status::OK();
403
5.16k
}
404
405
17.4k
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
17.4k
    BroadcastBlock partitioned_block;
407
17.4k
    bool eos;
408
17.4k
    Block block;
409
17.4k
    _data_queue[source_info.channel_id].set_eos();
410
17.4k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
17.4k
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
17.4k
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
60.5k
                                     Profile&& profile, SourceInfo&& source_info) {
418
60.5k
    BroadcastBlock partitioned_block;
419
420
60.5k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
60.5k
                      source_info.channel_id)) {
422
36.1k
        SCOPED_TIMER(profile.copy_data_timer);
423
36.1k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
36.1k
                block, partitioned_block.first->_data_block);
425
36.1k
        auto block_wrapper = partitioned_block.first;
426
36.1k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
36.1k
                                               partitioned_block.second.offset_start,
428
36.1k
                                               partitioned_block.second.length));
429
36.1k
    }
430
431
60.5k
    return Status::OK();
432
60.5k
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
631
                                                       SinkInfo& sink_info) {
436
631
    Block new_block;
437
631
    if (!_free_blocks.try_dequeue(new_block)) {
438
271
        new_block = {in_block->clone_empty()};
439
271
    }
440
631
    new_block.swap(*in_block);
441
631
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
631
    _enqueue_data_and_set_ready(
443
631
            channel_id, sink_info.local_state,
444
631
            BlockWrapper::create_shared(
445
631
                    std::move(new_block),
446
631
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
631
                    channel_id));
448
449
631
    sink_info.local_state->_memory_used_counter->set(
450
631
            sink_info.local_state->_shared_state->mem_usage);
451
631
    return Status::OK();
452
631
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
1.99k
                                                   SinkInfo& sink_info) {
456
1.99k
    std::vector<uint32_t> channel_ids;
457
1.99k
    const auto num_rows = block->rows();
458
1.99k
    channel_ids.resize(num_rows, 0);
459
1.99k
    if (num_rows <= _num_partitions) {
460
1.69k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
1.69k
    } else {
462
303
        size_t i = 0;
463
9.67k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
9.37k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
9.37k
        }
466
303
        if (i < num_rows - 1) {
467
166
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
166
        }
469
303
    }
470
471
1.99k
    sink_info.local_state->_memory_used_counter->set(
472
1.99k
            sink_info.local_state->_shared_state->mem_usage);
473
1.99k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
1.99k
    return Status::OK();
475
1.99k
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
1.99k
                                                 Block* block, SinkInfo& sink_info) {
480
1.99k
    const auto rows = cast_set<int32_t>(block->rows());
481
1.99k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
1.99k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
1.99k
    {
484
1.99k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
41.1k
        for (int32_t i = 0; i < rows; ++i) {
486
39.1k
            partition_rows_histogram[channel_ids[i]]++;
487
39.1k
        }
488
19.6k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
17.6k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
17.6k
        }
491
492
41.1k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
39.1k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
39.1k
            partition_rows_histogram[channel_ids[i]]--;
495
39.1k
        }
496
1.99k
    }
497
19.6k
    for (int32_t i = 0; i < _num_partitions; i++) {
498
17.6k
        const size_t start = partition_rows_histogram[i];
499
17.6k
        const size_t size = partition_rows_histogram[i + 1] - start;
500
17.6k
        if (size > 0) {
501
5.71k
            std::unique_ptr<MutableBlock> mutable_block =
502
5.71k
                    MutableBlock::create_unique(block->clone_empty());
503
5.71k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
5.71k
            auto new_block = mutable_block->to_block();
505
506
5.71k
            _enqueue_data_and_set_ready(
507
5.71k
                    i, sink_info.local_state,
508
5.71k
                    BlockWrapper::create_shared(
509
5.71k
                            std::move(new_block),
510
18.4E
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
5.71k
                            i));
512
5.71k
        }
513
17.6k
    }
514
1.99k
    return Status::OK();
515
1.99k
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
9.27k
                                          Profile&& profile, SinkInfo& sink_info) {
519
9.27k
    if (in_block->empty()) {
520
6.64k
        return Status::OK();
521
6.64k
    }
522
2.63k
    if (_is_pass_through) {
523
631
        return _passthrough_sink(state, in_block, sink_info);
524
2.00k
    } else {
525
2.00k
        if (++_total_block >= _num_partitions) {
526
275
            _is_pass_through = true;
527
275
        }
528
2.00k
        return _shuffle_sink(state, in_block, sink_info);
529
2.00k
    }
530
2.63k
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
14.4k
                                               Profile&& profile, SourceInfo&& source_info) {
534
14.4k
    BlockWrapperSPtr next_block;
535
14.4k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
14.4k
    return Status::OK();
537
14.4k
}
538
539
7.40k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
7.40k
    Block next_block;
541
7.40k
    bool eos;
542
7.40k
    BlockWrapperSPtr wrapper;
543
7.40k
    _data_queue[source_info.channel_id].set_eos();
544
7.41k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
7.41k
                         source_info.channel_id)) {
546
        // do nothing
547
3
    }
548
7.40k
}
549
550
} // namespace doris