Coverage Report

Created: 2026-08-02 04:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_schema.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/tablet_schema.h"
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/olap_file.pb.h>
22
#include <glog/logging.h>
23
#include <google/protobuf/io/coded_stream.h>
24
#include <google/protobuf/io/zero_copy_stream.h>
25
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
26
27
#include <algorithm>
28
#include <cctype>
29
// IWYU pragma: no_include <bits/std_abs.h>
30
#include <cmath> // IWYU pragma: keep
31
#include <memory>
32
#include <ostream>
33
#include <vector>
34
35
#include "common/compiler_util.h" // IWYU pragma: keep
36
#include "common/consts.h"
37
#include "common/status.h"
38
#include "core/block/block.h"
39
#include "core/column/column_nothing.h"
40
#include "core/data_type/data_type.h"
41
#include "core/data_type/data_type_factory.hpp"
42
#include "core/string_ref.h"
43
#include "exec/common/hex.h"
44
#include "exprs/aggregate/aggregate_function_simple_factory.h"
45
#include "exprs/aggregate/aggregate_function_state_union.h"
46
#include "storage/index/index_writer.h" // IndexColumnWriter::check_support_*_index
47
#include "storage/index/inverted/analyzer/analyzer.h"
48
#include "storage/index/inverted/inverted_index_parser.h"
49
#include "storage/olap_common.h"
50
#include "storage/olap_define.h"
51
#include "storage/tablet/tablet_column_object_pool.h"
52
#include "storage/tablet/tablet_meta.h"
53
#include "storage/tablet_info.h"
54
#include "storage/types.h"
55
#include "storage/utils.h"
56
#include "util/json/path_in_data.h"
57
58
namespace doris {
59
12.2k
FieldType TabletColumn::get_field_type_by_string(const std::string& type_str) {
60
12.2k
    std::string upper_type_str = type_str;
61
12.2k
    std::transform(type_str.begin(), type_str.end(), upper_type_str.begin(),
62
69.0k
                   [](auto c) { return std::toupper(c); });
63
12.2k
    FieldType type;
64
65
12.2k
    if (0 == upper_type_str.compare("TINYINT")) {
66
419
        type = FieldType::OLAP_FIELD_TYPE_TINYINT;
67
11.8k
    } else if (0 == upper_type_str.compare("SMALLINT")) {
68
524
        type = FieldType::OLAP_FIELD_TYPE_SMALLINT;
69
11.3k
    } else if (0 == upper_type_str.compare("INT")) {
70
2.26k
        type = FieldType::OLAP_FIELD_TYPE_INT;
71
9.08k
    } else if (0 == upper_type_str.compare("BIGINT")) {
72
213
        type = FieldType::OLAP_FIELD_TYPE_BIGINT;
73
8.87k
    } else if (0 == upper_type_str.compare("LARGEINT")) {
74
111
        type = FieldType::OLAP_FIELD_TYPE_LARGEINT;
75
8.76k
    } else if (0 == upper_type_str.compare("UNSIGNED_TINYINT")) {
76
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT;
77
8.76k
    } else if (0 == upper_type_str.compare("UNSIGNED_SMALLINT")) {
78
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT;
79
8.76k
    } else if (0 == upper_type_str.compare("UNSIGNED_INT")) {
80
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT;
81
8.76k
    } else if (0 == upper_type_str.compare("UNSIGNED_BIGINT")) {
82
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_BIGINT;
83
8.76k
    } else if (0 == upper_type_str.compare("IPV4")) {
84
17
        type = FieldType::OLAP_FIELD_TYPE_IPV4;
85
8.74k
    } else if (0 == upper_type_str.compare("IPV6")) {
86
17
        type = FieldType::OLAP_FIELD_TYPE_IPV6;
87
8.72k
    } else if (0 == upper_type_str.compare("FLOAT")) {
88
4
        type = FieldType::OLAP_FIELD_TYPE_FLOAT;
89
8.72k
    } else if (0 == upper_type_str.compare("DISCRETE_DOUBLE")) {
90
0
        type = FieldType::OLAP_FIELD_TYPE_DISCRETE_DOUBLE;
91
8.72k
    } else if (0 == upper_type_str.compare("DOUBLE")) {
92
5
        type = FieldType::OLAP_FIELD_TYPE_DOUBLE;
93
8.71k
    } else if (0 == upper_type_str.compare("CHAR")) {
94
112
        type = FieldType::OLAP_FIELD_TYPE_CHAR;
95
8.60k
    } else if (0 == upper_type_str.compare("DATE")) {
96
114
        type = FieldType::OLAP_FIELD_TYPE_DATE;
97
8.49k
    } else if (0 == upper_type_str.compare("DATEV2")) {
98
106
        type = FieldType::OLAP_FIELD_TYPE_DATEV2;
99
8.38k
    } else if (0 == upper_type_str.compare("DATETIMEV2")) {
100
6
        type = FieldType::OLAP_FIELD_TYPE_DATETIMEV2;
101
8.37k
    } else if (0 == upper_type_str.compare("DATETIMEV2_NANO")) {
102
0
        type = FieldType::OLAP_FIELD_TYPE_DATETIMEV2_NANO;
103
8.37k
    } else if (0 == upper_type_str.compare("DATETIME")) {
104
137
        type = FieldType::OLAP_FIELD_TYPE_DATETIME;
105
8.24k
    } else if (0 == upper_type_str.compare("TIMESTAMPTZ")) {
106
16
        type = FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ;
107
8.22k
    } else if (0 == upper_type_str.compare("DECIMAL32")) {
108
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL32;
109
8.22k
    } else if (0 == upper_type_str.compare("DECIMAL64")) {
110
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL64;
111
8.22k
    } else if (0 == upper_type_str.compare("DECIMAL128I")) {
112
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL128I;
113
8.22k
    } else if (0 == upper_type_str.compare("DECIMAL256")) {
114
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL256;
115
8.22k
    } else if (0 == upper_type_str.compare(0, 7, "DECIMAL")) {
116
        // Keep this generic prefix match after all specific DECIMAL types; otherwise DECIMAL32,
117
        // DECIMAL64, DECIMAL128I, and DECIMAL256 would all be classified as DECIMAL.
118
112
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL;
119
8.11k
    } else if (0 == upper_type_str.compare(0, 7, "VARCHAR")) {
120
167
        type = FieldType::OLAP_FIELD_TYPE_VARCHAR;
121
7.94k
    } else if (0 == upper_type_str.compare("STRING")) {
122
7.54k
        type = FieldType::OLAP_FIELD_TYPE_STRING;
123
7.54k
    } else if (0 == upper_type_str.compare("JSONB")) {
124
0
        type = FieldType::OLAP_FIELD_TYPE_JSONB;
125
403
    } else if (0 == upper_type_str.compare("VARIANT")) {
126
288
        type = FieldType::OLAP_FIELD_TYPE_VARIANT;
127
288
    } else if (0 == upper_type_str.compare("BOOLEAN")) {
128
5
        type = FieldType::OLAP_FIELD_TYPE_BOOL;
129
110
    } else if (0 == upper_type_str.compare(0, 3, "HLL")) {
130
4
        type = FieldType::OLAP_FIELD_TYPE_HLL;
131
106
    } else if (0 == upper_type_str.compare("STRUCT")) {
132
0
        type = FieldType::OLAP_FIELD_TYPE_STRUCT;
133
106
    } else if (0 == upper_type_str.compare("LIST")) {
134
0
        type = FieldType::OLAP_FIELD_TYPE_ARRAY;
135
106
    } else if (0 == upper_type_str.compare("MAP")) {
136
59
        type = FieldType::OLAP_FIELD_TYPE_MAP;
137
59
    } else if (0 == upper_type_str.compare("OBJECT")) {
138
0
        type = FieldType::OLAP_FIELD_TYPE_BITMAP;
139
47
    } else if (0 == upper_type_str.compare("BITMAP")) {
140
0
        type = FieldType::OLAP_FIELD_TYPE_BITMAP;
141
47
    } else if (0 == upper_type_str.compare("ARRAY")) {
142
47
        type = FieldType::OLAP_FIELD_TYPE_ARRAY;
143
47
    } else if (0 == upper_type_str.compare("QUANTILE_STATE")) {
144
0
        type = FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE;
145
0
    } else if (0 == upper_type_str.compare("AGG_STATE")) {
146
0
        type = FieldType::OLAP_FIELD_TYPE_AGG_STATE;
147
0
    } else {
148
0
        LOG(WARNING) << "invalid type string. [type='" << type_str << "']";
149
0
        type = FieldType::OLAP_FIELD_TYPE_UNKNOWN;
150
0
    }
151
152
12.2k
    return type;
153
12.2k
}
154
155
3.19k
FieldAggregationMethod TabletColumn::get_aggregation_type_by_string(const std::string& str) {
156
3.19k
    std::string upper_str = str;
157
3.19k
    std::transform(str.begin(), str.end(), upper_str.begin(),
158
15.2k
                   [](auto c) { return std::toupper(c); });
159
3.19k
    FieldAggregationMethod aggregation_type;
160
161
3.19k
    if (0 == upper_str.compare("NONE")) {
162
1.66k
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE;
163
1.66k
    } else if (0 == upper_str.compare("SUM")) {
164
540
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM;
165
993
    } else if (0 == upper_str.compare("MIN")) {
166
1
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN;
167
992
    } else if (0 == upper_str.compare("MAX")) {
168
1
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX;
169
991
    } else if (0 == upper_str.compare("REPLACE")) {
170
987
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE;
171
987
    } else if (0 == upper_str.compare("REPLACE_IF_NOT_NULL")) {
172
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL;
173
4
    } else if (0 == upper_str.compare("HLL_UNION")) {
174
4
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION;
175
4
    } else if (0 == upper_str.compare("BITMAP_UNION")) {
176
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION;
177
0
    } else if (0 == upper_str.compare("QUANTILE_UNION")) {
178
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION;
179
0
    } else if (!upper_str.empty()) {
180
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC;
181
0
    } else {
182
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN;
183
0
    }
184
185
3.19k
    return aggregation_type;
186
3.19k
}
187
188
31.7k
std::string TabletColumn::get_string_by_field_type(FieldType type) {
189
31.7k
    switch (type) {
190
1.75k
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
191
1.75k
        return "TINYINT";
192
193
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT:
194
0
        return "UNSIGNED_TINYINT";
195
196
2.51k
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
197
2.51k
        return "SMALLINT";
198
199
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT:
200
0
        return "UNSIGNED_SMALLINT";
201
202
7.62k
    case FieldType::OLAP_FIELD_TYPE_INT:
203
7.62k
        return "INT";
204
205
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT:
206
0
        return "UNSIGNED_INT";
207
208
1.15k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
209
1.15k
        return "BIGINT";
210
211
755
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
212
755
        return "LARGEINT";
213
214
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_BIGINT:
215
0
        return "UNSIGNED_BIGINT";
216
217
147
    case FieldType::OLAP_FIELD_TYPE_IPV4:
218
147
        return "IPV4";
219
220
147
    case FieldType::OLAP_FIELD_TYPE_IPV6:
221
147
        return "IPV6";
222
223
10
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
224
10
        return "FLOAT";
225
226
8
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
227
8
        return "DOUBLE";
228
229
0
    case FieldType::OLAP_FIELD_TYPE_DISCRETE_DOUBLE:
230
0
        return "DISCRETE_DOUBLE";
231
232
755
    case FieldType::OLAP_FIELD_TYPE_CHAR:
233
755
        return "CHAR";
234
235
759
    case FieldType::OLAP_FIELD_TYPE_DATE:
236
759
        return "DATE";
237
238
684
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
239
684
        return "DATEV2";
240
241
955
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
242
955
        return "DATETIME";
243
244
8
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
245
8
        return "DATETIMEV2";
246
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2_NANO:
247
0
        return "DATETIMEV2_NANO";
248
249
142
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
250
142
        return "TIMESTAMPTZ";
251
252
755
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
253
755
        return "DECIMAL";
254
255
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
256
0
        return "DECIMAL32";
257
258
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
259
0
        return "DECIMAL64";
260
261
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
262
0
        return "DECIMAL128I";
263
264
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
265
0
        return "DECIMAL256";
266
267
1.01k
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
268
1.01k
        return "VARCHAR";
269
270
0
    case FieldType::OLAP_FIELD_TYPE_JSONB:
271
0
        return "JSONB";
272
273
699
    case FieldType::OLAP_FIELD_TYPE_VARIANT:
274
699
        return "VARIANT";
275
276
11.6k
    case FieldType::OLAP_FIELD_TYPE_STRING:
277
11.6k
        return "STRING";
278
279
8
    case FieldType::OLAP_FIELD_TYPE_BOOL:
280
8
        return "BOOLEAN";
281
282
6
    case FieldType::OLAP_FIELD_TYPE_HLL:
283
6
        return "HLL";
284
285
0
    case FieldType::OLAP_FIELD_TYPE_STRUCT:
286
0
        return "STRUCT";
287
288
138
    case FieldType::OLAP_FIELD_TYPE_ARRAY:
289
138
        return "ARRAY";
290
291
103
    case FieldType::OLAP_FIELD_TYPE_MAP:
292
103
        return "MAP";
293
294
0
    case FieldType::OLAP_FIELD_TYPE_BITMAP:
295
0
        return "OBJECT";
296
0
    case FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE:
297
0
        return "QUANTILE_STATE";
298
0
    case FieldType::OLAP_FIELD_TYPE_AGG_STATE:
299
0
        return "AGG_STATE";
300
0
    default:
301
0
        return "UNKNOWN";
302
31.7k
    }
303
31.7k
}
304
305
2.42k
std::string TabletColumn::get_string_by_aggregation_type(FieldAggregationMethod type) {
306
2.42k
    switch (type) {
307
2.39k
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE:
308
2.39k
        return "NONE";
309
310
9
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM:
311
9
        return "SUM";
312
313
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN:
314
0
        return "MIN";
315
316
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX:
317
0
        return "MAX";
318
319
15
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE:
320
15
        return "REPLACE";
321
322
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL:
323
0
        return "REPLACE_IF_NOT_NULL";
324
325
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION:
326
0
        return "HLL_UNION";
327
328
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION:
329
0
        return "BITMAP_UNION";
330
331
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION:
332
0
        return "QUANTILE_UNION";
333
334
2
    default:
335
2
        return "UNKNOWN";
336
2.42k
    }
337
2.42k
}
338
339
2.01k
uint32_t TabletColumn::get_field_length_by_type(TPrimitiveType::type type, uint32_t string_length) {
340
2.01k
    switch (type) {
341
128
    case TPrimitiveType::TINYINT:
342
128
    case TPrimitiveType::BOOLEAN:
343
128
        return 1;
344
263
    case TPrimitiveType::SMALLINT:
345
263
        return 2;
346
676
    case TPrimitiveType::INT:
347
676
        return 4;
348
156
    case TPrimitiveType::BIGINT:
349
156
        return 8;
350
104
    case TPrimitiveType::LARGEINT:
351
104
        return 16;
352
16
    case TPrimitiveType::IPV4:
353
16
        return 4;
354
16
    case TPrimitiveType::IPV6:
355
16
        return 16;
356
104
    case TPrimitiveType::DATE:
357
104
        return 3;
358
92
    case TPrimitiveType::DATEV2:
359
92
        return 4;
360
112
    case TPrimitiveType::DATETIME:
361
112
        return 8;
362
0
    case TPrimitiveType::DATETIMEV2:
363
15
    case TPrimitiveType::TIMESTAMPTZ:
364
15
        return 8;
365
0
    case TPrimitiveType::FLOAT:
366
0
        return 4;
367
0
    case TPrimitiveType::DOUBLE:
368
0
        return 8;
369
0
    case TPrimitiveType::QUANTILE_STATE:
370
0
    case TPrimitiveType::BITMAP:
371
0
        return 16;
372
104
    case TPrimitiveType::CHAR:
373
104
        return string_length;
374
104
    case TPrimitiveType::VARCHAR:
375
104
    case TPrimitiveType::HLL:
376
104
    case TPrimitiveType::AGG_STATE:
377
104
        return string_length + sizeof(OLAP_VARCHAR_MAX_LENGTH);
378
5
    case TPrimitiveType::STRING:
379
5
    case TPrimitiveType::VARIANT:
380
5
        return string_length + sizeof(OLAP_STRING_MAX_LENGTH);
381
0
    case TPrimitiveType::JSONB:
382
0
        return string_length + sizeof(OLAP_JSONB_MAX_LENGTH);
383
0
    case TPrimitiveType::STRUCT:
384
        // Note that(xy): this is the length of struct type itself,
385
        // the length of its subtypes are not included.
386
0
        return OLAP_STRUCT_MAX_LENGTH;
387
19
    case TPrimitiveType::ARRAY:
388
19
        return OLAP_ARRAY_MAX_LENGTH;
389
0
    case TPrimitiveType::MAP:
390
0
        return OLAP_MAP_MAX_LENGTH;
391
0
    case TPrimitiveType::DECIMAL32:
392
0
        return 4;
393
0
    case TPrimitiveType::DECIMAL64:
394
0
        return 8;
395
0
    case TPrimitiveType::DECIMAL128I:
396
0
        return 16;
397
0
    case TPrimitiveType::DECIMAL256:
398
0
        return 32;
399
104
    case TPrimitiveType::DECIMALV2:
400
104
        return 12; // use 12 bytes in olap engine.
401
0
    default:
402
0
        LOG(WARNING) << "unknown field type. [type=" << type << "]";
403
0
        return 0;
404
2.01k
    }
405
2.01k
}
406
407
9
bool TabletColumn::has_char_type() const {
408
9
    switch (_type) {
409
3
    case FieldType::OLAP_FIELD_TYPE_CHAR: {
410
3
        return true;
411
0
    }
412
4
    case FieldType::OLAP_FIELD_TYPE_ARRAY:
413
4
    case FieldType::OLAP_FIELD_TYPE_MAP:
414
4
    case FieldType::OLAP_FIELD_TYPE_STRUCT: {
415
4
        return std::any_of(_sub_columns.begin(), _sub_columns.end(),
416
4
                           [&](const auto& sub) -> bool { return sub->has_char_type(); });
417
4
    }
418
2
    default:
419
2
        return false;
420
9
    }
421
9
}
422
423
24.6k
TabletColumn::TabletColumn() : _aggregation(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE) {}
424
425
21
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType type) {
426
21
    _aggregation = agg;
427
21
    _type = type;
428
21
}
429
430
33
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable) {
431
33
    _aggregation = agg;
432
33
    _type = filed_type;
433
33
    _length = cast_set<int32_t>(field_type_size(filed_type));
434
33
    _is_nullable = is_nullable;
435
33
}
436
437
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable,
438
500
                           int32_t unique_id, size_t length) {
439
500
    _aggregation = agg;
440
500
    _type = filed_type;
441
500
    _is_nullable = is_nullable;
442
500
    _unique_id = unique_id;
443
500
    _length = cast_set<int32_t>(length);
444
500
}
445
446
0
TabletColumn::TabletColumn(const ColumnPB& column) {
447
0
    init_from_pb(column);
448
0
}
449
450
4
TabletColumn::TabletColumn(const TColumn& column) {
451
4
    init_from_thrift(column);
452
4
}
453
454
32
void TabletColumn::init_from_thrift(const TColumn& tcolumn) {
455
32
    ColumnPB column_pb;
456
32
    TabletMeta::init_column_from_tcolumn(tcolumn.col_unique_id, tcolumn, &column_pb);
457
32
    init_from_pb(column_pb);
458
32
}
459
460
12.2k
void TabletColumn::init_from_pb(const ColumnPB& column) {
461
12.2k
    _unique_id = column.unique_id();
462
12.2k
    _col_name = column.name();
463
12.2k
    _col_name_lower_case = to_lower(_col_name);
464
12.2k
    _type = TabletColumn::get_field_type_by_string(column.type());
465
12.2k
    _is_key = column.is_key();
466
12.2k
    _is_nullable = column.is_nullable();
467
12.2k
    _is_auto_increment = column.is_auto_increment();
468
12.2k
    if (column.has_is_on_update_current_timestamp()) {
469
11.0k
        _is_on_update_current_timestamp = column.is_on_update_current_timestamp();
470
11.0k
    }
471
472
12.2k
    _has_default_value = column.has_default_value();
473
12.2k
    if (_has_default_value) {
474
89
        _default_value = column.default_value();
475
89
    }
476
477
12.2k
    if (column.has_precision()) {
478
2.89k
        _is_decimal = true;
479
2.89k
        _precision = column.precision();
480
9.39k
    } else {
481
9.39k
        _is_decimal = false;
482
9.39k
    }
483
12.2k
    if (column.has_frac()) {
484
2.89k
        _frac = column.frac();
485
2.89k
    }
486
12.2k
    if (_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2 && _frac > 6) {
487
0
        _type = FieldType::OLAP_FIELD_TYPE_DATETIMEV2_NANO;
488
0
    }
489
12.2k
    _length = column.length();
490
12.2k
    _index_length = column.index_length();
491
12.2k
    if (column.has_is_bf_column()) {
492
603
        _is_bf_column = column.is_bf_column();
493
11.6k
    } else {
494
11.6k
        _is_bf_column = false;
495
11.6k
    }
496
12.2k
    if (column.has_aggregation()) {
497
3.19k
        _aggregation = get_aggregation_type_by_string(column.aggregation());
498
3.19k
        _aggregation_name = column.aggregation();
499
3.19k
    }
500
501
12.2k
    if (_type == FieldType::OLAP_FIELD_TYPE_AGG_STATE) {
502
0
        _result_is_nullable = column.result_is_nullable();
503
0
        _be_exec_version = column.be_exec_version();
504
0
    }
505
506
12.2k
    if (column.has_visible()) {
507
11.0k
        _visible = column.visible();
508
11.0k
    }
509
12.2k
    if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
510
47
        CHECK(column.children_columns_size() == 1)
511
0
                << "ARRAY type should has 1 children types, but got "
512
0
                << column.children_columns_size();
513
47
    }
