Coverage Report

Created: 2026-03-19 05:19

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
263k
                                                       BlockType&& block) {
32
263k
    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
263k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
59.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
59.0k
        block.first->record_channel_id(channel_id);
44
204k
    } else {
45
204k
        block->record_channel_id(channel_id);
46
204k
    }
47
48
263k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
263k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
263k
    }
51
263k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
29.8k
                                                       BlockType&& block) {
32
29.8k
    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
29.8k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
29.8k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
29.8k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
29.8k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
29.8k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
29.8k
    }
51
29.8k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
204k
                                                       BlockType&& block) {
32
204k
    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
204k
    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
204k
    } else {
45
204k
        block->record_channel_id(channel_id);
46
204k
    }
47
48
204k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
204k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
204k
    }
51
204k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
29.2k
                                                       BlockType&& block) {
32
29.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
29.2k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
29.2k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
29.2k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
29.2k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
29.1k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
29.1k
    }
51
29.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.08M
                                         int channel_id) {
57
2.08M
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
2.08M
    bool all_finished = _running_sink_operators == 0;
61
2.08M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
58.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
58.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
204k
        } else {
66
204k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
204k
            data_block->swap(block->_data_block);
68
204k
        }
69
263k
        return true;
70
1.82M
    } else if (all_finished) {
71
1.65M
        *eos = true;
72
1.65M
    } else {
73
162k
        std::unique_lock l(*_m[channel_id]);
74
162k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
8
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
8
                local_state->_shared_state->sub_mem_usage(channel_id,
78
8
                                                          block.first->_allocated_bytes);
79
8
            } else {
80
8
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
8
                data_block->swap(block->_data_block);
82
8
            }
83
16
            return true;
84
16
        }
85
162k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
162k
        local_state->_dependency->block();
87
162k
    }
88
1.82M
    return false;
89
2.08M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
320k
                                         int channel_id) {
57
320k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
320k
    bool all_finished = _running_sink_operators == 0;
61
320k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
29.8k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
29.8k
            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
29.8k
        return true;
70
290k
    } else if (all_finished) {
71
270k
        *eos = true;
72
270k
    } else {
73
20.3k
        std::unique_lock l(*_m[channel_id]);
74
20.3k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
6
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
6
                local_state->_shared_state->sub_mem_usage(channel_id,
78
6
                                                          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
6
            return true;
84
6
        }
85
20.3k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
20.3k
        local_state->_dependency->block();
87
20.3k
    }
88
290k
    return false;
89
320k
}
_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
204k
        } else {
66
204k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
204k
            data_block->swap(block->_data_block);
68
204k
        }
69
204k
        return true;
70
1.49M
    } else if (all_finished) {
71
1.35M
        *eos = true;
72
1.35M
    } else {
73
135k
        std::unique_lock l(*_m[channel_id]);
74
135k
        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
8
            } else {
80
8
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
8
                data_block->swap(block->_data_block);
82
8
            }
83
8
            return true;
84
8
        }
85
135k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
135k
        local_state->_dependency->block();
87
135k
    }
88
1.49M
    return false;
89
1.69M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
65.6k
                                         int channel_id) {
57
65.6k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
65.6k
    bool all_finished = _running_sink_operators == 0;
61
65.6k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
29.1k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
29.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
29.1k
        return true;
70
36.4k
    } else if (all_finished) {
71
29.7k
        *eos = true;
72
29.7k
    } else {
73
6.72k
        std::unique_lock l(*_m[channel_id]);
74
6.72k
        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
6.72k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
6.72k
        local_state->_dependency->block();
87
6.72k
    }
88
36.4k
    return false;
89
65.6k
}
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
151k
                              SinkInfo& sink_info) {
117
151k
    if (in_block->empty()) {
118
136k
        return Status::OK();
119
136k
    }
120
15.4k
    {
121
15.4k
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
15.4k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
15.4k
    }
124
15.4k
    {
125
15.4k
        SCOPED_TIMER(profile.distribute_timer);
126
15.4k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
15.4k
                                    *sink_info.channel_id, sink_info.local_state,
128
15.4k
                                    sink_info.shuffle_idx_to_instance_idx));
