Coverage Report

Created: 2026-06-08 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/sink/vrow_distribution.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/sink/vrow_distribution.h"
19
20
#include <gen_cpp/FrontendService.h>
21
#include <gen_cpp/FrontendService_types.h>
22
#include <glog/logging.h>
23
24
#include <cstdint>
25
#include <memory>
26
#include <string>
27
28
#include "common/cast_set.h"
29
#include "common/logging.h"
30
#include "common/status.h"
31
#include "core/assert_cast.h"
32
#include "core/column/column.h"
33
#include "core/column/column_const.h"
34
#include "core/column/column_nullable.h"
35
#include "core/column/column_vector.h"
36
#include "core/data_type/data_type.h"
37
#include "exec/sink/writer/vtablet_writer.h"
38
#include "runtime/exec_env.h"
39
#include "runtime/query_context.h"
40
#include "runtime/runtime_state.h"
41
#include "service/backend_options.h"
42
#include "util/client_cache.h"
43
#include "util/debug_points.h"
44
#include "util/thrift_rpc_helper.h"
45
46
namespace doris {
47
48
44.1k
std::pair<VExprContextSPtrs, VExprSPtrs> VRowDistribution::_get_partition_function() {
49
44.1k
    return {_vpartition->get_part_func_ctx(), _vpartition->get_partition_function()};
50
44.1k
}
51
52
Status VRowDistribution::_save_missing_values(
53
        const Block& input_block,
54
        std::vector<std::vector<std::string>>& col_strs, // non-const ref for move
55
        int col_size, Block* block, const std::vector<uint32_t>& filter,
56
180
        const std::vector<const NullMap*>& col_null_maps) {
57
    // de-duplication for new partitions but save all rows.
58
180
    RETURN_IF_ERROR(
59
180
            _batching_block->add_rows(&input_block, filter.data(), filter.data() + filter.size()));
60
180
    std::vector<TNullableStringLiteral> cur_row_values;
61
19.7k
    for (int row = 0; row < col_strs[0].size(); ++row) {
62
19.5k
        cur_row_values.clear();
63
39.1k
        for (int col = 0; col < col_size; ++col) {
64
19.5k
            TNullableStringLiteral node;
65
19.5k
            const auto* null_map = col_null_maps[col]; // null map for this col
66
19.5k
            node.__set_is_null((null_map && (*null_map)[filter[row]])
67
19.5k
                                       ? true
68
19.5k
                                       : node.is_null); // if not, dont change(default false)
69
19.5k
            if (!node.is_null) {
70
19.5k
                node.__set_value(col_strs[col][row]);
71
19.5k
            }
72
19.5k
            cur_row_values.push_back(node);
73
19.5k
        }
74
19.5k
        if (!_deduper.contains(cur_row_values)) {
75
359
            _deduper.insert(cur_row_values);
76
359
            _partitions_need_create.emplace_back(cur_row_values);
77
359
        }
78
19.5k
    }
79
80
    // to avoid too large mem use
81
180
    if (_batching_block->rows() > _batch_size) {
82
2
        _deal_batched = true;
83
2
    }
84
180
    _batching_rows = _batching_block->rows();
85
180
    VLOG_NOTICE << "pushed some batching lines, now numbers = " << _batching_rows;
86
87
180
    return Status::OK();
88
180
}
89
90
173
void VRowDistribution::clear_batching_stats() {
91
173
    _partitions_need_create.clear();
92
173
    _batching_rows = 0;
93
173
    _batching_bytes = 0;
94
173
}
95
96
173
Status VRowDistribution::automatic_create_partition() {
97
173
    MonotonicStopWatch timer;
98
173
    if (_state->enable_profile() && _state->profile_level() >= 2) {
99
0
        timer.start();
100
0
    }
101
102
173
    SCOPED_TIMER(_add_partition_request_timer);
103
173
    TCreatePartitionRequest request;
104
173
    TCreatePartitionResult result;
105
173
    bool injected = false;
106
173
    std::string be_endpoint = BackendOptions::get_be_endpoint();
107
173
    request.__set_txn_id(_txn_id);
108
173
    request.__set_db_id(_vpartition->db_id());
109
173
    request.__set_table_id(_vpartition->table_id());
110
173
    request.__set_partitionValues(_partitions_need_create);
111
173
    request.__set_be_endpoint(be_endpoint);
112
173
    request.__set_write_single_replica(_write_single_replica);
113
173
    if (_state && _state->get_query_ctx()) {
114
        // Pass query_id to FE so it can determine if this is a multi-instance load by checking Coordinator
115
173
        request.__set_query_id(_state->get_query_ctx()->query_id());
116
173
    }
117
118
173
    DBUG_EXECUTE_IF("VRowDistribution.automatic_create_partition.inject_result", {
119
173
        DBUG_RUN_CALLBACK(&request, &result);
120
173
        injected = true;
121
173
    });
122
123
173
    VLOG_NOTICE << "automatic partition rpc begin request " << request;
124
173
    if (!injected) {
125
171
        std::shared_ptr<TNetworkAddress> master_addr;
126
171
        if (_vpartition->get_master_address() == nullptr) {
127
171
            auto* cluster_info = ExecEnv::GetInstance()->cluster_info();
128
171
            if (cluster_info == nullptr) {
129
0
                return Status::InternalError("cluster_info is null");
130
0
            }
131
171
            master_addr = std::make_shared<TNetworkAddress>(cluster_info->master_fe_addr);
132
171
        } else {
133
0
            master_addr = _vpartition->get_master_address();
134
0
        }
135
171
        int time_out = _state->execution_timeout() * 1000;
136
171
        RETURN_IF_ERROR(ThriftRpcHelper::rpc<FrontendServiceClient>(
137
171
                master_addr->hostname, master_addr->port,
138
171
                [&request, &result](FrontendServiceConnection& client) {
139
171
                    client->createPartition(result, request);
140
171
                },
141
171
                time_out));
142
171
    }
143
144
173
    Status status(Status::create(result.status));
145
173
    VLOG_NOTICE << "automatic partition rpc end response " << result;
146
173
    if (result.status.status_code == TStatusCode::OK) {
147
173
        RETURN_IF_ERROR(_create_partition_callback(_caller, &result));
148
        // add new created partitions
149
173
        RETURN_IF_ERROR(_vpartition->add_partitions(result.partitions));
150
359
        for (const auto& part : result.partitions) {
151
359
            _new_partition_ids.insert(part.id);
152
359
            VLOG_TRACE << "record new id: " << part.id;
153
359
        }
154
173
    }
155
156
    // Record this request's elapsed time
157
173
    if (_state->enable_profile() && _state->profile_level() >= 2) {
158
0
        int64_t elapsed_ns = timer.elapsed_time();
159
0
        _add_partition_request_times.push_back(elapsed_ns);
160
0
    }
161
173
    return status;
162
173
}
163
164
// for reuse the same create callback of create-partition
165
20
static TCreatePartitionResult cast_as_create_result(const TReplacePartitionResult& arg) {
166
20
    TCreatePartitionResult result;
167
20
    result.status = arg.status;
168
20
    result.nodes = arg.nodes;
169
20
    result.partitions = arg.partitions;
170
20
    result.tablets = arg.tablets;
171
20
    result.slave_tablets = arg.slave_tablets;
172
20
    return result;
173
20
}
174
175
// use _partitions and replace them
176
49
Status VRowDistribution::_replace_overwriting_partition() {
177
49
    SCOPED_TIMER(_add_partition_request_timer); // also for replace_partition
178
49
    TReplacePartitionRequest request;
179
49
    TReplacePartitionResult result;
180
49
    bool injected = false;
181
49
    request.__set_overwrite_group_id(_vpartition->get_overwrite_group_id());
182
49
    request.__set_db_id(_vpartition->db_id());
183
49
    request.__set_table_id(_vpartition->table_id());
184
49
    request.__set_write_single_replica(_write_single_replica);
185
186
    // only request for partitions not recorded for replacement
187
49
    std::set<int64_t> id_deduper;
188
60.0k
    for (const auto* part : _partitions) {
189
60.0k
        if (part != nullptr) {
190
50.0k
            if (_new_partition_ids.contains(part->id)) {
191
                // this is a new partition. dont replace again.
192
45.0k
                VLOG_TRACE << "skip new partition: " << part->id;
193
45.0k
            } else {
194
                // request for replacement
195
5.04k
                id_deduper.insert(part->id);
196
5.04k
            }
197
50.0k
        } else if (_missing_map.empty()) {
198
            // no origin partition. and not allow to create.
199
6
            return Status::InvalidArgument(
200
6
                    "Cannot found origin partitions in auto detect overwriting, stop "
201
6
                    "processing");
202
6
        } // else: part is null and _missing_map is not empty. dealed outside using auto-partition way. nothing to do here.
203
60.0k
    }
204
43
    if (id_deduper.empty()) {
205
23
        return Status::OK(); // no need to request
206
23
    }
207
    // de-duplicate. there's no check in FE
208
20
    std::vector<int64_t> request_part_ids(id_deduper.begin(), id_deduper.end());
209
210
20
    request.__set_partition_ids(request_part_ids);
211
212
20
    std::string be_endpoint = BackendOptions::get_be_endpoint();
213
20
    request.__set_be_endpoint(be_endpoint);
214
20
    if (_state && _state->get_query_ctx()) {
215
        // Pass query_id to FE so it can determine if this is a multi-instance load by checking Coordinator
216
20
        request.__set_query_id(_state->get_query_ctx()->query_id());
217
20
    }
218
219
20
    DBUG_EXECUTE_IF("VRowDistribution.replace_overwriting_partition.inject_result", {
220
20
        DBUG_RUN_CALLBACK(&request, &result);
221
20
        injected = true;
222
20
    });
223
224
20
    VLOG_NOTICE << "auto detect replace partition request: " << request;
225
20
    if (!injected) {
226
19
        std::shared_ptr<TNetworkAddress> master_addr;
227
19
        if (_vpartition->get_master_address() == nullptr) {
228
19
            auto* cluster_info = ExecEnv::GetInstance()->cluster_info();
229
19
            if (cluster_info == nullptr) {
230
0
                return Status::InternalError("cluster_info is null");
231
0
            }
232
19
            master_addr = std::make_shared<TNetworkAddress>(cluster_info->master_fe_addr);
233
19
        } else {
234
0
            master_addr = _vpartition->get_master_address();
235
0
        }
236
19
        int time_out = _state->execution_timeout() * 1000;
237
19
        RETURN_IF_ERROR(ThriftRpcHelper::rpc<FrontendServiceClient>(
238
19
                master_addr->hostname, master_addr->port,
239
19
                [&request, &result](FrontendServiceConnection& client) {
240
19
                    client->replacePartition(result, request);
241
19
                },
242
19
                time_out));
243
19
    }
244
245
20
    Status status(Status::create(result.status));
246
20
    VLOG_NOTICE << "auto detect replace partition result: " << result;
247
20
    if (result.status.status_code == TStatusCode::OK) {
248
        // Reuse the function as the args' structure are same. It adds nodes/locations
249
        // and waits for incremental_open before the new tablets become routable.
250
20
        auto result_as_create = cast_as_create_result(result);
251
20
        RETURN_IF_ERROR(_create_partition_callback(_caller, &result_as_create));
252
        // record new partitions
253
32
        for (const auto& part : result.partitions) {
254
32
            _new_partition_ids.insert(part.id);
255
32
            VLOG_TRACE << "record new id: " << part.id;
256
32
        }
257
        // replace data in _partitions
258
20
        RETURN_IF_ERROR(_vpartition->replace_partitions(request_part_ids, result.partitions));
259
20
    }
260
261
20
    return status;
262
20
}
263
264
void VRowDistribution::_get_tablet_ids(Block* block, int32_t index_idx,
265
44.6k
                                       std::vector<int64_t>& tablet_ids) {
266
44.6k
    tablet_ids.resize(block->rows());
267
35.7M
    for (int row_idx = 0; row_idx < block->rows(); row_idx++) {
268
35.7M
        if (_skip[row_idx]) {
269
20.9k
            continue;
270
20.9k
        }
271
35.7M
        auto& partition = _partitions[row_idx];
272
35.7M
        auto& tablet_index = _tablet_indexes[row_idx];
273
35.7M
        auto& index = partition->indexes[index_idx];
274
275
35.7M
        auto tablet_id = index.tablets[tablet_index];
276
35.7M
        tablet_ids[row_idx] = tablet_id;
277
35.7M
    }
278
44.6k
}
279
280
44.6k
void VRowDistribution::_filter_block_by_skip(Block* block, RowPartTabletIds& row_part_tablet_id) {
281
44.6k
    auto& row_ids = row_part_tablet_id.row_ids;
282
44.6k
    auto& partition_ids = row_part_tablet_id.partition_ids;
283
44.6k
    auto& tablet_ids = row_part_tablet_id.tablet_ids;
284
285
44.6k
    auto rows = block->rows();
286
    // row count of a block should not exceed UINT32_MAX
287
44.6k
    auto rows_uint32 = cast_set<uint32_t>(rows);
288
35.9M
    for (uint32_t i = 0; i < rows_uint32; i++) {
289
35.8M
        if (!_skip[i]) {
290
35.8M
            row_ids.emplace_back(i);
291
35.8M
            partition_ids.emplace_back(_partitions[i]->id);
292
35.8M
            tablet_ids.emplace_back(_tablet_ids[i]);
293
35.8M
        }
294
35.8M
    }
295
44.6k
}
296
297
Status VRowDistribution::_filter_block_by_skip_and_where_clause(
298
35
        Block* block, const VExprContextSPtr& where_clause, RowPartTabletIds& row_part_tablet_id) {
299
    // TODO
300
    //SCOPED_RAW_TIMER(&_stat.where_clause_ns);
301
35
    ColumnPtr filter_column;
302
35
    RETURN_IF_ERROR(where_clause->execute(block, filter_column));
303
304
35
    auto& row_ids = row_part_tablet_id.row_ids;
305
35
    auto& partition_ids = row_part_tablet_id.partition_ids;
306
35
    auto& tablet_ids = row_part_tablet_id.tablet_ids;
307
35
    if (const auto* nullable_column = check_and_get_column<ColumnNullable>(*filter_column)) {
308
29
        auto rows = block->rows();
309
        // row count of a block should not exceed UINT32_MAX
310
29
        auto rows_uint32 = cast_set<uint32_t>(rows);
311
58
        for (uint32_t i = 0; i < rows_uint32; i++) {
312
29
            if (nullable_column->get_bool_inline(i) && !_skip[i]) {
313
12
                row_ids.emplace_back(i);
314
12
                partition_ids.emplace_back(_partitions[i]->id);
315
12
                tablet_ids.emplace_back(_tablet_ids[i]);
316
12
            }
317
29
        }
318
29
    } else if (const auto* const_column = check_and_get_column<ColumnConst>(*filter_column)) {
319
1
        bool ret = const_column->get_bool(0);
320
1
        if (!ret) {
321
1
            return Status::OK();
322
1
        }
323
        // should we optimize?
324
0
        _filter_block_by_skip(block, row_part_tablet_id);
325
5
    } else {
326
5
        const auto& filter = assert_cast<const ColumnUInt8&>(*filter_column).get_data();
327
5
        auto rows = block->rows();
328
        // row count of a block should not exceed UINT32_MAX
329
5
        auto rows_uint32 = cast_set<uint32_t>(rows);
330
15
        for (uint32_t i = 0; i < rows_uint32; i++) {
331
10
            if (filter[i] != 0 && !_skip[i]) {
332
6
                row_ids.emplace_back(i);
333
6
                partition_ids.emplace_back(_partitions[i]->id);
334
6
                tablet_ids.emplace_back(_tablet_ids[i]);
335
6
            }
336
10
        }
337
5
    }
338
339
34
    return Status::OK();
340
35
}
341
342
Status VRowDistribution::_filter_block(Block* block,
343
43.6k
                                       std::vector<RowPartTabletIds>& row_part_tablet_ids) {
344
88.3k
    for (int i = 0; i < _schema->indexes().size(); i++) {
345
44.6k
        _get_tablet_ids(block, i, _tablet_ids);
346
44.6k
        auto& where_clause = _schema->indexes()[i]->where_clause;
347
44.6k
        if (where_clause != nullptr) {
348
35
            RETURN_IF_ERROR(_filter_block_by_skip_and_where_clause(block, where_clause,
349
35
                                                                   row_part_tablet_ids[i]));
350
44.6k
        } else {
351
44.6k
            _filter_block_by_skip(block, row_part_tablet_ids[i]);
352
44.6k
        }
353
44.6k
    }
354
43.6k
    return Status::OK();
355
43.6k
}
356
357
Status VRowDistribution::_generate_rows_distribution_for_non_auto_partition(
358
43.2k
        Block* block, bool has_filtered_rows, std::vector<RowPartTabletIds>& row_part_tablet_ids) {
359
43.2k
    int num_rows = cast_set<int>(block->rows());
360
361
43.2k
    RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
362
43.2k
                                                 _tablet_indexes, _skip));