514
12.2k
    if (_type == FieldType::OLAP_FIELD_TYPE_MAP) {
515
59
        DCHECK(column.children_columns_size() == 2)
516
0
                << "MAP type should has 2 children types, but got "
517
0
                << column.children_columns_size();
518
59
        if (UNLIKELY(column.children_columns_size() != 2)) {
519
0
            LOG(WARNING) << "MAP type should has 2 children types, but got "
520
0
                         << column.children_columns_size();
521
0
        }
522
59
    }
523
12.5k
    for (int i = 0; i < column.children_columns_size(); i++) {
524
278
        TabletColumn child_column;
525
278
        child_column.init_from_pb(column.children_columns(i));
526
278
        add_sub_column(child_column);
527
278
    }
528
12.2k
    if (column.has_column_path_info()) {
529
403
        _column_path = std::make_shared<PathInData>();
530
403
        _column_path->from_protobuf(column.column_path_info());
531
403
        _parent_col_unique_id = column.column_path_info().parrent_column_unique_id();
532
403
    }
533
12.2k
    if (is_variant_type() && !column.has_column_path_info()) {
534
        // set path info for variant root column, to prevent from missing
535
124
        _column_path = std::make_shared<PathInData>(_col_name_lower_case);
536
        // _parent_col_unique_id = _unique_id;
537
124
    }
538
12.2k
    if (column.has_variant_max_subcolumns_count()) {
539
11.1k
        _variant.max_subcolumns_count = column.variant_max_subcolumns_count();
540
11.1k
    }
541
12.2k
    if (column.has_variant_enable_typed_paths_to_sparse()) {
542
11.0k
        _variant.enable_typed_paths_to_sparse = column.variant_enable_typed_paths_to_sparse();
543
11.0k
    }
544
12.2k
    if (column.has_variant_max_sparse_column_statistics_size()) {
545
11.1k
        _variant.max_sparse_column_statistics_size =
546
11.1k
                column.variant_max_sparse_column_statistics_size();
547
11.1k
    }
548
12.2k
    if (column.has_variant_sparse_hash_shard_count()) {
549
9.13k
        _variant.sparse_hash_shard_count = column.variant_sparse_hash_shard_count();
550
9.13k
    }
551
12.2k
    if (column.has_variant_enable_doc_mode()) {
552
11.1k
        _variant.enable_doc_mode = column.variant_enable_doc_mode();
553
11.1k
    }
554
12.2k
    if (column.has_variant_doc_materialization_min_rows()) {
555
9.13k
        _variant.doc_materialization_min_rows = column.variant_doc_materialization_min_rows();
556
9.13k
    }
557
12.2k
    if (column.has_variant_doc_hash_shard_count()) {
558
9.05k
        _variant.doc_hash_shard_count = column.variant_doc_hash_shard_count();
559
9.05k
    }