129
15.4k
    }
130
131
15.4k
    sink_info.local_state->_memory_used_counter->set(
132
15.4k
            sink_info.local_state->_shared_state->mem_usage);
133
15.4k
    return Status::OK();
134
15.4k
}
135
136
137k
void ShuffleExchanger::close(SourceInfo&& source_info) {
137
137k
    PartitionedBlock partitioned_block;
138
137k
    bool eos;
139
137k
    Block block;
140
137k
    _data_queue[source_info.channel_id].set_eos();
141
137k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
137k
                         source_info.channel_id)) {
143
        // do nothing
144
74
    }
145
137k
}
146
147
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
148
153k
                                   SourceInfo&& source_info) {
149
153k
    PartitionedBlock partitioned_block;
150
153k
    MutableBlock mutable_block;
151
152
153k
    auto get_data = [&]() -> Status {
153
29.7k
        do {
154
29.7k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
29.7k
                                       partitioned_block.second.offset_start;
156
29.7k
            auto block_wrapper = partitioned_block.first;
157
29.7k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
29.7k
                                                   offset_start + partitioned_block.second.length));
159
29.7k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
29.7k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
29.2k
                               source_info.channel_id));
162
24.5k
        return Status::OK();
163
24.5k
    };
164
165
153k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
153k
                      source_info.channel_id)) {
167
24.5k
        SCOPED_TIMER(profile.copy_data_timer);
168
24.5k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
24.5k
                block, partitioned_block.first->_data_block);
170
24.5k
        RETURN_IF_ERROR(get_data());
171
24.5k
    }
172
153k
    return Status::OK();
173
153k
}
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
15.3k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
15.3k
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
15.3k
    const auto rows = cast_set<int32_t>(block->rows());
183
15.3k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
15.3k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
15.3k
    {
186
15.3k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
7.78M
        for (int32_t i = 0; i < rows; ++i) {
188
7.77M
            partition_rows_histogram[channel_ids[i]]++;
189
7.77M
        }
190
166k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
151k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
151k
        }
193
7.61M
        for (int32_t i = rows - 1; i >= 0; --i) {
194
7.59M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
7.59M
            partition_rows_histogram[channel_ids[i]]--;
196
7.59M
        }
197
15.3k
    }
198
199
15.3k
    Block data_block;
200
15.3k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
15.3k
    if (!_free_blocks.try_dequeue(data_block)) {
202
9.08k
        data_block = block->clone_empty();
203
9.08k
    }
204
15.3k
    data_block.swap(*block);
205
15.3k
    new_block_wrapper =
206
15.3k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
15.3k
    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
15.3k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
15.3k
    const auto& map = *shuffle_idx_to_instance_idx;
218
15.3k
    int32_t enqueue_rows = 0;
219
150k
    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
150k
        uint32_t start = partition_rows_histogram[it.first];
223
150k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
150k
        if (size > 0) {
225
29.8k
            enqueue_rows += size;
226
29.8k
            _enqueue_data_and_set_ready(
227
29.8k
                    it.second, local_state,
228
29.8k
                    {new_block_wrapper,
229
29.8k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
29.8k
        }
231
150k
    }
232
15.3k
    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
15.3k
    return Status::OK();
246
15.3k
}
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
342k
                                  SinkInfo& sink_info) {
292
342k
    if (in_block->empty()) {
293
146k
        return Status::OK();
294
146k
    }
295
196k
    Block new_block;
296
196k
    if (!_free_blocks.try_dequeue(new_block)) {
297
79.2k
        new_block = {in_block->clone_empty()};
298
79.2k
    }
299
196k
    new_block.swap(*in_block);
300
196k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
196k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
196k
            std::move(new_block),
303
196k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
196k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
196k
    sink_info.local_state->_memory_used_counter->set(
308
196k
            sink_info.local_state->_shared_state->mem_usage);
309
310
196k
    return Status::OK();
311
342k
}
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
27
    }
