Coverage Report

Created: 2026-07-24 01:26

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