Coverage Report

Created: 2026-05-18 05:00

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
283k
                                                       BlockType&& block) {
31
283k
    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
283k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
82.1k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
82.1k
        block.first->record_channel_id(channel_id);
43
201k
    } else {
44
201k
        block->record_channel_id(channel_id);
45
201k
    }
46
47
283k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
283k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
283k
    }
50
283k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
60.5k
                                                       BlockType&& block) {
31
60.5k
    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
60.5k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
60.5k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
60.5k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
60.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
60.4k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
60.4k
    }
50
60.5k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
201k
                                                       BlockType&& block) {
31
201k
    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
201k
    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
201k
    } else {
44
201k
        block->record_channel_id(channel_id);
45
201k
    }
46
47
201k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
201k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
201k
    }
50
201k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
21.5k
                                                       BlockType&& block) {
31
21.5k
    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
21.5k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
21.5k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
21.5k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
21.5k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
21.5k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
21.5k
    }
50
21.5k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.85M
                                         int channel_id) {
56
1.85M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.85M
    bool all_finished = _running_sink_operators == 0;
60
1.85M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
81.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
81.9k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
201k
        } else {
65
201k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
201k
            data_block->swap(block->_data_block);
67
201k
        }
68
283k
        return true;
69
1.56M
    } else if (all_finished) {
70
1.40M
        *eos = true;
71
1.40M
    } else {
72
164k
        std::unique_lock l(*_m[channel_id]);
73
164k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
9
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
9
                local_state->_shared_state->sub_mem_usage(channel_id,
77
9
                                                          block.first->_allocated_bytes);
78
9
            } else {
79
5
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
5
                data_block->swap(block->_data_block);
81
5
            }
82
14
            return true;
83
14
        }
84
164k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
164k
        local_state->_dependency->block();
86
164k
    }
87
1.56M
    return false;
88
1.85M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
218k
                                         int channel_id) {
56
218k
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
218k
    bool all_finished = _running_sink_operators == 0;
60
218k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
60.3k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
60.3k
            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
60.3k
        return true;
69
158k
    } else if (all_finished) {
70
130k
        *eos = true;
71
130k
    } else {
72
28.0k
        std::unique_lock l(*_m[channel_id]);
73
28.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
28.0k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
28.0k
        local_state->_dependency->block();
86
28.0k
    }
87
158k
    return false;
88
218k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.59M
                                         int channel_id) {
56
1.59M
    if (local_state == nullptr) {
57
8
        return _dequeue_data(block, eos, data_block, channel_id);
58
8
    }
59
1.59M
    bool all_finished = _running_sink_operators == 0;
60
1.59M
    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
201k
        } else {
65
201k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
201k
            data_block->swap(block->_data_block);
67
201k
        }
68
201k
        return true;
69
1.38M
    } else if (all_finished) {
70
1.25M
        *eos = true;
71
1.25M
    } 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
5
            } else {
79
5
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
5
                data_block->swap(block->_data_block);
81
5
            }
82
5
            return true;
83
5
        }
84
130k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
130k
        local_state->_dependency->block();
86
130k
    }
87
1.38M
    return false;
88
1.59M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
44.0k
                                         int channel_id) {
56
44.0k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
44.0k
    bool all_finished = _running_sink_operators == 0;
60
44.0k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
21.5k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
21.5k
            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
21.5k
        return true;
69
22.5k
    } else if (all_finished) {
70
16.9k
        *eos = true;
71
16.9k
    } else {
72
5.62k
        std::unique_lock l(*_m[channel_id]);
73
5.62k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
3
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
3
                local_state->_shared_state->sub_mem_usage(channel_id,
77
3
                                                          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
3
            return true;
83
3
        }
84
5.62k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
5.62k
        local_state->_dependency->block();
86
5.62k
    }
87
22.5k
    return false;
88
44.0k
}
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
8
                                         int channel_id) {
104
8
    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
8
    return false;
112
8
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
8
                                         int channel_id) {
104
8
    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
8
    return false;
112
8
}
_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
79.0k
                              SinkInfo& sink_info) {
116
79.0k
    if (in_block->empty()) {
117
60.2k
        return Status::OK();
118
60.2k
    }
119
18.8k
    {
120
18.8k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
18.8k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
18.8k
    }
123
18.8k
    {
124
18.8k
        SCOPED_TIMER(profile.distribute_timer);
125
18.8k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
18.8k
                                    *sink_info.channel_id, sink_info.local_state,
127
18.8k
                                    sink_info.shuffle_idx_to_instance_idx));
128
18.8k
    }
129
130
18.8k
    sink_info.local_state->_memory_used_counter->set(
131
18.8k
            sink_info.local_state->_shared_state->mem_usage);