322
666k
}
323
324
11.4k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
11.4k
    Block next_block;
326
11.4k
    BlockWrapperSPtr wrapper;
327
11.4k
    bool eos;
328
11.4k
    _data_queue[source_info.channel_id].set_eos();
329
11.4k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
11.4k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
11.4k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
995k
                                       Profile&& profile, SourceInfo&& source_info) {
337
995k
    BlockWrapperSPtr next_block;
338
995k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
995k
    return Status::OK();
340
995k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
4.12k
                                SinkInfo& sink_info) {
344
4.12k
    if (in_block->empty()) {
345
1.55k
        return Status::OK();
346
1.55k
    }
347
2.57k
    Block new_block;
348
2.57k
    if (!_free_blocks.try_dequeue(new_block)) {
349
1.73k
        new_block = {in_block->clone_empty()};
350
1.73k
    }
351
2.57k
    new_block.swap(*in_block);
352
353
2.57k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
2.57k
            std::move(new_block),
355
2.57k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
2.57k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
2.57k
    sink_info.local_state->_memory_used_counter->set(
359
2.57k
            sink_info.local_state->_shared_state->mem_usage);
360
361
2.57k
    return Status::OK();
362
4.12k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
4.90k
                                     Profile&& profile, SourceInfo&& source_info) {
366
4.90k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
4.89k
    BlockWrapperSPtr next_block;
371
4.89k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
4.89k
    return Status::OK();
373
4.90k
}
374
375
123k
void ExchangerBase::finalize() {
376
123k
    DCHECK(_running_source_operators == 0);
377
123k
    Block block;
378
221k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
97.3k
    }
381
123k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
5.52k
                                SinkInfo& sink_info) {
385
5.52k
    if (in_block->empty()) {
386
1.88k
        return Status::OK();
387
1.88k
    }
388
3.64k
    Block new_block;
389
3.64k
    if (!_free_blocks.try_dequeue(new_block)) {
390
2.49k
        new_block = {in_block->clone_empty()};
391
2.49k
    }
392
3.64k
    new_block.swap(*in_block);
393
3.64k
    auto wrapper = BlockWrapper::create_shared(
394
3.64k
            std::move(new_block),
395
3.64k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
32.8k
    for (int i = 0; i < _num_partitions; i++) {
397
29.2k
        _enqueue_data_and_set_ready(
398
29.2k
                i, sink_info.local_state,
399
29.2k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
29.2k
    }
401
402
3.64k
    return Status::OK();
403
5.52k
}
404
405
14.8k
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
14.8k
    BroadcastBlock partitioned_block;
407
14.8k
    bool eos;
408
14.8k
    Block block;
409
14.8k
    _data_queue[source_info.channel_id].set_eos();
410
14.8k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
14.8k
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
14.8k
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
50.7k
                                     Profile&& profile, SourceInfo&& source_info) {
418
50.7k
    BroadcastBlock partitioned_block;
419
420
50.7k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
50.7k
                      source_info.channel_id)) {
422
29.1k
        SCOPED_TIMER(profile.copy_data_timer);
423
29.1k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
29.1k
                block, partitioned_block.first->_data_block);
425
29.1k
        auto block_wrapper = partitioned_block.first;
426
29.1k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
29.1k
                                               partitioned_block.second.offset_start,
428
29.1k
                                               partitioned_block.second.length));
429
29.1k
    }
430
431
50.7k
    return Status::OK();
