Coverage Report

Created: 2026-07-19 13:27

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