132
18.8k
    return Status::OK();
133
18.8k
}
134
135
60.8k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
60.8k
    PartitionedBlock partitioned_block;
137
60.8k
    bool eos;
138
60.8k
    Block block;
139
60.8k
    _data_queue[source_info.channel_id].set_eos();
140
62.1k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
62.1k
                         source_info.channel_id)) {
142
        // do nothing
143
1.30k
    }
144
60.8k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
86.9k
                                   SourceInfo&& source_info) {
148
86.9k
    PartitionedBlock partitioned_block;
149
86.9k
    MutableBlock mutable_block;
150
151
86.9k
    auto get_data = [&]() -> Status {
152
53.0k
        do {
153
53.0k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
53.0k
                                       partitioned_block.second.offset_start;
155
53.0k
            auto block_wrapper = partitioned_block.first;
156
53.0k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
53.0k
                                                   offset_start + partitioned_block.second.length));
158
53.0k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
53.0k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
51.7k
                               source_info.channel_id));
161
33.2k
        return Status::OK();
162
33.2k
    };
163
164
86.9k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
86.9k
                      source_info.channel_id)) {
166
33.2k
        SCOPED_TIMER(profile.copy_data_timer);
167
33.2k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
33.2k
                block, partitioned_block.first->_data_block);
169
33.2k
        RETURN_IF_ERROR(get_data());
170
33.2k
    }
171
86.9k
    return Status::OK();
172
86.9k
}
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
18.7k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
18.7k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
18.7k
    const auto rows = cast_set<int32_t>(block->rows());
182
18.7k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
18.7k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
18.7k
    {
185
18.7k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
16.7M
        for (int32_t i = 0; i < rows; ++i) {
187
16.7M
            partition_rows_histogram[channel_ids[i]]++;
188
16.7M
        }
189
240k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
221k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
221k
        }
192
15.5M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
15.5M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
15.5M
            partition_rows_histogram[channel_ids[i]]--;
195
15.5M
        }
196
18.7k
    }
197
198
18.7k
    Block data_block;
199
18.7k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
18.7k
    if (!_free_blocks.try_dequeue(data_block)) {
201
9.26k
        data_block = block->clone_empty();
202
9.26k
    }
203
18.7k
    data_block.swap(*block);
204
18.7k
    new_block_wrapper =
205
18.7k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
18.7k
    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
18.7k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
18.7k
    const auto& map = *shuffle_idx_to_instance_idx;
217
18.7k
    int32_t enqueue_rows = 0;
218
221k
    for (const auto& it : map) {
219
18.4E
        DCHECK(it.second >= 0 && it.second < _num_partitions)
220
18.4E
                << it.first << " : " << it.second << " " << _num_partitions;
221
221k
        uint32_t start = partition_rows_histogram[it.first];
222
221k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
221k
        if (size > 0) {
224
54.5k
            enqueue_rows += size;
225
54.5k
            _enqueue_data_and_set_ready(
226
54.5k
                    it.second, local_state,
227
54.5k
                    {new_block_wrapper,
228
54.5k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
54.5k
        }
230
221k
    }
231
18.7k
    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
18.7k
    return Status::OK();
245
18.7k
}
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
316k
                                  SinkInfo& sink_info) {
291
316k
    if (in_block->empty()) {
292
117k
        return Status::OK();
293
117k
    }
294
198k
    Block new_block;
295
198k
    if (!_free_blocks.try_dequeue(new_block)) {
296
79.5k
        new_block = {in_block->clone_empty()};
297
79.5k
    }
298
198k
    new_block.swap(*in_block);
299
198k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
198k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
198k
            std::move(new_block),
302
198k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
198k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
198k
    sink_info.local_state->_memory_used_counter->set(
307
198k
            sink_info.local_state->_shared_state->mem_usage);
308
309
198k
    return Status::OK();
310
316k
}
311
312
622k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
622k
    Block next_block;
314
622k
    BlockWrapperSPtr wrapper;
315
622k
    bool eos;
316
622k
    _data_queue[source_info.channel_id].set_eos();
317
623k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
623k
                         source_info.channel_id)) {
319
        // do nothing
320
1.56k
    }
321
622k
}
322
323
12.0k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
12.0k
    Block next_block;
325
12.0k
    BlockWrapperSPtr wrapper;
326
12.0k
    bool eos;
327
12.0k
    _data_queue[source_info.channel_id].set_eos();
