Coverage Report

Created: 2026-04-22 12:25

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
257k
                                                       BlockType&& block) {
31
257k
    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
257k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
52.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
52.0k
        block.first->record_channel_id(channel_id);
43
205k
    } else {
44
205k
        block->record_channel_id(channel_id);
45
205k
    }
46
47
257k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
257k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
257k
    }
50
257k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
36.0k
                                                       BlockType&& block) {
31
36.0k
    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
36.0k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
36.0k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
36.0k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
36.0k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
35.8k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
35.8k
    }
50
36.0k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS4_
Line
Count
Source
30
205k
                                                       BlockType&& block) {
31
205k
    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
205k
    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
205k
    } else {
44
205k
        block->record_channel_id(channel_id);
45
205k
    }
46
47
205k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
205k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
205k
    }
50
205k
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiPNS_27LocalExchangeSinkLocalStateEOS7_
Line
Count
Source
30
15.9k
                                                       BlockType&& block) {
31
15.9k
    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
15.9k
    std::unique_lock l(*_m[channel_id]);
40
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
41
15.9k
                  std::is_same_v<BroadcastBlock, BlockType>) {
42
15.9k
        block.first->record_channel_id(channel_id);
43
    } else {
44
        block->record_channel_id(channel_id);
45
    }
46
47
15.9k
    if (_data_queue[channel_id].enqueue(std::move(block))) {
48
15.9k
        local_state->_shared_state->set_ready_to_read(channel_id);
49
15.9k
    }
50
15.9k
}
51
52
template <typename BlockType>
53
bool Exchanger<BlockType>::_dequeue_data(LocalExchangeSourceLocalState* local_state,
54
                                         BlockType& block, bool* eos, Block* data_block,
55
1.77M
                                         int channel_id) {
56
1.77M
    if (local_state == nullptr) {
57
20
        return _dequeue_data(block, eos, data_block, channel_id);
58
20
    }
59
1.77M
    bool all_finished = _running_sink_operators == 0;
60
1.77M
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
51.7k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
51.7k
            local_state->_shared_state->sub_mem_usage(channel_id, block.first->_allocated_bytes);
64
205k
        } else {
65
205k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
205k
            data_block->swap(block->_data_block);
67
205k
        }
68
257k
        return true;
69
1.52M
    } else if (all_finished) {
70
1.36M
        *eos = true;
71
1.36M
    } else {
72
153k
        std::unique_lock l(*_m[channel_id]);
73
153k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
15
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
15
                local_state->_shared_state->sub_mem_usage(channel_id,
77
15
                                                          block.first->_allocated_bytes);
78
16
            } else {
79
16
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
16
                data_block->swap(block->_data_block);
81
16
            }
82
31
            return true;
83
31
        }
84
153k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
153k
        local_state->_dependency->block();
86
153k
    }
87
1.52M
    return false;
88
1.77M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
213k
                                         int channel_id) {
56
213k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
213k
    bool all_finished = _running_sink_operators == 0;
60
213k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
35.8k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
35.8k
            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
35.8k
        return true;
69
177k
    } else if (all_finished) {
70
156k
        *eos = true;
71
156k
    } else {
72
21.7k
        std::unique_lock l(*_m[channel_id]);
73
21.7k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
14
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
14
                local_state->_shared_state->sub_mem_usage(channel_id,
77
14
                                                          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
14
            return true;
83
14
        }
84
21.7k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
21.7k
        local_state->_dependency->block();
86
21.7k
    }
87
177k
    return false;
88
213k
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS4_PbPNS_5BlockEi
Line
Count
Source
55
1.52M
                                         int channel_id) {
56
1.52M
    if (local_state == nullptr) {
57
12
        return _dequeue_data(block, eos, data_block, channel_id);
58
12
    }
59
1.52M
    bool all_finished = _running_sink_operators == 0;
60
1.52M
    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
205k
        } else {
65
205k
            local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
66
205k
            data_block->swap(block->_data_block);
67
205k
        }
68
205k
        return true;
69
1.32M
    } else if (all_finished) {
70
1.19M
        *eos = true;
71
1.19M
    } else {
72
128k
        std::unique_lock l(*_m[channel_id]);
73
128k
        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
16
            } else {
79
16
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
16
                data_block->swap(block->_data_block);
81
16
            }
82
16
            return true;
83
16
        }
