Coverage Report

Created: 2026-04-14 08:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/local_exchanger.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exec/exchange/local_exchanger.h"
19
20
#include "common/cast_set.h"
21
#include "common/status.h"
22
#include "exec/exchange/local_exchange_sink_operator.h"
23
#include "exec/exchange/local_exchange_source_operator.h"
24
#include "exec/partitioner/partitioner.h"
25
26
namespace doris {
27
template <typename BlockType>
28
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
29
                                                       LocalExchangeSinkLocalState* local_state,
30
261k
                                                       BlockType&& block) {
31
261k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
261k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
54.7k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
54.7k
        block.first->record_channel_id(channel_id);
43
206k
    } else {
44
206k
        block->record_channel_id(channel_id);
45
206k
    }
46
47
261k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
260k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
260k
    }
50
261k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
40.3k
                                                       BlockType&& block) {
31
40.3k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
40.3k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
40.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
40.3k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
40.3k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
39.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
39.5k
    }
50
40.3k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
206k
                                                       BlockType&& block) {
31
206k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
206k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
        block.first->record_channel_id(channel_id);
43
206k
    } else {
44
206k
        block->record_channel_id(channel_id);
45
206k
    }
46
47
206k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
206k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
206k
    }
50
206k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
14.4k
                                                       BlockType&& block) {
31
14.4k
    if (local_state == nullptr) {
32
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
33
0
        return;
34
0
    }
35
    // PartitionedBlock is used by shuffle exchanger.
36
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
37
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
38
    // one queue.
39
14.4k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
14.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
14.4k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
14.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
14.4k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
14.4k
    }
50
14.4k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
2.06M
                                         int channel_id) {
56
2.06M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
2.06M
    bool all_finished = _running_sink_operators == 0;
60
2.06M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
53.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
53.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
206k
        } else {
65
206k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
206k
            data_block->swap(block->_data_block);
67
206k
        }
68
260k
        return true;
69
1.80M
    } else if (all_finished) {
70
1.64M
        *eos = true;
71
1.64M
    } else {
72
158k
        std::unique_lock l(*_m[channel_id]);
73
158k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
7
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
7
                local_state->_shared_state->sub_mem_usage(channel_id,
77
7
                                                          block.first->_allocated_bytes);
78
14
            } else {
79
14
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
14
                data_block->swap(block->_data_block);
81
14
            }
82
21
            return true;
83
21
        }
84
158k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
158k
        local_state->_dependency->block();
86
158k
    }
87
1.80M
    return false;
88
2.06M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
253k
                                         int channel_id) {
56
253k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
253k
    bool all_finished = _running_sink_operators == 0;
60
253k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
39.4k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
39.4k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
39.4k
        return true;
69
213k
    } else if (all_finished) {
70
189k
        *eos = true;
71
189k
    } else {
72
24.0k
        std::unique_lock l(*_m[channel_id]);
73
24.0k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
6
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
6
                local_state->_shared_state->sub_mem_usage(channel_id,
77
6
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
6
            return true;
83
6
        }
84
24.0k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
24.0k
        local_state->_dependency->block();
86
24.0k
    }
87
213k
    return false;
88
253k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.78M
                                         int channel_id) {
56
1.78M
    if (local_state == nullptr) {
57
12
        return _dequeue_data(block, eos, data_block, channel_id);
58
12
    }
59
1.78M
    bool all_finished = _running_sink_operators == 0;
60
1.78M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
206k
        } else {
65
206k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
206k
            data_block->swap(block->_data_block);
67
206k
        }
68
206k
        return true;
69
1.57M
    } else if (all_finished) {
70
1.44M
        *eos = true;
71
1.44M
    } else {
72
130k
        std::unique_lock l(*_m[channel_id]);
73
130k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
                local_state->_shared_state->sub_mem_usage(channel_id,
77
                                                          block.first->_allocated_bytes);
78
14
            } else {
79
14
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
14
                data_block->swap(block->_data_block);
81
14
            }
82
14
            return true;
83
14
        }
84
130k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
130k
        local_state->_dependency->block();
86
130k
    }
87
1.57M
    return false;
