Coverage Report

Created: 2026-08-07 00:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet_info.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 "storage/tablet_info.h"
19
20
#include <butil/logging.h>
21
#include <gen_cpp/Descriptors_types.h>
22
#include <gen_cpp/Exprs_types.h>
23
#include <gen_cpp/Partitions_types.h>
24
#include <gen_cpp/Types_types.h>
25
#include <gen_cpp/descriptors.pb.h>
26
#include <gen_cpp/olap_file.pb.h>
27
#include <glog/logging.h>
28
29
#include <algorithm>
30
#include <cstddef>
31
#include <cstdint>
32
#include <memory>
33
#include <ostream>
34
#include <string>
35
#include <tuple>
36
37
#include "common/exception.h"
38
#include "common/logging.h"
39
#include "common/status.h"
40
#include "core/column/column.h"
41
#include "core/data_type/data_type.h"
42
#include "core/data_type/data_type_factory.hpp"
43
#include "core/data_type/define_primitive_type.h"
44
#include "core/data_type/primitive_type.h"
45
#include "core/value/large_int_value.h"
46
#include "runtime/descriptors.h"
47
#include "runtime/memory/mem_tracker.h"
48
#include "storage/tablet/tablet_schema.h"
49
#include "util/raw_value.h"
50
#include "util/string_parser.hpp"
51
#include "util/string_util.h"
52
// NOLINTNEXTLINE(unused-includes)
53
#include "core/value/vdatetime_value.h"
54
#include "exprs/function/cast/cast_to_date_or_datetime_impl.hpp"
55
#include "exprs/function/cast/cast_to_datetimev2_impl.hpp"
56
#include "exprs/function/cast/cast_to_datev2_impl.hpp"
57
#include "exprs/function/cast/cast_to_timestamptz.h"
58
#include "exprs/vexpr_context.h" // IWYU pragma: keep
59
#include "exprs/vliteral.h"
60
61
namespace doris {
62
63
2
const OlapTableIndexSchema* OlapTableSchemaParam::row_binlog_index_schema(int64_t index_id) const {
64
2
    for (auto* schema : _row_binlog_index_schemas) {
65
2
        if (schema->index_id == index_id) {
66
2
            return schema;
67
2
        }
68
2
    }
69
0
    return nullptr;
70
2
}
71
72
55
void OlapTableIndexSchema::to_protobuf(POlapTableIndexSchema* pindex) const {
73
55
    pindex->set_id(index_id);
74
55
    pindex->set_schema_hash(schema_hash);
75
55
    if (row_binlog_id > 0) {
76
2
        pindex->set_row_binlog_id(row_binlog_id);
77
2
    }
78
55
    for (auto* slot : slots) {
79
5
        pindex->add_columns(slot->col_name());
80
5
    }
81
55
    for (auto* column : columns) {
82
20
        column->to_schema_pb(pindex->add_columns_desc());
83
20
    }
84
55
    for (auto* index : indexes) {
85
0
        index->to_schema_pb(pindex->add_indexes_desc());
86
0
    }
87
55
}
88
89
bool VOlapTablePartKeyComparator::operator()(const BlockRowWithIndicator& lhs,
90
118
                                             const BlockRowWithIndicator& rhs) const {
91
118
    Block* l_block = std::get<0>(lhs);
92
118
    Block* r_block = std::get<0>(rhs);
93
118
    int32_t l_row = std::get<1>(lhs);
94
118
    int32_t r_row = std::get<1>(rhs);
95
118
    bool l_use_new = std::get<2>(lhs);
96
118
    bool r_use_new = std::get<2>(rhs);
97
98
118
    VLOG_TRACE << '\n' << l_block->dump_data() << '\n' << r_block->dump_data();
99
100
118
    if (l_row == -1) {
101
0
        return false;
102
118
    } else if (r_row == -1) {
103
0
        return true;
104
0
    }
105
106
118
    if (_param_locs.empty()) { // no transform, use origin column
107
90
        for (auto slot_loc : _slot_locs) {
108
90
            auto res = l_block->get_by_position(slot_loc).column->compare_at(
109
90
                    l_row, r_row, *r_block->get_by_position(slot_loc).column, -1);
110
90
            if (res != 0) {
111
86
                return res < 0;
112
86
            }
113
90
        }
114
90
    } else { // use transformed column to compare
115
28
        DCHECK(_slot_locs.size() == _param_locs.size())
116
0
                << _slot_locs.size() << ' ' << _param_locs.size();
117
118
28
        const std::vector<uint16_t>* l_index = l_use_new ? &_param_locs : &_slot_locs;
119
28
        const std::vector<uint16_t>* r_index = r_use_new ? &_param_locs : &_slot_locs;
120
121
28
        for (int i = 0; i < _slot_locs.size(); i++) {
122
28
            ColumnPtr l_col = l_block->get_by_position((*l_index)[i]).column;
123
28
            ColumnPtr r_col = r_block->get_by_position((*r_index)[i]).column;
124
125
28
            auto res = l_col->compare_at(l_row, r_row, *r_col, -1);
126
28
            if (res != 0) {
127
28
                return res < 0;
128
28
            }
129
28
        }
130
28
    }
131
132
    // equal, return false
133
4
    return false;
134
118
}
135
136
17
Status OlapTableSchemaParam::init(const POlapTableSchemaParam& pschema) {
137
17
    _db_id = pschema.db_id();
138
17
    _table_id = pschema.table_id();
139
17
    _version = pschema.version();
140
17
    if (pschema.has_unique_key_update_mode()) {
141
14
        _unique_key_update_mode = pschema.unique_key_update_mode();
142
14
        if (pschema.has_sequence_map_col_unique_id()) {
143
14
            _sequence_map_col_uid = pschema.sequence_map_col_unique_id();
144
14
        }
145
14
    } else {
146
        // for backward compatibility
147
3
        if (pschema.has_partial_update() && pschema.partial_update()) {
148
0
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS;
149
3
        } else {
150
3
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT;
151
3
        }
152
3
    }
153
17
    _is_strict_mode = pschema.is_strict_mode();
154
17
    if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
155
0
        _auto_increment_column = pschema.auto_increment_column();
156
0
        if (!_auto_increment_column.empty() && pschema.auto_increment_column_unique_id() == -1) {
157
0
            return Status::InternalError(
158
0
                    "Auto increment column id is not set in FE. Maybe FE is an older version "
159
0
                    "different from BE.");
160
0
        }
161
0
        _auto_increment_column_unique_id = pschema.auto_increment_column_unique_id();
162
0
    }
163
17
    if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT) {
164
0
        if (pschema.has_partial_update_new_key_policy()) {
165
0
            _partial_update_new_row_policy = pschema.partial_update_new_key_policy();
166
0
        }
167
0
    }
