Coverage Report

Created: 2026-03-19 03:31

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