Coverage Report

Created: 2026-03-13 14:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/local_exchanger.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "exec/exchange/local_exchanger.h"
19
20
#include "common/cast_set.h"
21
#include "common/status.h"
22
#include "exec/exchange/local_exchange_sink_operator.h"
23
#include "exec/exchange/local_exchange_source_operator.h"
24
#include "exec/partitioner/partitioner.h"
25
26
namespace doris {
27
#include "common/compile_check_begin.h"
28
template <typename BlockType>
29
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id,
30
                                                       LocalExchangeSinkLocalState* local_state,
31
252k
                                                       BlockType&& block) {
32
252k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
252k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
54.3k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
54.3k
        block.first->record_channel_id(channel_id);
44
198k
    } else {
45
198k
        block->record_channel_id(channel_id);
46
198k
    }
47
48
252k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
252k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
252k
    }
51
252k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
39.4k
                                                       BlockType&& block) {
32
39.4k
    if (local_state == nullptr) {
33
0
        _enqueue_data_and_set_ready(channel_id, std::move(block));
34
0
        return;
35
0
    }
36
    // PartitionedBlock is used by shuffle exchanger.
37
    // PartitionedBlock will be push into multiple queues with different row ranges, so it will be
38
    // referenced multiple times. Otherwise, we only ref the block once because it is only push into
39
    // one queue.
40
39.4k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
39.4k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
39.4k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
39.4k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
39.3k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
39.3k
    }
51
39.4k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
31
198k
                                                       BlockType&& block) {
32
198k
    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
198k
    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
198k
    } else {
45
198k
        block->record_channel_id(channel_id);
46
198k
    }
47
48
198k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
198k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
198k
    }
51
198k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
31
14.9k
                                                       BlockType&& block) {
32
14.9k
    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
14.9k
    std::unique_lock l(*_m[channel_id]);
41
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
42
14.9k
                  std::is_same_v<BroadcastBlock, BlockType>) {
43
14.9k
        block.first->record_channel_id(channel_id);
44
    } else {
45
        block->record_channel_id(channel_id);
46
    }
47
48
14.9k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
49
14.9k
        local_state->_shared_state->set_ready_to_read(channel_id);
50
14.9k
    }
51
14.9k
}
52
53
template <typename BlockType>
54
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
55
                                         BlockType& block, bool* eos, Block* data_block,
56
1.95M
                                         int channel_id) {
57
1.95M
    if (local_state == nullptr) {
58
20
        return _dequeue_data(block, eos, data_block, channel_id);
59
20
    }
60
1.95M
    bool all_finished = _running_sink_operators == 0;
61
1.95M
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
54.2k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
54.2k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
65
198k
        } else {
66
198k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
198k
            data_block->swap(block->_data_block);
68
198k
        }
69
252k
        return true;
70
1.70M
    } else if (all_finished) {
71
1.57M
        *eos = true;
72
1.57M
    } else {
73
128k
        std::unique_lock l(*_m[channel_id]);
74
128k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
10
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
10
                local_state->_shared_state->sub_mem_usage(channel_id,
78
10
                                                          block.first->_allocated_bytes);
79
15
            } else {
80
15
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
15
                data_block->swap(block->_data_block);
82
15
            }
83
25
            return true;
84
25
        }
85
128k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
128k
        local_state->_dependency->block();
87
128k
    }
88
1.70M
    return false;
89
1.95M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
347k
                                         int channel_id) {
57
347k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
347k
    bool all_finished = _running_sink_operators == 0;
61
347k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
39.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
39.3k
            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
39.3k
        return true;
70
307k
    } else if (all_finished) {
71
286k
        *eos = true;
72
286k
    } else {
73
21.1k
        std::unique_lock l(*_m[channel_id]);
74
21.1k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
7
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
7
                local_state->_shared_state->sub_mem_usage(channel_id,
78
7
                                                          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
7
            return true;
84
7
        }
85
21.1k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
21.1k
        local_state->_dependency->block();
87
21.1k
    }