168
17
    _timestamp_ms = pschema.timestamp_ms();
169
17
    if (pschema.has_nano_seconds()) {
170
14
        _nano_seconds = pschema.nano_seconds();
171
14
    }
172
17
    _timezone = pschema.timezone();
173
174
17
    for (const auto& col : pschema.partial_update_input_columns()) {
175
0
        _partial_update_input_columns.insert(col);
176
0
    }
177
17
    std::unordered_map<std::string, SlotDescriptor*> slots_map;
178
179
17
    _tuple_desc = _obj_pool.add(new TupleDescriptor(pschema.tuple_desc()));
180
181
84
    for (const auto& p_slot_desc : pschema.slot_descs()) {
182
84
        auto* slot_desc = _obj_pool.add(new SlotDescriptor(p_slot_desc));
183
84
        _tuple_desc->add_slot(slot_desc);
184
84
        std::string data_type;
185
84
        EnumToString(TPrimitiveType, to_thrift(slot_desc->col_type()), data_type);
186
84
        std::string is_null_str = slot_desc->is_nullable() ? "true" : "false";
187
84
        std::string data_type_str =
188
84
                std::to_string(int64_t(TabletColumn::get_field_type_by_string(data_type)));
189
84
        slots_map.emplace(to_lower(slot_desc->col_name()) + "+" + data_type_str + is_null_str,
190
84
                          slot_desc);
191
84
    }
192
193
28
    for (const auto& p_index : pschema.indexes()) {
194
28
        auto* index = _obj_pool.add(new OlapTableIndexSchema());
195
28
        index->index_id = p_index.id();
196
28
        index->schema_hash = p_index.schema_hash();
197
28
        if (p_index.has_row_binlog_id()) {
198
0
            index->row_binlog_id = p_index.row_binlog_id();
199
0
        }
200
28
        for (const auto& pcolumn_desc : p_index.columns_desc()) {
201
0
            if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS ||
202
0
                _partial_update_input_columns.contains(pcolumn_desc.name())) {
203
0
                std::string is_null_str = pcolumn_desc.is_nullable() ? "true" : "false";
204
0
                std::string data_type_str = std::to_string(
205
0
                        int64_t(TabletColumn::get_field_type_by_string(pcolumn_desc.type())));
206
0
                auto it = slots_map.find(to_lower(pcolumn_desc.name()) + "+" + data_type_str +
207
0
                                         is_null_str);
208
0
                if (it == std::end(slots_map)) {
209
0
                    std::string keys {};
210
0
                    for (const auto& [key, _] : slots_map) {
211
0
                        keys += fmt::format("{},", key);
212
0
                    }
213
0
                    LOG_EVERY_SECOND(WARNING) << fmt::format(
214
0
                            "[OlapTableSchemaParam::init(const POlapTableSchemaParam& pschema)]: "
215
0
                            "unknown index column, column={}, type={}, data_type_str={}, "
216
0
                            "is_null_str={}, slots_map.keys()=[{}], {}\npschema={}",
217
0
                            pcolumn_desc.name(), pcolumn_desc.type(), data_type_str, is_null_str,
218
0
                            keys, debug_string(), pschema.ShortDebugString());
219
220
0
                    return Status::InternalError("unknown index column, column={}, type={}",
221
0
                                                 pcolumn_desc.name(), pcolumn_desc.type());
222
0
                }
223
0
                index->slots.emplace_back(it->second);
224
0
            }
225
0
            TabletColumn* tc = _obj_pool.add(new TabletColumn());
226
0
            tc->init_from_pb(pcolumn_desc);
227
0
            index->columns.emplace_back(tc);
228
0
        }
229
28
        for (const auto& pindex_desc : p_index.indexes_desc()) {
230
0
            TabletIndex* ti = _obj_pool.add(new TabletIndex());
231
0
            ti->init_from_pb(pindex_desc);
232
0
            index->indexes.emplace_back(ti);
233
0
        }
234
28
        _indexes.emplace_back(index);
235
28
    }
236
237
17
    for (const auto& p_index : pschema.row_binlog_index_schemas()) {
238
0
        auto* index = _obj_pool.add(new OlapTableIndexSchema());
239
0
        index->index_id = p_index.id();
240
0
        index->schema_hash = p_index.schema_hash();
241
0
        if (p_index.has_row_binlog_id()) {
242
0
            index->row_binlog_id = p_index.row_binlog_id();
243
0
        }
244
0
        for (const auto& pcolumn_desc : p_index.columns_desc()) {
245
0
            TabletColumn* tc = _obj_pool.add(new TabletColumn());
246
0
            tc->init_from_pb(pcolumn_desc);
247
0
            index->columns.emplace_back(tc);
248
0
        }
249
0
        for (const auto& pindex_desc : p_index.indexes_desc()) {
250
0
            TabletIndex* ti = _obj_pool.add(new TabletIndex());
251
0
            ti->init_from_pb(pindex_desc);
252
0
            index->indexes.emplace_back(ti);
253
0
        }
254
0
        _row_binlog_index_schemas.emplace_back(index);
255
0
    }
256
257
17
    std::sort(_indexes.begin(), _indexes.end(),
258
28
              [](const OlapTableIndexSchema* lhs, const OlapTableIndexSchema* rhs) {
259
28
                  return lhs->index_id < rhs->index_id;
260
28
              });
261
17
    return Status::OK();