560
12.2k
    if (column.has_variant_enable_nested_group()) {
561
9.14k
        _variant.enable_nested_group = column.variant_enable_nested_group();
562
9.14k
    }
563
12.2k
    if (column.has_pattern_type()) {
564
9.09k
        _pattern_type = column.pattern_type();
565
9.09k
    }
566
12.2k
}
567
568
TabletColumn TabletColumn::create_materialized_variant_column(const std::string& root,
569
                                                              const std::vector<std::string>& paths,
570
                                                              int32_t parent_unique_id,
571
                                                              int32_t max_subcolumns_count,
572
1
                                                              bool enable_doc_mode) {
573
1
    TabletColumn subcol;
574
1
    subcol.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
575
1
    subcol.set_is_nullable(true);
576
1
    subcol.set_unique_id(-1);
577
1
    subcol.set_parent_unique_id(parent_unique_id);
578
1
    PathInData path(root, paths);
579
1
    subcol.set_path_info(path);
580
1
    subcol.set_name(path.get_path());
581
1
    subcol.set_variant_max_subcolumns_count(max_subcolumns_count);
582
1
    subcol.set_variant_enable_doc_mode(enable_doc_mode);
583
1
    return subcol;
584
1
}
585
586
31.6k
void TabletColumn::to_schema_pb(ColumnPB* column) const {
587
31.6k
    column->set_unique_id(_unique_id);
588
31.6k
    column->set_name(_col_name);
589
31.6k
    column->set_type(get_string_by_field_type(_type));
590
31.6k
    column->set_is_key(_is_key);
591
31.6k
    column->set_is_nullable(_is_nullable);
592
31.6k
    column->set_is_auto_increment(_is_auto_increment);
593
31.6k
    column->set_is_on_update_current_timestamp(_is_on_update_current_timestamp);
594
31.6k
    if (_has_default_value) {
595
265
        column->set_default_value(_default_value);
596
265
    }
597
31.6k
    if (_is_decimal) {
598
14.4k
        column->set_precision(_precision);
599
14.4k
        column->set_frac(_frac);
600
14.4k
    }
601
31.6k
    column->set_length(_length);
602
31.6k
    column->set_index_length(_index_length);
603
31.6k
    if (_is_bf_column) {
604
31
        column->set_is_bf_column(_is_bf_column);
605
31
    }
606
31.6k
    if (!_aggregation_name.empty()) {
607
15.0k
        column->set_aggregation(_aggregation_name);
608
15.0k
    }
609
31.6k
    column->set_result_is_nullable(_result_is_nullable);
610
31.6k
    column->set_be_exec_version(_be_exec_version);
611
31.6k
    column->set_visible(_visible);
612
613
31.6k
    if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
614
131
        CHECK(_sub_columns.size() == 1)
615
0
                << "ARRAY type should has 1 children types, but got " << _sub_columns.size();
616
131
    }
617
31.6k
    if (_type == FieldType::OLAP_FIELD_TYPE_MAP) {
618
101
        DCHECK(_sub_columns.size() == 2)
619
0
                << "MAP type should has 2 children types, but got " << _sub_columns.size();
620
101
        if (UNLIKELY(_sub_columns.size() != 2)) {
621
0
            LOG(WARNING) << "MAP type should has 2 children types, but got " << _sub_columns.size();
622
0
        }
623
101
    }
624
625
32.4k
    for (size_t i = 0; i < _sub_columns.size(); i++) {
626
773
        ColumnPB* child = column->add_children_columns();
627
773
        _sub_columns[i]->to_schema_pb(child);
628
773
    }
629
630
    // set parts info
631
31.6k
    if (has_path_info()) {
632
        // CHECK_GT(_parent_col_unique_id, 0);
633
1.08k
        _column_path->to_protobuf(column->mutable_column_path_info(), _parent_col_unique_id);
634
        // Update unstable information for variant columns. Some of the fields in the tablet schema
635
        // are irrelevant for variant sub-columns, but retaining them may lead to an excessive growth
636
        // in the number of tablet schema cache entries.
637
1.08k
        if (_type == FieldType::OLAP_FIELD_TYPE_STRING) {
638
154
            column->set_length(INT_MAX);
639
154
        }
640
1.08k
        column->set_index_length(0);
641
1.08k
    }
642
31.6k
    column->set_variant_max_subcolumns_count(_variant.max_subcolumns_count);
643
31.6k
    column->set_pattern_type(_pattern_type);
644
31.6k
    column->set_variant_enable_typed_paths_to_sparse(_variant.enable_typed_paths_to_sparse);
645
31.6k
    column->set_variant_max_sparse_column_statistics_size(
646
31.6k
            _variant.max_sparse_column_statistics_size);
647
31.6k
    column->set_variant_sparse_hash_shard_count(_variant.sparse_hash_shard_count);
648
31.6k
    column->set_variant_enable_doc_mode(_variant.enable_doc_mode);
649
31.6k
    column->set_variant_doc_materialization_min_rows(_variant.doc_materialization_min_rows);
650
31.6k
    column->set_variant_doc_hash_shard_count(_variant.doc_hash_shard_count);
651
31.6k
    column->set_variant_enable_nested_group(_variant.enable_nested_group);
