Coverage Report

Created: 2026-07-29 16:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/exchange/exchange_writer.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/exchange_writer.h"
19
20
#include <glog/logging.h>
21
22
#include <algorithm>
23
#include <cstdint>
24
#include <vector>
25
26
#include "common/logging.h"
27
#include "common/status.h"
28
#include "core/assert_cast.h"
29
#include "core/block/block.h"
30
#include "exec/operator/exchange_sink_operator.h"
31
#include "exec/sink/tablet_sink_hash_partitioner.h"
32
33
namespace doris {
34
35
ExchangeWriterBase::ExchangeWriterBase(ExchangeSinkLocalState& local_state)
36
725k
        : _local_state(local_state), _partitioner(local_state.partitioner()) {}
37
38
template <typename ChannelPtrType>
39
Status ExchangeWriterBase::_handle_eof_channel(RuntimeState* state, ChannelPtrType channel,
40
88.7k
                                               Status st) const {
41
88.7k
    channel->set_receiver_eof(st);
42
    // Channel will not send RPC to the downstream when eof, so close channel by OK status.
43
88.7k
    return channel->close(state);
44
88.7k
}
45
46
// NOLINTBEGIN(readability-function-cognitive-complexity)
47
Status ExchangeWriterBase::_add_rows_impl(RuntimeState* state,
48
                                          std::vector<std::shared_ptr<Channel>>& channels,
49
264k
                                          size_t channel_count, Block* block, bool eos) {
50
264k
    Status status = Status::OK();
51
264k
    uint32_t offset = 0;
52
2.69M
    for (size_t i = 0; i < channel_count; ++i) {
53
2.42M
        uint32_t size = _channel_rows_histogram[i];
54
2.42M
        if (!channels[i]->is_receiver_eof() && size > 0) {
55
18.4E
            VLOG_DEBUG << fmt::format("partition {} of {}, block:\n{}, start: {}, size: {}", i,
56
18.4E
                                      channel_count, block->dump_data(), offset, size);
57
83.6k
            status = channels[i]->add_rows(block, _origin_row_idx.data(), offset, size, false);
58
83.6k
            HANDLE_CHANNEL_STATUS(state, channels[i], status);
59
83.6k
        }
60
2.42M
        offset += size;
61
2.42M
    }
62
264k
    if (eos) {
63
2.49M
        for (int i = 0; i < channel_count; ++i) {
64
2.24M
            if (!channels[i]->is_receiver_eof()) {
65
18.4E
                VLOG_DEBUG << fmt::format("EOS partition {} of {}, block:\n{}", i, channel_count,
66
18.4E
                                          block->dump_data());
67
2.24M
                status = channels[i]->add_rows(block, _origin_row_idx.data(), 0, 0, true);
68
2.24M
                HANDLE_CHANNEL_STATUS(state, channels[i], status);
69
2.24M
            }
70
2.24M
        }
71
246k
    }
72
264k
    return Status::OK();
73
264k
}
74
// NOLINTEND(readability-function-cognitive-complexity)
75
76
2.34k
Status ExchangeOlapWriter::write(RuntimeState* state, Block* block, bool eos) {
77
2.34k
    Block prior_block;
78
2.34k
    auto* tablet_partitioner = assert_cast<TabletSinkHashPartitioner*>(_partitioner);
79
2.34k
    RETURN_IF_ERROR(tablet_partitioner->try_cut_in_line(prior_block));
80
2.34k
    if (!prior_block.empty()) {
81
        // prior_block (batching rows) cuts in line, deal it first.
82
0
        RETURN_IF_ERROR(_write_impl(state, &prior_block));
83
0
        tablet_partitioner->finish_cut_in_line();
84
0
    }
85
86
2.34k
    RETURN_IF_ERROR(_write_impl(state, block));
87
88
    // all data wrote. consider batched rows before eos.
89
2.34k
    if (eos) {
90
        // get all batched rows
91
2.10k
        tablet_partitioner->mark_last_block();
92
2.10k
        Block final_batching_block;
93
2.10k
        RETURN_IF_ERROR(tablet_partitioner->try_cut_in_line(final_batching_block));
94
2.10k
        if (!final_batching_block.empty()) {
95
0
            RETURN_IF_ERROR(_write_impl(state, &final_batching_block, true));
96
2.10k
        } else {
97
            // No batched rows, send empty block with eos signal.
98
2.10k
            Block empty_block = block->clone_empty();
99
2.10k
            RETURN_IF_ERROR(_write_impl(state, &empty_block, true));
100
2.10k
        }
101
2.10k
    }
102
2.34k
    return Status::OK();
103
2.34k
}
104
105
4.42k
Status ExchangeOlapWriter::_write_impl(RuntimeState* state, Block* block, bool eos) {
106
4.42k
    auto rows = block->rows();
107
4.42k
    auto* tablet_partitioner = assert_cast<TabletSinkHashPartitioner*>(_partitioner);
108
4.42k
    {
109
4.42k
        SCOPED_TIMER(_local_state.split_block_hash_compute_timer());
110
4.42k
        RETURN_IF_ERROR(tablet_partitioner->do_partitioning(state, block));
111
4.42k
    }
112
4.42k
    {
113
4.42k
        SCOPED_TIMER(_local_state.distribute_rows_into_channels_timer());
114
4.42k
        const auto& channel_ids = tablet_partitioner->get_channel_ids();
115
4.42k
        const auto invalid_val = tablet_partitioner->invalid_sentinel();
116
4.42k
        DCHECK_EQ(channel_ids.size(), rows);
117
118
        // decrease not sinked rows this time
119
4.42k
        COUNTER_UPDATE(_local_state.rows_input_counter(),
120
4.42k
                       -1LL * std::ranges::count(channel_ids, invalid_val));
121
122
4.42k
        RETURN_IF_ERROR(_channel_add_rows(state, _local_state.channels,
123
4.42k
                                          _local_state.channels.size(), channel_ids, rows, block,
124
4.42k
                                          eos, invalid_val));
125
4.42k
    }
126
4.42k
    return Status::OK();
127
4.42k
}
128
129
258k
Status ExchangeTrivialWriter::write(RuntimeState* state, Block* block, bool eos) {
130
258k
    {
131
258k
        SCOPED_TIMER(_local_state.split_block_hash_compute_timer());
132
258k
        RETURN_IF_ERROR(_partitioner->do_partitioning(state, block));
133
258k
    }
134
258k
    {
135
258k
        auto rows = block->rows();
136
258k
        SCOPED_TIMER(_local_state.distribute_rows_into_channels_timer());
137
258k
        const auto& channel_ids = _partitioner->get_channel_ids();
138
139
258k
        RETURN_IF_ERROR(_channel_add_rows(state, _local_state.channels,
140
258k
                                          _local_state.channels.size(), channel_ids, rows, block,
141
258k
                                          eos));
142
258k
    }
143
144
258k
    return Status::OK();
145
258k
}
146
147
Status ExchangeOlapWriter::_channel_add_rows(RuntimeState* state,
148
                                             std::vector<std::shared_ptr<Channel>>& channels,
149
                                             size_t channel_count,
150
                                             const std::vector<HashValType>& channel_ids,
151
                                             size_t rows, Block* block, bool eos,
152
4.45k
                                             HashValType invalid_val) {
153
4.45k
    size_t effective_rows = 0;
154
4.45k
    effective_rows =
155
4.45k
            std::ranges::count_if(channel_ids, [=](int64_t cid) { return cid != invalid_val; });
156
157
    // row index will skip all skipped rows.
158
4.45k
    _origin_row_idx.resize(effective_rows);
159
4.45k
    _channel_rows_histogram.assign(channel_count, 0U);
160
4.45k
    _channel_pos_offsets.resize(channel_count);
161
4.97k
    for (size_t i = 0; i < rows; ++i) {
162
518
        if (channel_ids[i] == invalid_val) {
163
2
            continue;
164
2
        }
165
516
        auto cid = channel_ids[i];
166
516
        _channel_rows_histogram[cid]++;
167
516
    }
168
4.45k
    _channel_pos_offsets[0] = 0;
169
33.4k
    for (size_t i = 1; i < channel_count; ++i) {
170
28.9k
        _channel_pos_offsets[i] = _channel_pos_offsets[i - 1] + _channel_rows_histogram[i - 1];
171
28.9k
    }
172
4.97k
    for (uint32_t i = 0; i < rows; ++i) {
173
518
        if (channel_ids[i] == invalid_val) {
174
2
            continue;
175
2
        }
176
516
        auto cid = channel_ids[i];
177
516
        auto pos = _channel_pos_offsets[cid]++;
178
516
        _origin_row_idx[pos] = i;
179
516
    }
180
181
4.45k
    return _add_rows_impl(state, channels, channel_count, block, eos);
182
4.45k
}
183
184
Status ExchangeTrivialWriter::_channel_add_rows(RuntimeState* state,
185
                                                std::vector<std::shared_ptr<Channel>>& channels,
186
                                                size_t channel_count,
187
                                                const std::vector<HashValType>& channel_ids,
188
258k
                                                size_t rows, Block* block, bool eos) {
189
258k
    _origin_row_idx.resize(rows);
190
258k
    _channel_rows_histogram.assign(channel_count, 0U);
191
258k
    _channel_pos_offsets.resize(channel_count);
192
37.5M
    for (size_t i = 0; i < rows; ++i) {
193
37.2M
        _channel_rows_histogram[channel_ids[i]]++;
194
37.2M
    }
195
258k
    _channel_pos_offsets[0] = 0;
196
2.39M
    for (size_t i = 1; i < channel_count; ++i) {
197
2.13M
        _channel_pos_offsets[i] = _channel_pos_offsets[i - 1] + _channel_rows_histogram[i - 1];
198
2.13M
    }
199
37.4M
    for (uint32_t i = 0; i < rows; i++) {
200
37.1M
        auto cid = channel_ids[i];
201
37.1M
        auto pos = _channel_pos_offsets[cid]++;
202
37.1M
        _origin_row_idx[pos] = i;
203
37.1M
    }
204
205
258k
    return _add_rows_impl(state, channels, channel_count, block, eos);
206
258k
}
207
208
} // namespace doris