363
43.2k
    if (has_filtered_rows) {
364
635
        for (int i = 0; i < num_rows; i++) {
365
563
            _skip[i] = _skip[i] || _block_convertor->filter_map()[i];
366
563
        }
367
72
    }
368
43.2k
    RETURN_IF_ERROR(_filter_block(block, row_part_tablet_ids));
369
43.2k
    return Status::OK();
370
43.2k
}
371
372
Status VRowDistribution::_deal_missing_map(const Block& input_block, Block* block,
373
                                           const std::vector<uint16_t>& partition_cols_idx,
374
180
                                           int64_t& rows_stat_val) {
375
    // for missing partition keys, calc the missing partition and save in _partitions_need_create
376
180
    auto [part_ctxs, part_exprs] = _get_partition_function();
377
180
    int part_col_num = cast_set<int>(part_exprs.size());
378
    // the two vectors are in column-first-order
379
180
    std::vector<std::vector<std::string>> col_strs;
380
180
    std::vector<const NullMap*> col_null_maps;
381
180
    col_strs.resize(part_col_num);
382
180
    col_null_maps.reserve(part_col_num);
383
384
180
    auto format_options = DataTypeSerDe::get_default_format_options();
385
180
    format_options.timezone = &_state->timezone_obj();
386
387
368
    for (int i = 0; i < part_col_num; ++i) {
388
188
        auto return_type = part_exprs[i]->data_type();
389
        // expose the data column. the return type would be nullable
390
188
        const auto& [range_left_col, col_const] =
391
188
                unpack_if_const(block->get_by_position(partition_cols_idx[i]).column);
392
188
        if (range_left_col->is_nullable()) {
393
50
            col_null_maps.push_back(&(
394
50
                    assert_cast<const ColumnNullable*>(range_left_col.get())->get_null_map_data()));
395
138
        } else {
396
138
            col_null_maps.push_back(nullptr);
397
138
        }
398
19.5k
        for (auto row : _missing_map) {
399
19.5k
            col_strs[i].push_back(return_type->to_string(
400
19.5k
                    *range_left_col, index_check_const(row, col_const), format_options));
401
19.5k
        }
402
188
    }
403
404
    // calc the end value and save them. in the end of sending, we will create partitions for them and deal them.
405
    // NOTE: must save old batching stats before calling _save_missing_values(),
406
    // because _save_missing_values() will update _batching_rows internally.
407
180
    size_t old_bt_rows = _batching_rows;
408
180
    size_t old_bt_bytes = _batching_bytes;
409
410
180
    RETURN_IF_ERROR(_save_missing_values(input_block, col_strs, part_col_num, block, _missing_map,
411
180
                                         col_null_maps));
412
413
180
    size_t new_bt_rows = _batching_block->rows();
414
180
    size_t new_bt_bytes = _batching_block->bytes();
415
180
    rows_stat_val -= new_bt_rows - old_bt_rows;
416
180
    _state->update_num_rows_load_total(old_bt_rows - new_bt_rows);
417
180
    _state->update_num_bytes_load_total(old_bt_bytes - new_bt_bytes);
418
419
180
    return Status::OK();
420
180
}
421
422
Status VRowDistribution::_generate_rows_distribution_for_auto_partition(
423
        const Block& input_block, Block* block, const std::vector<uint16_t>& partition_cols_idx,
424
        bool has_filtered_rows, std::vector<RowPartTabletIds>& row_part_tablet_ids,
425
348
        int64_t& rows_stat_val) {
426
348
    int num_rows = cast_set<int>(block->rows());
427
348
    std::vector<uint16_t> partition_keys = _vpartition->get_partition_keys();
428
429
348
    auto& partition_col = block->get_by_position(partition_keys[0]);
430
348
    _missing_map.clear();
431
348
    _missing_map.reserve(partition_col.column->size());
432
433
348
    RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
434
348
                                                 _tablet_indexes, _skip, &_missing_map));