88
1.78M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
32.8k
                                         int channel_id) {
56
32.8k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
32.8k
    bool all_finished = _running_sink_operators == 0;
60
32.8k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
14.4k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
14.4k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
        } else {
65
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
            data_block->swap(block->_data_block);
67
        }
68
14.4k
        return true;
69
18.4k
    } else if (all_finished) {
70
14.8k
        *eos = true;
71
14.8k
    } else {
72
3.51k
        std::unique_lock l(*_m[channel_id]);
73
3.51k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
1
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
1
                local_state->_shared_state->sub_mem_usage(channel_id,
77
1
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
1
            return true;
83
1
        }
84
3.51k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
3.51k
        local_state->_dependency->block();
86
3.51k
    }
87
18.4k
    return false;
88
32.8k
}
89
90
template <typename BlockType>
91
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
92
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
93
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
94
0
        block.first->record_channel_id(channel_id);
95
0
    } else {
96
0
        block->record_channel_id(channel_id);
97
0
    }
98
0
    _data_queue[channel_id].enqueue(std::move(block));
99
0
}
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_
Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_
100
101
template <typename BlockType>
102
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
103
20
                                         int channel_id) {
104
20
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
20
    return false;
112
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
12
                                         int channel_id) {
104
12
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
12
    return false;
112
12
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
113
114
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
115
115k
                              SinkInfo& sink_info) {
116
115k
    if (in_block->empty()) {
117
95.0k
        return Status::OK();
118
95.0k
    }
119
20.4k
    {
120
20.4k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
20.4k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
20.4k
    }
123
20.4k
    {
124
20.4k
        SCOPED_TIMER(profile.distribute_timer);
125
20.4k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
20.4k
                                    *sink_info.channel_id, sink_info.local_state,
127
20.4k
                                    sink_info.shuffle_idx_to_instance_idx));
128
20.4k
    }
129
130
20.4k
    sink_info.local_state->_memory_used_counter->set(
131
20.4k
            sink_info.local_state->_shared_state->mem_usage);
132
20.4k
    return Status::OK();
133
20.4k
}
134
135
95.8k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
95.8k
    PartitionedBlock partitioned_block;
137
95.8k
    bool eos;
138
95.8k
    Block block;
139
95.8k
    _data_queue[source_info.channel_id].set_eos();
140
95.8k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
95.8k
                         source_info.channel_id)) {
142
        // do nothing
143
3
    }
144
95.8k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
118k
                                   SourceInfo&& source_info) {
148
118k
    PartitionedBlock partitioned_block;
149
118k
    MutableBlock mutable_block;
150
151
118k
    auto get_data = [&]() -> Status {
152
39.4k
        do {
153
39.4k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
39.4k
                                       partitioned_block.second.offset_start;
155
39.4k
            auto block_wrapper = partitioned_block.first;
156
39.4k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
39.4k
                                                   offset_start + partitioned_block.second.length));
158
39.4k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
39.4k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
38.4k
                               source_info.channel_id));
161
30.8k
        return Status::OK();
162
30.8k
    };
163
164
118k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
118k
                      source_info.channel_id)) {
166
30.8k
        SCOPED_TIMER(profile.copy_data_timer);
167
30.8k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
30.8k
                block, partitioned_block.first->_data_block);
169
30.8k
        RETURN_IF_ERROR(get_data());
170
30.8k
    }
171
118k
    return Status::OK();
172
118k
}
173
174
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
175
                                     Block* block, int channel_id,
176
                                     LocalExchangeSinkLocalState* local_state,
177
20.3k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
20.3k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
20.3k
    const auto rows = cast_set<int32_t>(block->rows());
182
20.3k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
20.3k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
20.3k
    {
185
20.3k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
14.2M
        for (int32_t i = 0; i < rows; ++i) {
187
14.1M
            partition_rows_histogram[channel_ids[i]]++;
188
14.1M
        }
189
220k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
199k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
199k
        }
192
13.2M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
13.2M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
13.2M
            partition_rows_histogram[channel_ids[i]]--;
195
13.2M
        }
196
20.3k
    }
197
198
20.3k
    Block data_block;
199
20.3k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
20.3k
    if (!_free_blocks.try_dequeue(data_block)) {
201
9.99k
        data_block = block->clone_empty();
202
9.99k
    }