432
50.7k
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
578
                                                       SinkInfo& sink_info) {
436
578
    Block new_block;
437
578
    if (!_free_blocks.try_dequeue(new_block)) {
438
304
        new_block = {in_block->clone_empty()};
439
304
    }
440
578
    new_block.swap(*in_block);
441
578
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
578
    _enqueue_data_and_set_ready(
443
578
            channel_id, sink_info.local_state,
444
578
            BlockWrapper::create_shared(
445
578
                    std::move(new_block),
446
578
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
578
                    channel_id));
448
449
578
    sink_info.local_state->_memory_used_counter->set(
450
578
            sink_info.local_state->_shared_state->mem_usage);
451
578
    return Status::OK();
452
578
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
2.15k
                                                   SinkInfo& sink_info) {
456
2.15k
    std::vector<uint32_t> channel_ids;
457
2.15k
    const auto num_rows = block->rows();
458
2.15k
    channel_ids.resize(num_rows, 0);
459
2.15k
    if (num_rows <= _num_partitions) {
460
1.78k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
1.78k
    } else {
462
369
        size_t i = 0;
463
10.7k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
10.4k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
10.4k
        }
466
369
        if (i < num_rows - 1) {
467
177
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
177
        }
469
369
    }
470
471
2.15k
    sink_info.local_state->_memory_used_counter->set(
472
2.15k
            sink_info.local_state->_shared_state->mem_usage);
473
2.15k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
2.15k
    return Status::OK();
475
2.15k
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
2.15k
                                                 Block* block, SinkInfo& sink_info) {
480
2.15k
    const auto rows = cast_set<int32_t>(block->rows());
481
2.15k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
2.15k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
2.15k
    {
484
2.15k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
50.1k
        for (int32_t i = 0; i < rows; ++i) {
486
47.9k
            partition_rows_histogram[channel_ids[i]]++;
487
47.9k
        }
488
15.2k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
13.1k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
13.1k
        }
491
492
50.1k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
47.9k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
47.9k
            partition_rows_histogram[channel_ids[i]]--;
495
47.9k
        }
496
2.15k
    }
497
15.2k
    for (int32_t i = 0; i < _num_partitions; i++) {
498
13.1k
        const size_t start = partition_rows_histogram[i];
499
13.1k
        const size_t size = partition_rows_histogram[i + 1] - start;
500
13.1k
        if (size > 0) {
501
5.55k
            std::unique_ptr<MutableBlock> mutable_block =
502
5.55k
                    MutableBlock::create_unique(block->clone_empty());
503
5.55k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
5.55k
            auto new_block = mutable_block->to_block();
505
506
5.55k
            _enqueue_data_and_set_ready(
507
5.55k
                    i, sink_info.local_state,
508
5.55k
                    BlockWrapper::create_shared(
509
5.55k
                            std::move(new_block),
510
18.4E
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
5.55k
                            i));
512
5.55k
        }
513
13.1k
    }
514
2.15k
    return Status::OK();
515
2.15k
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
7.40k
                                          Profile&& profile, SinkInfo& sink_info) {
519
7.40k
    if (in_block->empty()) {
520
4.66k
        return Status::OK();
521
4.66k
    }
522
2.74k
    if (_is_pass_through) {
523
578
        return _passthrough_sink(state, in_block, sink_info);
524
2.16k
    } else {
525
2.16k
        if (++_total_block >= _num_partitions) {
526
275
            _is_pass_through = true;
527
275
        }
528
2.16k
        return _shuffle_sink(state, in_block, sink_info);
529
2.16k
    }
530
2.74k
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
12.1k
                                               Profile&& profile, SourceInfo&& source_info) {
534
12.1k
    BlockWrapperSPtr next_block;
535
12.1k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
12.1k
    return Status::OK();
537
12.1k
}
538
539
5.24k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
5.24k
    Block next_block;
541
5.24k
    bool eos;
542
5.24k
    BlockWrapperSPtr wrapper;
543
5.24k
    _data_queue[source_info.channel_id].set_eos();
544
5.25k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
5.25k
                         source_info.channel_id)) {
546
        // do nothing
547
5
    }
548
5.24k
}
549
550
} // namespace doris