435
436
    // the missing vals for auto partition are also skipped.
437
348
    if (has_filtered_rows) {
438
8
        for (int i = 0; i < num_rows; i++) {
439
4
            _skip[i] = _skip[i] || _block_convertor->filter_map()[i];
440
4
        }
441
4
    }
442
348
    RETURN_IF_ERROR(_filter_block(block, row_part_tablet_ids));
443
444
348
    if (!_missing_map.empty()) {
445
175
        RETURN_IF_ERROR(_deal_missing_map(input_block, block, partition_cols_idx,
446
175
                                          rows_stat_val)); // send input block to save
447
175
    }
448
348
    return Status::OK();
449
348
}
450
451
Status VRowDistribution::_generate_rows_distribution_for_auto_overwrite(
452
        const Block& input_block, Block* block, const std::vector<uint16_t>& partition_cols_idx,
453
        bool has_filtered_rows, std::vector<RowPartTabletIds>& row_part_tablet_ids,
454
49
        int64_t& rows_stat_val) {
455
49
    int num_rows = cast_set<int>(block->rows());
456
457
    // for non-auto-partition situation, goes into two 'else' branch. just find the origin partitions, replace them by rpc,
458
    //  and find the new partitions to use.
459
    // for auto-partition's, find and save origins in _partitions and replace them. at meanwhile save the missing values for auto
460
    //  partition. then we find partition again to get replaced partitions in _partitions. this time _missing_map is ignored cuz
461
    //  we already saved missing values.
462
49
    if (_vpartition->is_auto_partition() &&
463
49
        _state->query_options().enable_auto_create_when_overwrite) {
464
        // allow auto create partition for missing rows.
465
25
        std::vector<uint16_t> partition_keys = _vpartition->get_partition_keys();
466
25
        auto partition_col = block->get_by_position(partition_keys[0]);
467
25
        _missing_map.clear();
468
25
        _missing_map.reserve(partition_col.column->size());
469
470
25
        RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
471
25
                                                     _tablet_indexes, _skip, &_missing_map));