203
20.3k
    data_block.swap(*block);
204
20.3k
    new_block_wrapper =
205
20.3k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
20.3k
    if (new_block_wrapper->_data_block.empty()) {
207
0
        return Status::OK();
208
0
    }
209
    /**
210
     * Data are hash-shuffled and distributed to all instances of
211
     * all BEs. So we need a shuffleId-To-InstanceId mapping.
212
     * For example, row 1 get a hash value 1 which means we should distribute to instance 1 on
213
     * BE 1 and row 2 get a hash value 2 which means we should distribute to instance 1 on BE 3.
214
     */
215
20.3k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
20.3k
    const auto& map = *shuffle_idx_to_instance_idx;
217
20.3k
    int32_t enqueue_rows = 0;
218
199k
    for (const auto& it : map) {
219
199k
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
2
                << it.first << " : " << it.second << " " << _num_partitions;
221
199k
        uint32_t start = partition_rows_histogram[it.first];
222
199k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
199k
        if (size > 0) {
224
40.3k
            enqueue_rows += size;
225
40.3k
            _enqueue_data_and_set_ready(
226
40.3k
                    it.second, local_state,
227
40.3k
                    {new_block_wrapper,
228
40.3k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
40.3k
        }
230
199k
    }
231
20.3k
    if (enqueue_rows != rows) [[unlikely]] {
232
1
        fmt::memory_buffer debug_string_buffer;
233
1
        fmt::format_to(debug_string_buffer, "Type: {}, Local Exchange Id: {}, Shuffled Map: ",
234
1
                       get_exchange_type_name(get_type()), local_state->parent()->node_id());
235
3
        for (const auto& it : map) {
236
3
            fmt::format_to(debug_string_buffer, "[{}:{}], ", it.first, it.second);
237
3
        }
238
1
        return Status::InternalError(
239
1
                "Rows mismatched! Data may be lost. [Expected enqueue rows={}, Real enqueue "
240
1
                "rows={}, Detail: {}]",
241
1
                rows, enqueue_rows, fmt::to_string(debug_string_buffer));
242
1
    }
243
244
20.3k
    return Status::OK();
245
20.3k
}
246
247
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
248
0
                                     Block* block, int channel_id) {
249
0
    const auto rows = cast_set<int32_t>(block->rows());
250
0
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
251
0
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
252
0
    {
253
0
        partition_rows_histogram.assign(_num_partitions + 1, 0);
254
0
        for (int32_t i = 0; i < rows; ++i) {
255
0
            partition_rows_histogram[channel_ids[i]]++;
256
0
        }
257
0
        for (int32_t i = 1; i <= _num_partitions; ++i) {
258
0
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
259
0
        }
260
0
        for (int32_t i = rows - 1; i >= 0; --i) {
261
0
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
262
0
            partition_rows_histogram[channel_ids[i]]--;
263
0
        }
264
0
    }
265
266
0
    Block data_block;
267
0
    std::shared_ptr<BlockWrapper> new_block_wrapper;
268
0
    if (!_free_blocks.try_dequeue(data_block)) {
269
0
        data_block = block->clone_empty();
270
0
    }
271
0
    data_block.swap(*block);
272
0
    new_block_wrapper = BlockWrapper::create_shared(std::move(data_block), nullptr, -1);
273
0
    if (new_block_wrapper->_data_block.empty()) {
274
0
        return Status::OK();
275
0
    }
276
0
    for (int i = 0; i < _num_partitions; i++) {
277
0
        uint32_t start = partition_rows_histogram[i];
278
0
        uint32_t size = partition_rows_histogram[i + 1] - start;
279
0
        if (size > 0) {
280
0
            _enqueue_data_and_set_ready(
281
0
                    i, {new_block_wrapper,
282
0
                        {.row_idxs = row_idx, .offset_start = start, .length = size}});
283
0
        }
284
0
    }
285
286
0
    return Status::OK();
287
0
}
288
289
Status PassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
290
313k
                                  SinkInfo& sink_info) {
291
313k
    if (in_block->empty()) {
292
115k
        return Status::OK();
293
115k
    }
294
197k
    Block new_block;
295
197k
    if (!_free_blocks.try_dequeue(new_block)) {
296
81.6k
        new_block = {in_block->clone_empty()};
297
81.6k
    }
298
197k
    new_block.swap(*in_block);
299
197k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
197k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
197k
            std::move(new_block),
302
197k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
197k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
197k
    sink_info.local_state->_memory_used_counter->set(
307
197k
            sink_info.local_state->_shared_state->mem_usage);
308
309
197k
    return Status::OK();
310
313k
}
311
312
708k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
708k
    Block next_block;