88
307k
    return false;
89
347k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
56
1.57M
                                         int channel_id) {
57
1.57M
    if (local_state == nullptr) {
58
12
        return _dequeue_data(block, eos, data_block, channel_id);
59
12
    }
60
1.57M
    bool all_finished = _running_sink_operators == 0;
61
1.57M
    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
198k
        } else {
66
198k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
67
198k
            data_block->swap(block->_data_block);
68
198k
        }
69
198k
        return true;
70
1.37M
    } else if (all_finished) {
71
1.27M
        *eos = true;
72
1.27M
    } else {
73
102k
        std::unique_lock l(*_m[channel_id]);
74
102k
        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
15
            } else {
80
15
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
81
15
                data_block->swap(block->_data_block);
82
15
            }
83
15
            return true;
84
15
        }
85
102k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
102k
        local_state->_dependency->block();
87
102k
    }
88
1.37M
    return false;
89
1.57M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
56
35.2k
                                         int channel_id) {
57
35.2k
    if (local_state == nullptr) {
58
4
        return _dequeue_data(block, eos, data_block, channel_id);
59
4
    }
60
35.2k
    bool all_finished = _running_sink_operators == 0;
61
35.2k
    if (_data_queue[channel_id].try_dequeue(block)) {
62
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
63
14.8k
                      std::is_same_v<BroadcastBlock, BlockType>) {
64
14.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
14.8k
        return true;
70
20.3k
    } else if (all_finished) {
71
16.3k
        *eos = true;
72
16.3k
    } else {
73
3.97k
        std::unique_lock l(*_m[channel_id]);
74
3.97k
        if (_data_queue[channel_id].try_dequeue(block)) {
75
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
76
3
                          std::is_same_v<BroadcastBlock, BlockType>) {
77
3
                local_state->_shared_state->sub_mem_usage(channel_id,
78
3
                                                          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
3
            return true;
84
3
        }
85
3.97k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
86
3.97k
        local_state->_dependency->block();
87
3.97k
    }
88
20.3k
    return false;
89
35.2k
}
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
165k
                              SinkInfo& sink_info) {
117
165k
    if (in_block->empty()) {
118
143k
        return Status::OK();
119
143k
    }
120
21.3k
    {
121
21.3k
        SCOPED_TIMER(profile.compute_hash_value_timer);
122
21.3k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
123
21.3k
    }
124
21.3k
    {
125
21.3k
        SCOPED_TIMER(profile.distribute_timer);
126
21.3k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
127
21.3k
                                    *sink_info.channel_id, sink_info.local_state,
128
21.3k
                                    sink_info.shuffle_idx_to_instance_idx));
129
21.3k
    }
130
131
21.3k
    sink_info.local_state->_memory_used_counter->set(
132
21.3k
            sink_info.local_state->_shared_state->mem_usage);
133
21.3k
    return Status::OK();
134
21.3k
}
135
136
145k
void ShuffleExchanger::close(SourceInfo&& source_info) {
137
145k
    PartitionedBlock partitioned_block;
138
145k
    bool eos;
139
145k
    Block block;
140
145k
    _data_queue[source_info.channel_id].set_eos();
141
145k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
142
145k
                         source_info.channel_id)) {
143
        // do nothing
144
146
    }
145
145k
}
146
147
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
148
164k
                                   SourceInfo&& source_info) {
149
164k
    PartitionedBlock partitioned_block;
150
164k
    MutableBlock mutable_block;
151
152
164k
    auto get_data = [&]() -> Status {
153
39.1k
        do {
154
39.1k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
155
39.1k
                                       partitioned_block.second.offset_start;
156
39.1k
            auto block_wrapper = partitioned_block.first;
157
39.1k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
158
39.1k
                                                   offset_start + partitioned_block.second.length));
159
39.1k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
160
39.1k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
161
37.8k
                               source_info.channel_id));
162
28.7k
        return Status::OK();
163
28.7k
    };