84
128k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
128k
        local_state->_dependency->block();
86
128k
    }
87
1.32M
    return false;
88
1.52M
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataEPNS_29LocalExchangeSourceLocalStateERS7_PbPNS_5BlockEi
Line
Count
Source
55
38.1k
                                         int channel_id) {
56
38.1k
    if (local_state == nullptr) {
57
4
        return _dequeue_data(block, eos, data_block, channel_id);
58
4
    }
59
38.1k
    bool all_finished = _running_sink_operators == 0;
60
38.1k
    if (_data_queue[channel_id].try_dequeue(block)) {
61
        if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
62
15.9k
                      std::is_same_v<BroadcastBlock, BlockType>) {
63
15.9k
            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
15.9k
        return true;
69
22.2k
    } else if (all_finished) {
70
19.0k
        *eos = true;
71
19.0k
    } else {
72
3.12k
        std::unique_lock l(*_m[channel_id]);
73
3.12k
        if (_data_queue[channel_id].try_dequeue(block)) {
74
            if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
75
1
                          std::is_same_v<BroadcastBlock, BlockType>) {
76
1
                local_state->_shared_state->sub_mem_usage(channel_id,
77
1
                                                          block.first->_allocated_bytes);
78
            } else {
79
                local_state->_shared_state->sub_mem_usage(channel_id, block->_allocated_bytes);
80
                data_block->swap(block->_data_block);
81
            }
82
1
            return true;
83
1
        }
84
3.12k
        COUNTER_UPDATE(local_state->_get_block_failed_counter, 1);
85
3.12k
        local_state->_dependency->block();
86
3.12k
    }
87
22.2k
    return false;
88
38.1k
}
89
90
template <typename BlockType>
91
0
void Exchanger<BlockType>::_enqueue_data_and_set_ready(int channel_id, BlockType&& block) {
92
    if constexpr (std::is_same_v<PartitionedBlock, BlockType> ||
93
0
                  std::is_same_v<BroadcastBlock, BlockType>) {
94
0
        block.first->record_channel_id(channel_id);
95
0
    } else {
96
0
        block->record_channel_id(channel_id);
97
0
    }
98
0
    _data_queue[channel_id].enqueue(std::move(block));
99
0
}
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE27_enqueue_data_and_set_readyEiOS7_
Unexecuted instantiation: _ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE27_enqueue_data_and_set_readyEiOS4_
Unexecuted instantiation: _ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE27_enqueue_data_and_set_readyEiOS7_
100
101
template <typename BlockType>
102
bool Exchanger<BlockType>::_dequeue_data(BlockType& block, bool* eos, Block* data_block,
103
20
                                         int channel_id) {
104
20
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
20
    return false;
112
20
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_18PartitionedRowIdxsEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
_ZN5doris9ExchangerISt10shared_ptrINS_13ExchangerBase12BlockWrapperEEE13_dequeue_dataERS4_PbPNS_5BlockEi
Line
Count
Source
103
12
                                         int channel_id) {
104
12
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
0
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
0
            data_block->swap(block->_data_block);
108
0
        }
109
0
        return true;
110
0
    }
111
12
    return false;
112
12
}
_ZN5doris9ExchangerISt4pairISt10shared_ptrINS_13ExchangerBase12BlockWrapperEENS_17BroadcastRowRangeEEE13_dequeue_dataERS7_PbPNS_5BlockEi
Line
Count
Source
103
4
                                         int channel_id) {
104
4
    if (_data_queue[channel_id].try_dequeue(block)) {
105
        if constexpr (!std::is_same_v<PartitionedBlock, BlockType> &&
106
                      !std::is_same_v<BroadcastBlock, BlockType>) {
107
            data_block->swap(block->_data_block);
108
        }
109
0
        return true;
110
0
    }
111
4
    return false;
112
4
}
113
114
Status ShuffleExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
115
96.3k
                              SinkInfo& sink_info) {
116
96.3k
    if (in_block->empty()) {
117
78.2k
        return Status::OK();
118
78.2k
    }
119
18.1k
    {
120
18.1k
        SCOPED_TIMER(profile.compute_hash_value_timer);
121
18.1k
        RETURN_IF_ERROR(sink_info.partitioner->do_partitioning(state, in_block));
122
18.1k
    }
123
18.1k
    {
124
18.1k
        SCOPED_TIMER(profile.distribute_timer);
125
18.1k
        RETURN_IF_ERROR(_split_rows(state, sink_info.partitioner->get_channel_ids(), in_block,
126
18.1k
                                    *sink_info.channel_id, sink_info.local_state,
127
18.1k
                                    sink_info.shuffle_idx_to_instance_idx));
128
18.1k
    }
129
130
18.1k
    sink_info.local_state->_memory_used_counter->set(
131
18.1k
            sink_info.local_state->_shared_state->mem_usage);
132
18.1k
    return Status::OK();
133
18.1k
}
134
135
78.9k
void ShuffleExchanger::close(SourceInfo&& source_info) {
136
78.9k
    PartitionedBlock partitioned_block;
137
78.9k
    bool eos;
138
78.9k
    Block block;
139
78.9k
    _data_queue[source_info.channel_id].set_eos();
140
78.9k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
141
78.9k
                         source_info.channel_id)) {
142
        // do nothing
143
3
    }