328
12.0k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
12.0k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
12.0k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
947k
                                       Profile&& profile, SourceInfo&& source_info) {
336
947k
    BlockWrapperSPtr next_block;
337
947k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
947k
    return Status::OK();
339
947k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
4.73k
                                SinkInfo& sink_info) {
343
4.73k
    if (in_block->empty()) {
344
1.66k
        return Status::OK();
345
1.66k
    }
346
3.07k
    Block new_block;
347
3.07k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.88k
        new_block = {in_block->clone_empty()};
349
1.88k
    }
350
3.07k
    new_block.swap(*in_block);
351
352
3.07k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
3.07k
            std::move(new_block),
354
18.4E
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
3.07k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
3.07k
    sink_info.local_state->_memory_used_counter->set(
358
3.07k
            sink_info.local_state->_shared_state->mem_usage);
359
360
3.07k
    return Status::OK();
361
4.73k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
5.67k
                                     Profile&& profile, SourceInfo&& source_info) {
365
5.67k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
5.67k
    BlockWrapperSPtr next_block;
370
5.67k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
5.67k
    return Status::OK();
372
5.67k
}
373
374
102k
void ExchangerBase::finalize() {
375
102k
    DCHECK(_running_source_operators == 0);
376
102k
    Block block;
377
193k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
91.6k
    }
380
102k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
3.84k
                                SinkInfo& sink_info) {
384
3.84k
    if (in_block->empty()) {
385
1.26k
        return Status::OK();
386
1.26k
    }
387
2.58k
    Block new_block;
388
2.58k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.84k
        new_block = {in_block->clone_empty()};
390
1.84k
    }
391
2.58k
    new_block.swap(*in_block);
392
2.58k
    auto wrapper = BlockWrapper::create_shared(
393
2.58k
            std::move(new_block),
394
2.58k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
24.1k
    for (int i = 0; i < _num_partitions; i++) {
396
21.5k
        _enqueue_data_and_set_ready(
397
21.5k
                i, sink_info.local_state,
398
21.5k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
21.5k
    }
400
401
2.58k
    return Status::OK();
402
3.84k
}
403
404
8.44k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
8.44k
    BroadcastBlock partitioned_block;
406
8.44k
    bool eos;
407
8.44k
    Block block;
408
8.44k
    _data_queue[source_info.channel_id].set_eos();
409
8.44k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
8.44k
                         source_info.channel_id)) {
411
        // do nothing
412
0
    }
413
8.44k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
35.6k
                                     Profile&& profile, SourceInfo&& source_info) {
417
35.6k
    BroadcastBlock partitioned_block;
418
419
35.6k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
35.6k
                      source_info.channel_id)) {
421
21.5k
        SCOPED_TIMER(profile.copy_data_timer);
422
21.5k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
21.5k
                block, partitioned_block.first->_data_block);
424
21.5k
        auto block_wrapper = partitioned_block.first;
425
21.5k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
21.5k
                                               partitioned_block.second.offset_start,
427
21.5k
                                               partitioned_block.second.length));
428
21.5k
    }
429
430
35.6k
    return Status::OK();
431
35.6k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
858
                                                       SinkInfo& sink_info) {
435
858
    Block new_block;
436
858
    if (!_free_blocks.try_dequeue(new_block)) {
437
313
        new_block = {in_block->clone_empty()};
438
313
    }
439
858
    new_block.swap(*in_block);
440
858
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
858
    _enqueue_data_and_set_ready(
442
858
            channel_id, sink_info.local_state,
443
858
            {BlockWrapper::create_shared(
444
858
                     std::move(new_block),
445
858
                     sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1),
446
858
             {.row_idxs = nullptr, .offset_start = 0, .length = 0}});
447
448
858
    sink_info.local_state->_memory_used_counter->set(
449
858
            sink_info.local_state->_shared_state->mem_usage);
450
858
    return Status::OK();
451
858
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
2.03k
                                                   SinkInfo& sink_info) {
455
2.03k
    std::vector<uint32_t> channel_ids;
456
2.03k
    const auto num_rows = block->rows();
457
2.03k
    channel_ids.resize(num_rows, 0);
458
2.03k
    if (num_rows <= _num_partitions) {
459
1.48k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.48k
    } else {
461
552
        size_t i = 0;
462
11.5k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
11.0k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
11.0k
        }
465
552
        if (i < num_rows - 1) {
466
273
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
273
        }
468
552
    }
469
470
2.03k
    sink_info.local_state->_memory_used_counter->set(
471
2.03k
            sink_info.local_state->_shared_state->mem_usage);
472
2.03k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
2.03k
    return Status::OK();