652
31.6k
}
653
654
2.21k
void TabletColumn::add_sub_column(TabletColumn& sub_column) {
655
2.21k
    _sub_columns.push_back(std::make_shared<TabletColumn>(sub_column));
656
2.21k
    sub_column._parent_col_unique_id = this->_unique_id;
657
2.21k
    _sub_column_count += 1;
658
2.21k
}
659
660
34.0k
bool TabletColumn::is_row_store_column() const {
661
34.0k
    return _col_name == BeConsts::ROW_STORE_COL;
662
34.0k
}
663
664
AggregateFunctionPtr TabletColumn::get_aggregate_function_union(DataTypePtr type,
665
0
                                                                int current_be_exec_version) const {
666
0
    const auto* state_type = assert_cast<const DataTypeAggState*>(type.get());
667
0
    BeExecVersionManager::check_function_compatibility(
668
0
            current_be_exec_version, _be_exec_version,
669
0
            state_type->get_nested_function()->get_name());
670
0
    return AggregateStateUnion::create(state_type->get_nested_function(), {type}, type);
671
0
}
672
673
AggregateFunctionPtr TabletColumn::get_aggregate_function(std::string suffix,
674
24
                                                          int current_be_exec_version) const {
675
24
    AggregateFunctionPtr function = nullptr;
676
677
24
    auto type = DataTypeFactory::instance().create_data_type(*this);
678
24
    if (type && type->get_primitive_type() == PrimitiveType::TYPE_AGG_STATE) {
679
0
        function = get_aggregate_function_union(type, current_be_exec_version);
680
24
    } else {
681
24
        std::string origin_name = TabletColumn::get_string_by_aggregation_type(_aggregation);
682
24
        std::string agg_name = origin_name + suffix;
683
24
        std::transform(agg_name.begin(), agg_name.end(), agg_name.begin(),
684
258
                       [](unsigned char c) { return std::tolower(c); });
685
24
        function = AggregateFunctionSimpleFactory::instance().get(
686
24
                agg_name, {type}, type, type->is_nullable(),
687
24
                BeExecVersionManager::get_newest_version());
688
24
        if (!function) {
689
0
            LOG(WARNING) << "get column aggregate function failed, aggregation_name=" << origin_name
690
0
                         << ", column_type=" << type->get_name();
691
0
        }
692
24
    }
693
24
    if (function) {
694
24
        function->set_version(_be_exec_version);
695
24
        return function;
696
24
    }
697
0
    return nullptr;
698
24
}
699
700
2.49k
void TabletColumn::set_path_info(const PathInData& path) {
701
2.49k
    _column_path = std::make_shared<PathInData>(path);
702
2.49k
}
703
704
187
DataTypePtr TabletColumn::get_vec_type() const {
705
187
    return DataTypeFactory::instance().create_data_type(*this);
706
187
}
707
708
// escape '.' and '_'
709
41.8k
std::string escape_for_path_name(const std::string& s) {
710
41.8k
    std::string res;
711
41.8k
    const char* pos = s.data();
712
41.8k
    const char* end = pos + s.size();
713
46.7k
    while (pos != end) {
714
4.89k
        unsigned char c = *pos;
715
4.89k
        if (c == '.' || c == '_') {
716
638
            res += '%';
717
638
            res += hex_digit_uppercase(c / 16);
718
638
            res += hex_digit_uppercase(c % 16);
719
4.25k
        } else {
720
4.25k
            res += c;
721
4.25k
        }
722
4.89k
        ++pos;
723
4.89k
    }
724
41.8k
    return res;
725
41.8k
}
726
727
197
void TabletIndex::set_escaped_escaped_index_suffix_path(const std::string& path_name) {
728
197
    std::string escaped_path = escape_for_path_name(path_name);
729
197
    _escaped_index_suffix_path = escaped_path;
730
197
}
731
732
void TabletIndex::init_from_thrift(const TOlapTableIndex& index,
733
20
                                   const TabletSchema& tablet_schema) {
734
20
    _index_id = index.index_id;
735
20
    _index_name = index.index_name;
736
    // init col_unique_id in index at be side, since col_unique_id may be -1 at fe side
737
    // get column unique id by name
738
20
    std::vector<int32_t> col_unique_ids(index.columns.size());
739
40
    for (size_t i = 0; i < index.columns.size(); i++) {
740
20
        auto column_idx = tablet_schema.field_index(index.columns[i]);
741
20
        if (column_idx >= 0) {
742
16
            col_unique_ids[i] = tablet_schema.column(column_idx).unique_id();
743
16
        } else {
744
            // if column unique id not found by column name, find by column unique id
745
            // column unique id can not found means this column is a new column added by light schema change
746
4
            if (index.__isset.column_unique_ids && !index.column_unique_ids.empty() &&
747
4
                tablet_schema.has_column_unique_id(index.column_unique_ids[i])) {
748
1
                col_unique_ids[i] = index.column_unique_ids[i];
749
3
            } else {
750
3
                col_unique_ids[i] = -1;
751
3
            }
752
4
        }
753
20
    }
754
20
    _col_unique_ids = std::move(col_unique_ids);
755
756
20
    switch (index.index_type) {
757
0
    case TIndexType::BITMAP:
758
0
        _index_type = IndexType::BITMAP;
759
0
        break;
760
19
    case TIndexType::INVERTED:
761
19
        _index_type = IndexType::INVERTED;
762
19
        break;
763
1
    case TIndexType::ANN:
764
1
        _index_type = IndexType::ANN;
765
1
        break;
766
0
    case TIndexType::BLOOMFILTER:
767
0
        _index_type = IndexType::BLOOMFILTER;
768
0
        break;
769
0
    case TIndexType::NGRAM_BF:
770
0
        _index_type = IndexType::NGRAM_BF;
771
0
        break;
772
20
    }
773
20
    if (index.__isset.properties) {
774
4
        for (auto kv : index.properties) {
775
4
            _properties[kv.first] = kv.second;
776
4
        }
777
1
    }
778
20
}
779
780
void TabletIndex::init_from_thrift(const TOlapTableIndex& index,
781
1
                                   const std::vector<int32_t>& column_uids) {
782
1
    _index_id = index.index_id;
783
1
    _index_name = index.index_name;
784
1
    _col_unique_ids = column_uids;
785
786
1
    switch (index.index_type) {
787
0
    case TIndexType::BITMAP:
788
0
        _index_type = IndexType::BITMAP;
789
0
        break;
790
1
    case TIndexType::INVERTED:
791
1
        _index_type = IndexType::INVERTED;
792
1
        break;
793
0
    case TIndexType::ANN:
794
0
        _index_type = IndexType::ANN;
795
0
        break;
796
0
    case TIndexType::BLOOMFILTER:
797
0
        _index_type = IndexType::BLOOMFILTER;
798
0
        break;
799
0
    case TIndexType::NGRAM_BF:
800
0
        _index_type = IndexType::NGRAM_BF;
801
0
        break;
802
1
    }
803
1
    if (index.__isset.properties) {
804
3
        for (auto kv : index.properties) {
805
3
            _properties[kv.first] = kv.second;
806
3
        }
807
1
    }
808
1
}
809
810
7.82k
void TabletIndex::init_from_pb(const TabletIndexPB& index) {
811
7.82k
    _index_id = index.index_id();
812
7.82k
    _index_name = index.index_name();
813
7.82k
    _col_unique_ids.clear();
814
7.82k
    for (auto col_unique_id : index.col_unique_id()) {
815
7.67k
        _col_unique_ids.push_back(col_unique_id);
816
7.67k
    }
817
7.82k
    _index_type = index.index_type();
818
41.2k
    for (const auto& kv : index.properties()) {
819
41.2k
        _properties[kv.first] = kv.second;
820
41.2k
    }
821
7.82k
    _escaped_index_suffix_path = index.index_suffix_name();
822
7.82k
}
823
824
11.5k
void TabletIndex::to_schema_pb(TabletIndexPB* index) const {
825
11.5k
    index->set_index_id(_index_id);
826
11.5k
    index->set_index_name(_index_name);
827
11.5k
    index->clear_col_unique_id();
828
11.5k
    for (auto col_unique_id : _col_unique_ids) {
829
11.5k
        index->add_col_unique_id(col_unique_id);
830
11.5k
    }
831
11.5k
    index->set_index_type(_index_type);
832
61.5k
    for (const auto& kv : _properties) {
833
61.5k
        DBUG_EXECUTE_IF("tablet_schema.to_schema_pb", {
834
61.5k
            if (kv.first == INVERTED_INDEX_PARSER_LOWERCASE_KEY) {
835
61.5k
                continue;
836
61.5k
            }
837
61.5k
        })
838
61.5k
        (*index->mutable_properties())[kv.first] = kv.second;
839
61.5k
    }
840
11.5k
    index->set_index_suffix_name(_escaped_index_suffix_path);
841
842
11.5k
    DBUG_EXECUTE_IF("tablet_schema.to_schema_pb", { return; })
843
844
    // Only add lower_case=true default for built-in analyzers/parsers, NOT for custom analyzers
845
    // Custom analyzer: lower_case is determined by analyzer's internal token filter
846
11.5k
    if (!_properties.empty() && !_properties.contains(INVERTED_INDEX_PARSER_LOWERCASE_KEY)) {
847
279
        bool has_parser = _properties.contains(INVERTED_INDEX_PARSER_KEY) ||
848
279
                          _properties.contains(INVERTED_INDEX_PARSER_KEY_ALIAS);
849
279
        std::string analyzer_name = get_analyzer_name_from_properties(_properties);
850
279
        bool is_builtin = analyzer_name.empty() ||
851
279
                          segment_v2::inverted_index::InvertedIndexAnalyzer::is_builtin_analyzer(
852
0
                                  analyzer_name);
853
279
        if (has_parser || is_builtin) {
854
279
            (*index->mutable_properties())[INVERTED_INDEX_PARSER_LOWERCASE_KEY] =
855
279
                    INVERTED_INDEX_PARSER_TRUE;
856
279
        }
857
279
    }
858
11.5k
}
859
860
3.37k
TabletSchema::TabletSchema() = default;
861
862
3.18k
TabletSchema::~TabletSchema() {}
863
864
2.21k
int64_t TabletSchema::get_metadata_size() const {
865
2.21k
    return sizeof(TabletSchema);
866
2.21k
}
867
868
4.82k
void TabletSchema::append_column(TabletColumn column, ColumnType col_type) {
869
4.82k
    if (column.is_key()) {
870
450
        _num_key_columns++;
871
450
    }
872
4.82k
    if (column.is_nullable()) {
873
4.09k
        _num_null_columns++;
874
4.09k
    }
875
4.82k
    if (column.is_variant_type()) {
876
213
        ++_num_variant_columns;
877
213
        if (!column.has_path_info()) {
878
28
            const std::string& col_name = column.name_lower_case();
879
28
            PathInData path(col_name);
880
28
            column.set_path_info(path);
881
28
        }
882
213
    }
883
4.82k
    if (UNLIKELY(column.name() == DELETE_SIGN)) {
884
21
        _delete_sign_idx = _num_columns;
885
4.80k
    } else if (UNLIKELY(column.name() == SEQUENCE_COL)) {
886
18
        _sequence_col_idx = _num_columns;
887
4.78k
    } else if (UNLIKELY(column.name() == VERSION_COL)) {
888
0
        _version_col_idx = _num_columns;
889
4.78k
    } else if (UNLIKELY(column.name() == SKIP_BITMAP_COL)) {
890
0
        _skip_bitmap_col_idx = _num_columns;
891
4.78k
    } else if (UNLIKELY(column.name() == COMMIT_TSO_COL)) {
892
3
        _commit_tso_col_idx = _num_columns;
893
4.78k
    } else if (UNLIKELY(column.name() == BINLOG_TSO_COL)) {
894
0
        _binlog_tso_col_idx = _num_columns;
895
4.78k
    } else if (UNLIKELY(column.name() == BINLOG_LSN_COL)) {
896
0
        _binlog_lsn_col_idx = _num_columns;
897
4.78k
    } else if (UNLIKELY(column.name() == BINLOG_OP_COL)) {
898
0
        _binlog_op_col_idx = _num_columns;
899
4.78k
    } else if (UNLIKELY(column.name().starts_with(BeConsts::VIRTUAL_COLUMN_PREFIX))) {
900
0
        _vir_col_idx_to_unique_id[_num_columns] = column.unique_id();
901
0
    }
902
4.82k
    _field_uniqueid_to_index[column.unique_id()] = _num_columns;
903
4.82k
    _cols.push_back(std::make_shared<TabletColumn>(std::move(column)));
904
    // The dropped column may have same name with exsiting column, so that
905
    // not add to name to index map, only for uid to index map
906
4.82k
    if (col_type == ColumnType::VARIANT || _cols.back()->is_variant_type() ||
907
4.82k
        _cols.back()->is_extracted_column()) {
908
467
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
909
467
        _field_path_to_index[_cols.back()->path_info_ptr().get()] = _num_columns;
910
4.35k
    } else if (col_type == ColumnType::NORMAL) {
911
4.35k
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
912
4.35k
    }
913
4.82k
    _num_columns++;
914
4.82k
    _num_virtual_columns = _vir_col_idx_to_unique_id.size();
915
    // generate column index mapping for seq map
916
4.82k
    if (_seq_col_uid_to_value_cols_uid.contains(column.unique_id())) {
917
0
        const auto seq_idx = _field_uniqueid_to_index[column.unique_id()];
918
0
        if (!_seq_col_idx_to_value_cols_idx.contains(seq_idx)) {
919
0
            _seq_col_idx_to_value_cols_idx[seq_idx] = {};
920
0
        }
921
0
    }
922
4.82k
    if (_value_col_uid_to_seq_col_uid.contains(column.unique_id())) {
923
0
        const auto seq_uid = _value_col_uid_to_seq_col_uid[column.unique_id()];
924
0
        if (_field_uniqueid_to_index.contains(seq_uid)) {
925
0
            bool all_uid_index_found = true;
926
0
            std::vector<int32_t> value_cols_index;
927
0
            for (const auto value_col_uid : _seq_col_uid_to_value_cols_uid[seq_uid]) {
928
0
                if (!_field_uniqueid_to_index.contains(value_col_uid)) {
929
0
                    all_uid_index_found = false;
930
0
                    break;
931
0
                }
932
0
                value_cols_index.push_back(_field_uniqueid_to_index[value_col_uid]);
933
0
            }
934
0
            if (all_uid_index_found) {
935
0
                const auto seq_idx = _field_uniqueid_to_index[seq_uid];
936
0
                for (const auto col_idx : value_cols_index) {
937
0
                    _seq_col_idx_to_value_cols_idx[seq_idx].push_back(col_idx);
938
0
                    _value_col_idx_to_seq_col_idx[col_idx] = seq_idx;
939
0
                }
940
0
                _value_col_idx_to_seq_col_idx[seq_idx] = seq_idx;
941
0
            }
942
0
        }
943
0
    }
944
4.82k
}
945
946
97
void TabletSchema::append_index(TabletIndex&& index) {
947
97
    size_t index_pos = _indexes.size();
948
97
    _indexes.push_back(std::make_shared<TabletIndex>(index));
949
100
    for (int32_t id : _indexes.back()->col_unique_ids()) {
950
100
        if (auto field_pattern = _indexes.back()->field_pattern(); !field_pattern.empty()) {
951
9
            auto& pattern_to_index_map = _index_by_unique_id_with_pattern[id];
952
9
            pattern_to_index_map[field_pattern].emplace_back(_indexes.back());
953
91
        } else {
954
91
            IndexKey key = std::make_tuple(_indexes.back()->index_type(), id,
955
91
                                           _indexes.back()->get_index_suffix());
956
91
            _col_id_suffix_to_index[key].push_back(index_pos);
957
91
        }
958
100
    }
959
97
}
960
961
0
void TabletSchema::replace_column(size_t pos, TabletColumn new_col) {
962
0
    CHECK_LT(pos, num_columns()) << " outof range";
963
0
    _cols[pos] = std::make_shared<TabletColumn>(std::move(new_col));
964
0
}
965
966
3
void TabletSchema::clear_index() {
967
3
    _indexes.clear();
968
3
    _col_id_suffix_to_index.clear();
969
3
    _index_by_unique_id_with_pattern.clear();
970
3
}
971
972
9
void TabletSchema::remove_index(int64_t index_id) {
973
9
    std::vector<TabletIndexPtr> new_indexes;
974
13
    for (auto& index : _indexes) {
975
13
        if (index->index_id() != index_id) {
976
4
            new_indexes.emplace_back(std::move(index));
977
4
        }
978
13
    }
979
9
    _indexes = std::move(new_indexes);
980
9
    _col_id_suffix_to_index.clear();
981
9
    _index_by_unique_id_with_pattern.clear();
982
13
    for (size_t new_pos = 0; new_pos < _indexes.size(); ++new_pos) {
983
4
        const auto& index = _indexes[new_pos];
984
4
        for (int32_t col_uid : index->col_unique_ids()) {
985
4
            if (auto field_pattern = index->field_pattern(); !field_pattern.empty()) {
986
0
                auto& pattern_to_index_map = _index_by_unique_id_with_pattern[col_uid];
987
0
                pattern_to_index_map[field_pattern].emplace_back(index);
988
4
            } else {
989
4
                IndexKey key = std::make_tuple(_indexes.back()->index_type(), col_uid,
990
4
                                               _indexes.back()->get_index_suffix());
991
4
                _col_id_suffix_to_index[key].push_back(new_pos);
992
4
            }
993
4
        }
994
4
    }
995
9
}
996
997
1
void TabletSchema::clear_columns() {
998
1
    _field_path_to_index.clear();
999
1
    _field_name_to_index.clear();
1000
1
    _field_uniqueid_to_index.clear();
1001
1
    _num_columns = 0;
1002
1
    _num_variant_columns = 0;
1003
1
    _num_null_columns = 0;
1004
1
    _num_key_columns = 0;
1005
1
    _seq_col_idx_to_value_cols_idx.clear();
1006
1
    _value_col_idx_to_seq_col_idx.clear();
1007
1
    _cols.clear();
1008
1
}
1009
1010
void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
1011
2.02k
                                bool reuse_cache_column) {
1012
2.02k
    _keys_type = schema.keys_type();
1013
2.02k
    _num_columns = 0;
1014
2.02k
    _num_variant_columns = 0;
1015
2.02k
    _num_key_columns = 0;
1016
2.02k
    _num_null_columns = 0;
1017
2.02k
    _cols.clear();
1018
2.02k
    _indexes.clear();
1019
2.02k
    _index_by_unique_id_with_pattern.clear();
1020
2.02k
    _col_id_suffix_to_index.clear();
1021
2.02k
    _field_name_to_index.clear();
1022
2.02k
    _field_uniqueid_to_index.clear();
1023
2.02k
    _cluster_key_uids.clear();
1024
2.02k
    for (const auto& i : schema.cluster_key_uids()) {
1025
6
        _cluster_key_uids.push_back(i);
1026
6
    }
1027
12.3k
    for (auto& column_pb : schema.column()) {
1028
12.3k
        TabletColumnPtr column;
1029
12.3k
        if (reuse_cache_column) {
1030
671
            auto pair = TabletColumnObjectPool::instance()->insert(
1031
671
                    deterministic_string_serialize(column_pb));
1032
671
            column = pair.second;
1033
            // Release the handle quickly, because we use shared ptr to manage column.
1034
            // It often core during tablet schema copy to another schema because handle's
1035
            // reference count should be managed mannually.
1036
671
            TabletColumnObjectPool::instance()->release(pair.first);
1037
11.6k
        } else {
1038
11.6k
            column = std::make_shared<TabletColumn>();
1039
11.6k
            column->init_from_pb(column_pb);
1040
11.6k
        }
1041
12.3k
        if (ignore_extracted_columns && column->is_extracted_column()) {
1042
0
            continue;
1043
0
        }
1044
12.3k
        if (column->is_key()) {
1045
2.29k
            _num_key_columns++;
1046
2.29k
        }
1047
12.3k
        if (column->is_nullable()) {
1048
7.74k
            _num_null_columns++;
1049
7.74k
        }
1050
12.3k
        if (column->is_variant_type()) {
1051
349
            ++_num_variant_columns;
1052
349
        }
1053
1054
12.3k
        _cols.emplace_back(std::move(column));
1055
12.3k
        if (!_cols.back()->is_extracted_column()) {
1056
12.0k
            _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
1057
12.0k
            _field_uniqueid_to_index[_cols.back()->unique_id()] = _num_columns;
1058
12.0k
        }
1059
12.3k
        _num_columns++;
1060
12.3k
    }
1061
7.62k
    for (const auto& index_pb : schema.index()) {
1062
7.62k
        TabletIndexPtr index;
1063
7.62k
        if (reuse_cache_column) {
1064
232
            auto pair = TabletColumnObjectPool::instance()->insert_index(
1065
232
                    deterministic_string_serialize(index_pb));
1066
232
            index = pair.second;
1067
            //  Only need the value to be cached by the pool, release it quickly because the handle need
1068
            // record reference count mannually, or it will core during tablet schema copy method.
1069
232
            TabletColumnObjectPool::instance()->release(pair.first);
1070
7.38k
        } else {
1071
7.38k
            index = std::make_shared<TabletIndex>();
1072
7.38k
            index->init_from_pb(index_pb);
1073
7.38k
        }
1074
7.62k
        size_t index_pos = _indexes.size();
1075
7.62k
        _indexes.emplace_back(std::move(index));
1076
7.62k
        for (int32_t col_uid : _indexes.back()->col_unique_ids()) {
1077
7.61k
            if (auto field_pattern = _indexes.back()->field_pattern(); !field_pattern.empty()) {
1078
145
                auto& pattern_to_index_map = _index_by_unique_id_with_pattern[col_uid];
1079
145
                pattern_to_index_map[field_pattern].emplace_back(_indexes.back());
1080
7.47k
            } else {
1081
7.47k
                IndexKey key = std::make_tuple(_indexes.back()->index_type(), col_uid,
1082
7.47k
                                               _indexes.back()->get_index_suffix());
1083
7.47k
                _col_id_suffix_to_index[key].push_back(index_pos);
1084
7.47k
            }
1085
7.61k
        }
1086
7.62k
    }
1087
2.02k
    _num_short_key_columns = schema.num_short_key_columns();
1088
2.02k
    _num_rows_per_row_block = schema.num_rows_per_row_block();
1089
2.02k
    _compress_kind = schema.compress_kind();
1090
2.02k
    _next_column_unique_id = schema.next_column_unique_id();
1091
2.02k
    if (schema.has_bf_fpp()) {
1092
3
        _has_bf_fpp = true;
1093
3
        _bf_fpp = schema.bf_fpp();
1094
2.01k
    } else {
1095
2.01k
        _has_bf_fpp = false;
1096
2.01k
        _bf_fpp = BLOOM_FILTER_DEFAULT_FPP;
1097
2.01k
    }
1098
2.02k
    _is_in_memory = schema.is_in_memory();
1099
2.02k
    _disable_auto_compaction = schema.disable_auto_compaction();
1100
2.02k
    _store_row_column = schema.store_row_column();
1101
2.02k
    _skip_write_index_on_load = schema.skip_write_index_on_load();
1102
2.02k
    _delete_sign_idx = schema.delete_sign_idx();
1103
2.02k
    _sequence_col_idx = schema.sequence_col_idx();
1104
2.02k
    _version_col_idx = schema.version_col_idx();
1105
2.02k
    _skip_bitmap_col_idx = schema.skip_bitmap_col_idx();
1106
2.02k
    _commit_tso_col_idx = schema.commit_tso_col_idx();
1107
2.02k
    _binlog_tso_col_idx = schema.binlog_tso_col_idx();
1108
2.02k
    _binlog_lsn_col_idx = schema.binlog_lsn_col_idx();
1109
2.02k
    _binlog_op_col_idx = schema.binlog_op_col_idx();
1110
2.02k
    _sort_type = schema.sort_type();
1111
2.02k
    _sort_col_num = schema.sort_col_num();
1112
2.02k
    _compression_type = schema.compression_type();
1113
2.02k
    _row_store_page_size = schema.row_store_page_size();
1114
2.02k
    _storage_page_size = schema.storage_page_size();
1115
2.02k
    _storage_dict_page_size = schema.storage_dict_page_size();
1116
2.02k
    _schema_version = schema.schema_version();
1117
2.02k
    if (schema.has_seq_map()) {
1118
1.54k
        auto column_groups_pb = schema.seq_map();
1119
1.54k
        _seq_col_uid_to_value_cols_uid.clear();
1120
1.54k
        _value_col_uid_to_seq_col_uid.clear();
1121
1.54k
        _seq_col_idx_to_value_cols_idx.clear();
1122
1.54k
        _value_col_idx_to_seq_col_idx.clear();
1123
        /*
1124
         * ColumnGroupsPB is a list of cg_pb, and
1125
         * ColumnGroupsPB do not have begin() or end() method.
1126
         * we must use for(i=0;i<xx;i++) loop
1127
         */
1128
1.54k
        for (int i = 0; i < column_groups_pb.cg_size(); i++) {
1129
0
            ColumnGroupPB cg_pb = column_groups_pb.cg(i);
1130
0
            uint32_t key_uid = cg_pb.sequence_column();
1131
0
            auto found = _field_uniqueid_to_index.find(key_uid);
1132
0
            DCHECK(found != _field_uniqueid_to_index.end())
1133
0
                    << "could not find sequence col with unique id = " << key_uid
1134
0
                    << " table_id=" << _table_id;
1135
0
            int32_t seq_index = found->second;
1136
0
            _seq_col_uid_to_value_cols_uid[key_uid] = {};
1137
0
            _seq_col_idx_to_value_cols_idx[seq_index] = {};
1138
0
            for (auto val_uid : cg_pb.columns_in_group()) {
1139
0
                _seq_col_uid_to_value_cols_uid[key_uid].push_back(val_uid);
1140
0
                found = _field_uniqueid_to_index.find(val_uid);
1141
0
                DCHECK(found != _field_uniqueid_to_index.end())
1142
0
                        << "could not find value col with unique id = " << key_uid
1143
0
                        << " table_id=" << _table_id;
1144
0
                int32_t val_index = found->second;
1145
0
                _seq_col_idx_to_value_cols_idx[seq_index].push_back(val_index);
1146
0
            }
1147
0
        }
1148
1149
1.54k
        if (!_seq_col_uid_to_value_cols_uid.empty()) {
1150
            /*
1151
                |** KEY **|        ** VALUE **     |
1152
                ------------------------------------
1153
                |** KEY **|  CDE is value| sequence|
1154
                |----|----|----|----|----|----|----|
1155
                A    B    C    D    E   S1   S2
1156
                0    1    2    3    4    5    6
1157
                for example: _seq_map is {5:{2,3}, 6:{4}}
1158
                then, _value_to_seq = {2:5,3:5,5:5,4:6,6:6}
1159
            */
1160
0
            for (auto& [seq_uid, cols_uid] : _seq_col_uid_to_value_cols_uid) {
1161
0
                for (auto col_uid : cols_uid) {
1162
0
                    _value_col_uid_to_seq_col_uid[col_uid] = seq_uid;
1163
0
                }
1164
0
                _value_col_uid_to_seq_col_uid[seq_uid] = seq_uid;
1165
0
            }
1166
1167
0
            for (auto& [seq_idx, value_cols_idx] : _seq_col_idx_to_value_cols_idx) {
1168
0
                for (auto col_idx : value_cols_idx) {
1169
0
                    _value_col_idx_to_seq_col_idx[col_idx] = seq_idx;
1170
0
                }
1171
0
                _value_col_idx_to_seq_col_idx[seq_idx] = seq_idx;
1172
0
            }
1173
0
        }
1174
1.54k
    }
1175
    // Default to V1 inverted index storage format for backward compatibility if not specified in schema.
1176
2.02k
    if (!schema.has_inverted_index_storage_format()) {
1177
365
        _inverted_index_storage_format = InvertedIndexStorageFormatPB::V1;
1178
1.65k
    } else {
1179
1.65k
        _inverted_index_storage_format = schema.inverted_index_storage_format();
1180
1.65k
    }
1181
1182
2.02k
    _row_store_column_unique_ids.assign(schema.row_store_column_unique_ids().begin(),
1183
2.02k
                                        schema.row_store_column_unique_ids().end());
1184
2.02k
    _deprecated_enable_variant_flatten_nested = schema.enable_variant_flatten_nested();
1185
2.02k
    if (schema.has_storage_format()) {
1186
930
        _storage_format = schema.storage_format();
1187
1.09k
    } else if (schema.is_external_segment_column_meta_used() ||
1188
1.09k
               schema.integer_type_default_use_plain_encoding() ||
1189
1.09k
               schema.binary_plain_encoding_default_impl() ==
1190
1.08k
                       BinaryPlainEncodingTypePB::BINARY_PLAIN_ENCODING_V2) {
1191
        // Old PB without storage_format: any of the three legacy V3-flavor flags implies V3.
1192
1
        _storage_format = TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V3;
1193
1.08k
    } else {
1194
1.08k
        _storage_format = TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V2;
1195
1.08k
    }
1196
2.02k
    update_metadata_size();
1197
2.02k
}
1198
1199
475
void TabletSchema::copy_from(const TabletSchema& tablet_schema) {
1200
475
    TabletSchemaPB tablet_schema_pb;
1201
475
    tablet_schema.to_schema_pb(&tablet_schema_pb);
1202
475
    init_from_pb(tablet_schema_pb);
1203
475
    _table_id = tablet_schema.table_id();
1204
475
    _path_set_info_map = tablet_schema._path_set_info_map;
1205
475
}
1206
1207
173
void TabletSchema::shawdow_copy_without_columns(const TabletSchema& tablet_schema) {
1208
173
    *this = tablet_schema;
1209
173
    _field_path_to_index.clear();
1210
173
    _field_name_to_index.clear();
1211
173
    _field_uniqueid_to_index.clear();
1212
173
    _num_columns = 0;
1213
173
    _num_variant_columns = 0;
1214
173
    _num_null_columns = 0;
1215
173
    _num_key_columns = 0;
1216
173
    _cols.clear();
1217
173
    _delete_sign_idx = -1;
1218
173
    _sequence_col_idx = -1;
1219
173
    _version_col_idx = -1;
1220
173
    _skip_bitmap_col_idx = -1;
1221
173
    _commit_tso_col_idx = -1;
1222
173
    _binlog_tso_col_idx = -1;
1223
173
    _binlog_lsn_col_idx = -1;
1224
173
    _binlog_op_col_idx = -1;
1225
173
}
1226
1227
0
void TabletSchema::update_index_info_from(const TabletSchema& tablet_schema) {
1228
0
    for (auto& col : _cols) {
1229
0
        if (col->unique_id() < 0) {
1230
0
            continue;
1231
0
        }
1232
0
        const auto iter = tablet_schema._field_uniqueid_to_index.find(col->unique_id());
1233
0
        if (iter == tablet_schema._field_uniqueid_to_index.end()) {
1234
0
            continue;
1235
0
        }
1236
0
        auto col_idx = iter->second;
1237
0
        if (col_idx < 0 || col_idx >= tablet_schema._cols.size()) {
1238
0
            continue;
1239
0
        }
1240
0
        col->set_is_bf_column(tablet_schema._cols[col_idx]->is_bf_column());
1241
0
    }
1242
0
}
1243
1244
6.41k
std::string TabletSchema::to_key() const {
1245
6.41k
    TabletSchemaPB pb;
1246
6.41k
    to_schema_pb(&pb);
1247
6.41k
    return TabletSchema::deterministic_string_serialize(pb);
1248
6.41k
}
1249
1250
void TabletSchema::build_current_tablet_schema(int64_t index_id, int32_t version,
1251
                                               const OlapTableIndexSchema* index,
1252
2
                                               const TabletSchema& ori_tablet_schema) {
1253
    // copy from ori_tablet_schema
1254
2
    _keys_type = ori_tablet_schema.keys_type();
1255
2
    _num_short_key_columns = ori_tablet_schema.num_short_key_columns();
1256
2
    _num_rows_per_row_block = ori_tablet_schema.num_rows_per_row_block();
1257
2
    _compress_kind = ori_tablet_schema.compress_kind();
1258
1259
    // todo(yixiu): unique_id
1260
2
    _next_column_unique_id = ori_tablet_schema.next_column_unique_id();
1261
2
    _is_in_memory = ori_tablet_schema.is_in_memory();
1262
2
    _disable_auto_compaction = ori_tablet_schema.disable_auto_compaction();
1263
2
    _skip_write_index_on_load = ori_tablet_schema.skip_write_index_on_load();
1264
2
    _sort_type = ori_tablet_schema.sort_type();
1265
2
    _sort_col_num = ori_tablet_schema.sort_col_num();
1266
2
    _row_store_page_size = ori_tablet_schema.row_store_page_size();
1267
2
    _storage_page_size = ori_tablet_schema.storage_page_size();
1268
2
    _storage_dict_page_size = ori_tablet_schema.storage_dict_page_size();
1269
2
    _deprecated_enable_variant_flatten_nested =
1270
2
            ori_tablet_schema.deprecated_variant_flatten_nested();
1271
1272
    // copy from table_schema_param
1273
2
    _schema_version = version;
1274
2
    _num_columns = 0;
1275
2
    _num_variant_columns = 0;
1276
2
    _num_key_columns = 0;
1277
2
    _num_null_columns = 0;
1278
2
    bool has_bf_columns = false;
1279
2
    _cols.clear();
1280
2
    _indexes.clear();
1281
2
    _col_id_suffix_to_index.clear();
1282
2
    _index_by_unique_id_with_pattern.clear();
1283
2
    _field_name_to_index.clear();
1284
2
    _field_uniqueid_to_index.clear();
1285
2
    _delete_sign_idx = -1;
1286
2
    _sequence_col_idx = -1;
1287
2
    _version_col_idx = -1;
1288
2
    _skip_bitmap_col_idx = -1;
1289
2
    _commit_tso_col_idx = -1;
1290
2
    _binlog_tso_col_idx = -1;
1291
2
    _binlog_lsn_col_idx = -1;
1292
2
    _binlog_op_col_idx = -1;
1293
2
    _cluster_key_uids.clear();
1294
2
    for (const auto& i : ori_tablet_schema._cluster_key_uids) {
1295
0
        _cluster_key_uids.push_back(i);
1296
0
    }
1297
3
    for (auto& column : index->columns) {
1298
3
        if (column->is_key()) {
1299
2
            _num_key_columns++;
1300
2
        }
1301
3
        if (column->is_nullable()) {
1302
0
            _num_null_columns++;
1303
0
        }
1304
3
        if (column->is_bf_column()) {
1305
0
            has_bf_columns = true;
1306
0
        }
1307
3
        if (column->is_variant_type()) {
1308
0
            ++_num_variant_columns;
1309
0
        }
1310
3
        if (UNLIKELY(column->name() == DELETE_SIGN)) {
1311
0
            _delete_sign_idx = _num_columns;
1312
3
        } else if (UNLIKELY(column->name() == SEQUENCE_COL)) {
1313
0
            _sequence_col_idx = _num_columns;
1314
3
        } else if (UNLIKELY(column->name() == VERSION_COL)) {
1315
0
            _version_col_idx = _num_columns;
1316
3
        } else if (UNLIKELY(column->name() == SKIP_BITMAP_COL)) {
1317
0
            _skip_bitmap_col_idx = _num_columns;
1318
3
        } else if (UNLIKELY(column->name() == COMMIT_TSO_COL)) {
1319
1
            _commit_tso_col_idx = _num_columns;
1320
2
        } else if (UNLIKELY(column->name() == BINLOG_TSO_COL)) {
1321
0
            _binlog_tso_col_idx = _num_columns;
1322
2
        } else if (UNLIKELY(column->name() == BINLOG_LSN_COL)) {
1323
0
            _binlog_lsn_col_idx = _num_columns;
1324
2
        } else if (UNLIKELY(column->name() == BINLOG_OP_COL)) {
1325
0
            _binlog_op_col_idx = _num_columns;
1326
0
        }
1327
        // Reuse TabletColumn object from pool to reduce memory consumption
1328
3
        TabletColumnPtr new_column;
1329
3
        ColumnPB column_pb;
1330
3
        column->to_schema_pb(&column_pb);
1331
3
        auto pair = TabletColumnObjectPool::instance()->insert(
1332
3
                deterministic_string_serialize(column_pb));
1333
3
        new_column = pair.second;
1334
        // Release the handle quickly, because we use shared ptr to manage column
1335
3
        TabletColumnObjectPool::instance()->release(pair.first);
1336
3
        _cols.emplace_back(std::move(new_column));
1337
3
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
1338
3
        _field_uniqueid_to_index[_cols.back()->unique_id()] = _num_columns;
1339
3
        _num_columns++;
1340
3
    }
1341
1342
2
    for (const auto& i : index->indexes) {
1343
0
        size_t index_pos = _indexes.size();
1344
        // Reuse TabletIndex object from pool to reduce memory consumption
1345
0
        TabletIndexPtr new_index;
1346
0
        TabletIndexPB index_pb;
1347
0
        i->to_schema_pb(&index_pb);
1348
0
        auto pair = TabletColumnObjectPool::instance()->insert_index(
1349
0
                deterministic_string_serialize(index_pb));
1350
0
        new_index = pair.second;
1351
        // Release the handle quickly, because we use shared ptr to manage index
1352
0
        TabletColumnObjectPool::instance()->release(pair.first);
1353
0
        _indexes.emplace_back(std::move(new_index));
1354
0
        for (int32_t col_uid : _indexes.back()->col_unique_ids()) {
1355
0
            if (auto field_pattern = _indexes.back()->field_pattern(); !field_pattern.empty()) {
1356
0
                auto& pattern_to_index_map = _index_by_unique_id_with_pattern[col_uid];
1357
0
                pattern_to_index_map[field_pattern].emplace_back(_indexes.back());
1358
0
            } else {
1359
0
                IndexKey key = std::make_tuple(_indexes.back()->index_type(), col_uid,
1360
0
                                               _indexes.back()->get_index_suffix());
1361
0
                _col_id_suffix_to_index[key].push_back(index_pos);
1362
0
            }
1363
0
        }
1364
0
    }
1365
1366
2
    if (has_bf_columns) {
1367
0
        _has_bf_fpp = true;
1368
0
        _bf_fpp = ori_tablet_schema.bloom_filter_fpp();
1369
2
    } else {
1370
2
        _has_bf_fpp = false;
1371
2
        _bf_fpp = BLOOM_FILTER_DEFAULT_FPP;
1372
2
    }
1373
2
}
1374
1375
149
void TabletSchema::merge_dropped_columns(const TabletSchema& src_schema) {
1376
    // If they are the same tablet schema object, then just return
1377
149
    if (this == &src_schema) {
1378
0
        return;
1379
0
    }
1380
2.90k
    for (const auto& src_col : src_schema.columns()) {
1381
2.90k
        if (_field_uniqueid_to_index.find(src_col->unique_id()) == _field_uniqueid_to_index.end()) {
1382
0
            CHECK(!src_col->is_key())
1383
0
                    << src_col->name() << " is key column, should not be dropped.";
1384
0
            ColumnPB src_col_pb;
1385
            // There are some pointer in tablet column, not sure the reference relation, so
1386
            // that deep copy it.
1387
0
            src_col->to_schema_pb(&src_col_pb);
1388
0
            TabletColumn new_col(src_col_pb);
1389
0
            append_column(new_col, TabletSchema::ColumnType::DROPPED);
1390
0
        }
1391
2.90k
    }
1392
149
}
1393
1394
67
TabletSchemaSPtr TabletSchema::copy_without_variant_extracted_columns() {
1395
67
    TabletSchemaSPtr copy = std::make_shared<TabletSchema>();
1396
67
    copy->shawdow_copy_without_columns(*this);
1397
193
    for (auto& col : this->columns()) {
1398
193
        if (col->is_extracted_column()) {
1399
57
            continue;
1400
57
        }
1401
136
        copy->append_column(*col);
1402
136
    }
1403
67
    return copy;
1404
67
}
1405
1406
// Dropped column is in _field_uniqueid_to_index but not in _field_name_to_index
1407
// Could refer to append_column method
1408
6.78k
bool TabletSchema::is_dropped_column(const TabletColumn& col) const {
1409
6.78k
    CHECK(_field_uniqueid_to_index.find(col.unique_id()) != _field_uniqueid_to_index.end())
1410
0
            << "could not find col with unique id = " << col.unique_id()
1411
0
            << " and name = " << col.name() << " table_id=" << _table_id;
1412
6.78k
    auto it = _field_name_to_index.find(StringRef {col.name()});
1413
6.78k
    return it == _field_name_to_index.end() || _cols[it->second]->unique_id() != col.unique_id();
1414
6.78k
}
1415
1416
0
void TabletSchema::copy_extracted_columns(const TabletSchema& src_schema) {
1417
0
    std::unordered_set<int32_t> variant_columns;
1418
0
    for (const auto& col : columns()) {
1419
0
        if (col->is_variant_type()) {
1420
0
            variant_columns.insert(col->unique_id());
1421
0
        }
1422
0
    }
1423
0
    for (const TabletColumnPtr& col : src_schema.columns()) {
1424
0
        if (col->is_extracted_column() && variant_columns.contains(col->parent_unique_id())) {
1425
0
            ColumnPB col_pb;
1426
0
            col->to_schema_pb(&col_pb);
1427
0
            TabletColumn new_col(col_pb);
1428
0
            append_column(new_col, ColumnType::VARIANT);
1429
0
        }
1430
0
    }
1431
0
}
1432
1433
0
void TabletSchema::reserve_extracted_columns() {
1434
0
    for (auto it = _cols.begin(); it != _cols.end();) {
1435
0
        if (!(*it)->is_extracted_column()) {
1436
0
            it = _cols.erase(it);
1437
0
        } else {
1438
0
            ++it;
1439
0
        }
1440
0
    }
1441
0
}
1442
1443
8.34k
void TabletSchema::to_schema_pb(TabletSchemaPB* tablet_schema_pb) const {
1444
8.34k
    for (const auto& i : _cluster_key_uids) {
1445
32
        tablet_schema_pb->add_cluster_key_uids(i);
1446
32
    }
1447
8.34k
    tablet_schema_pb->set_keys_type(_keys_type);
1448
30.8k
    for (const auto& col : _cols) {
1449
30.8k
        ColumnPB* column = tablet_schema_pb->add_column();
1450
30.8k
        col->to_schema_pb(column);
1451
30.8k
    }
1452
11.5k
    for (const auto& index : _indexes) {
1453
11.5k
        auto* index_pb = tablet_schema_pb->add_index();
1454
11.5k
        index->to_schema_pb(index_pb);
1455
11.5k
    }
1456
8.34k
    tablet_schema_pb->set_num_short_key_columns(cast_set<int32_t>(_num_short_key_columns));
1457
8.34k
    tablet_schema_pb->set_num_rows_per_row_block(cast_set<int32_t>(_num_rows_per_row_block));
1458
8.34k
    tablet_schema_pb->set_compress_kind(_compress_kind);
1459
8.34k
    if (_has_bf_fpp) {
1460
4
        tablet_schema_pb->set_bf_fpp(_bf_fpp);
1461
4
    }
1462
8.34k
    tablet_schema_pb->set_next_column_unique_id(cast_set<uint32_t>(_next_column_unique_id));
1463
8.34k
    tablet_schema_pb->set_is_in_memory(_is_in_memory);
1464
8.34k
    tablet_schema_pb->set_disable_auto_compaction(_disable_auto_compaction);
1465
8.34k
    tablet_schema_pb->set_store_row_column(_store_row_column);
1466
8.34k
    tablet_schema_pb->set_skip_write_index_on_load(_skip_write_index_on_load);
1467
8.34k
    tablet_schema_pb->set_delete_sign_idx(_delete_sign_idx);
1468
8.34k
    tablet_schema_pb->set_sequence_col_idx(_sequence_col_idx);
1469
8.34k
    tablet_schema_pb->set_sort_type(_sort_type);
1470
8.34k
    tablet_schema_pb->set_sort_col_num(cast_set<int32_t>(_sort_col_num));
1471
8.34k
    tablet_schema_pb->set_schema_version(_schema_version);
1472
8.34k
    tablet_schema_pb->set_compression_type(_compression_type);
1473
8.34k
    tablet_schema_pb->set_row_store_page_size(_row_store_page_size);
1474
8.34k
    tablet_schema_pb->set_storage_page_size(_storage_page_size);
1475
8.34k
    tablet_schema_pb->set_storage_dict_page_size(_storage_dict_page_size);
1476
8.34k
    tablet_schema_pb->set_version_col_idx(_version_col_idx);
1477
8.34k
    tablet_schema_pb->set_skip_bitmap_col_idx(_skip_bitmap_col_idx);
1478
8.34k
    tablet_schema_pb->set_commit_tso_col_idx(_commit_tso_col_idx);
1479
8.34k
    tablet_schema_pb->set_binlog_tso_col_idx(_binlog_tso_col_idx);
1480
8.34k
    tablet_schema_pb->set_binlog_lsn_col_idx(_binlog_lsn_col_idx);
1481
8.34k
    tablet_schema_pb->set_binlog_op_col_idx(_binlog_op_col_idx);
1482
8.34k
    tablet_schema_pb->set_inverted_index_storage_format(_inverted_index_storage_format);
1483
8.34k
    tablet_schema_pb->mutable_row_store_column_unique_ids()->Assign(
1484
8.34k
            _row_store_column_unique_ids.begin(), _row_store_column_unique_ids.end());
1485
8.34k
    tablet_schema_pb->set_enable_variant_flatten_nested(_deprecated_enable_variant_flatten_nested);
1486
8.34k
    tablet_schema_pb->set_storage_format(_storage_format);
1487
    // Backward downgrade safety: if a new BE rewrites tablet_meta.json carrying only
1488
    // storage_format and the deployment is then rolled back to an old BE, the old BE
1489
    // does not know the new field and would default-derive V2 for a V3 tablet, causing
1490
    // it to write V2-encoded segments into a V3 tablet. Redundantly emit the three
1491
    // legacy V3-flavor flags so old BEs can recover the format via the prior "any of
1492
    // these implies V3" rule. ~3 bytes per schema PB; only paid for V3 tablets.
1493
8.34k
    if (_storage_format == TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V3) {
1494
578
        tablet_schema_pb->set_is_external_segment_column_meta_used(true);
1495
578
        tablet_schema_pb->set_integer_type_default_use_plain_encoding(true);
1496
578
        tablet_schema_pb->set_binary_plain_encoding_default_impl(
1497
578
                BinaryPlainEncodingTypePB::BINARY_PLAIN_ENCODING_V2);
1498
578
    }
1499
8.34k
    auto column_groups_pb = tablet_schema_pb->mutable_seq_map();
1500
8.34k
    for (const auto& it : _seq_col_uid_to_value_cols_uid) {
1501
0
        uint32_t key = it.first;
1502
0
        ColumnGroupPB* cg_pb = column_groups_pb->add_cg(); // ColumnGroupPB {key: {v1, v2, v3}}
1503
0
        cg_pb->set_sequence_column(key);
1504
0
        for (auto v : it.second) {
1505
0
            cg_pb->add_columns_in_group(v);
1506
0
        }
1507
0
    }
1508
8.34k
}
1509
1510
0
size_t TabletSchema::row_size() const {
1511
0
    size_t size = 0;
1512
0
    for (const auto& column : _cols) {
1513
0
        size += column->length();
1514
0
    }
1515
0
    size += (_num_columns + 7) / 8;
1516
1517
0
    return size;
1518
0
}
1519
1520
760
int32_t TabletSchema::field_index(const std::string& field_name) const {
1521
760
    const auto& found = _field_name_to_index.find(StringRef(field_name));
1522
760
    return (found == _field_name_to_index.end()) ? -1 : found->second;
1523
760
}
1524
1525
50
int32_t TabletSchema::field_index(const PathInData& path) const {
1526
50
    const auto& found = _field_path_to_index.find(PathInDataRef(&path));
1527
50
    return (found == _field_path_to_index.end()) ? -1 : found->second;
1528
50
}
1529
1530
9.03k
int32_t TabletSchema::field_index(int32_t col_unique_id) const {
1531
9.03k
    const auto& found = _field_uniqueid_to_index.find(col_unique_id);
1532
9.03k
    return (found == _field_uniqueid_to_index.end()) ? -1 : found->second;
1533
9.03k
}
1534
1535
30.4k
const std::vector<TabletColumnPtr>& TabletSchema::columns() const {
1536
30.4k
    return _cols;
1537
30.4k
}
1538
1539
73.9k
const TabletColumn& TabletSchema::column(size_t ordinal) const {
1540
73.9k
    DCHECK(ordinal < _num_columns) << "ordinal:" << ordinal << ", _num_columns:" << _num_columns;
1541
73.9k
    return *_cols[ordinal];
1542
73.9k
}
1543
1544
7.68k
const TabletColumn& TabletSchema::column_by_uid(int32_t col_unique_id) const {
1545
7.68k
    return *_cols.at(_field_uniqueid_to_index.at(col_unique_id));
1546
7.68k
}
1547
1548
41
TabletColumn& TabletSchema::mutable_column_by_uid(int32_t col_unique_id) {
1549
41
    return *_cols.at(_field_uniqueid_to_index.at(col_unique_id));
1550
41
}
1551
1552
137
TabletColumn& TabletSchema::mutable_column(size_t ordinal) {
1553
137
    return *_cols.at(ordinal);
1554
137
}
1555
1556
1
void TabletSchema::update_indexes_from_thrift(const std::vector<doris::TOlapTableIndex>& tindexes) {
1557
1
    std::vector<TabletIndexPtr> indexes;
1558
2
    for (const auto& tindex : tindexes) {
1559
2
        TabletIndex index;
1560
2
        index.init_from_thrift(tindex, *this);
1561
2
        indexes.emplace_back(std::make_shared<TabletIndex>(std::move(index)));
1562
2
    }
1563
1
    _indexes = std::move(indexes);
1564
1
    _col_id_suffix_to_index.clear();
1565
1
    _index_by_unique_id_with_pattern.clear();
1566
1
    size_t index_pos = 0;
1567
2
    for (auto& index : _indexes) {
1568
2
        for (int32_t col_uid : index->col_unique_ids()) {
1569
2
            if (auto field_pattern = index->field_pattern(); !field_pattern.empty()) {
1570
0
                auto& pattern_to_index_map = _index_by_unique_id_with_pattern[col_uid];
1571
0
                pattern_to_index_map[field_pattern].emplace_back(index);
1572
2
            } else {
1573
2
                IndexKey key =
1574
2
                        std::make_tuple(index->index_type(), col_uid, index->get_index_suffix());
1575
2
                _col_id_suffix_to_index[key].push_back(index_pos);
1576
2
            }
1577
2
        }
1578
2
        index_pos++;
1579
2
    }
1580
1
}
1581
1582
2
bool TabletSchema::exist_column(const std::string& field_name) const {
1583
2
    return _field_name_to_index.contains(StringRef {field_name});
1584
2
}
1585
1586
11.8k
bool TabletSchema::has_column_unique_id(int32_t col_unique_id) const {
1587
11.8k
    return _field_uniqueid_to_index.contains(col_unique_id);
1588
11.8k
}
1589
1590
4.04k
Status TabletSchema::have_column(const std::string& field_name) const {
1591
4.04k
    if (!_field_name_to_index.contains(StringRef(field_name))) {
1592
4.04k
        return Status::Error<ErrorCode::INTERNAL_ERROR>(
1593
4.04k
                "Not found field_name, field_name:{}, schema:{}", field_name,
1594
4.04k
                get_all_field_names());
1595
4.04k
    }
1596
0
    return Status::OK();
1597
4.04k
}
1598
1599
183
Result<const TabletColumn*> TabletSchema::column(const std::string& field_name) const {
1600
183
    auto it = _field_name_to_index.find(StringRef {field_name});
1601
183
    if (it == _field_name_to_index.end()) {
1602
0
        DCHECK(false) << "field_name=" << field_name << ", table_id=" << _table_id
1603
0
                      << ", field_name_to_index=" << get_all_field_names();
1604
0
        return ResultError(
1605
0
                Status::InternalError("column not found, name={}, table_id={}, schema_version={}",
1606
0
                                      field_name, _table_id, _schema_version));
1607
0
    }
1608
183
    return _cols[it->second].get();
1609
183
}
1610
1611
void TabletSchema::update_tablet_columns(const TabletSchema& tablet_schema,
1612
1
                                         const std::vector<TColumn>& t_columns) {
1613
1
    copy_from(tablet_schema);
1614
1
    if (!t_columns.empty() && t_columns[0].col_unique_id >= 0) {
1615
0
        clear_columns();
1616
0
        for (const auto& column : t_columns) {
1617
0
            append_column(TabletColumn(column));
1618
0
        }
1619
0
    }
1620
1
}
1621
1622
88
bool TabletSchema::has_inverted_index_with_index_id(int64_t index_id) const {
1623
107
    for (size_t i = 0; i < _indexes.size(); i++) {
1624
60
        if ((_indexes[i]->index_type() == IndexType::INVERTED ||
1625
60
             _indexes[i]->index_type() == IndexType::ANN) &&
1626
60
            _indexes[i]->index_id() == index_id) {
1627
41
            return true;
1628
41
        }
1629
60
    }
1630
47
    return false;
1631
88
}
1632
1633
std::vector<const TabletIndex*> TabletSchema::inverted_indexs(
1634
23.7k
        int32_t col_unique_id, const std::string& suffix_path) const {
1635
23.7k
    std::vector<const TabletIndex*> result;
1636
23.7k
    const std::string escaped_suffix = escape_for_path_name(suffix_path);
1637
23.7k
    auto it = _col_id_suffix_to_index.find(
1638
23.7k
            std::make_tuple(IndexType::INVERTED, col_unique_id, escaped_suffix));
1639
23.7k
    if (it != _col_id_suffix_to_index.end()) {
1640
7.99k
        for (size_t pos : it->second) {
1641
7.99k
            if (pos < _indexes.size()) {
1642
7.99k
                result.push_back(_indexes[pos].get());
1643
7.99k
            }
1644
7.99k
        }
1645
7.95k
    }
1646
23.7k
    return result;
1647
23.7k
}
1648
1649
std::vector<TabletIndexPtr> TabletSchema::inverted_index_by_field_pattern(
1650
217
        int32_t col_unique_id, const std::string& field_pattern) const {
1651
217
    auto id_to_pattern_map = _index_by_unique_id_with_pattern.find(col_unique_id);
1652
217
    if (id_to_pattern_map == _index_by_unique_id_with_pattern.end()) {
1653
65
        return {};
1654
65
    }
1655
152
    auto pattern_to_index_map = id_to_pattern_map->second.find(field_pattern);
1656
152
    if (pattern_to_index_map == id_to_pattern_map->second.end()) {
1657
8
        return {};
1658
8
    }
1659
144
    return pattern_to_index_map->second;
1660
152
}
1661
1662
18.3k
std::vector<const TabletIndex*> TabletSchema::inverted_indexs(const TabletColumn& col) const {
1663
    // Some columns(Float, Double, JSONB ...) from the variant do not support inverted index
1664
18.3k
    if (!segment_v2::IndexColumnWriter::check_support_inverted_index(col)) {
1665
560
        return {};
1666
560
    }
1667
1668
    // TODO use more efficient impl
1669
    // Use parent id if unique not assigned, this could happend when accessing subcolumns of variants
1670
17.8k
    int32_t col_unique_id = col.is_extracted_column() ? col.parent_unique_id() : col.unique_id();
1671
17.8k
    std::vector<const TabletIndex*> result;
1672
17.8k
    if (result = inverted_indexs(col_unique_id, escape_for_path_name(col.suffix_path()));
1673
17.8k
        !result.empty()) {
1674
5.26k
        return result;
1675
5.26k
    }
1676
    // variant's typed column has it's own index
1677
12.5k
    else if (col.is_extracted_column() && col.path_info_ptr()->get_is_typed()) {
1678
16
        std::string relative_path = col.path_info_ptr()->copy_pop_front().get_path();
1679
16
        if (_path_set_info_map.find(col_unique_id) == _path_set_info_map.end()) {
1680
0
            return result;
1681
0
        }
1682
16
        const auto& path_set_info = _path_set_info_map.at(col_unique_id);
1683
16
        if (path_set_info.typed_path_set.find(relative_path) ==
1684
16
            path_set_info.typed_path_set.end()) {
1685
0
            return result;
1686
0
        }
1687
16
        for (const auto& index : path_set_info.typed_path_set.at(relative_path).indexes) {
1688
16
            result.push_back(index.get());
1689
16
        }
1690
16
        return result;
1691
16
    }
1692
    // variant's subcolumns has it's own index
1693
12.5k
    else if (col.is_extracted_column()) {
1694
47
        std::string relative_path = col.path_info_ptr()->copy_pop_front().get_path();
1695
47
        if (_path_set_info_map.find(col_unique_id) == _path_set_info_map.end()) {
1696
5
            return result;
1697
5
        }
1698
42
        const auto& path_set_info = _path_set_info_map.at(col_unique_id);
1699
42
        if (path_set_info.subcolumn_indexes.find(relative_path) ==
1700
42
            path_set_info.subcolumn_indexes.end()) {
1701
25
            return result;
1702
25
        }
1703
17
        for (const auto& index : path_set_info.subcolumn_indexes.at(relative_path)) {
1704
3
            result.push_back(index.get());
1705
3
        }
1706
17
    }
1707
12.5k
    return result;
1708
17.8k
}
1709
1710
const TabletIndex* TabletSchema::ann_index(int32_t col_unique_id,
1711
12
                                           const std::string& suffix_path) const {
1712
21
    for (size_t i = 0; i < _indexes.size(); i++) {
1713
13
        if (_indexes[i]->index_type() == IndexType::ANN) {
1714
4
            for (int32_t id : _indexes[i]->col_unique_ids()) {
1715
4
                if (id == col_unique_id &&
1716
4
                    _indexes[i]->get_index_suffix() == escape_for_path_name(suffix_path)) {
1717
4
                    return _indexes[i].get();
1718
4
                }
1719
4
            }
1720
4
        }
1721
13
    }
1722
8
    return nullptr;
1723
12
}
1724
1725
15.5k
const TabletIndex* TabletSchema::ann_index(const TabletColumn& col) const {
1726
15.5k
    if (!segment_v2::IndexColumnWriter::check_support_ann_index(col)) {
1727
15.5k
        return nullptr;
1728
15.5k
    }
1729
    // TODO use more efficient impl
1730
    // Use parent id if unique not assigned, this could happend when accessing subcolumns of variants
1731
12
    int32_t col_unique_id = col.is_extracted_column() ? col.parent_unique_id() : col.unique_id();
1732
12
    return ann_index(col_unique_id, escape_for_path_name(col.suffix_path()));
1733
15.5k
}
1734
1735
0
bool TabletSchema::has_ngram_bf_index(int32_t col_unique_id) const {
1736
0
    IndexKey index_key(IndexType::NGRAM_BF, col_unique_id, "");
1737
0
    auto it = _col_id_suffix_to_index.find(index_key);
1738
0
    return it != _col_id_suffix_to_index.end();
1739
0
}
1740
1741
10.6k
const TabletIndex* TabletSchema::get_ngram_bf_index(int32_t col_unique_id) const {
1742
    // Get the ngram bf index for the given column unique id
1743
10.6k
    IndexKey index_key(IndexType::NGRAM_BF, col_unique_id, "");
1744
10.6k
    auto it = _col_id_suffix_to_index.find(index_key);
1745
10.6k
    if (it != _col_id_suffix_to_index.end()) {
1746
1
        if (!it->second.empty() && it->second[0] < _indexes.size()) {
1747
1
            return _indexes[it->second[0]].get();
1748
1
        }
1749
1
    }
1750
10.6k
    return nullptr;
1751
10.6k
}
1752
1753
const TabletIndex* TabletSchema::get_index(int32_t col_unique_id, IndexType index_type,
1754
14
                                           const std::string& suffix_path) const {
1755
14
    IndexKey index_key(index_type, col_unique_id, suffix_path);
1756
14
    auto it = _col_id_suffix_to_index.find(index_key);
1757
14
    if (it != _col_id_suffix_to_index.end()) {
1758
12
        if (!it->second.empty() && it->second[0] < _indexes.size()) {
1759
12
            return _indexes[it->second[0]].get();
1760
12
        }
1761
12
    }
1762
2
    return nullptr;
1763
14
}
1764
1765
Block TabletSchema::create_block(
1766
        const std::vector<uint32_t>& return_columns,
1767
778
        const std::unordered_set<uint32_t>* tablet_columns_need_convert_null) const {
1768
778
    Block block;
1769
3.02k
    for (int i = 0; i < return_columns.size(); ++i) {
1770
2.25k
        const ColumnId cid = return_columns[i];
1771
2.25k
        const auto& col = *_cols[cid];
1772
2.25k
        bool is_nullable = (tablet_columns_need_convert_null != nullptr &&
1773
2.25k
                            tablet_columns_need_convert_null->find(cid) !=
1774
0
                                    tablet_columns_need_convert_null->end());
1775
2.25k
        auto data_type = DataTypeFactory::instance().create_data_type(col, is_nullable);
1776
2.25k
        if (col.type() == FieldType::OLAP_FIELD_TYPE_STRUCT ||
1777
2.25k
            col.type() == FieldType::OLAP_FIELD_TYPE_MAP ||
1778
2.25k
            col.type() == FieldType::OLAP_FIELD_TYPE_ARRAY) {
1779
29
            if (_pruned_columns_data_type.contains(col.unique_id())) {
1780
0
                data_type = _pruned_columns_data_type.at(col.unique_id());
1781
0
            }
1782
29
        }
1783
1784
2.25k
        if (_vir_col_idx_to_unique_id.contains(cid)) {
1785
0
            block.insert({ColumnNothing::create(0), data_type, col.name()});
1786
0
            VLOG_DEBUG << fmt::format(
1787
0
                    "Create block from tablet schema, column cid {} is virtual column, col_name: "
1788
0
                    "{}, col_unique_id: {}, type {}",
1789
0
                    cid, col.name(), col.unique_id(), data_type->get_name());
1790
2.25k
        } else {
1791
2.25k
            block.insert({data_type->create_column(), data_type, col.name()});
1792
2.25k
        }
1793
2.25k
    }
1794
778
    return block;
1795
778
}
1796
1797
2.12k
Block TabletSchema::create_block() const {
1798
2.12k
    Block block;
1799
6.78k
    for (const auto& col : _cols) {
1800
6.78k
        if (is_dropped_column(*col)) {
1801
0
            continue;
1802
0
        }
1803
1804
6.78k
        auto data_type = DataTypeFactory::instance().create_data_type(*col);
1805
6.78k
        if (col->type() == FieldType::OLAP_FIELD_TYPE_STRUCT) {
1806
0
            if (_pruned_columns_data_type.contains(col->unique_id())) {
1807
0
                data_type = _pruned_columns_data_type.at(col->unique_id());
1808
0
            }
1809
0
        }
1810
6.78k
        block.insert({data_type->create_column(), data_type, col->name()});
1811
6.78k
    }
1812
2.12k
    return block;
1813
2.12k
}
1814
1815
218
Block TabletSchema::create_block_by_cids(const std::vector<uint32_t>& cids) const {
1816
218
    Block block;
1817
449
    for (const auto& cid : cids) {
1818
449
        const auto& col = *_cols[cid];
1819
449
        auto data_type = DataTypeFactory::instance().create_data_type(col);
1820
449
        if (col.type() == FieldType::OLAP_FIELD_TYPE_STRUCT) {
1821
0
            if (_pruned_columns_data_type.contains(col.unique_id())) {
1822
0
                data_type = _pruned_columns_data_type.at(col.unique_id());
1823
0
            }
1824
0
        }
1825
449
        block.insert({data_type->create_column(), data_type, col.name()});
1826
449
    }
1827
218
    return block;
1828
218
}
1829
1830
0
bool operator==(const TabletColumn& a, const TabletColumn& b) {
1831
0
    if (a._unique_id != b._unique_id) return false;
1832
0
    if (a._col_name != b._col_name) return false;
1833
0
    if (a._type != b._type) return false;
1834
0
    if (a._is_key != b._is_key) return false;
1835
0
    if (a._aggregation != b._aggregation) return false;
1836
0
    if (a._is_nullable != b._is_nullable) return false;
1837
0
    if (a._has_default_value != b._has_default_value) return false;
1838
0
    if (a._has_default_value) {
1839
0
        if (a._default_value != b._default_value) return false;
1840
0
    }
1841
0
    if (a._is_decimal != b._is_decimal) return false;
1842
0
    if (a._is_decimal) {
1843
0
        if (a._precision != b._precision) return false;
1844
0
        if (a._frac != b._frac) return false;
1845
0
    }
1846
0
    if (a._length != b._length) return false;
1847
0
    if (a._index_length != b._index_length) return false;
1848
0
    if (a._is_bf_column != b._is_bf_column) return false;
1849
0
    if (a._column_path == nullptr && a._column_path != nullptr) return false;
1850
0
    if (b._column_path == nullptr && a._column_path != nullptr) return false;
1851
0
    if (b._column_path != nullptr && a._column_path != nullptr &&
1852
0
        *a._column_path != *b._column_path)
1853
0
        return false;
1854
0
    return true;
1855
0
}
1856
1857
0
bool operator!=(const TabletColumn& a, const TabletColumn& b) {
1858
0
    return !(a == b);
1859
0
}
1860
1861
3
bool operator==(const TabletSchema& a, const TabletSchema& b) {
1862
3
    if (a._keys_type != b._keys_type) return false;
1863
3
    if (a._cols.size() != b._cols.size()) return false;
1864
3
    for (int i = 0; i < a._cols.size(); ++i) {
1865
0
        if (*a._cols[i] != *b._cols[i]) return false;
1866
0
    }
1867
3
    if (a._num_columns != b._num_columns) return false;
1868
3
    if (a._num_key_columns != b._num_key_columns) return false;
1869
3
    if (a._num_null_columns != b._num_null_columns) return false;
1870
3
    if (a._num_short_key_columns != b._num_short_key_columns) return false;
1871
3
    if (a._num_rows_per_row_block != b._num_rows_per_row_block) return false;
1872
3
    if (a._compress_kind != b._compress_kind) return false;
1873
3
    if (a._next_column_unique_id != b._next_column_unique_id) return false;
1874
3
    if (a._has_bf_fpp != b._has_bf_fpp) return false;
1875
3
    if (a._has_bf_fpp) {
1876
0
        if (std::abs(a._bf_fpp - b._bf_fpp) > 1e-6) return false;
1877
0
    }
1878
3
    if (a._is_in_memory != b._is_in_memory) return false;
1879
3
    if (a._delete_sign_idx != b._delete_sign_idx) return false;
1880
3
    if (a._sequence_col_idx != b._sequence_col_idx) return false;
1881
3
    if (a._version_col_idx != b._version_col_idx) return false;
1882
3
    if (a._skip_bitmap_col_idx != b._skip_bitmap_col_idx) return false;
1883
3
    if (a._commit_tso_col_idx != b._commit_tso_col_idx) return false;
1884
3
    if (a._binlog_tso_col_idx != b._binlog_tso_col_idx) return false;
1885
3
    if (a._binlog_lsn_col_idx != b._binlog_lsn_col_idx) return false;
1886
3
    if (a._binlog_op_col_idx != b._binlog_op_col_idx) return false;
1887
3
    if (a._disable_auto_compaction != b._disable_auto_compaction) return false;
1888
3
    if (a._store_row_column != b._store_row_column) return false;
1889
3
    if (a._row_store_page_size != b._row_store_page_size) return false;
1890
3
    if (a._storage_page_size != b._storage_page_size) return false;
1891
3
    if (a._storage_dict_page_size != b._storage_dict_page_size) return false;
1892
3
    if (a._skip_write_index_on_load != b._skip_write_index_on_load) return false;
1893
3
    if (a._deprecated_enable_variant_flatten_nested !=
1894
3
        b._deprecated_enable_variant_flatten_nested) {
1895
0
        return false;
1896
0
    }
1897
3
    if (a._storage_format != b._storage_format) return false;
1898
3
    return true;
1899
3
}
1900
1901
3
bool operator!=(const TabletSchema& a, const TabletSchema& b) {
1902
3
    return !(a == b);
1903
3
}
1904
} // namespace doris