Coverage Report

Created: 2026-08-13 04:14

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