314
708k
    BlockWrapperSPtr wrapper;
315
708k
    bool eos;
316
708k
    _data_queue[source_info.channel_id].set_eos();
317
709k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
709k
                         source_info.channel_id)) {
319
        // do nothing
320
1.43k
    }
321
708k
}
322
323
14.5k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
14.5k
    Block next_block;
325
14.5k
    BlockWrapperSPtr wrapper;
326
14.5k
    bool eos;
327
14.5k
    _data_queue[source_info.channel_id].set_eos();
328
14.5k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
14.5k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
14.5k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
1.03M
                                       Profile&& profile, SourceInfo&& source_info) {
336
1.03M
    BlockWrapperSPtr next_block;
337
1.03M
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
1.03M
    return Status::OK();
339
1.03M
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
5.01k
                                SinkInfo& sink_info) {
343
5.01k
    if (in_block->empty()) {
344
1.76k
        return Status::OK();
345
1.76k
    }
346
3.25k
    Block new_block;
347
3.25k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.98k
        new_block = {in_block->clone_empty()};
349
1.98k
    }
350
3.25k
    new_block.swap(*in_block);
351
352
3.25k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
3.25k
            std::move(new_block),
354
3.25k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
3.25k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
3.25k
    sink_info.local_state->_memory_used_counter->set(
358
3.25k
            sink_info.local_state->_shared_state->mem_usage);
359
360
3.25k
    return Status::OK();
361
5.01k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
6.05k
                                     Profile&& profile, SourceInfo&& source_info) {
365
6.05k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
6.05k
    BlockWrapperSPtr next_block;
370
6.05k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
6.05k
    return Status::OK();
372
6.05k
}
373
374
115k
void ExchangerBase::finalize() {
375
115k
    DCHECK(_running_source_operators == 0);
376
115k
    Block block;
377
213k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
98.0k
    }
380
115k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
2.89k
                                SinkInfo& sink_info) {
384
2.89k
    if (in_block->empty()) {
385
1.01k
        return Status::OK();
386
1.01k
    }
387
1.88k
    Block new_block;
388
1.88k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.41k
        new_block = {in_block->clone_empty()};
390
1.41k
    }
391
1.88k
    new_block.swap(*in_block);
392
1.88k
    auto wrapper = BlockWrapper::create_shared(
393
1.88k
            std::move(new_block),
394
1.88k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
16.3k
    for (int i = 0; i < _num_partitions; i++) {
396
14.4k
        _enqueue_data_and_set_ready(
397
14.4k
                i, sink_info.local_state,
398
14.4k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
14.4k
    }
400
401
1.88k
    return Status::OK();
402
2.89k
}
403
404
7.43k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
7.43k
    BroadcastBlock partitioned_block;
406
7.43k
    bool eos;
407
7.43k
    Block block;
408
7.43k
    _data_queue[source_info.channel_id].set_eos();
409
7.43k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
7.43k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
7.43k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
25.3k
                                     Profile&& profile, SourceInfo&& source_info) {
417
25.3k
    BroadcastBlock partitioned_block;
418
419
25.3k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
25.3k
                      source_info.channel_id)) {
421
14.4k
        SCOPED_TIMER(profile.copy_data_timer);
422
14.4k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
14.4k
                block, partitioned_block.first->_data_block);
424
14.4k
        auto block_wrapper = partitioned_block.first;
425
14.4k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
14.4k
                                               partitioned_block.second.offset_start,
427
14.4k
                                               partitioned_block.second.length));
428
14.4k
    }
429
430
25.3k
    return Status::OK();