144
78.9k
}
145
146
Status ShuffleExchanger::get_block(RuntimeState* state, Block* block, bool* eos, Profile&& profile,
147
99.8k
                                   SourceInfo&& source_info) {
148
99.8k
    PartitionedBlock partitioned_block;
149
99.8k
    MutableBlock mutable_block;
150
151
99.8k
    auto get_data = [&]() -> Status {
152
35.7k
        do {
153
35.7k
            const auto* offset_start = partitioned_block.second.row_idxs->data() +
154
35.7k
                                       partitioned_block.second.offset_start;
155
35.7k
            auto block_wrapper = partitioned_block.first;
156
35.7k
            RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block, offset_start,
157
35.7k
                                                   offset_start + partitioned_block.second.length));
158
35.7k
        } while (mutable_block.rows() < state->batch_size() && !*eos &&
159
35.7k
                 _dequeue_data(source_info.local_state, partitioned_block, eos, block,
160
34.7k
                               source_info.channel_id));
161
27.7k
        return Status::OK();
162
27.7k
    };
163
164
99.8k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
165
99.8k
                      source_info.channel_id)) {
166
27.8k
        SCOPED_TIMER(profile.copy_data_timer);
167
27.8k
        mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
168
27.8k
                block, partitioned_block.first->_data_block);
169
27.8k
        RETURN_IF_ERROR(get_data());
170
27.8k
    }
171
99.8k
    return Status::OK();
172
99.8k
}
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.1k
                                     std::map<int, int>* shuffle_idx_to_instance_idx) {
178
18.1k
    if (local_state == nullptr) {
179
0
        return _split_rows(state, channel_ids, block, channel_id);
180
0
    }
181
18.1k
    const auto rows = cast_set<int32_t>(block->rows());
182
18.1k
    auto row_idx = std::make_shared<PODArray<uint32_t>>(rows);
183
18.1k
    auto& partition_rows_histogram = _partition_rows_histogram[channel_id];
184
18.1k
    {
185
18.1k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
186
15.5M
        for (int32_t i = 0; i < rows; ++i) {
187
15.5M
            partition_rows_histogram[channel_ids[i]]++;
188
15.5M
        }
189
154k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
190
136k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
191
136k
        }
192
14.4M
        for (int32_t i = rows - 1; i >= 0; --i) {
193
14.4M
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
194
14.4M
            partition_rows_histogram[channel_ids[i]]--;
195
14.4M
        }
196
18.1k
    }
197
198
18.1k
    Block data_block;
199
18.1k
    std::shared_ptr<BlockWrapper> new_block_wrapper;
200
18.1k
    if (!_free_blocks.try_dequeue(data_block)) {
201
9.46k
        data_block = block->clone_empty();
202
9.46k
    }
203
18.1k
    data_block.swap(*block);
204
18.1k
    new_block_wrapper =
205
18.1k
            BlockWrapper::create_shared(std::move(data_block), local_state->_shared_state, -1);
206
18.1k
    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.1k
    DCHECK(shuffle_idx_to_instance_idx && shuffle_idx_to_instance_idx->size() > 0);
216
18.1k
    const auto& map = *shuffle_idx_to_instance_idx;
217
18.1k
    int32_t enqueue_rows = 0;
218
136k
    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
136k
        uint32_t start = partition_rows_histogram[it.first];