164
165
164k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
166
164k
                      source_info.channel_id)) {
167
28.8k
        SCOPED_TIMER(profile.copy_data_timer);
168
28.8k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
169
28.8k
                block, partitioned_block.first->_data_block);
170
28.8k
        RETURN_IF_ERROR(get_data());
171
28.8k
    }
172
164k
    return Status::OK();
173
164k
}
174
175
Status ShuffleExchanger::_split_rows(RuntimeState* state, const std::vector<uint32_t>& channel_ids,
176
                                     Block* block, int channel_id,
177
                                     LocalExchangeSinkLocalState* local_state,
178
21.2k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
179
21.2k
    if (local_state == nullptr) {
180
0
        return _split_rows(state, channel_ids, block, channel_id);
181
0
    }
182
21.2k
    const auto rows = cast_set<int32_t>(block->rows());
183
21.2k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
184
21.2k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
185
21.2k
    {
186
21.2k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
187
18.1M
        for (int32_t i = 0; i < rows; ++i) {
188
18.1M
            partition_rows_histogram[channel_ids[i]]++;
189
18.1M
        }
190
213k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
191
192k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
192
192k
        }
193
16.3M
        for (int32_t i = rows - 1; i >= 0; --i) {
194
16.3M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
195
16.3M
            partition_rows_histogram[channel_ids[i]]--;
196
16.3M
        }
197
21.2k
    }
198
199
21.2k
    Block data_block;
200
21.2k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
201
21.2k
    if (!_free_blocks.try_dequeue(data_block)) {
202
13.5k
        data_block = block->clone_empty();
203
13.5k
    }
204
21.2k
    data_block.swap(*block);
205
21.2k
    new_block_wrapper =
206
21.2k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
207
21.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
21.2k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
217
21.2k
    const auto& map = *shuffle_idx_to_instance_idx;
218
21.2k
    int32_t enqueue_rows = 0;
219
191k
    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
191k
        uint32_t start = partition_rows_histogram[it.first];
223
191k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
224
191k
        if (size > 0) {
225
39.4k
            enqueue_rows += size;
226
39.4k
            _enqueue_data_and_set_ready(
227
39.4k
                    it.second, local_state,
228
39.4k
                    {new_block_wrapper,
229
39.4k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
230
39.4k
        }
231
191k
    }
232
21.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
21.2k
    return Status::OK();
246
21.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
310k
                                  SinkInfo& sink_info) {
292
310k
    if (in_block->empty()) {
293
121k
        return Status::OK();
294
121k
    }
295
189k
    Block new_block;
296
189k
    if (!_free_blocks.try_dequeue(new_block)) {
297
89.5k
        new_block = {in_block->clone_empty()};
298
89.5k
    }
299
189k
    new_block.swap(*in_block);
300
189k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
301
189k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
302
189k
            std::move(new_block),
303
189k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
304
305
189k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
306
307
189k
    sink_info.local_state->_memory_used_counter->set(
308
189k
            sink_info.local_state->_shared_state->mem_usage);
309
310
189k
    return Status::OK();
311
310k
}
312
313
623k
void PassthroughExchanger::close(SourceInfo&& source_info) {
314
623k
    Block next_block;
315
623k
    BlockWrapperSPtr wrapper;
316
623k
    bool eos;
317
623k
    _data_queue[source_info.channel_id].set_eos();
318
625k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
319
625k
                         source_info.channel_id)) {
320
        // do nothing
321
2.09k
    }
322
623k
}
323
324
11.1k
void PassToOneExchanger::close(SourceInfo&& source_info) {
325
11.1k
    Block next_block;
326
11.1k
    BlockWrapperSPtr wrapper;
327
11.1k
    bool eos;
328
11.1k
    _data_queue[source_info.channel_id].set_eos();
329
11.1k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
330
11.1k
                         source_info.channel_id)) {
331
        // do nothing
332
0
    }