431
25.3k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
958
                                                       SinkInfo& sink_info) {
435
958
    Block new_block;
436
958
    if (!_free_blocks.try_dequeue(new_block)) {
437
379
        new_block = {in_block->clone_empty()};
438
379
    }
439
958
    new_block.swap(*in_block);
440
958
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
958
    _enqueue_data_and_set_ready(
442
958
            channel_id, sink_info.local_state,
443
958
            BlockWrapper::create_shared(
444
958
                    std::move(new_block),
445
958
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
446
958
                    channel_id));
447
448
958
    sink_info.local_state->_memory_used_counter->set(
449
958
            sink_info.local_state->_shared_state->mem_usage);
450
958
    return Status::OK();
451
958
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
1.95k
                                                   SinkInfo& sink_info) {
455
1.95k
    std::vector<uint32_t> channel_ids;
456
1.95k
    const auto num_rows = block->rows();
457
1.95k
    channel_ids.resize(num_rows, 0);
458
1.95k
    if (num_rows <= _num_partitions) {
459
1.51k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.51k
    } else {
461
445
        size_t i = 0;
462
11.2k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
10.7k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
10.7k
        }
465
445
        if (i < num_rows - 1) {
466
351
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
351
        }
468
445
    }
469
470
1.95k
    sink_info.local_state->_memory_used_counter->set(
471
1.95k
            sink_info.local_state->_shared_state->mem_usage);
472
1.95k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
1.95k
    return Status::OK();
474
1.95k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
1.95k
                                                 Block* block, SinkInfo& sink_info) {
479
1.95k
    const auto rows = cast_set<int32_t>(block->rows());
480
1.95k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
481
1.95k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
482
1.95k
    {
483
1.95k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
55.7k
        for (int32_t i = 0; i < rows; ++i) {
485
53.7k
            partition_rows_histogram[channel_ids[i]]++;
486
53.7k
        }
487
12.2k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
10.2k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
10.2k
        }
490
491
55.7k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
53.7k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
53.7k
            partition_rows_histogram[channel_ids[i]]--;
494
53.7k
        }
495
1.95k
    }
496
12.2k
    for (int32_t i = 0; i < _num_partitions; i++) {
497
10.2k
        const size_t start = partition_rows_histogram[i];
498
10.2k
        const size_t size = partition_rows_histogram[i + 1] - start;
499
10.2k
        if (size > 0) {
500
5.09k
            std::unique_ptr<MutableBlock> mutable_block =
501
5.09k
                    MutableBlock::create_unique(block->clone_empty());
502
5.09k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
503
5.09k
            auto new_block = mutable_block->to_block();
504
505
5.09k
            _enqueue_data_and_set_ready(
506
5.09k
                    i, sink_info.local_state,
507
5.09k
                    BlockWrapper::create_shared(
508
5.09k
                            std::move(new_block),
509
5.09k
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
510
5.09k
                            i));
511
5.09k
        }
512
10.2k
    }
513
1.95k
    return Status::OK();
514
1.95k
}
515
516
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
517
7.03k
                                          Profile&& profile, SinkInfo& sink_info) {
518
7.03k
    if (in_block->empty()) {
519
4.11k
        return Status::OK();
520
4.11k
    }
521
2.92k
    if (_is_pass_through) {
522
958
        return _passthrough_sink(state, in_block, sink_info);
523
1.96k
    } else {
524
1.96k
        if (++_total_block >= _num_partitions) {
525
439
            _is_pass_through = true;
526
439
        }
527
1.96k
        return _shuffle_sink(state, in_block, sink_info);
528
1.96k
    }
529
2.92k
}
530
531
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
532
11.7k
                                               Profile&& profile, SourceInfo&& source_info) {
533
11.7k
    BlockWrapperSPtr next_block;
534
11.7k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
535
11.7k
    return Status::OK();
536
11.7k
}
537
538
4.94k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
539
4.94k
    Block next_block;
540
4.94k
    bool eos;
541
4.94k
    BlockWrapperSPtr wrapper;
542
4.94k
    _data_queue[source_info.channel_id].set_eos();
543
4.95k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
544
4.95k
                         source_info.channel_id)) {
545
        // do nothing
546
5
    }
547
4.94k
}
548
549
} // namespace doris