222
136k
        uint32_t size = partition_rows_histogram[it.first + 1] - start;
223
136k
        if (size > 0) {
224
36.0k
            enqueue_rows += size;
225
36.0k
            _enqueue_data_and_set_ready(
226
36.0k
                    it.second, local_state,
227
36.0k
                    {new_block_wrapper,
228
36.0k
                     {.row_idxs = row_idx, .offset_start = start, .length = size}});
229
36.0k
        }
230
136k
    }
231
18.1k
    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.1k
    return Status::OK();
245
18.1k
}
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
307k
                                  SinkInfo& sink_info) {
291
307k
    if (in_block->empty()) {
292
111k
        return Status::OK();
293
111k
    }
294
195k
    Block new_block;
295
195k
    if (!_free_blocks.try_dequeue(new_block)) {
296
80.3k
        new_block = {in_block->clone_empty()};
297
80.3k
    }
298
195k
    new_block.swap(*in_block);
299
195k
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
300
195k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
301
195k
            std::move(new_block),
302
195k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, channel_id);
303
304
195k
    _enqueue_data_and_set_ready(channel_id, sink_info.local_state, std::move(wrapper));
305
306
195k
    sink_info.local_state->_memory_used_counter->set(
307
195k
            sink_info.local_state->_shared_state->mem_usage);
308
309
195k
    return Status::OK();
310
307k
}
311
312
584k
void PassthroughExchanger::close(SourceInfo&& source_info) {
313
584k
    Block next_block;
314
584k
    BlockWrapperSPtr wrapper;
315
584k
    bool eos;
316
584k
    _data_queue[source_info.channel_id].set_eos();
317
586k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
318
586k
                         source_info.channel_id)) {
319
        // do nothing
320
1.48k
    }
321
584k
}
322
323
10.1k
void PassToOneExchanger::close(SourceInfo&& source_info) {
324
10.1k
    Block next_block;
325
10.1k
    BlockWrapperSPtr wrapper;
326
10.1k
    bool eos;
327
10.1k
    _data_queue[source_info.channel_id].set_eos();
328
10.1k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
329
10.1k
                         source_info.channel_id)) {
330
        // do nothing
331
0
    }