333
11.1k
}
334
335
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
336
910k
                                       Profile&& profile, SourceInfo&& source_info) {
337
910k
    BlockWrapperSPtr next_block;
338
910k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
339
910k
    return Status::OK();
340
910k
}
341
342
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
343
4.36k
                                SinkInfo& sink_info) {
344
4.36k
    if (in_block->empty()) {
345
1.57k
        return Status::OK();
346
1.57k
    }
347
2.78k
    Block new_block;
348
2.78k
    if (!_free_blocks.try_dequeue(new_block)) {
349
1.73k
        new_block = {in_block->clone_empty()};
350
1.73k
    }
351
2.78k
    new_block.swap(*in_block);
352
353
2.78k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
354
2.78k
            std::move(new_block),
355
2.78k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
356
2.78k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
357
358
2.78k
    sink_info.local_state->_memory_used_counter->set(
359
2.78k
            sink_info.local_state->_shared_state->mem_usage);
360
361
2.78k
    return Status::OK();
362
4.36k
}
363
364
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
365
5.03k
                                     Profile&& profile, SourceInfo&& source_info) {
366
5.03k
    if (source_info.channel_id != 0) {
367
3
        *eos = true;
368
3
        return Status::OK();
369
3
    }
370
5.02k
    BlockWrapperSPtr next_block;
371
5.02k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
372
5.02k
    return Status::OK();
373
5.03k
}
374
375
124k
void ExchangerBase::finalize() {
376
124k
    DCHECK(_running_source_operators == 0);
377
124k
    Block block;
378
231k
    while (_free_blocks.try_dequeue(block)) {
379
        // do nothing
380
107k
    }
381
124k
}
382
383
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
384
2.98k
                                SinkInfo& sink_info) {
385
2.98k
    if (in_block->empty()) {
386
1.08k
        return Status::OK();
387
1.08k
    }
388
1.89k
    Block new_block;
389
1.89k
    if (!_free_blocks.try_dequeue(new_block)) {
390
1.52k
        new_block = {in_block->clone_empty()};
391
1.52k
    }
392
1.89k
    new_block.swap(*in_block);
393
1.89k
    auto wrapper = BlockWrapper::create_shared(
394
1.89k
            std::move(new_block),
395
1.89k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
396
16.8k
    for (int i = 0; i < _num_partitions; i++) {
397
14.9k
        _enqueue_data_and_set_ready(
398
14.9k
                i, sink_info.local_state,
399
14.9k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
400
14.9k
    }
401
402
1.89k
    return Status::OK();
403
2.98k
}
404
405
8.20k
void BroadcastExchanger::close(SourceInfo&& source_info) {
406
8.20k
    BroadcastBlock partitioned_block;
407
8.20k
    bool eos;
408
8.20k
    Block block;
409
8.20k
    _data_queue[source_info.channel_id].set_eos();
410
8.20k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
411
8.20k
                         source_info.channel_id)) {
412
        // do nothing
413
0
    }
414
8.20k
}
415
416
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
417
27.0k
                                     Profile&& profile, SourceInfo&& source_info) {
418
27.0k
    BroadcastBlock partitioned_block;
419
420
27.0k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
421
27.0k
                      source_info.channel_id)) {
422
14.9k
        SCOPED_TIMER(profile.copy_data_timer);
423
14.9k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
424
14.9k
                block, partitioned_block.first->_data_block);
425
14.9k
        auto block_wrapper = partitioned_block.first;
426
14.9k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
427
14.9k
                                               partitioned_block.second.offset_start,
428
14.9k
                                               partitioned_block.second.length));
429
14.9k
    }
430
431
27.0k
    return Status::OK();