472
473
        // allow and really need to create during auto-detect-overwriting.
474
25
        if (!_missing_map.empty()) {
475
5
            RETURN_IF_ERROR(
476
5
                    _deal_missing_map(input_block, block, partition_cols_idx, rows_stat_val));
477
5
        }
478
25
    } else {
479
24
        RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
480
24
                                                     _tablet_indexes, _skip));
481
24
    }
482
49
    RETURN_IF_ERROR(_replace_overwriting_partition());
483
484
    // regenerate locations for new partitions & tablets
485
43
    _reset_find_tablets(num_rows);
486
43
    if (_vpartition->is_auto_partition() &&
487
43
        _state->query_options().enable_auto_create_when_overwrite) {
488
        // here _missing_map is just a placeholder
489
25
        RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
490
25
                                                     _tablet_indexes, _skip, &_missing_map));
491
25
        if (VLOG_TRACE_IS_ON) {
492
0
            std::string tmp;
493
0
            for (auto v : _missing_map) {
494
0
                tmp += std::to_string(v).append(", ");
495
0
            }
496
0
            VLOG_TRACE << "Trace missing map of " << this << ':' << tmp;
497
0
        }
498
25
    } else {
499
18
        RETURN_IF_ERROR(_tablet_finder->find_tablets(_state, block, num_rows, _partitions,
500
18
                                                     _tablet_indexes, _skip));
501
18
    }