332
10.1k
}
333
334
Status PassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
335
904k
                                       Profile&& profile, SourceInfo&& source_info) {
336
904k
    BlockWrapperSPtr next_block;
337
904k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
338
904k
    return Status::OK();
339
904k
}
340
341
Status PassToOneExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
342
4.16k
                                SinkInfo& sink_info) {
343
4.16k
    if (in_block->empty()) {
344
1.49k
        return Status::OK();
345
1.49k
    }
346
2.67k
    Block new_block;
347
2.67k
    if (!_free_blocks.try_dequeue(new_block)) {
348
1.69k
        new_block = {in_block->clone_empty()};
349
1.69k
    }
350
2.67k
    new_block.swap(*in_block);
351
352
2.67k
    BlockWrapperSPtr wrapper = BlockWrapper::create_shared(
353
2.67k
            std::move(new_block),
354
2.67k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, 0);
355
2.67k
    _enqueue_data_and_set_ready(0, sink_info.local_state, std::move(wrapper));
356
357
2.67k
    sink_info.local_state->_memory_used_counter->set(
358
2.67k
            sink_info.local_state->_shared_state->mem_usage);
359
360
2.67k
    return Status::OK();
361
4.16k
}
362
363
Status PassToOneExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
364
4.98k
                                     Profile&& profile, SourceInfo&& source_info) {
365
4.98k
    if (source_info.channel_id != 0) {
366
3
        *eos = true;
367
3
        return Status::OK();
368
3
    }
369
4.98k
    BlockWrapperSPtr next_block;
370
4.98k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
371
4.98k
    return Status::OK();
372
4.98k
}
373
374
112k
void ExchangerBase::finalize() {
375
112k
    DCHECK(_running_source_operators == 0);
376
112k
    Block block;
377
209k
    while (_free_blocks.try_dequeue(block)) {
378
        // do nothing
379
96.4k
    }
380
112k
}
381
382
Status BroadcastExchanger::sink(RuntimeState* state, Block* in_block, bool eos, Profile&& profile,
383
3.88k
                                SinkInfo& sink_info) {
384
3.88k
    if (in_block->empty()) {
385
1.49k
        return Status::OK();
386
1.49k
    }
387
2.39k
    Block new_block;
388
2.39k
    if (!_free_blocks.try_dequeue(new_block)) {
389
1.83k
        new_block = {in_block->clone_empty()};
390
1.83k
    }
391
2.39k
    new_block.swap(*in_block);
392
2.39k
    auto wrapper = BlockWrapper::create_shared(
393
2.39k
            std::move(new_block),
394
2.39k
            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr, -1);
395
18.3k
    for (int i = 0; i < _num_partitions; i++) {
396
15.9k
        _enqueue_data_and_set_ready(
397
15.9k
                i, sink_info.local_state,
398
15.9k
                {wrapper, {.offset_start = 0, .length = wrapper->_data_block.rows()}});
399
15.9k
    }
400
401
2.39k
    return Status::OK();
402
3.88k
}
403
404
9.53k
void BroadcastExchanger::close(SourceInfo&& source_info) {
405
9.53k
    BroadcastBlock partitioned_block;
406
9.53k
    bool eos;
407
9.53k
    Block block;
408
9.53k
    _data_queue[source_info.channel_id].set_eos();
409
9.54k
    while (_dequeue_data(source_info.local_state, partitioned_block, &eos, &block,
410
9.54k
                         source_info.channel_id)) {
411
        // do nothing
412
15
    }
413
9.53k
}
414
415
Status BroadcastExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
416
28.5k
                                     Profile&& profile, SourceInfo&& source_info) {
417
28.5k
    BroadcastBlock partitioned_block;
418
419
28.5k
    if (_dequeue_data(source_info.local_state, partitioned_block, eos, block,
420
28.5k
                      source_info.channel_id)) {
421
15.9k
        SCOPED_TIMER(profile.copy_data_timer);
422
15.9k
        MutableBlock mutable_block = VectorizedUtils::build_mutable_mem_reuse_block(
423
15.9k
                block, partitioned_block.first->_data_block);
424
15.9k
        auto block_wrapper = partitioned_block.first;
425
15.9k
        RETURN_IF_ERROR(mutable_block.add_rows(&block_wrapper->_data_block,
426
15.9k
                                               partitioned_block.second.offset_start,
427
15.9k
                                               partitioned_block.second.length));
428
15.9k
    }
429
430
28.5k
    return Status::OK();
431
28.5k
}
432
433
Status AdaptivePassthroughExchanger::_passthrough_sink(RuntimeState* state, Block* in_block,
434
684
                                                       SinkInfo& sink_info) {
435
684
    Block new_block;
436
684
    if (!_free_blocks.try_dequeue(new_block)) {
437
248
        new_block = {in_block->clone_empty()};
438
248
    }
439
684
    new_block.swap(*in_block);
440
684
    auto channel_id = ((*sink_info.channel_id)++) % _num_partitions;
441
684
    _enqueue_data_and_set_ready(
442
684
            channel_id, sink_info.local_state,
443
684
            BlockWrapper::create_shared(
444
684
                    std::move(new_block),
445
684
                    sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
446
684
                    channel_id));
447
448
684
    sink_info.local_state->_memory_used_counter->set(
449
684
            sink_info.local_state->_shared_state->mem_usage);
450
684
    return Status::OK();
451
684
}
452
453
Status AdaptivePassthroughExchanger::_shuffle_sink(RuntimeState* state, Block* block,
454
2.18k
                                                   SinkInfo& sink_info) {
455
2.18k
    std::vector<uint32_t> channel_ids;
456
2.18k
    const auto num_rows = block->rows();
457
2.18k
    channel_ids.resize(num_rows, 0);
458
2.18k
    if (num_rows <= _num_partitions) {
459
1.66k
        std::iota(channel_ids.begin(), channel_ids.end(), 0);
460
1.66k
    } else {
461
521
        size_t i = 0;
462
11.9k
        for (; i < num_rows - _num_partitions; i += _num_partitions) {
463
11.4k
            std::iota(channel_ids.begin() + i, channel_ids.begin() + i + _num_partitions, 0);
464
11.4k
        }
465
521
        if (i < num_rows - 1) {
466
353
            std::iota(channel_ids.begin() + i, channel_ids.end(), 0);
467
353
        }
468
521
    }
469
470
2.18k
    sink_info.local_state->_memory_used_counter->set(
471
2.18k
            sink_info.local_state->_shared_state->mem_usage);
472
2.18k
    RETURN_IF_ERROR(_split_rows(state, channel_ids, block, sink_info));
473
2.18k
    return Status::OK();
474
2.18k
}
475
476
Status AdaptivePassthroughExchanger::_split_rows(RuntimeState* state,
477
                                                 const std::vector<uint32_t>& channel_ids,
478
2.18k
                                                 Block* block, SinkInfo& sink_info) {
479
2.18k
    const auto rows = cast_set<int32_t>(block->rows());
480
2.18k
    auto row_idx = std::make_shared<std::vector<uint32_t>>(rows);
481
2.18k
    auto& partition_rows_histogram = _partition_rows_histogram[*sink_info.channel_id];
482
2.18k
    {
483
2.18k
        partition_rows_histogram.assign(_num_partitions + 1, 0);
484
52.5k
        for (int32_t i = 0; i < rows; ++i) {
485
50.4k
            partition_rows_histogram[channel_ids[i]]++;
486
50.4k
        }
487
14.9k
        for (int32_t i = 1; i <= _num_partitions; ++i) {
488
12.7k
            partition_rows_histogram[i] += partition_rows_histogram[i - 1];
489
12.7k
        }
490
491
52.5k
        for (int32_t i = rows - 1; i >= 0; --i) {
492
50.4k
            (*row_idx)[partition_rows_histogram[channel_ids[i]] - 1] = i;
493
50.4k
            partition_rows_histogram[channel_ids[i]]--;
494
50.4k
        }
495
2.18k
    }
496
14.9k
    for (int32_t i = 0; i < _num_partitions; i++) {
497
12.7k
        const size_t start = partition_rows_histogram[i];
498
12.7k
        const size_t size = partition_rows_histogram[i + 1] - start;
499
12.7k
        if (size > 0) {
500
6.48k
            std::unique_ptr<MutableBlock> mutable_block =
501
6.48k
                    MutableBlock::create_unique(block->clone_empty());
502
6.48k
            RETURN_IF_ERROR(mutable_block->add_rows(block, start, size));
503
6.48k
            auto new_block = mutable_block->to_block();
504
505
6.48k
            _enqueue_data_and_set_ready(
506
6.48k
                    i, sink_info.local_state,
507
6.48k
                    BlockWrapper::create_shared(
508
6.48k
                            std::move(new_block),
509
6.48k
                            sink_info.local_state ? sink_info.local_state->_shared_state : nullptr,
510
6.48k
                            i));
511
6.48k
        }
512
12.7k
    }
513
2.18k
    return Status::OK();
514
2.18k
}
515
516
Status AdaptivePassthroughExchanger::sink(RuntimeState* state, Block* in_block, bool eos,
517
6.99k
                                          Profile&& profile, SinkInfo& sink_info) {
518
6.99k
    if (in_block->empty()) {
519
4.11k
        return Status::OK();
520
4.11k
    }
521
2.87k
    if (_is_pass_through) {
522
684
        return _passthrough_sink(state, in_block, sink_info);
523
2.18k
    } else {
524
2.18k
        if (++_total_block >= _num_partitions) {
525
263
            _is_pass_through = true;
526
263
        }
527
2.18k
        return _shuffle_sink(state, in_block, sink_info);
528
2.18k
    }
529
2.87k
}
530
531
Status AdaptivePassthroughExchanger::get_block(RuntimeState* state, Block* block, bool* eos,
532
13.2k
                                               Profile&& profile, SourceInfo&& source_info) {
533
13.2k
    BlockWrapperSPtr next_block;
534
13.2k
    _dequeue_data(source_info.local_state, next_block, eos, block, source_info.channel_id);
535
13.2k
    return Status::OK();
536
13.2k
}
537
538
5.17k
void AdaptivePassthroughExchanger::close(SourceInfo&& source_info) {
539
5.17k
    Block next_block;
540
5.17k
    bool eos;
541
5.17k
    BlockWrapperSPtr wrapper;
542
5.17k
    _data_queue[source_info.channel_id].set_eos();
543
5.18k
    while (_dequeue_data(source_info.local_state, wrapper, &eos, &next_block,
544
5.18k
                         source_info.channel_id)) {
545
        // do nothing
546
5
    }
547
5.17k
}
548
549
} // namespace doris