432
27.0k
}
433
434
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
435
801
                                                       SinkInfo& sink_info) {
436
801
    Block new_block;
437
801
    if (!_free_blocks.try_dequeue(new_block)) {
438
381
        new_block = {in_block->clone_empty()};
439
381
    }
440
801
    new_block.swap(*in_block);
441
801
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
442
801
    _enqueue_data_and_set_ready(
443
801
            channel_id, sink_info.local_state,
444
801
            BlockWrapper::create_shared(
445
801
                    std::move(new_block),
446
801
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
447
801
                    channel_id));
448
449
801
    sink_info.local_state->_memory_used_counter->set(
450
801
            sink_info.local_state->_shared_state->mem_usage);
451
801
    return Status::OK();
452
801
}
453
454
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
455
1.90k
                                                   SinkInfo& sink_info) {
456
1.90k
    std::vector<uint32_t> channel_ids;
457
1.90k
    const auto num_rows = block->rows();
458
1.90k
    channel_ids.resize(num_rows, 0);
459
1.90k
    if (num_rows <= _num_partitions) {
460
1.42k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
461
1.42k
    } else {
462
480
        size_t i = 0;
463
7.53k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
464
7.05k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
465
7.05k
        }
466
480
        if (i < num_rows - 1) {
467
302
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
468
302
        }
469
480
    }
470
471
1.90k
    sink_info.local_state->_memory_used_counter->set(
472
1.90k
            sink_info.local_state->_shared_state->mem_usage);
473
1.90k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
474
1.90k
    return Status::OK();
475
1.90k
}
476
477
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
478
                                                 const std::vector<uint32_t>& channel_ids,
479
1.90k
                                                 Block* block, SinkInfo& sink_info) {
480
1.90k
    const auto rows = cast_set<int32_t>(block->rows());
481
1.90k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
482
1.90k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
483
1.90k
    {
484
1.90k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
485
49.6k
        for (int32_t i = 0; i < rows; ++i) {
486
47.7k
            partition_rows_histogram[channel_ids[i]]++;
487
47.7k
        }
488
11.6k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
489
9.78k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
490
9.78k
        }
491
492
49.6k
        for (int32_t i = rows - 1; i >= 0; --i) {
493
47.7k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
494
47.7k
            partition_rows_histogram[channel_ids[i]]--;
495
47.7k
        }
496
1.90k
    }
497
11.7k
    for (int32_t i = 0; i < _num_partitions; i++) {
498
9.80k
        const size_t start = partition_rows_histogram[i];
499
9.80k
        const size_t size = partition_rows_histogram[i + 1] - start;
500
9.80k
        if (size > 0) {
501
5.52k
            std::unique_ptr<MutableBlock> mutable_block =
502
5.52k
                    MutableBlock::create_unique(block->clone_empty());
503
5.52k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
504
5.52k
            auto new_block = mutable_block->to_block();
505
506
5.52k
            _enqueue_data_and_set_ready(
507
5.52k
                    i, sink_info.local_state,
508
5.52k
                    BlockWrapper::create_shared(
509
5.52k
                            std::move(new_block),
510
18.4E
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
511
5.52k
                            i));
512
5.52k
        }
513
9.80k
    }
514
1.90k
    return Status::OK();
515
1.90k
}
516
517
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
518
6.83k
                                          Profile&& profile, SinkInfo& sink_info) {
519
6.83k
    if (in_block->empty()) {
520
4.11k
        return Status::OK();
521
4.11k
    }
522
2.72k
    if (_is_pass_through) {
523
801
        return _passthrough_sink(state, in_block, sink_info);
524
1.91k
    } else {
525
1.91k
        if (++_total_block >= _num_partitions) {
526
379
            _is_pass_through = true;
527
379
        }
528
1.91k
        return _shuffle_sink(state, in_block, sink_info);
529
1.91k
    }
530
2.72k
}
531
532
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
533
12.2k
                                               Profile&& profile, SourceInfo&& source_info) {
534
12.2k
    BlockWrapperSPtr next_block;
535
12.2k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
536
12.2k
    return Status::OK();
537
12.2k
}
538
539
5.16k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
540
5.16k
    Block next_block;
541
5.16k
    bool eos;
542
5.16k
    BlockWrapperSPtr wrapper;
543
5.16k
    _data_queue[source_info.channel_id].set_eos();
544
5.17k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
545
5.17k
                         source_info.channel_id)) {
546
        // do nothing
547
5
    }
548
5.16k
}
549
550
} // namespace doris