262
17
}
263
264
32
Status OlapTableSchemaParam::init_unique_key_update_mode(const TOlapTableSchemaParam& tschema) {
265
32
    if (tschema.__isset.unique_key_update_mode) {
266
0
        switch (tschema.unique_key_update_mode) {
267
0
        case doris::TUniqueKeyUpdateMode::UPSERT: {
268
0
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT;
269
0
            break;
270
0
        }
271
0
        case doris::TUniqueKeyUpdateMode::UPDATE_FIXED_COLUMNS: {
272
0
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS;
273
0
            break;
274
0
        }
275
0
        case doris::TUniqueKeyUpdateMode::UPDATE_FLEXIBLE_COLUMNS: {
276
0
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS;
277
0
            break;
278
0
        }
279
0
        default: {
280
0
            return Status::InternalError(
281
0
                    "Unknown unique_key_update_mode: {}, should be one of "
282
0
                    "UPSERT/UPDATE_FIXED_COLUMNS/UPDATE_FLEXIBLE_COLUMNS",
283
0
                    tschema.unique_key_update_mode);
284
0
        }
285
0
        }
286
0
        if (tschema.__isset.sequence_map_col_unique_id) {
287
0
            _sequence_map_col_uid = tschema.sequence_map_col_unique_id;
288
0
        }
289
32
    } else {
290
        // for backward compatibility
291
32
        if (tschema.__isset.is_partial_update && tschema.is_partial_update) {
292
0
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS;
293
32
        } else {
294
32
            _unique_key_update_mode = UniqueKeyUpdateModePB::UPSERT;
295
32
        }
296
32
    }
297
32
    return Status::OK();
298
32
}
299
300
32
Status OlapTableSchemaParam::init(const TOlapTableSchemaParam& tschema) {
301
32
    _db_id = tschema.db_id;
302
32
    _table_id = tschema.table_id;
303
32
    _version = tschema.version;
304
32
    RETURN_IF_ERROR(init_unique_key_update_mode(tschema));
305
32
    if (tschema.__isset.is_strict_mode) {
306
32
        _is_strict_mode = tschema.is_strict_mode;
307
32
    }
308
32
    if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
309
0
        _auto_increment_column = tschema.auto_increment_column;
310
0
        if (!_auto_increment_column.empty() && tschema.auto_increment_column_unique_id == -1) {
311
0
            return Status::InternalError(
312
0
                    "Auto increment column id is not set in FE. Maybe FE is an older version "
313
0
                    "different from BE.");
314
0
        }
315
0
        _auto_increment_column_unique_id = tschema.auto_increment_column_unique_id;
316
0
    }
317
318
32
    if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT) {
319
0
        if (tschema.__isset.partial_update_new_key_policy) {
320
0
            switch (tschema.partial_update_new_key_policy) {
321
0
            case doris::TPartialUpdateNewRowPolicy::APPEND: {
322
0
                _partial_update_new_row_policy = PartialUpdateNewRowPolicyPB::APPEND;
323
0
                break;
324
0
            }
325
0
            case doris::TPartialUpdateNewRowPolicy::ERROR: {
326
0
                _partial_update_new_row_policy = PartialUpdateNewRowPolicyPB::ERROR;
327
0
                break;
328
0
            }
329
0
            default: {
330
0
                return Status::InvalidArgument(
331
0
                        "Unknown partial_update_new_key_behavior: {}, should be one of "
332
0
                        "'APPEND' or 'ERROR'",
333
0
                        tschema.partial_update_new_key_policy);
334
0
            }
335
0
            }
336
0
        }
337
0
    }
338
339
32
    for (const auto& tcolumn : tschema.partial_update_input_columns) {
340
0
        _partial_update_input_columns.insert(tcolumn);
341
0
    }
342
32
    std::unordered_map<std::string, SlotDescriptor*> slots_map;
343
32
    _tuple_desc = _obj_pool.add(new TupleDescriptor(tschema.tuple_desc));
344
120
    for (const auto& t_slot_desc : tschema.slot_descs) {
345
120
        auto* slot_desc = _obj_pool.add(new SlotDescriptor(t_slot_desc));
346
120
        _tuple_desc->add_slot(slot_desc);
347
120
        std::string is_null_str = slot_desc->is_nullable() ? "true" : "false";
348
120
        std::string data_type_str = std::to_string(int64_t(slot_desc->col_type()));
349
120
        slots_map.emplace(to_lower(slot_desc->col_name()) + "+" + data_type_str + is_null_str,
350
120
                          slot_desc);
351
120
    }
352
353
48
    for (const auto& t_index : tschema.indexes) {
354
48
        std::unordered_map<std::string, int32_t> index_slots_map;
355
48
        auto* index = _obj_pool.add(new OlapTableIndexSchema());
356
48
        index->index_id = t_index.id;
357
48
        index->schema_hash = t_index.schema_hash;
358
48
        if (t_index.__isset.row_binlog_id) {
359
5
            index->row_binlog_id = t_index.row_binlog_id;
360
5
        }
361
48
        for (const auto& tcolumn_desc : t_index.columns_desc) {
362
15
            if (_unique_key_update_mode != UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS ||
363
15
                _partial_update_input_columns.contains(tcolumn_desc.column_name)) {
364
15
                std::string is_null_str = tcolumn_desc.is_allow_null ? "true" : "false";
365
15
                std::string data_type_str =
366
15
                        std::to_string(int64_t(thrift_to_type(tcolumn_desc.column_type.type)));
367
15
                auto it = slots_map.find(to_lower(tcolumn_desc.column_name) + "+" + data_type_str +
368
15
                                         is_null_str);
369
15
                if (it == slots_map.end()) {
370
0
                    std::stringstream ss;
371
0
                    ss << tschema;
372
0
                    std::string keys {};
373
0
                    for (const auto& [key, _] : slots_map) {
374
0
                        keys += fmt::format("{},", key);
375
0
                    }
376
0
                    LOG_EVERY_SECOND(WARNING) << fmt::format(
377
0
                            "[OlapTableSchemaParam::init(const TOlapTableSchemaParam& tschema)]: "
378
0
                            "unknown index column, column={}, type={}, data_type_str={}, "
379
0
                            "is_null_str={}, slots_map.keys()=[{}], {}\ntschema={}",
380
0
                            tcolumn_desc.column_name, tcolumn_desc.column_type.type, data_type_str,
381
0
                            is_null_str, keys, debug_string(), ss.str());
382
0
                    return Status::InternalError("unknown index column, column={}, type={}",
383
0
                                                 tcolumn_desc.column_name,
384
0
                                                 tcolumn_desc.column_type.type);
385
0
                }
386
15
                index->slots.emplace_back(it->second);
387
15
            }
388
15
            index_slots_map.emplace(to_lower(tcolumn_desc.column_name), tcolumn_desc.col_unique_id);
389
15
            TabletColumn* tc = _obj_pool.add(new TabletColumn());
390
15
            tc->init_from_thrift(tcolumn_desc);
391
15
            index->columns.emplace_back(tc);
392
15
        }
393
48
        if (t_index.__isset.indexes_desc) {
394
0
            for (const auto& tindex_desc : t_index.indexes_desc) {
395
0
                std::vector<int32_t> column_unique_ids(tindex_desc.columns.size());
396
0
                for (size_t i = 0; i < tindex_desc.columns.size(); i++) {
397
0
                    auto it = index_slots_map.find(to_lower(tindex_desc.columns[i]));
398
0
                    if (it != index_slots_map.end()) {
399
0
                        column_unique_ids[i] = it->second;
400
0
                    }
401
0
                }
402
0
                TabletIndex* ti = _obj_pool.add(new TabletIndex());
403
0
                ti->init_from_thrift(tindex_desc, column_unique_ids);
404
0
                index->indexes.emplace_back(ti);
405
0
            }
406
0
        }
407
48
        if (t_index.__isset.where_clause) {
408
0
            RETURN_IF_ERROR(VExpr::create_expr_tree(t_index.where_clause, index->where_clause));
409
0
        }
410
48
        _indexes.emplace_back(index);
411
48
    }