474
2.03k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
2.03k
                                                 Block* block, SinkInfo& sink_info) {
479
2.03k
    const auto rows = cast_set<int32_t>(block->rows());
480
2.03k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
481
2.03k
    auto& partition_rows_histogram = _partition_rows_histogram[sink_info.ins_idx];
482
2.03k
    {
483
2.03k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
48.2k
        for (int32_t i = 0; i < rows; ++i) {
485
46.1k
            partition_rows_histogram[channel_ids[i]]++;
486
46.1k
        }
487
12.8k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
10.8k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
10.8k
        }
490
491
47.6k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
45.6k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
45.6k
            partition_rows_histogram[channel_ids[i]]--;
494
45.6k
        }
495
2.03k
    }
496
2.03k
    Block data_block;
497
2.03k
    if (!_free_blocks.try_dequeue(data_block)) {
498
1.80k
        data_block = block->clone_empty();
499
1.80k
    }
500
2.03k
    data_block.swap(*block);
501
2.03k
    std::shared_ptr<BlockWrapper> new_block_wrapper = BlockWrapper::create_shared(
502
2.03k
            std::move(data_block), sink_info.local_state->_shared_state, sink_info.ins_idx);
503
2.03k
    if (new_block_wrapper->_data_block.empty()) {
504
0
        return Status::OK();
505
0
    }
506
12.8k
    for (int32_t i = 0; i < _num_partitions; i++) {
507
10.8k
        const uint32_t start = partition_rows_histogram[i];
508
10.8k
        const uint32_t size = partition_rows_histogram[i + 1] - start;
509
10.8k
        if (size > 0) {
510
5.13k
            _enqueue_data_and_set_ready(
511
5.13k
                    i, sink_info.local_state,
512
5.13k
                    {new_block_wrapper,
513
5.13k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
514
5.13k
        }
515
10.8k
    }
516
2.03k
    return Status::OK();
517
2.03k
}
518
519
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
520
6.92k
                                          Profile&& profile, SinkInfo& sink_info) {
521
6.92k
    if (in_block->empty()) {
522
4.03k
        return Status::OK();
523
4.03k
    }
524
2.89k
    if (_is_pass_through) {
525
858
        return _passthrough_sink(state, in_block, sink_info);
526
2.03k
    } else {
527
2.03k
        if (++_total_block >= _num_partitions) {
528
399
            _is_pass_through = true;
529
399
        }
530
2.03k
        return _shuffle_sink(state, in_block, sink_info);
531
2.03k
    }
532
2.89k
}
533
534
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
535
7.60k
                                               Profile&& profile, SourceInfo&& source_info) {
536
7.60k
    if (!_tmp_block[source_info.channel_id].empty()) {
537
198
        *block = std::move(_tmp_block[source_info.channel_id]);
538
198
        *eos = _tmp_eos[source_info.channel_id];
539
198
        _tmp_block[source_info.channel_id] = {};
540
198
        return Status::OK();
541
198
    }
542
7.40k
    PartitionedBlock partitioned_block;
543
7.40k
    MutableBlock mutable_block;
544
545
7.40k
    auto get_data = [&]() -> Status {
546
5.97k
        do {
547
5.97k
            if (partitioned_block.second.row_idxs == nullptr) {
548
                // The passthrough path which means the block is not partitioned, we can directly move the block without copying.
549
854
                if (mutable_block.rows() > 0) {
550
198
                    _tmp_block[source_info.channel_id] =
551
198
                            std::move(partitioned_block.first->_data_block);
552
198
                    _tmp_eos[source_info.channel_id] = *eos;
553
198
                    *eos = false;
554
656
                } else {
555
656
                    *block = std::move(partitioned_block.first->_data_block);
556
656
                }
557
854
                break;
558
854
            }
559
5.12k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
560
5.12k
                                       partitioned_block.second.offset_start;
561
5.12k
            auto block_wrapper = partitioned_block.first;
562
5.12k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
563
5.12k
                                                   offset_start + partitioned_block.second.length));
564
5.12k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
565
5.12k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
566
5.12k
                               source_info.channel_id));
567
3.45k
        return Status::OK();
568
3.45k
    };
569
570
7.40k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
571
7.40k
                      source_info.channel_id)) {
572
3.46k
        SCOPED_TIMER(profile.copy_data_timer);
573
3.46k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
574
3.46k
                block, partitioned_block.first->_data_block);
575
3.46k
        RETURN_IF_ERROR(get_data());
576
3.46k
    }
577
7.40k
    return Status::OK();
578
7.40k
}
579
580
5.02k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
581
5.02k
    PartitionedBlock partitioned_block;
582
5.02k
    bool eos;
583
5.02k
    Block block;
584
5.02k
    _data_queue[source_info.channel_id].set_eos();
585
5.03k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
586
5.03k
                         source_info.channel_id)) {
587
        // do nothing
588
5
    }
589
5.02k
}
590
591
} // namespace doris