502
43
    if (has_filtered_rows) {
503
0
        for (int i = 0; i < num_rows; i++) {
504
0
            _skip[i] = _skip[i] || _block_convertor->filter_map()[i];
505
0
        }
506
0
    }
507
43
    RETURN_IF_ERROR(_filter_block(block, row_part_tablet_ids));
508
43
    return Status::OK();
509
43
}
510
511
void VRowDistribution::_reset_row_part_tablet_ids(
512
43.6k
        std::vector<RowPartTabletIds>& row_part_tablet_ids, int64_t rows) {
513
43.6k
    row_part_tablet_ids.resize(_schema->indexes().size());
514
44.6k
    for (auto& row_part_tablet_id : row_part_tablet_ids) {
515
44.6k
        auto& row_ids = row_part_tablet_id.row_ids;
516
44.6k
        auto& partition_ids = row_part_tablet_id.partition_ids;
517
44.6k
        auto& tablet_ids = row_part_tablet_id.tablet_ids;
518
519
44.6k
        row_ids.clear();
520
44.6k
        partition_ids.clear();
521
44.6k
        tablet_ids.clear();
522
        // This is important for performance.
523
44.6k
        row_ids.reserve(rows);
524
44.6k
        partition_ids.reserve(rows);
525
44.6k
        tablet_ids.reserve(rows);
526
44.6k
    }
527
43.6k
}
528
529
Status VRowDistribution::generate_rows_distribution(
530
        Block& input_block, std::shared_ptr<Block>& block,
531
43.6k
        std::vector<RowPartTabletIds>& row_part_tablet_ids, int64_t& rows_stat_val) {
532
43.6k
    auto input_rows = input_block.rows();
533
43.6k
    _reset_row_part_tablet_ids(row_part_tablet_ids, input_rows);
534
535
    // we store the batching block with value of `input_block`. so just do all of these again.
536
43.6k
    bool has_filtered_rows = false;
537
43.6k
    RETURN_IF_ERROR(_block_convertor->validate_and_convert_block(
538
43.6k
            _state, &input_block, block, *_vec_output_expr_ctxs, input_rows, has_filtered_rows));
539
540
    // batching block rows which need new partitions. deal together at finish.
541
43.6k
    if (!_batching_block) [[unlikely]] {
542
35.5k
        std::unique_ptr<Block> tmp_block = input_block.create_same_struct_block(0);
543
35.5k
        _batching_block = MutableBlock::create_unique(std::move(*tmp_block));
544
35.5k
    }
545
546
43.6k
    auto num_rows = block->rows();
547
43.6k
    _reset_find_tablets(num_rows);
548
549
    // if there's projection of partition calc, we need to calc it first.
550
43.6k
    auto [part_ctxs, part_funcs] = _get_partition_function();
551
43.6k
    std::vector<uint16_t> partition_cols_idx;
552
43.6k
    if (_vpartition->is_projection_partition()) {
553
        // calc the start value of missing partition ranges.
554
544
        auto func_size = part_funcs.size();
555
1.20k
        for (int i = 0; i < func_size; ++i) {
556
662
            int result_idx = -1;
557
            // we just calc left range here. leave right to FE to avoid dup calc.
558
662
            RETURN_IF_ERROR(part_funcs[i]->execute(part_ctxs[i].get(), block.get(), &result_idx));
559
560
662
            VLOG_DEBUG << "Partition-calculated block:\n" << block->dump_data(0, 1);
561
662
            DCHECK(result_idx != -1);
562
563
662
            partition_cols_idx.push_back(cast_set<uint16_t>(result_idx));
564
662
        }
565
566
        // change the column to compare to transformed.
567
544
        _vpartition->set_transformed_slots(partition_cols_idx);
568
544
    }
569
570
43.6k
    Status st = Status::OK();
571
43.6k
    if (_vpartition->is_auto_detect_overwrite() && !_deal_batched) {
572
        // when overwrite, no auto create partition allowed.
573
49
        st = _generate_rows_distribution_for_auto_overwrite(input_block, block.get(),
574
49
                                                            partition_cols_idx, has_filtered_rows,
575
49
                                                            row_part_tablet_ids, rows_stat_val);
576
43.6k
    } else if (_vpartition->is_auto_partition() && !_deal_batched) {
577
348
        st = _generate_rows_distribution_for_auto_partition(input_block, block.get(),
578
348
                                                            partition_cols_idx, has_filtered_rows,
579
348
                                                            row_part_tablet_ids, rows_stat_val);
580
43.2k
    } else { // not auto partition
581
43.2k
        st = _generate_rows_distribution_for_non_auto_partition(block.get(), has_filtered_rows,
582
43.2k
                                                                row_part_tablet_ids);
583
43.2k
    }
584
585
43.6k
    return st;
586
43.6k
}
587
588
// reuse vars for find_tablets
589
43.7k
void VRowDistribution::_reset_find_tablets(int64_t rows) {
590
43.7k
    _tablet_finder->filter_bitmap().Reset(rows);
591
43.7k
    _partitions.assign(rows, nullptr);
592
43.7k
    _skip.assign(rows, false);
593
43.7k
    _tablet_indexes.assign(rows, 0);
594
43.7k
}
595
596
} // namespace doris