412
413
32
    if (tschema.__isset.row_binlog_index_schemas) {
414
5
        for (const auto& t_index : tschema.row_binlog_index_schemas) {
415
5
            auto* index = _obj_pool.add(new OlapTableIndexSchema());
416
5
            index->index_id = t_index.id;
417
5
            index->schema_hash = t_index.schema_hash;
418
5
            if (t_index.__isset.row_binlog_id) {
419
0
                index->row_binlog_id = t_index.row_binlog_id;
420
0
            }
421
19
            for (const auto& tcolumn_desc : t_index.columns_desc) {
422
19
                TabletColumn* tc = _obj_pool.add(new TabletColumn());
423
19
                tc->init_from_thrift(tcolumn_desc);
424
19
                index->columns.emplace_back(tc);
425
19
            }
426
5
            _row_binlog_index_schemas.emplace_back(index);
427
5
        }
428
5
    }
429
430
32
    std::sort(_indexes.begin(), _indexes.end(),
431
32
              [](const OlapTableIndexSchema* lhs, const OlapTableIndexSchema* rhs) {
432
32
                  return lhs->index_id < rhs->index_id;
433
32
              });
434
32
    return Status::OK();
435
32
}
436
437
16
void OlapTableSchemaParam::to_protobuf(POlapTableSchemaParam* pschema) const {
438
16
    pschema->set_db_id(_db_id);
439
16
    pschema->set_table_id(_table_id);
440
16
    pschema->set_version(_version);
441
16
    pschema->set_unique_key_update_mode(_unique_key_update_mode);
442
16
    if (_unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS) {
443
        // for backward compatibility
444
0
        pschema->set_partial_update(true);
445
0
    }
446
16
    pschema->set_partial_update_new_key_policy(_partial_update_new_row_policy);
447
16
    pschema->set_is_strict_mode(_is_strict_mode);
448
16
    pschema->set_auto_increment_column(_auto_increment_column);
449
16
    pschema->set_auto_increment_column_unique_id(_auto_increment_column_unique_id);
450
16
    pschema->set_timestamp_ms(_timestamp_ms);
451
16
    pschema->set_timezone(_timezone);
452
16
    pschema->set_nano_seconds(_nano_seconds);
453
16
    pschema->set_sequence_map_col_unique_id(_sequence_map_col_uid);
454
16
    for (auto col : _partial_update_input_columns) {
455
0
        *pschema->add_partial_update_input_columns() = col;
456
0
    }
457
16
    _tuple_desc->to_protobuf(pschema->mutable_tuple_desc());
458
96
    for (auto* slot : _tuple_desc->slots()) {
459
96
        slot->to_protobuf(pschema->add_slot_descs());
460
96
    }
461
32
    for (auto* index : _indexes) {
462
32
        index->to_protobuf(pschema->add_indexes());
463
32
    }
464
16
    for (auto* index : _row_binlog_index_schemas) {
465
0
        index->to_protobuf(pschema->add_row_binlog_index_schemas());
466
0
    }
467
16
}
468
469
0
std::string OlapTableSchemaParam::debug_string() const {
470
0
    std::stringstream ss;
471
0
    ss << "tuple_desc=" << _tuple_desc->debug_string();
472
0
    return ss.str();
473
0
}
474
475
VOlapTablePartitionParam::VOlapTablePartitionParam(std::shared_ptr<OlapTableSchemaParam>& schema,
476
                                                   const TOlapTablePartitionParam& t_param)
477
11
        : _schema(schema),
478
11
          _t_param(t_param),
479
11
          _slots(_schema->tuple_desc()->slots()),
480
11
          _mem_tracker(std::make_unique<MemTracker>("OlapTablePartitionParam")),
481
11
          _part_type(t_param.partition_type) {
482
11
    if (t_param.__isset.enable_automatic_partition && t_param.enable_automatic_partition) {
483
2
        _is_auto_partition = true;
484
2
        auto size = t_param.partition_function_exprs.size();
485
2
        _part_func_ctx.resize(size);
486
2
        _partition_function.resize(size);
487
2
        DCHECK((t_param.partition_type == TPartitionType::RANGE_PARTITIONED && size == 1) ||
488
0
               (t_param.partition_type == TPartitionType::LIST_PARTITIONED && size >= 1))
489
0
                << "now support only 1 partition column for auto range partitions. "
490
0
                << t_param.partition_type << " " << size;
491
4
        for (int i = 0; i < size; ++i) {
492
2
            Status st =
493
2
                    VExpr::create_expr_tree(t_param.partition_function_exprs[i], _part_func_ctx[i]);
494
2
            if (!st.ok()) {
495
0
                throw Exception(Status::InternalError("Partition function expr is not valid"),
496
0
                                "Partition function expr is not valid");
497
0
            }
498
2
            _partition_function[i] = _part_func_ctx[i]->root();
499
2
        }
500
2
    }
501
502
11
    if (t_param.__isset.enable_auto_detect_overwrite && t_param.enable_auto_detect_overwrite) {
503
1
        _is_auto_detect_overwrite = true;
504
1
        DCHECK(t_param.__isset.overwrite_group_id);
505
1
        _overwrite_group_id = t_param.overwrite_group_id;
506
1
    }
507
508
11
    if (t_param.__isset.master_address) {
509
0
        _master_address = std::make_shared<TNetworkAddress>(t_param.master_address);
510
0
    }
511
512
11
    if (_is_auto_partition) {
513
        // the nullable mode depends on partition_exprs. not column slots. so use them.
514
2
        DCHECK(_partition_function.size() <= _slots.size())
515
0
                << _partition_function.size() << ", " << _slots.size();
516
517
        // suppose (k0, [k1], [k2]), so get [k1, 0], [k2, 1]
518
2
        std::map<std::string, int> partition_slots_map; // name to idx in part_exprs
519
4
        for (size_t i = 0; i < t_param.partition_columns.size(); i++) {
520
2
            partition_slots_map.emplace(t_param.partition_columns[i], i);
521
2
        }
522
523
        // here we rely on the same order and number of the _part_funcs and _slots in the prefix
524
        // _part_block contains all slots of table.
525
2
        for (auto* slot : _slots) {
526
            // try to replace with partition expr.
527
2
            if (auto it = partition_slots_map.find(slot->col_name());
528
2
                it != partition_slots_map.end()) { // it's a partition column slot
529
2
                auto& expr_type = _partition_function[it->second]->data_type();
530
2
                _partition_block.insert({expr_type->create_column(), expr_type, slot->col_name()});
531
2
            } else {
532
0
                _partition_block.insert({slot->get_empty_mutable_column(),
533
0
                                         slot->get_data_type_ptr(), slot->col_name()});
534
0
            }
535
2
        }
536
2
        VLOG_TRACE << _partition_block.dump_structure();
537
9
    } else {
538
        // we insert all. but not all will be used. it will controlled by _partition_slot_locs
539
9
        for (auto* slot : _slots) {
540
9
            _partition_block.insert({slot->get_empty_mutable_column(), slot->get_data_type_ptr(),
541
9
                                     slot->col_name()});
542
9
        }
543
9
    }
544
11
}
545
546
11
VOlapTablePartitionParam::~VOlapTablePartitionParam() {
547
11
    _mem_tracker->release(_mem_usage);
548
11
}
549
550
11
Status VOlapTablePartitionParam::init() {
551
11
    std::vector<std::string> slot_column_names;
552
11
    for (auto* slot_desc : _schema->tuple_desc()->slots()) {
553
11
        slot_column_names.emplace_back(slot_desc->col_name());
554
11
    }
555
556
11
    auto find_slot_locs = [&slot_column_names](const std::string& slot_name,
557
11
                                               std::vector<uint16_t>& locs,
558
21
                                               const std::string& column_type) {
559
21
        auto it = std::find(slot_column_names.begin(), slot_column_names.end(), slot_name);
560
21
        if (it == slot_column_names.end()) {
561
0
            return Status::InternalError("{} column not found, column ={}", column_type, slot_name);
562
0
        }
563
21
        locs.emplace_back(it - slot_column_names.begin());
564
21
        return Status::OK();
565
21
    };
566
567
    // here we find the partition columns. others maybe non-partition columns/special columns.
568
11
    if (_t_param.__isset.partition_columns) {
569
11
        for (auto& part_col : _t_param.partition_columns) {
570
11
            RETURN_IF_ERROR(find_slot_locs(part_col, _partition_slot_locs, "partition"));
571
11
        }
572
11
    }
573
574
11
    _partitions_map = std::make_unique<
575
11
            std::map<BlockRowWithIndicator, VOlapTablePartition*, VOlapTablePartKeyComparator>>(
576
11
            VOlapTablePartKeyComparator(_partition_slot_locs, _transformed_slot_locs));
577
11
    if (_t_param.__isset.distributed_columns) {
578
10
        for (auto& col : _t_param.distributed_columns) {
579
10
            RETURN_IF_ERROR(find_slot_locs(col, _distributed_slot_locs, "distributed"));
580
10
        }
581
10
    }
582
583
    // for both auto/non-auto partition table.
584
11
    _is_in_partition = _part_type == TPartitionType::type::LIST_PARTITIONED;
585
586
    // initial partitions. if meet dummy partitions only for open BE nodes, not generate key of them for finding
587
21
    for (const auto& t_part : _t_param.partitions) {
588
21
        VOlapTablePartition* part = nullptr;
589
21
        RETURN_IF_ERROR(generate_partition_from(t_part, part));
590
21
        _partitions.emplace_back(part);
591
592
21
        if (!_t_param.partitions_is_fake) {
593
21
            if (_is_in_partition) {
594
0
                for (auto& in_key : part->in_keys) {
595
0
                    _partitions_map->emplace(std::tuple {in_key.first, in_key.second, false}, part);
596
0
                }
597
21
            } else {
598
21
                _partitions_map->emplace(
599
21
                        std::tuple {part->end_key.first, part->end_key.second, false}, part);
600
21
            }
601
21
        }
602
21
    }
603
604
11
    _mem_usage = _partition_block.allocated_bytes();
605
11
    _mem_tracker->consume(_mem_usage);
606
11
    return Status::OK();
607
11
}
608
609
bool VOlapTablePartitionParam::_part_contains(VOlapTablePartition* part,
610
30
                                              BlockRowWithIndicator key) const {
611
30
    VOlapTablePartKeyComparator comparator(_partition_slot_locs, _transformed_slot_locs);
612
    // we have used upper_bound to find to ensure key < part.right and this part is closest(right - key is min)
613
    // now we only have to check (key >= part.left). the comparator(a,b) means a < b, so we use anti
614
30
    return part->start_key.second == -1 /* spj: start_key.second == -1 means only single partition*/
615
30
           || !comparator(key, std::tuple {part->start_key.first, part->start_key.second, false});
616
30
}
617
618
// insert value into _partition_block's column
619
// NOLINTBEGIN(readability-function-size)
620
46
static Status _create_partition_key(const TExprNode& t_expr, BlockRow* part_key, uint16_t pos) {
621
46
    auto column = std::move(*part_key->first->get_by_position(pos).column).mutate();
622
46
    switch (t_expr.node_type) {
623
0
    case TExprNodeType::DATE_LITERAL: {
624
0
        auto primitive_type =
625
0
                DataTypeFactory::instance().create_data_type(t_expr.type)->get_primitive_type();
626
0
        if (primitive_type == TYPE_DATEV2) {
627
0
            DateV2Value<DateV2ValueType> dt;
628
0
            CastParameters params;
629
0
            if (!CastToDateV2::from_string_strict_mode<DatelikeParseMode::STRICT>(
630
0
                        {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt,
631
0
                        nullptr, params)) {
632
0
                std::stringstream ss;
633
0
                ss << "invalid date literal in partition column, date=" << t_expr.date_literal;
634
0
                return Status::InternalError(ss.str());
635
0
            }
636
0
            column->insert_data(reinterpret_cast<const char*>(&dt), 0);
637
0
        } else if (primitive_type == TYPE_DATETIMEV2) {
638
0
            DateV2Value<DateTimeV2ValueType> dt;
639
0
            const int32_t scale =
640
0
                    t_expr.type.types.empty() ? -1 : t_expr.type.types.front().scalar_type.scale;
641
0
            CastParameters params;
642
0
            if (!CastToDatetimeV2::from_string_strict_mode<DatelikeParseMode::STRICT>(
643
0
                        {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt,
644
0
                        nullptr, scale, params)) {
645
0
                std::stringstream ss;
646
0
                ss << "invalid date literal in partition column, date=" << t_expr.date_literal;
647
0
                return Status::InternalError(ss.str());
648
0
            }
649
0
            column->insert_data(reinterpret_cast<const char*>(&dt), 0);
650
0
        } else if (primitive_type == TYPE_TIMESTAMPTZ) {
651
0
            TimestampTzValue res;
652
0
            CastParameters params {.status = Status::OK(), .is_strict = true};
653
0
            const int32_t scale =
654
0
                    t_expr.type.types.empty() ? -1 : t_expr.type.types.front().scalar_type.scale;
655
0
            if (!CastToTimestampTz::from_string(
656
0
                        {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, res,
657
0
                        params, nullptr, scale)) [[unlikely]] {
658
0
                std::stringstream ss;
659
0
                ss << "invalid timestamptz literal in partition column, value="
660
0
                   << t_expr.date_literal;
661
0
                return Status::InternalError(ss.str());
662
0
            }
663
0
            column->insert_data(reinterpret_cast<const char*>(&res), 0);
664
0
        } else {
665
0
            VecDateTimeValue dt;
666
0
            CastParameters params;
667
0
            if (!CastToDateOrDatetime::from_string_strict_mode<DatelikeParseMode::STRICT,
668
0
                                                               DatelikeTargetType::DATE_TIME>(
669
0
                        {t_expr.date_literal.value.c_str(), t_expr.date_literal.value.size()}, dt,
670
0
                        nullptr, params)) {
671
0
                std::stringstream ss;
672
0
                ss << "invalid date literal in partition column, date=" << t_expr.date_literal;
673
0
                return Status::InternalError(ss.str());
674
0
            }
675
0
            if (primitive_type == TYPE_DATE) {
676
0
                dt.cast_to_date();
677
0
            }
678
0
            column->insert_data(reinterpret_cast<const char*>(&dt), 0);
679
0
        }
680
0
        break;
681
0
    }
682
46
    case TExprNodeType::INT_LITERAL: {
683
46
        switch (t_expr.type.types[0].scalar_type.type) {
684
0
        case TPrimitiveType::TINYINT: {
685
0
            auto value = cast_set<int8_t>(t_expr.int_literal.value);
686
0
            column->insert_data(reinterpret_cast<const char*>(&value), 0);
687
0
            break;
688
0
        }
689
0
        case TPrimitiveType::SMALLINT: {
690
0
            auto value = cast_set<int16_t>(t_expr.int_literal.value);
691
0
            column->insert_data(reinterpret_cast<const char*>(&value), 0);
692
0
            break;
693
0
        }
694
46
        case TPrimitiveType::INT: {
695
46
            auto value = cast_set<int32_t>(t_expr.int_literal.value);
696
46
            column->insert_data(reinterpret_cast<const char*>(&value), 0);
697
46
            break;
698
0
        }
699
0
        default:
700
0
            int64_t value = t_expr.int_literal.value;
701
0
            column->insert_data(reinterpret_cast<const char*>(&value), 0);
702
46
        }
703
46
        break;
704
46
    }
705
46
    case TExprNodeType::LARGE_INT_LITERAL: {
706
0
        StringParser::ParseResult parse_result = StringParser::PARSE_SUCCESS;
707
0
        auto value = StringParser::string_to_int<__int128>(t_expr.large_int_literal.value.c_str(),
708
0
                                                           t_expr.large_int_literal.value.size(),
709
0
                                                           &parse_result);
710
0
        if (parse_result != StringParser::PARSE_SUCCESS) {
711
0
            value = MAX_INT128;
712
0
        }
713
0
        column->insert_data(reinterpret_cast<const char*>(&value), 0);
714
0
        break;
715
46
    }
716
0
    case TExprNodeType::STRING_LITERAL: {
717
0
        size_t len = t_expr.string_literal.value.size();
718
0
        const char* str_val = t_expr.string_literal.value.c_str();
719
0
        column->insert_data(str_val, len);
720
0
        break;
721
46
    }
722
0
    case TExprNodeType::BOOL_LITERAL: {
723
0
        column->insert_data(reinterpret_cast<const char*>(&t_expr.bool_literal.value), 0);
724
0
        break;
725
46
    }
726
0
    case TExprNodeType::NULL_LITERAL: {
727
        // insert a null literal
728
0
        if (!column->is_nullable()) {
729
            // https://github.com/apache/doris/pull/39449 have forbid this cause. always add this check as protective measures
730
0
            return Status::InternalError("The column {} is not null, can't insert into NULL value.",
731
0
                                         part_key->first->get_by_position(pos).name);
732
0
        }
733
0
        column->insert_data(nullptr, 0);
734
0
        break;
735
0
    }
736
0
    default: {
737
0
        return Status::InternalError("unsupported partition column node type, type={}",
738
0
                                     t_expr.node_type);
739
0
    }
740
46
    }
741
46
    part_key->second = cast_set<int32_t>(column->size() - 1);
742
46
    return Status::OK();
743
46
}
744
// NOLINTEND(readability-function-size)
745
746
Status VOlapTablePartitionParam::_create_partition_keys(const std::vector<TExprNode>& t_exprs,
747
46
                                                        BlockRow* part_key) {
748
92
    for (int i = 0; i < t_exprs.size(); i++) {
749
46
        RETURN_IF_ERROR(_create_partition_key(t_exprs[i], part_key, _partition_slot_locs[i]));
750
46
    }
751
46
    return Status::OK();
752
46
}
753
754
Status VOlapTablePartitionParam::generate_partition_from(const TOlapTablePartition& t_part,
755
21
                                                         VOlapTablePartition*& part_result) {
756
21
    DCHECK(part_result == nullptr);
757
    // here we set the default value of partition bounds first! if it doesn't have some key, it will be -1.
758
21
    part_result = _obj_pool.add(new VOlapTablePartition(&_partition_block));
759
21
    part_result->id = t_part.id;
760
21
    part_result->is_mutable = t_part.is_mutable;
761
21
    if (t_part.__isset.load_tablet_idx) {
762
1
        part_result->load_tablet_idx = t_part.load_tablet_idx;
763
1
    }
764
21
    if (t_part.__isset.bucket_be_id) {
765
0
        part_result->bucket_be_id = t_part.bucket_be_id;
766
0
    }
767
21
    if (t_part.__isset.local_bucket_seqs) {
768
0
        part_result->local_bucket_seqs = t_part.local_bucket_seqs;
769
0
    }
770
771
21
    if (_is_in_partition) {
772
0
        for (const auto& keys : t_part.in_keys) {
773
0
            RETURN_IF_ERROR(_create_partition_keys(
774
0
                    keys, &part_result->in_keys.emplace_back(&_partition_block, -1)));
775
0
        }
776
0
        if (t_part.__isset.is_default_partition && t_part.is_default_partition &&
777
0
            _default_partition == nullptr) {
778
0
            _default_partition = part_result;
779
0
        }
780
21
    } else { // range
781
21
        if (t_part.__isset.start_keys) {
782
21
            RETURN_IF_ERROR(_create_partition_keys(t_part.start_keys, &part_result->start_key));
783
21
        }
784
        // we generate the right bound but not insert into partition map
785
21
        if (t_part.__isset.end_keys) {
786
21
            RETURN_IF_ERROR(_create_partition_keys(t_part.end_keys, &part_result->end_key));
787
21
        }
788
21
    }
789
790
21
    part_result->num_buckets = t_part.num_buckets;
791
21
    auto num_indexes = _schema->indexes().size();
792
21
    if (t_part.indexes.size() != num_indexes) {
793
0
        return Status::InternalError(
794
0
                "number of partition's index is not equal with schema's"
795
0
                ", num_part_indexes={}, num_schema_indexes={}",
796
0
                t_part.indexes.size(), num_indexes);
797
0
    }
798
21
    part_result->indexes = t_part.indexes;
799
21
    std::sort(part_result->indexes.begin(), part_result->indexes.end(),
800
21
              [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) {
801
0
                  return lhs.index_id < rhs.index_id;
802
0
              });
803
    // check index
804
42
    for (int j = 0; j < num_indexes; ++j) {
805
21
        if (part_result->indexes[j].index_id != _schema->indexes()[j]->index_id) {
806
0
            return Status::InternalError(
807
0
                    "partition's index is not equal with schema's"
808
0
                    ", part_index={}, schema_index={}",
809
0
                    part_result->indexes[j].index_id, _schema->indexes()[j]->index_id);
810
0
        }
811
21
    }
812
21
    if (t_part.__isset.total_replica_num) {
813
0
        part_result->total_replica_num = t_part.total_replica_num;
814
0
    }
815
21
    if (t_part.__isset.load_required_replica_num) {
816
0
        part_result->load_required_replica_num = t_part.load_required_replica_num;
817
0
    }
818
21
    if (t_part.__isset.tablet_version_gap_backends) {
819
0
        for (const auto& [tablet_id, backend_ids] : t_part.tablet_version_gap_backends) {
820
0
            auto& gap_set = part_result->tablet_version_gap_backends[tablet_id];
821
0
            for (auto backend_id : backend_ids) {
822
0
                gap_set.insert(backend_id);
823
0
            }
824
0
        }
825
0
    }
826
21
    return Status::OK();
827
21
}
828
829
Status VOlapTablePartitionParam::add_partitions(
830
2
        const std::vector<TOlapTablePartition>& partitions) {
831
2
    for (const auto& t_part : partitions) {
832
2
        auto* part = _obj_pool.add(new VOlapTablePartition(&_partition_block));
833
2
        part->id = t_part.id;
834
2
        part->is_mutable = t_part.is_mutable;
835
836
        // we dont pass right keys when it's MAX_VALUE. so there's possibility we only have start_key but not end_key
837
        // range partition
838
2
        if (t_part.__isset.start_keys) {
839
2
            RETURN_IF_ERROR(_create_partition_keys(t_part.start_keys, &part->start_key));
840
2
        }
841
2
        if (t_part.__isset.end_keys) {
842
2
            RETURN_IF_ERROR(_create_partition_keys(t_part.end_keys, &part->end_key));
843
2
        }
844
        // list partition - we only set 1 value in 1 partition for new created ones
845
2
        if (t_part.__isset.in_keys) {
846
0
            for (const auto& keys : t_part.in_keys) {
847
0
                RETURN_IF_ERROR(_create_partition_keys(
848
0
                        keys, &part->in_keys.emplace_back(&_partition_block, -1)));
849
0
            }
850
0
            if (t_part.__isset.is_default_partition && t_part.is_default_partition) {
851
0
                _default_partition = part;
852
0
            }
853
0
        }
854
855
2
        part->num_buckets = t_part.num_buckets;
856
2
        if (t_part.__isset.load_tablet_idx) {
857
0
            part->load_tablet_idx = t_part.load_tablet_idx;
858
0
        }
859
2
        if (t_part.__isset.bucket_be_id) {
860
0
            part->bucket_be_id = t_part.bucket_be_id;
861
0
        }
862
2
        if (t_part.__isset.local_bucket_seqs) {
863
0
            part->local_bucket_seqs = t_part.local_bucket_seqs;
864
0
        }
865
2
        auto num_indexes = _schema->indexes().size();
866
2
        if (t_part.indexes.size() != num_indexes) {
867
0
            return Status::InternalError(
868
0
                    "number of partition's index is not equal with schema's"
869
0
                    ", num_part_indexes={}, num_schema_indexes={}",
870
0
                    t_part.indexes.size(), num_indexes);
871
0
        }
872
2
        part->indexes = t_part.indexes;
873
2
        std::sort(part->indexes.begin(), part->indexes.end(),
874
2
                  [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) {
875
0
                      return lhs.index_id < rhs.index_id;
876
0
                  });
877
        // check index
878
4
        for (int j = 0; j < num_indexes; ++j) {
879
2
            if (part->indexes[j].index_id != _schema->indexes()[j]->index_id) {
880
0
                return Status::InternalError(
881
0
                        "partition's index is not equal with schema's"
882
0
                        ", part_index={}, schema_index={}",
883
0
                        part->indexes[j].index_id, _schema->indexes()[j]->index_id);
884
0
            }
885
2
        }
886
2
        _partitions.emplace_back(part);
887
        // after _creating_partiton_keys
888
2
        if (_is_in_partition) {
889
0
            for (auto& in_key : part->in_keys) {
890
0
                _partitions_map->emplace(std::tuple {in_key.first, in_key.second, false}, part);
891
0
            }
892
2
        } else {
893
2
            _partitions_map->emplace(std::tuple {part->end_key.first, part->end_key.second, false},
894
2
                                     part);
895
2
        }
896
2
    }
897
898
2
    return Status::OK();
899
2
}
900
901
Status VOlapTablePartitionParam::replace_partitions(
902
        std::vector<int64_t>& old_partition_ids,
903
1
        const std::vector<TOlapTablePartition>& new_partitions) {
904
    // remove old replaced partitions
905
1
    DCHECK(old_partition_ids.size() == new_partitions.size());
906
907
    // init and add new partitions. insert into _partitions
908
3
    for (int i = 0; i < new_partitions.size(); i++) {
909
2
        const auto& t_part = new_partitions[i];
910
        // pair old_partition_ids and new_partitions one by one. TODO: sort to opt performance
911
2
        VOlapTablePartition* old_part = nullptr;
912
2
        auto old_part_id = old_partition_ids[i];
913
2
        if (auto it = std::find_if(
914
2
                    _partitions.begin(), _partitions.end(),
915
3
                    [=](const VOlapTablePartition* lhs) { return lhs->id == old_part_id; });
916
2
            it != _partitions.end()) {
917
2
            old_part = *it;
918
2
        } else {
919
0
            return Status::InternalError("Cannot find old tablet {} in replacing", old_part_id);
920
0
        }
921
922
2
        auto* part = _obj_pool.add(new VOlapTablePartition(&_partition_block));
923
2
        part->id = t_part.id;
924
2
        part->is_mutable = t_part.is_mutable;
925
926
        /// just substitute directly. no need to remove and reinsert keys.
927
        // range partition
928
2
        part->start_key = std::move(old_part->start_key);
929
2
        part->end_key = std::move(old_part->end_key);
930
        // list partition
931
2
        part->in_keys = std::move(old_part->in_keys);
932
2
        if (t_part.__isset.is_default_partition && t_part.is_default_partition) {
933
0
            _default_partition = part;
934
0
        }
935
936
2
        part->num_buckets = t_part.num_buckets;
937
2
        if (t_part.__isset.load_tablet_idx) {
938
0
            part->load_tablet_idx = t_part.load_tablet_idx;
939
0
        }
940
2
        if (t_part.__isset.bucket_be_id) {
941
0
            part->bucket_be_id = t_part.bucket_be_id;
942
0
        }
943
2
        if (t_part.__isset.local_bucket_seqs) {
944
0
            part->local_bucket_seqs = t_part.local_bucket_seqs;
945
0
        }
946
2
        auto num_indexes = _schema->indexes().size();
947
2
        if (t_part.indexes.size() != num_indexes) {
948
0
            return Status::InternalError(
949
0
                    "number of partition's index is not equal with schema's"
950
0
                    ", num_part_indexes={}, num_schema_indexes={}",
951
0
                    t_part.indexes.size(), num_indexes);
952
0
        }
953
2
        part->indexes = t_part.indexes;
954
2
        std::sort(part->indexes.begin(), part->indexes.end(),
955
2
                  [](const OlapTableIndexTablets& lhs, const OlapTableIndexTablets& rhs) {
956
0
                      return lhs.index_id < rhs.index_id;
957
0
                  });
958
        // check index
959
4
        for (int j = 0; j < num_indexes; ++j) {
960
2
            if (part->indexes[j].index_id != _schema->indexes()[j]->index_id) {
961
0
                return Status::InternalError(
962
0
                        "partition's index is not equal with schema's"
963
0
                        ", part_index={}, schema_index={}",
964
0
                        part->indexes[j].index_id, _schema->indexes()[j]->index_id);
965
0
            }
966
2
        }
967
968
        // add new partitions with new id.
969
2
        _partitions.emplace_back(part);
970
2
        VLOG_NOTICE << "params add new partition " << part->id;
971
972
        // replace items in _partition_maps
973
2
        if (_is_in_partition) {
974
0
            for (auto& in_key : part->in_keys) {
975
0
                (*_partitions_map)[std::tuple {in_key.first, in_key.second, false}] = part;
976
0
            }
977
2
        } else {
978
2
            (*_partitions_map)[std::tuple {part->end_key.first, part->end_key.second, false}] =
979
2
                    part;
980
2
        }
981
2
    }
982
    // remove old partitions by id
983
1
    std::ranges::sort(old_partition_ids);
984
5
    for (auto it = _partitions.begin(); it != _partitions.end();) {
985
4
        if (std::ranges::binary_search(old_partition_ids, (*it)->id)) {
986
2
            it = _partitions.erase(it);
987
2
        } else {
988
2
            it++;
989
2
        }
990
4
    }
991
992
1
    return Status::OK();
993
1
}
994
995
} // namespace doris