Coverage Report

Created: 2025-04-12 13:58

/root/doris/be/src/olap/tablet_schema.cpp
Line
Count
Source (jump to first uncovered line)
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 "olap/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 "exec/tablet_info.h"
39
#include "olap/inverted_index_parser.h"
40
#include "olap/olap_define.h"
41
#include "olap/tablet_column_object_pool.h"
42
#include "olap/types.h"
43
#include "olap/utils.h"
44
#include "tablet_meta.h"
45
#include "vec/aggregate_functions/aggregate_function_simple_factory.h"
46
#include "vec/aggregate_functions/aggregate_function_state_union.h"
47
#include "vec/common/hex.h"
48
#include "vec/common/string_ref.h"
49
#include "vec/core/block.h"
50
#include "vec/data_types/data_type.h"
51
#include "vec/data_types/data_type_factory.hpp"
52
#include "vec/json/path_in_data.h"
53
54
namespace doris {
55
56
0
FieldType TabletColumn::get_field_type_by_type(PrimitiveType primitiveType) {
57
0
    switch (primitiveType) {
58
0
    case PrimitiveType::INVALID_TYPE:
59
0
        return FieldType::OLAP_FIELD_TYPE_UNKNOWN;
60
0
    case PrimitiveType::TYPE_NULL:
61
0
        return FieldType::OLAP_FIELD_TYPE_NONE;
62
0
    case PrimitiveType::TYPE_BOOLEAN:
63
0
        return FieldType::OLAP_FIELD_TYPE_BOOL;
64
0
    case PrimitiveType::TYPE_TINYINT:
65
0
        return FieldType::OLAP_FIELD_TYPE_TINYINT;
66
0
    case PrimitiveType::TYPE_SMALLINT:
67
0
        return FieldType::OLAP_FIELD_TYPE_SMALLINT;
68
0
    case PrimitiveType::TYPE_INT:
69
0
        return FieldType::OLAP_FIELD_TYPE_INT;
70
0
    case PrimitiveType::TYPE_BIGINT:
71
0
        return FieldType::OLAP_FIELD_TYPE_BIGINT;
72
0
    case PrimitiveType::TYPE_LARGEINT:
73
0
        return FieldType::OLAP_FIELD_TYPE_LARGEINT;
74
0
    case PrimitiveType::TYPE_FLOAT:
75
0
        return FieldType::OLAP_FIELD_TYPE_FLOAT;
76
0
    case PrimitiveType::TYPE_DOUBLE:
77
0
        return FieldType::OLAP_FIELD_TYPE_DOUBLE;
78
0
    case PrimitiveType::TYPE_VARCHAR:
79
0
        return FieldType::OLAP_FIELD_TYPE_VARCHAR;
80
0
    case PrimitiveType::TYPE_DATE:
81
0
        return FieldType::OLAP_FIELD_TYPE_DATE;
82
0
    case PrimitiveType::TYPE_DATETIME:
83
0
        return FieldType::OLAP_FIELD_TYPE_DATETIME;
84
0
    case PrimitiveType::TYPE_BINARY:
85
0
        return FieldType::OLAP_FIELD_TYPE_UNKNOWN; // Not implemented
86
0
    case PrimitiveType::TYPE_CHAR:
87
0
        return FieldType::OLAP_FIELD_TYPE_CHAR;
88
0
    case PrimitiveType::TYPE_STRUCT:
89
0
        return FieldType::OLAP_FIELD_TYPE_STRUCT;
90
0
    case PrimitiveType::TYPE_ARRAY:
91
0
        return FieldType::OLAP_FIELD_TYPE_ARRAY;
92
0
    case PrimitiveType::TYPE_MAP:
93
0
        return FieldType::OLAP_FIELD_TYPE_MAP;
94
0
    case PrimitiveType::TYPE_HLL:
95
0
        return FieldType::OLAP_FIELD_TYPE_HLL;
96
0
    case PrimitiveType::TYPE_DECIMALV2:
97
0
        return FieldType::OLAP_FIELD_TYPE_UNKNOWN; // Not implemented
98
0
    case PrimitiveType::TYPE_OBJECT:
99
0
        return FieldType::OLAP_FIELD_TYPE_OBJECT;
100
0
    case PrimitiveType::TYPE_STRING:
101
0
        return FieldType::OLAP_FIELD_TYPE_STRING;
102
0
    case PrimitiveType::TYPE_QUANTILE_STATE:
103
0
        return FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE;
104
0
    case PrimitiveType::TYPE_DATEV2:
105
0
        return FieldType::OLAP_FIELD_TYPE_DATEV2;
106
0
    case PrimitiveType::TYPE_DATETIMEV2:
107
0
        return FieldType::OLAP_FIELD_TYPE_DATETIMEV2;
108
0
    case PrimitiveType::TYPE_TIMEV2:
109
0
        return FieldType::OLAP_FIELD_TYPE_TIMEV2;
110
0
    case PrimitiveType::TYPE_DECIMAL32:
111
0
        return FieldType::OLAP_FIELD_TYPE_DECIMAL32;
112
0
    case PrimitiveType::TYPE_DECIMAL64:
113
0
        return FieldType::OLAP_FIELD_TYPE_DECIMAL64;
114
0
    case PrimitiveType::TYPE_DECIMAL128I:
115
0
        return FieldType::OLAP_FIELD_TYPE_DECIMAL128I;
116
0
    case PrimitiveType::TYPE_JSONB:
117
0
        return FieldType::OLAP_FIELD_TYPE_JSONB;
118
0
    case PrimitiveType::TYPE_VARIANT:
119
0
        return FieldType::OLAP_FIELD_TYPE_VARIANT;
120
0
    case PrimitiveType::TYPE_LAMBDA_FUNCTION:
121
0
        return FieldType::OLAP_FIELD_TYPE_UNKNOWN; // Not implemented
122
0
    case PrimitiveType::TYPE_AGG_STATE:
123
0
        return FieldType::OLAP_FIELD_TYPE_AGG_STATE;
124
0
    default:
125
0
        return FieldType::OLAP_FIELD_TYPE_UNKNOWN;
126
0
    }
127
0
}
128
129
12.1k
FieldType TabletColumn::get_field_type_by_string(const std::string& type_str) {
130
12.1k
    std::string upper_type_str = type_str;
131
12.1k
    std::transform(type_str.begin(), type_str.end(), upper_type_str.begin(),
132
68.2k
                   [](auto c) { return std::toupper(c); });
133
12.1k
    FieldType type;
134
135
12.1k
    if (0 == upper_type_str.compare("TINYINT")) {
136
430
        type = FieldType::OLAP_FIELD_TYPE_TINYINT;
137
11.7k
    } else if (0 == upper_type_str.compare("SMALLINT")) {
138
876
        type = FieldType::OLAP_FIELD_TYPE_SMALLINT;
139
10.8k
    } else if (0 == upper_type_str.compare("INT")) {
140
2.46k
        type = FieldType::OLAP_FIELD_TYPE_INT;
141
8.37k
    } else if (0 == upper_type_str.compare("BIGINT")) {
142
182
        type = FieldType::OLAP_FIELD_TYPE_BIGINT;
143
8.19k
    } else if (0 == upper_type_str.compare("LARGEINT")) {
144
137
        type = FieldType::OLAP_FIELD_TYPE_LARGEINT;
145
8.05k
    } else if (0 == upper_type_str.compare("UNSIGNED_TINYINT")) {
146
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT;
147
8.05k
    } else if (0 == upper_type_str.compare("UNSIGNED_SMALLINT")) {
148
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT;
149
8.05k
    } else if (0 == upper_type_str.compare("UNSIGNED_INT")) {
150
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT;
151
8.05k
    } else if (0 == upper_type_str.compare("UNSIGNED_BIGINT")) {
152
0
        type = FieldType::OLAP_FIELD_TYPE_UNSIGNED_BIGINT;
153
8.05k
    } else if (0 == upper_type_str.compare("IPV4")) {
154
0
        type = FieldType::OLAP_FIELD_TYPE_IPV4;
155
8.05k
    } else if (0 == upper_type_str.compare("IPV6")) {
156
0
        type = FieldType::OLAP_FIELD_TYPE_IPV6;
157
8.05k
    } else if (0 == upper_type_str.compare("FLOAT")) {
158
0
        type = FieldType::OLAP_FIELD_TYPE_FLOAT;
159
8.05k
    } else if (0 == upper_type_str.compare("DISCRETE_DOUBLE")) {
160
0
        type = FieldType::OLAP_FIELD_TYPE_DISCRETE_DOUBLE;
161
8.05k
    } else if (0 == upper_type_str.compare("DOUBLE")) {
162
0
        type = FieldType::OLAP_FIELD_TYPE_DOUBLE;
163
8.05k
    } else if (0 == upper_type_str.compare("CHAR")) {
164
138
        type = FieldType::OLAP_FIELD_TYPE_CHAR;
165
7.91k
    } else if (0 == upper_type_str.compare("DATE")) {
166
140
        type = FieldType::OLAP_FIELD_TYPE_DATE;
167
7.77k
    } else if (0 == upper_type_str.compare("DATEV2")) {
168
133
        type = FieldType::OLAP_FIELD_TYPE_DATEV2;
169
7.64k
    } else if (0 == upper_type_str.compare("DATETIMEV2")) {
170
0
        type = FieldType::OLAP_FIELD_TYPE_DATETIMEV2;
171
7.64k
    } else if (0 == upper_type_str.compare("DATETIME")) {
172
167
        type = FieldType::OLAP_FIELD_TYPE_DATETIME;
173
7.47k
    } else if (0 == upper_type_str.compare("DECIMAL32")) {
174
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL32;
175
7.47k
    } else if (0 == upper_type_str.compare("DECIMAL64")) {
176
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL64;
177
7.47k
    } else if (0 == upper_type_str.compare("DECIMAL128I")) {
178
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL128I;
179
7.47k
    } else if (0 == upper_type_str.compare("DECIMAL256")) {
180
0
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL256;
181
7.47k
    } else if (0 == upper_type_str.compare(0, 7, "DECIMAL")) {
182
141
        type = FieldType::OLAP_FIELD_TYPE_DECIMAL;
183
7.33k
    } else if (0 == upper_type_str.compare(0, 7, "VARCHAR")) {
184
183
        type = FieldType::OLAP_FIELD_TYPE_VARCHAR;
185
7.15k
    } else if (0 == upper_type_str.compare("STRING")) {
186
7.14k
        type = FieldType::OLAP_FIELD_TYPE_STRING;
187
7.14k
    } else if (0 == upper_type_str.compare("JSONB")) {
188
0
        type = FieldType::OLAP_FIELD_TYPE_JSONB;
189
11
    } else if (0 == upper_type_str.compare("VARIANT")) {
190
2
        type = FieldType::OLAP_FIELD_TYPE_VARIANT;
191
9
    } else if (0 == upper_type_str.compare("BOOLEAN")) {
192
0
        type = FieldType::OLAP_FIELD_TYPE_BOOL;
193
9
    } else if (0 == upper_type_str.compare(0, 3, "HLL")) {
194
7
        type = FieldType::OLAP_FIELD_TYPE_HLL;
195
7
    } else if (0 == upper_type_str.compare("STRUCT")) {
196
0
        type = FieldType::OLAP_FIELD_TYPE_STRUCT;
197
2
    } else if (0 == upper_type_str.compare("LIST")) {
198
0
        type = FieldType::OLAP_FIELD_TYPE_ARRAY;
199
2
    } else if (0 == upper_type_str.compare("MAP")) {
200
0
        type = FieldType::OLAP_FIELD_TYPE_MAP;
201
2
    } else if (0 == upper_type_str.compare("OBJECT")) {
202
0
        type = FieldType::OLAP_FIELD_TYPE_OBJECT;
203
2
    } else if (0 == upper_type_str.compare("ARRAY")) {
204
2
        type = FieldType::OLAP_FIELD_TYPE_ARRAY;
205
2
    } else if (0 == upper_type_str.compare("QUANTILE_STATE")) {
206
0
        type = FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE;
207
0
    } else if (0 == upper_type_str.compare("AGG_STATE")) {
208
0
        type = FieldType::OLAP_FIELD_TYPE_AGG_STATE;
209
0
    } else {
210
0
        LOG(WARNING) << "invalid type string. [type='" << type_str << "']";
211
0
        type = FieldType::OLAP_FIELD_TYPE_UNKNOWN;
212
0
    }
213
214
12.1k
    return type;
215
12.1k
}
216
217
3.97k
FieldAggregationMethod TabletColumn::get_aggregation_type_by_string(const std::string& str) {
218
3.97k
    std::string upper_str = str;
219
3.97k
    std::transform(str.begin(), str.end(), upper_str.begin(),
220
20.5k
                   [](auto c) { return std::toupper(c); });
221
3.97k
    FieldAggregationMethod aggregation_type;
222
223
3.97k
    if (0 == upper_str.compare("NONE")) {
224
1.67k
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE;
225
2.29k
    } else if (0 == upper_str.compare("SUM")) {
226
557
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM;
227
1.74k
    } else if (0 == upper_str.compare("MIN")) {
228
4
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN;
229
1.73k
    } else if (0 == upper_str.compare("MAX")) {
230
4
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX;
231
1.73k
    } else if (0 == upper_str.compare("REPLACE")) {
232
1.72k
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE;
233
1.72k
    } else if (0 == upper_str.compare("REPLACE_IF_NOT_NULL")) {
234
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL;
235
7
    } else if (0 == upper_str.compare("HLL_UNION")) {
236
7
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION;
237
7
    } else if (0 == upper_str.compare("BITMAP_UNION")) {
238
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION;
239
0
    } else if (0 == upper_str.compare("QUANTILE_UNION")) {
240
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION;
241
0
    } else if (!upper_str.empty()) {
242
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC;
243
0
    } else {
244
0
        aggregation_type = FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN;
245
0
    }
246
247
3.97k
    return aggregation_type;
248
3.97k
}
249
250
30.5k
std::string TabletColumn::get_string_by_field_type(FieldType type) {
251
30.5k
    switch (type) {
252
1.64k
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
253
1.64k
        return "TINYINT";
254
255
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT:
256
0
        return "UNSIGNED_TINYINT";
257
258
3.62k
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
259
3.62k
        return "SMALLINT";
260
261
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT:
262
0
        return "UNSIGNED_SMALLINT";
263
264
8.52k
    case FieldType::OLAP_FIELD_TYPE_INT:
265
8.52k
        return "INT";
266
267
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT:
268
0
        return "UNSIGNED_INT";
269
270
836
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
271
836
        return "BIGINT";
272
273
717
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
274
717
        return "LARGEINT";
275
276
0
    case FieldType::OLAP_FIELD_TYPE_UNSIGNED_BIGINT:
277
0
        return "UNSIGNED_BIGINT";
278
279
0
    case FieldType::OLAP_FIELD_TYPE_IPV4:
280
0
        return "IPV4";
281
282
0
    case FieldType::OLAP_FIELD_TYPE_IPV6:
283
0
        return "IPV6";
284
285
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
286
0
        return "FLOAT";
287
288
0
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
289
0
        return "DOUBLE";
290
291
0
    case FieldType::OLAP_FIELD_TYPE_DISCRETE_DOUBLE:
292
0
        return "DISCRETE_DOUBLE";
293
294
717
    case FieldType::OLAP_FIELD_TYPE_CHAR:
295
717
        return "CHAR";
296
297
721
    case FieldType::OLAP_FIELD_TYPE_DATE:
298
721
        return "DATE";
299
300
670
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
301
670
        return "DATEV2";
302
303
912
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
304
912
        return "DATETIME";
305
306
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
307
0
        return "DATETIMEV2";
308
309
717
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
310
717
        return "DECIMAL";
311
312
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
313
0
        return "DECIMAL32";
314
315
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
316
0
        return "DECIMAL64";
317
318
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
319
0
        return "DECIMAL128I";
320
321
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
322
0
        return "DECIMAL256";
323
324
721
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
325
721
        return "VARCHAR";
326
327
0
    case FieldType::OLAP_FIELD_TYPE_JSONB:
328
0
        return "JSONB";
329
330
0
    case FieldType::OLAP_FIELD_TYPE_VARIANT:
331
0
        return "VARIANT";
332
333
10.7k
    case FieldType::OLAP_FIELD_TYPE_STRING:
334
10.7k
        return "STRING";
335
336
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
337
0
        return "BOOLEAN";
338
339
6
    case FieldType::OLAP_FIELD_TYPE_HLL:
340
6
        return "HLL";
341
342
0
    case FieldType::OLAP_FIELD_TYPE_STRUCT:
343
0
        return "STRUCT";
344
345
4
    case FieldType::OLAP_FIELD_TYPE_ARRAY:
346
4
        return "ARRAY";
347
348
0
    case FieldType::OLAP_FIELD_TYPE_MAP:
349
0
        return "MAP";
350
351
0
    case FieldType::OLAP_FIELD_TYPE_OBJECT:
352
0
        return "OBJECT";
353
0
    case FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE:
354
0
        return "QUANTILE_STATE";
355
0
    case FieldType::OLAP_FIELD_TYPE_AGG_STATE:
356
0
        return "AGG_STATE";
357
0
    default:
358
0
        return "UNKNOWN";
359
30.5k
    }
360
30.5k
}
361
362
28
std::string TabletColumn::get_string_by_aggregation_type(FieldAggregationMethod type) {
363
28
    switch (type) {
364
4
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE:
365
4
        return "NONE";
366
367
9
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM:
368
9
        return "SUM";
369
370
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN:
371
0
        return "MIN";
372
373
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX:
374
0
        return "MAX";
375
376
15
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE:
377
15
        return "REPLACE";
378
379
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL:
380
0
        return "REPLACE_IF_NOT_NULL";
381
382
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION:
383
0
        return "HLL_UNION";
384
385
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION:
386
0
        return "BITMAP_UNION";
387
388
0
    case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION:
389
0
        return "QUANTILE_UNION";
390
391
0
    default:
392
0
        return "UNKNOWN";
393
28
    }
394
28
}
395
396
2.11k
uint32_t TabletColumn::get_field_length_by_type(TPrimitiveType::type type, uint32_t string_length) {
397
2.11k
    switch (type) {
398
110
    case TPrimitiveType::TINYINT:
399
110
    case TPrimitiveType::BOOLEAN:
400
110
        return 1;
401
357
    case TPrimitiveType::SMALLINT:
402
357
        return 2;
403
823
    case TPrimitiveType::INT:
404
823
        return 4;
405
114
    case TPrimitiveType::BIGINT:
406
114
        return 8;
407
101
    case TPrimitiveType::LARGEINT:
408
101
        return 16;
409
0
    case TPrimitiveType::IPV4:
410
0
        return 4;
411
0
    case TPrimitiveType::IPV6:
412
0
        return 16;
413
101
    case TPrimitiveType::DATE:
414
101
        return 3;
415
92
    case TPrimitiveType::DATEV2:
416
92
        return 4;
417
109
    case TPrimitiveType::DATETIME:
418
109
        return 8;
419
0
    case TPrimitiveType::DATETIMEV2:
420
0
        return 8;
421
0
    case TPrimitiveType::FLOAT:
422
0
        return 4;
423
0
    case TPrimitiveType::DOUBLE:
424
0
        return 8;
425
0
    case TPrimitiveType::QUANTILE_STATE:
426
0
    case TPrimitiveType::OBJECT:
427
0
        return 16;
428
101
    case TPrimitiveType::CHAR:
429
101
        return string_length;
430
101
    case TPrimitiveType::VARCHAR:
431
101
    case TPrimitiveType::HLL:
432
101
    case TPrimitiveType::AGG_STATE:
433
101
        return string_length + sizeof(OLAP_VARCHAR_MAX_LENGTH);
434
0
    case TPrimitiveType::STRING:
435
0
    case TPrimitiveType::VARIANT:
436
0
        return string_length + sizeof(OLAP_STRING_MAX_LENGTH);
437
0
    case TPrimitiveType::JSONB:
438
0
        return string_length + sizeof(OLAP_JSONB_MAX_LENGTH);
439
0
    case TPrimitiveType::STRUCT:
440
        // Note that(xy): this is the length of struct type itself,
441
        // the length of its subtypes are not included.
442
0
        return OLAP_STRUCT_MAX_LENGTH;
443
0
    case TPrimitiveType::ARRAY:
444
0
        return OLAP_ARRAY_MAX_LENGTH;
445
0
    case TPrimitiveType::MAP:
446
0
        return OLAP_MAP_MAX_LENGTH;
447
0
    case TPrimitiveType::DECIMAL32:
448
0
        return 4;
449
0
    case TPrimitiveType::DECIMAL64:
450
0
        return 8;
451
0
    case TPrimitiveType::DECIMAL128I:
452
0
        return 16;
453
0
    case TPrimitiveType::DECIMAL256:
454
0
        return 32;
455
101
    case TPrimitiveType::DECIMALV2:
456
101
        return 12; // use 12 bytes in olap engine.
457
0
    default:
458
0
        LOG(WARNING) << "unknown field type. [type=" << type << "]";
459
0
        return 0;
460
2.11k
    }
461
2.11k
}
462
463
12.2k
TabletColumn::TabletColumn() : _aggregation(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE) {}
464
465
133
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType type) {
466
133
    _aggregation = agg;
467
133
    _type = type;
468
133
}
469
470
17
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable) {
471
17
    _aggregation = agg;
472
17
    _type = filed_type;
473
17
    _length = get_scalar_type_info(filed_type)->size();
474
17
    _is_nullable = is_nullable;
475
17
}
476
477
TabletColumn::TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable,
478
16
                           int32_t unique_id, size_t length) {
479
16
    _aggregation = agg;
480
16
    _type = filed_type;
481
16
    _is_nullable = is_nullable;
482
16
    _unique_id = unique_id;
483
16
    _length = length;
484
16
}
485
486
0
TabletColumn::TabletColumn(const ColumnPB& column) {
487
0
    init_from_pb(column);
488
0
}
489
490
0
TabletColumn::TabletColumn(const TColumn& column) {
491
0
    init_from_thrift(column);
492
0
}
493
494
0
void TabletColumn::init_from_thrift(const TColumn& tcolumn) {
495
0
    ColumnPB column_pb;
496
0
    TabletMeta::init_column_from_tcolumn(tcolumn.col_unique_id, tcolumn, &column_pb);
497
0
    init_from_pb(column_pb);
498
0
}
499
500
12.0k
void TabletColumn::init_from_pb(const ColumnPB& column) {
501
12.0k
    _unique_id = column.unique_id();
502
12.0k
    _col_name = column.name();
503
12.0k
    _col_name_lower_case = to_lower(_col_name);
504
12.0k
    _type = TabletColumn::get_field_type_by_string(column.type());
505
12.0k
    _is_key = column.is_key();
506
12.0k
    _is_nullable = column.is_nullable();
507
12.0k
    _is_auto_increment = column.is_auto_increment();
508
509
12.0k
    _has_default_value = column.has_default_value();
510
12.0k
    if (_has_default_value) {
511
29
        _default_value = column.default_value();
512
29
    }
513
514
12.0k
    if (column.has_precision()) {
515
3.92k
        _is_decimal = true;
516
3.92k
        _precision = column.precision();
517
8.13k
    } else {
518
8.13k
        _is_decimal = false;
519
8.13k
    }
520
12.0k
    if (column.has_frac()) {
521
3.92k
        _frac = column.frac();
522
3.92k
    }
523
12.0k
    _length = column.length();
524
12.0k
    _index_length = column.index_length();
525
12.0k
    if (column.has_is_bf_column()) {
526
355
        _is_bf_column = column.is_bf_column();
527
11.7k
    } else {
528
11.7k
        _is_bf_column = false;
529
11.7k
    }
530
12.0k
    if (column.has_has_bitmap_index()) {
531
2.11k
        _has_bitmap_index = column.has_bitmap_index();
532
9.95k
    } else {
533
9.95k
        _has_bitmap_index = false;
534
9.95k
    }
535
12.0k
    if (column.has_aggregation()) {
536
3.97k
        _aggregation = get_aggregation_type_by_string(column.aggregation());
537
3.97k
        _aggregation_name = column.aggregation();
538
3.97k
    }
539
540
12.0k
    if (_type == FieldType::OLAP_FIELD_TYPE_AGG_STATE) {
541
0
        _result_is_nullable = column.result_is_nullable();
542
0
        _be_exec_version = column.be_exec_version();
543
0
    }
544
545
12.0k
    if (column.has_visible()) {
546
9.12k
        _visible = column.visible();
547
9.12k
    }
548
12.0k
    if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
549
2
        CHECK(column.children_columns_size() == 1)
550
0
                << "ARRAY type should has 1 children types, but got "
551
0
                << column.children_columns_size();
552
2
    }
553
12.0k
    if (_type == FieldType::OLAP_FIELD_TYPE_MAP) {
554
0
        DCHECK(column.children_columns_size() == 2)
555
0
                << "MAP type should has 2 children types, but got "
556
0
                << column.children_columns_size();
557
0
        if (UNLIKELY(column.children_columns_size() != 2)) {
558
0
            LOG(WARNING) << "MAP type should has 2 children types, but got "
559
0
                         << column.children_columns_size();
560
0
        }
561
0
    }
562
12.0k
    for (size_t i = 0; i < column.children_columns_size(); i++) {
563
2
        TabletColumn child_column;
564
2
        child_column.init_from_pb(column.children_columns(i));
565
2
        add_sub_column(child_column);
566
2
    }
567
12.0k
    if (column.has_column_path_info()) {
568
0
        _column_path = std::make_shared<vectorized::PathInData>();
569
0
        _column_path->from_protobuf(column.column_path_info());
570
0
        _parent_col_unique_id = column.column_path_info().parrent_column_unique_id();
571
0
    }
572
12.0k
    if (is_variant_type() && !column.has_column_path_info()) {
573
        // set path info for variant root column, to prevent from missing
574
2
        _column_path = std::make_shared<vectorized::PathInData>(_col_name_lower_case);
575
2
    }
576
12.0k
    for (const auto& column_pb : column.sparse_columns()) {
577
0
        TabletColumn column;
578
0
        column.init_from_pb(column_pb);
579
0
        _sparse_cols.emplace_back(std::make_shared<TabletColumn>(std::move(column)));
580
0
        _num_sparse_columns++;
581
0
    }
582
12.0k
}
583
584
TabletColumn TabletColumn::create_materialized_variant_column(const std::string& root,
585
                                                              const std::vector<std::string>& paths,
586
0
                                                              int32_t parent_unique_id) {
587
0
    TabletColumn subcol;
588
0
    subcol.set_type(FieldType::OLAP_FIELD_TYPE_VARIANT);
589
0
    subcol.set_is_nullable(true);
590
0
    subcol.set_unique_id(-1);
591
0
    subcol.set_parent_unique_id(parent_unique_id);
592
0
    vectorized::PathInData path(root, paths);
593
0
    subcol.set_path_info(path);
594
0
    subcol.set_name(path.get_path());
595
0
    return subcol;
596
0
}
597
598
30.5k
void TabletColumn::to_schema_pb(ColumnPB* column) const {
599
30.5k
    column->set_unique_id(_unique_id);
600
30.5k
    column->set_name(_col_name);
601
30.5k
    column->set_type(get_string_by_field_type(_type));
602
30.5k
    column->set_is_key(_is_key);
603
30.5k
    column->set_is_nullable(_is_nullable);
604
30.5k
    if (_has_default_value) {
605
156
        column->set_default_value(_default_value);
606
156
    }
607
30.5k
    if (_is_decimal) {
608
16.7k
        column->set_precision(_precision);
609
16.7k
        column->set_frac(_frac);
610
16.7k
    }
611
30.5k
    column->set_length(_length);
612
30.5k
    column->set_index_length(_index_length);
613
30.5k
    if (_is_bf_column) {
614
8
        column->set_is_bf_column(_is_bf_column);
615
8
    }
616
30.5k
    if (!_aggregation_name.empty()) {
617
16.8k
        column->set_aggregation(_aggregation_name);
618
16.8k
    }
619
30.5k
    column->set_result_is_nullable(_result_is_nullable);
620
30.5k
    column->set_be_exec_version(_be_exec_version);
621
30.5k
    if (_has_bitmap_index) {
622
0
        column->set_has_bitmap_index(_has_bitmap_index);
623
0
    }
624
30.5k
    column->set_visible(_visible);
625
626
30.5k
    if (_type == FieldType::OLAP_FIELD_TYPE_ARRAY) {
627
4
        CHECK(_sub_columns.size() == 1)
628
0
                << "ARRAY type should has 1 children types, but got " << _sub_columns.size();
629
4
    }
630
30.5k
    if (_type == FieldType::OLAP_FIELD_TYPE_MAP) {
631
0
        DCHECK(_sub_columns.size() == 2)
632
0
                << "MAP type should has 2 children types, but got " << _sub_columns.size();
633
0
        if (UNLIKELY(_sub_columns.size() != 2)) {
634
0
            LOG(WARNING) << "MAP type should has 2 children types, but got " << _sub_columns.size();
635
0
        }
636
0
    }
637
638
30.5k
    for (size_t i = 0; i < _sub_columns.size(); i++) {
639
4
        ColumnPB* child = column->add_children_columns();
640
4
        _sub_columns[i]->to_schema_pb(child);
641
4
    }
642
643
    // set parts info
644
30.5k
    if (has_path_info()) {
645
        // CHECK_GT(_parent_col_unique_id, 0);
646
0
        _column_path->to_protobuf(column->mutable_column_path_info(), _parent_col_unique_id);
647
        // Update unstable information for variant columns. Some of the fields in the tablet schema
648
        // are irrelevant for variant sub-columns, but retaining them may lead to an excessive growth
649
        // in the number of tablet schema cache entries.
650
0
        if (_type == FieldType::OLAP_FIELD_TYPE_STRING) {
651
0
            column->set_length(INT_MAX);
652
0
        }
653
0
        column->set_index_length(0);
654
0
    }
655
30.5k
    for (auto& col : _sparse_cols) {
656
0
        ColumnPB* sparse_column = column->add_sparse_columns();
657
0
        col->to_schema_pb(sparse_column);
658
0
    }
659
30.5k
}
660
661
32
void TabletColumn::add_sub_column(TabletColumn& sub_column) {
662
32
    _sub_columns.push_back(std::make_shared<TabletColumn>(sub_column));
663
32
    sub_column._parent_col_unique_id = this->_unique_id;
664
32
    _sub_column_count += 1;
665
32
}
666
667
22.1k
bool TabletColumn::is_row_store_column() const {
668
22.1k
    return _col_name == BeConsts::ROW_STORE_COL;
669
22.1k
}
670
671
vectorized::AggregateFunctionPtr TabletColumn::get_aggregate_function_union(
672
0
        vectorized::DataTypePtr type, int current_be_exec_version) const {
673
0
    const auto* state_type = assert_cast<const vectorized::DataTypeAggState*>(type.get());
674
0
    BeExecVersionManager::check_function_compatibility(
675
0
            current_be_exec_version, _be_exec_version,
676
0
            state_type->get_nested_function()->get_name());
677
0
    return vectorized::AggregateStateUnion::create(state_type->get_nested_function(), {type}, type);
678
0
}
679
680
vectorized::AggregateFunctionPtr TabletColumn::get_aggregate_function(
681
24
        std::string suffix, int current_be_exec_version) const {
682
24
    vectorized::AggregateFunctionPtr function = nullptr;
683
684
24
    auto type = vectorized::DataTypeFactory::instance().create_data_type(*this);
685
24
    if (type && type->get_type_as_type_descriptor().type == PrimitiveType::TYPE_AGG_STATE) {
686
0
        function = get_aggregate_function_union(type, current_be_exec_version);
687
24
    } else {
688
24
        std::string origin_name = TabletColumn::get_string_by_aggregation_type(_aggregation);
689
24
        std::string agg_name = origin_name + suffix;
690
24
        std::transform(agg_name.begin(), agg_name.end(), agg_name.begin(),
691
258
                       [](unsigned char c) { return std::tolower(c); });
692
24
        function = vectorized::AggregateFunctionSimpleFactory::instance().get(
693
24
                agg_name, {type}, type->is_nullable(), BeExecVersionManager::get_newest_version());
694
24
        if (!function) {
695
0
            LOG(WARNING) << "get column aggregate function failed, aggregation_name=" << origin_name
696
0
                         << ", column_type=" << type->get_name();
697
0
        }
698
24
    }
699
24
    if (function) {
700
24
        function->set_version(_be_exec_version);
701
24
        return function;
702
24
    }
703
0
    return nullptr;
704
24
}
705
706
5
void TabletColumn::set_path_info(const vectorized::PathInData& path) {
707
5
    _column_path = std::make_shared<vectorized::PathInData>(path);
708
5
}
709
710
0
vectorized::DataTypePtr TabletColumn::get_vec_type() const {
711
0
    return vectorized::DataTypeFactory::instance().create_data_type(*this);
712
0
}
713
714
// escape '.' and '_'
715
42.1k
std::string escape_for_path_name(const std::string& s) {
716
42.1k
    std::string res;
717
42.1k
    const char* pos = s.data();
718
42.1k
    const char* end = pos + s.size();
719
42.4k
    while (pos != end) {
720
215
        unsigned char c = *pos;
721
215
        if (c == '.' || c == '_') {
722
19
            res += '%';
723
19
            res += vectorized::hex_digit_uppercase(c / 16);
724
19
            res += vectorized::hex_digit_uppercase(c % 16);
725
196
        } else {
726
196
            res += c;
727
196
        }
728
215
        ++pos;
729
215
    }
730
42.1k
    return res;
731
42.1k
}
732
733
18
void TabletIndex::set_escaped_escaped_index_suffix_path(const std::string& path_name) {
734
18
    std::string escaped_path = escape_for_path_name(path_name);
735
18
    _escaped_index_suffix_path = escaped_path;
736
18
}
737
738
void TabletIndex::init_from_thrift(const TOlapTableIndex& index,
739
14
                                   const TabletSchema& tablet_schema) {
740
14
    _index_id = index.index_id;
741
14
    _index_name = index.index_name;
742
    // init col_unique_id in index at be side, since col_unique_id may be -1 at fe side
743
    // get column unique id by name
744
14
    std::vector<int32_t> col_unique_ids(index.columns.size());
745
28
    for (size_t i = 0; i < index.columns.size(); i++) {
746
14
        auto column_idx = tablet_schema.field_index(index.columns[i]);
747
14
        if (column_idx >= 0) {
748
11
            col_unique_ids[i] = tablet_schema.column(column_idx).unique_id();
749
11
        } else {
750
            // if column unique id not found by column name, find by column unique id
751
            // column unique id can not bigger than tablet schema column size, if bigger than column size means
752
            // this column is a new column added by light schema change
753
3
            if (index.__isset.column_unique_ids &&
754
3
                index.column_unique_ids[i] < tablet_schema.num_columns()) {
755
0
                col_unique_ids[i] = index.column_unique_ids[i];
756
3
            } else {
757
3
                col_unique_ids[i] = -1;
758
3
            }
759
3
        }
760
14
    }
761
14
    _col_unique_ids = std::move(col_unique_ids);
762
763
14
    switch (index.index_type) {
764
0
    case TIndexType::BITMAP:
765
0
        _index_type = IndexType::BITMAP;
766
0
        break;
767
14
    case TIndexType::INVERTED:
768
14
        _index_type = IndexType::INVERTED;
769
14
        break;
770
0
    case TIndexType::BLOOMFILTER:
771
0
        _index_type = IndexType::BLOOMFILTER;
772
0
        break;
773
0
    case TIndexType::NGRAM_BF:
774
0
        _index_type = IndexType::NGRAM_BF;
775
0
        break;
776
14
    }
777
14
    if (index.__isset.properties) {
778
0
        for (auto kv : index.properties) {
779
0
            _properties[kv.first] = kv.second;
780
0
        }
781
0
    }
782
14
}
783
784
void TabletIndex::init_from_thrift(const TOlapTableIndex& index,
785
0
                                   const std::vector<int32_t>& column_uids) {
786
0
    _index_id = index.index_id;
787
0
    _index_name = index.index_name;
788
0
    _col_unique_ids = column_uids;
789
790
0
    switch (index.index_type) {
791
0
    case TIndexType::BITMAP:
792
0
        _index_type = IndexType::BITMAP;
793
0
        break;
794
0
    case TIndexType::INVERTED:
795
0
        _index_type = IndexType::INVERTED;
796
0
        break;
797
0
    case TIndexType::BLOOMFILTER:
798
0
        _index_type = IndexType::BLOOMFILTER;
799
0
        break;
800
0
    case TIndexType::NGRAM_BF:
801
0
        _index_type = IndexType::NGRAM_BF;
802
0
        break;
803
0
    }
804
0
    if (index.__isset.properties) {
805
0
        for (auto kv : index.properties) {
806
0
            _properties[kv.first] = kv.second;
807
0
        }
808
0
    }
809
0
}
810
811
7.28k
void TabletIndex::init_from_pb(const TabletIndexPB& index) {
812
7.28k
    _index_id = index.index_id();
813
7.28k
    _index_name = index.index_name();
814
7.28k
    _col_unique_ids.clear();
815
7.28k
    for (auto col_unique_id : index.col_unique_id()) {
816
7.25k
        _col_unique_ids.push_back(col_unique_id);
817
7.25k
    }
818
7.28k
    _index_type = index.index_type();
819
40.9k
    for (const auto& kv : index.properties()) {
820
40.9k
        _properties[kv.first] = kv.second;
821
40.9k
    }
822
7.28k
    _escaped_index_suffix_path = index.index_suffix_name();
823
7.28k
}
824
825
10.9k
void TabletIndex::to_schema_pb(TabletIndexPB* index) const {
826
10.9k
    index->set_index_id(_index_id);
827
10.9k
    index->set_index_name(_index_name);
828
10.9k
    index->clear_col_unique_id();
829
10.9k
    for (auto col_unique_id : _col_unique_ids) {
830
10.9k
        index->add_col_unique_id(col_unique_id);
831
10.9k
    }
832
10.9k
    index->set_index_type(_index_type);
833
60.7k
    for (const auto& kv : _properties) {
834
60.7k
        DBUG_EXECUTE_IF("tablet_schema.to_schema_pb", {
835
60.7k
            if (kv.first == INVERTED_INDEX_PARSER_LOWERCASE_KEY) {
836
60.7k
                continue;
837
60.7k
            }
838
60.7k
        })
839
60.7k
        (*index->mutable_properties())[kv.first] = kv.second;
840
60.7k
    }
841
10.9k
    index->set_index_suffix_name(_escaped_index_suffix_path);
842
843
10.9k
    DBUG_EXECUTE_IF("tablet_schema.to_schema_pb", { return; })
844
845
    // lowercase by default
846
10.9k
    if (!_properties.empty()) {
847
9.74k
        if (!_properties.contains(INVERTED_INDEX_PARSER_LOWERCASE_KEY)) {
848
168
            (*index->mutable_properties())[INVERTED_INDEX_PARSER_LOWERCASE_KEY] =
849
168
                    INVERTED_INDEX_PARSER_TRUE;
850
168
        }
851
9.74k
    }
852
10.9k
}
853
854
2.58k
TabletSchema::TabletSchema() = default;
855
856
2.53k
TabletSchema::~TabletSchema() {
857
2.53k
    clear_column_cache_handlers();
858
2.53k
}
859
860
1.59k
int64_t TabletSchema::get_metadata_size() const {
861
1.59k
    return sizeof(TabletSchema);
862
1.59k
}
863
864
126
void TabletSchema::append_column(TabletColumn column, ColumnType col_type) {
865
126
    if (column.is_key()) {
866
52
        _num_key_columns++;
867
52
    }
868
126
    if (column.is_nullable()) {
869
48
        _num_null_columns++;
870
48
    }
871
126
    if (column.is_variant_type()) {
872
0
        ++_num_variant_columns;
873
0
        if (!column.has_path_info()) {
874
0
            const std::string& col_name = column.name_lower_case();
875
0
            vectorized::PathInData path(col_name);
876
0
            column.set_path_info(path);
877
0
        }
878
0
    }
879
126
    if (UNLIKELY(column.name() == DELETE_SIGN)) {
880
0
        _delete_sign_idx = _num_columns;
881
126
    } else if (UNLIKELY(column.name() == SEQUENCE_COL)) {
882
4
        _sequence_col_idx = _num_columns;
883
122
    } else if (UNLIKELY(column.name() == VERSION_COL)) {
884
0
        _version_col_idx = _num_columns;
885
122
    } else if (UNLIKELY(column.name() == SKIP_BITMAP_COL)) {
886
0
        _skip_bitmap_col_idx = _num_columns;
887
0
    }
888
126
    _field_id_to_index[column.unique_id()] = _num_columns;
889
126
    _cols.push_back(std::make_shared<TabletColumn>(std::move(column)));
890
    // The dropped column may have same name with exsiting column, so that
891
    // not add to name to index map, only for uid to index map
892
126
    if (col_type == ColumnType::VARIANT || _cols.back()->is_variant_type() ||
893
126
        _cols.back()->is_extracted_column()) {
894
5
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
895
5
        _field_path_to_index[_cols.back()->path_info_ptr().get()] = _num_columns;
896
121
    } else if (col_type == ColumnType::NORMAL) {
897
121
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
898
121
    }
899
126
    _num_columns++;
900
126
}
901
902
0
void TabletColumn::append_sparse_column(TabletColumn column) {
903
0
    _sparse_cols.push_back(std::make_shared<TabletColumn>(column));
904
0
    _num_sparse_columns++;
905
0
}
906
907
34
void TabletSchema::append_index(TabletIndex&& index) {
908
37
    for (int32_t id : index.col_unique_ids()) {
909
37
        _col_id_suffix_to_index.emplace(
910
37
                std::make_tuple(index.index_type(), id, index.get_index_suffix()), _indexes.size());
911
37
    }
912
34
    _indexes.push_back(std::make_shared<TabletIndex>(index));
913
34
}
914
915
void TabletSchema::update_index(const TabletColumn& col, const IndexType& index_type,
916
2
                                TabletIndex&& index) {
917
2
    int32_t col_unique_id = col.is_extracted_column() ? col.parent_unique_id() : col.unique_id();
918
2
    const std::string& suffix_path = escape_for_path_name(col.suffix_path());
919
2
    IndexKey key(index_type, col_unique_id, suffix_path);
920
2
    auto iter = _col_id_suffix_to_index.find(key);
921
2
    if (iter != _col_id_suffix_to_index.end()) {
922
1
        _indexes[iter->second] = std::make_shared<TabletIndex>(std::move(index));
923
1
        return;
924
1
    }
925
1
    LOG(WARNING) << " failed to update_index: " << index_type << " " << col_unique_id << " "
926
1
                 << suffix_path;
927
1
}
928
929
0
void TabletSchema::replace_column(size_t pos, TabletColumn new_col) {
930
0
    CHECK_LT(pos, num_columns()) << " outof range";
931
0
    _cols[pos] = std::make_shared<TabletColumn>(std::move(new_col));
932
0
}
933
934
1.54k
void TabletSchema::clear_index_cache_handlers() {
935
1.54k
    for (auto* handle : _index_cache_handlers) {
936
0
        TabletColumnObjectPool::instance()->release(handle);
937
0
    }
938
1.54k
    _index_cache_handlers.clear();
939
1.54k
}
940
941
1
void TabletSchema::clear_index() {
942
1
    clear_index_cache_handlers();
943
1
    _indexes.clear();
944
1
    _col_id_suffix_to_index.clear();
945
1
}
946
947
4
void TabletSchema::remove_index(int64_t index_id) {
948
4
    std::vector<TabletIndexPtr> indexes;
949
4
    std::unordered_map<IndexKey, int32_t, IndexKeyHash> col_id_suffix_to_index;
950
5
    for (auto index : _indexes) {
951
5
        if (index->index_id() == index_id) {
952
4
            continue;
953
4
        }
954
1
        for (int32_t col_uid : index->col_unique_ids()) {
955
1
            col_id_suffix_to_index.emplace(
956
1
                    std::make_tuple(index->index_type(), col_uid, index->get_index_suffix()),
957
1
                    indexes.size());
958
1
        }
959
1
        indexes.emplace_back(std::move(index));
960
1
    }
961
4
    _indexes = std::move(indexes);
962
4
    _col_id_suffix_to_index = std::move(col_id_suffix_to_index);
963
4
}
964
965
0
void TabletSchema::clear_columns() {
966
0
    _field_path_to_index.clear();
967
0
    _field_name_to_index.clear();
968
0
    _field_id_to_index.clear();
969
0
    _num_columns = 0;
970
0
    _num_variant_columns = 0;
971
0
    _num_null_columns = 0;
972
0
    _num_key_columns = 0;
973
0
    _cols.clear();
974
0
    clear_column_cache_handlers();
975
0
}
976
977
4.07k
void TabletSchema::clear_column_cache_handlers() {
978
4.07k
    for (auto* cache_handle : _column_cache_handlers) {
979
0
        TabletColumnObjectPool::instance()->release(cache_handle);
980
0
    }
981
4.07k
    _column_cache_handlers.clear();
982
4.07k
}
983
984
void TabletSchema::init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns,
985
1.54k
                                bool reuse_cache_column) {
986
1.54k
    _keys_type = schema.keys_type();
987
1.54k
    _num_columns = 0;
988
1.54k
    _num_variant_columns = 0;
989
1.54k
    _num_key_columns = 0;
990
1.54k
    _num_null_columns = 0;
991
1.54k
    _cols.clear();
992
1.54k
    _indexes.clear();
993
1.54k
    _col_id_suffix_to_index.clear();
994
1.54k
    _field_name_to_index.clear();
995
1.54k
    _field_id_to_index.clear();
996
1.54k
    _cluster_key_uids.clear();
997
1.54k
    clear_column_cache_handlers();
998
1.54k
    clear_index_cache_handlers();
999
1.54k
    for (const auto& i : schema.cluster_key_uids()) {
1000
6
        _cluster_key_uids.push_back(i);
1001
6
    }
1002
12.2k
    for (auto& column_pb : schema.column()) {
1003
12.2k
        TabletColumnPtr column;
1004
12.2k
        if (reuse_cache_column) {
1005
250
            auto pair = TabletColumnObjectPool::instance()->insert(
1006
250
                    deterministic_string_serialize(column_pb));
1007
250
            column = pair.second;
1008
250
            _column_cache_handlers.push_back(pair.first);
1009
11.9k
        } else {
1010
11.9k
            column = std::make_shared<TabletColumn>();
1011
11.9k
            column->init_from_pb(column_pb);
1012
11.9k
        }
1013
12.2k
        if (ignore_extracted_columns && column->is_extracted_column()) {
1014
0
            continue;
1015
0
        }
1016
12.2k
        if (column->is_key()) {
1017
2.26k
            _num_key_columns++;
1018
2.26k
        }
1019
12.2k
        if (column->is_nullable()) {
1020
7.37k
            _num_null_columns++;
1021
7.37k
        }
1022
12.2k
        if (column->is_variant_type()) {
1023
2
            ++_num_variant_columns;
1024
2
        }
1025
1026
12.2k
        _cols.emplace_back(std::move(column));
1027
12.2k
        if (!_cols.back()->is_extracted_column()) {
1028
12.2k
            _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
1029
12.2k
            _field_id_to_index[_cols.back()->unique_id()] = _num_columns;
1030
12.2k
        }
1031
12.2k
        _num_columns++;
1032
12.2k
    }
1033
7.30k
    for (const auto& index_pb : schema.index()) {
1034
7.30k
        TabletIndexPtr index;
1035
7.30k
        if (reuse_cache_column) {
1036
109
            auto pair = TabletColumnObjectPool::instance()->insert_index(
1037
109
                    deterministic_string_serialize(index_pb));
1038
109
            index = pair.second;
1039
109
            _index_cache_handlers.push_back(pair.first);
1040
7.19k
        } else {
1041
7.19k
            index = std::make_shared<TabletIndex>();
1042
7.19k
            index->init_from_pb(index_pb);
1043
7.19k
        }
1044
7.30k
        for (int32_t col_uid : index->col_unique_ids()) {
1045
7.30k
            _col_id_suffix_to_index.emplace(
1046
7.30k
                    std::make_tuple(index->index_type(), col_uid, index->get_index_suffix()),
1047
7.30k
                    _indexes.size());
1048
7.30k
        }
1049
7.30k
        _indexes.emplace_back(std::move(index));
1050
7.30k
    }
1051
1.54k
    _num_short_key_columns = schema.num_short_key_columns();
1052
1.54k
    _num_rows_per_row_block = schema.num_rows_per_row_block();
1053
1.54k
    _compress_kind = schema.compress_kind();
1054
1.54k
    _next_column_unique_id = schema.next_column_unique_id();
1055
1.54k
    if (schema.has_bf_fpp()) {
1056
3
        _has_bf_fpp = true;
1057
3
        _bf_fpp = schema.bf_fpp();
1058
1.54k
    } else {
1059
1.54k
        _has_bf_fpp = false;
1060
1.54k
        _bf_fpp = BLOOM_FILTER_DEFAULT_FPP;
1061
1.54k
    }
1062
1.54k
    _is_in_memory = schema.is_in_memory();
1063
1.54k
    _disable_auto_compaction = schema.disable_auto_compaction();
1064
1.54k
    _enable_single_replica_compaction = schema.enable_single_replica_compaction();
1065
1.54k
    _store_row_column = schema.store_row_column();
1066
1.54k
    _skip_write_index_on_load = schema.skip_write_index_on_load();
1067
1.54k
    _delete_sign_idx = schema.delete_sign_idx();
1068
1.54k
    _sequence_col_idx = schema.sequence_col_idx();
1069
1.54k
    _version_col_idx = schema.version_col_idx();
1070
1.54k
    _skip_bitmap_col_idx = schema.skip_bitmap_col_idx();
1071
1.54k
    _sort_type = schema.sort_type();
1072
1.54k
    _sort_col_num = schema.sort_col_num();
1073
1.54k
    _compression_type = schema.compression_type();
1074
1.54k
    _row_store_page_size = schema.row_store_page_size();
1075
1.54k
    _storage_page_size = schema.storage_page_size();
1076
1.54k
    _schema_version = schema.schema_version();
1077
    // Default to V1 inverted index storage format for backward compatibility if not specified in schema.
1078
1.54k
    if (!schema.has_inverted_index_storage_format()) {
1079
158
        _inverted_index_storage_format = InvertedIndexStorageFormatPB::V1;
1080
1.38k
    } else {
1081
1.38k
        _inverted_index_storage_format = schema.inverted_index_storage_format();
1082
1.38k
    }
1083
1084
1.54k
    _row_store_column_unique_ids.assign(schema.row_store_column_unique_ids().begin(),
1085
1.54k
                                        schema.row_store_column_unique_ids().end());
1086
1.54k
    _enable_variant_flatten_nested = schema.enable_variant_flatten_nested();
1087
1.54k
    update_metadata_size();
1088
1.54k
}
1089
1090
373
void TabletSchema::copy_from(const TabletSchema& tablet_schema) {
1091
373
    TabletSchemaPB tablet_schema_pb;
1092
373
    tablet_schema.to_schema_pb(&tablet_schema_pb);
1093
373
    init_from_pb(tablet_schema_pb);
1094
373
    _table_id = tablet_schema.table_id();
1095
373
}
1096
1097
0
void TabletSchema::shawdow_copy_without_columns(const TabletSchema& tablet_schema) {
1098
0
    *this = tablet_schema;
1099
0
    _field_path_to_index.clear();
1100
0
    _field_name_to_index.clear();
1101
0
    _field_id_to_index.clear();
1102
0
    _num_columns = 0;
1103
0
    _num_variant_columns = 0;
1104
0
    _num_null_columns = 0;
1105
0
    _num_key_columns = 0;
1106
0
    _cols.clear();
1107
    // notice : do not ref columns
1108
0
    _column_cache_handlers.clear();
1109
0
}
1110
1111
0
void TabletSchema::update_index_info_from(const TabletSchema& tablet_schema) {
1112
0
    for (auto& col : _cols) {
1113
0
        if (col->unique_id() < 0) {
1114
0
            continue;
1115
0
        }
1116
0
        const auto iter = tablet_schema._field_id_to_index.find(col->unique_id());
1117
0
        if (iter == tablet_schema._field_id_to_index.end()) {
1118
0
            continue;
1119
0
        }
1120
0
        auto col_idx = iter->second;
1121
0
        if (col_idx < 0 || col_idx >= tablet_schema._cols.size()) {
1122
0
            continue;
1123
0
        }
1124
0
        col->set_is_bf_column(tablet_schema._cols[col_idx]->is_bf_column());
1125
0
        col->set_has_bitmap_index(tablet_schema._cols[col_idx]->has_bitmap_index());
1126
0
    }
1127
0
}
1128
1129
2.79k
std::string TabletSchema::to_key() const {
1130
2.79k
    TabletSchemaPB pb;
1131
2.79k
    to_schema_pb(&pb);
1132
2.79k
    return TabletSchema::deterministic_string_serialize(pb);
1133
2.79k
}
1134
1135
void TabletSchema::build_current_tablet_schema(int64_t index_id, int32_t version,
1136
                                               const OlapTableIndexSchema* index,
1137
0
                                               const TabletSchema& ori_tablet_schema) {
1138
    // copy from ori_tablet_schema
1139
0
    _keys_type = ori_tablet_schema.keys_type();
1140
0
    _num_short_key_columns = ori_tablet_schema.num_short_key_columns();
1141
0
    _num_rows_per_row_block = ori_tablet_schema.num_rows_per_row_block();
1142
0
    _compress_kind = ori_tablet_schema.compress_kind();
1143
1144
    // todo(yixiu): unique_id
1145
0
    _next_column_unique_id = ori_tablet_schema.next_column_unique_id();
1146
0
    _is_in_memory = ori_tablet_schema.is_in_memory();
1147
0
    _disable_auto_compaction = ori_tablet_schema.disable_auto_compaction();
1148
0
    _enable_single_replica_compaction = ori_tablet_schema.enable_single_replica_compaction();
1149
0
    _skip_write_index_on_load = ori_tablet_schema.skip_write_index_on_load();
1150
0
    _sort_type = ori_tablet_schema.sort_type();
1151
0
    _sort_col_num = ori_tablet_schema.sort_col_num();
1152
0
    _row_store_page_size = ori_tablet_schema.row_store_page_size();
1153
0
    _storage_page_size = ori_tablet_schema.storage_page_size();
1154
0
    _enable_variant_flatten_nested = ori_tablet_schema.variant_flatten_nested();
1155
1156
    // copy from table_schema_param
1157
0
    _schema_version = version;
1158
0
    _num_columns = 0;
1159
0
    _num_variant_columns = 0;
1160
0
    _num_key_columns = 0;
1161
0
    _num_null_columns = 0;
1162
0
    bool has_bf_columns = false;
1163
0
    _cols.clear();
1164
0
    _indexes.clear();
1165
0
    _col_id_suffix_to_index.clear();
1166
0
    _field_name_to_index.clear();
1167
0
    _field_id_to_index.clear();
1168
0
    _delete_sign_idx = -1;
1169
0
    _sequence_col_idx = -1;
1170
0
    _version_col_idx = -1;
1171
0
    _skip_bitmap_col_idx = -1;
1172
0
    _cluster_key_uids.clear();
1173
0
    clear_column_cache_handlers();
1174
0
    for (const auto& i : ori_tablet_schema._cluster_key_uids) {
1175
0
        _cluster_key_uids.push_back(i);
1176
0
    }
1177
0
    for (auto& column : index->columns) {
1178
0
        if (column->is_key()) {
1179
0
            _num_key_columns++;
1180
0
        }
1181
0
        if (column->is_nullable()) {
1182
0
            _num_null_columns++;
1183
0
        }
1184
0
        if (column->is_bf_column()) {
1185
0
            has_bf_columns = true;
1186
0
        }
1187
0
        if (column->is_variant_type()) {
1188
0
            ++_num_variant_columns;
1189
0
        }
1190
0
        if (UNLIKELY(column->name() == DELETE_SIGN)) {
1191
0
            _delete_sign_idx = _num_columns;
1192
0
        } else if (UNLIKELY(column->name() == SEQUENCE_COL)) {
1193
0
            _sequence_col_idx = _num_columns;
1194
0
        } else if (UNLIKELY(column->name() == VERSION_COL)) {
1195
0
            _version_col_idx = _num_columns;
1196
0
        } else if (UNLIKELY(column->name() == SKIP_BITMAP_COL)) {
1197
0
            _skip_bitmap_col_idx = _num_columns;
1198
0
        }
1199
0
        _cols.emplace_back(std::make_shared<TabletColumn>(*column));
1200
0
        _field_name_to_index.emplace(StringRef(_cols.back()->name()), _num_columns);
1201
0
        _field_id_to_index[_cols.back()->unique_id()] = _num_columns;
1202
0
        _num_columns++;
1203
0
    }
1204
1205
0
    for (const auto& i : index->indexes) {
1206
0
        for (int32_t col_uid : i->col_unique_ids()) {
1207
0
            _col_id_suffix_to_index.emplace(
1208
0
                    std::make_tuple(i->index_type(), col_uid, i->get_index_suffix()),
1209
0
                    _indexes.size());
1210
0
        }
1211
0
        _indexes.emplace_back(std::make_shared<TabletIndex>(*i));
1212
0
    }
1213
1214
0
    if (has_bf_columns) {
1215
0
        _has_bf_fpp = true;
1216
0
        _bf_fpp = ori_tablet_schema.bloom_filter_fpp();
1217
0
    } else {
1218
0
        _has_bf_fpp = false;
1219
0
        _bf_fpp = BLOOM_FILTER_DEFAULT_FPP;
1220
0
    }
1221
0
}
1222
1223
149
void TabletSchema::merge_dropped_columns(const TabletSchema& src_schema) {
1224
    // If they are the same tablet schema object, then just return
1225
149
    if (this == &src_schema) {
1226
0
        return;
1227
0
    }
1228
2.90k
    for (const auto& src_col : src_schema.columns()) {
1229
2.90k
        if (_field_id_to_index.find(src_col->unique_id()) == _field_id_to_index.end()) {
1230
0
            CHECK(!src_col->is_key())
1231
0
                    << src_col->name() << " is key column, should not be dropped.";
1232
0
            ColumnPB src_col_pb;
1233
            // There are some pointer in tablet column, not sure the reference relation, so
1234
            // that deep copy it.
1235
0
            src_col->to_schema_pb(&src_col_pb);
1236
0
            TabletColumn new_col(src_col_pb);
1237
0
            append_column(new_col, TabletSchema::ColumnType::DROPPED);
1238
0
        }
1239
2.90k
    }
1240
149
}
1241
1242
0
TabletSchemaSPtr TabletSchema::copy_without_variant_extracted_columns() {
1243
0
    TabletSchemaSPtr copy = std::make_shared<TabletSchema>();
1244
0
    TabletSchemaPB tablet_schema_pb;
1245
0
    this->to_schema_pb(&tablet_schema_pb);
1246
0
    copy->init_from_pb(tablet_schema_pb, true /*ignore extracted_columns*/);
1247
0
    return copy;
1248
0
}
1249
1250
// Dropped column is in _field_id_to_index but not in _field_name_to_index
1251
// Could refer to append_column method
1252
5.90k
bool TabletSchema::is_dropped_column(const TabletColumn& col) const {
1253
5.90k
    CHECK(_field_id_to_index.find(col.unique_id()) != _field_id_to_index.end())
1254
0
            << "could not find col with unique id = " << col.unique_id()
1255
0
            << " and name = " << col.name() << " table_id=" << _table_id;
1256
5.90k
    auto it = _field_name_to_index.find(StringRef {col.name()});
1257
5.90k
    return it == _field_name_to_index.end() || _cols[it->second]->unique_id() != col.unique_id();
1258
5.90k
}
1259
1260
0
void TabletSchema::copy_extracted_columns(const TabletSchema& src_schema) {
1261
0
    std::unordered_set<int32_t> variant_columns;
1262
0
    for (const auto& col : columns()) {
1263
0
        if (col->is_variant_type()) {
1264
0
            variant_columns.insert(col->unique_id());
1265
0
        }
1266
0
    }
1267
0
    for (const TabletColumnPtr& col : src_schema.columns()) {
1268
0
        if (col->is_extracted_column() && variant_columns.contains(col->parent_unique_id())) {
1269
0
            ColumnPB col_pb;
1270
0
            col->to_schema_pb(&col_pb);
1271
0
            TabletColumn new_col(col_pb);
1272
0
            append_column(new_col, ColumnType::VARIANT);
1273
0
        }
1274
0
    }
1275
0
}
1276
1277
0
void TabletSchema::reserve_extracted_columns() {
1278
0
    for (auto it = _cols.begin(); it != _cols.end();) {
1279
0
        if (!(*it)->is_extracted_column()) {
1280
0
            it = _cols.erase(it);
1281
0
        } else {
1282
0
            ++it;
1283
0
        }
1284
0
    }
1285
0
}
1286
1287
4.99k
void TabletSchema::to_schema_pb(TabletSchemaPB* tablet_schema_pb) const {
1288
4.99k
    for (const auto& i : _cluster_key_uids) {
1289
30
        tablet_schema_pb->add_cluster_key_uids(i);
1290
30
    }
1291
4.99k
    tablet_schema_pb->set_keys_type(_keys_type);
1292
30.5k
    for (const auto& col : _cols) {
1293
30.5k
        ColumnPB* column = tablet_schema_pb->add_column();
1294
30.5k
        col->to_schema_pb(column);
1295
30.5k
    }
1296
10.9k
    for (const auto& index : _indexes) {
1297
10.9k
        auto* index_pb = tablet_schema_pb->add_index();
1298
10.9k
        index->to_schema_pb(index_pb);
1299
10.9k
    }
1300
4.99k
    tablet_schema_pb->set_num_short_key_columns(_num_short_key_columns);
1301
4.99k
    tablet_schema_pb->set_num_rows_per_row_block(_num_rows_per_row_block);
1302
4.99k
    tablet_schema_pb->set_compress_kind(_compress_kind);
1303
4.99k
    if (_has_bf_fpp) {
1304
4
        tablet_schema_pb->set_bf_fpp(_bf_fpp);
1305
4
    }
1306
4.99k
    tablet_schema_pb->set_next_column_unique_id(_next_column_unique_id);
1307
4.99k
    tablet_schema_pb->set_is_in_memory(_is_in_memory);
1308
4.99k
    tablet_schema_pb->set_disable_auto_compaction(_disable_auto_compaction);
1309
4.99k
    tablet_schema_pb->set_enable_single_replica_compaction(_enable_single_replica_compaction);
1310
4.99k
    tablet_schema_pb->set_store_row_column(_store_row_column);
1311
4.99k
    tablet_schema_pb->set_skip_write_index_on_load(_skip_write_index_on_load);
1312
4.99k
    tablet_schema_pb->set_delete_sign_idx(_delete_sign_idx);
1313
4.99k
    tablet_schema_pb->set_sequence_col_idx(_sequence_col_idx);
1314
4.99k
    tablet_schema_pb->set_sort_type(_sort_type);
1315
4.99k
    tablet_schema_pb->set_sort_col_num(_sort_col_num);
1316
4.99k
    tablet_schema_pb->set_schema_version(_schema_version);
1317
4.99k
    tablet_schema_pb->set_compression_type(_compression_type);
1318
4.99k
    tablet_schema_pb->set_row_store_page_size(_row_store_page_size);
1319
4.99k
    tablet_schema_pb->set_storage_page_size(_storage_page_size);
1320
4.99k
    tablet_schema_pb->set_version_col_idx(_version_col_idx);
1321
4.99k
    tablet_schema_pb->set_skip_bitmap_col_idx(_skip_bitmap_col_idx);
1322
4.99k
    tablet_schema_pb->set_inverted_index_storage_format(_inverted_index_storage_format);
1323
4.99k
    tablet_schema_pb->mutable_row_store_column_unique_ids()->Assign(
1324
4.99k
            _row_store_column_unique_ids.begin(), _row_store_column_unique_ids.end());
1325
4.99k
    tablet_schema_pb->set_enable_variant_flatten_nested(_enable_variant_flatten_nested);
1326
4.99k
}
1327
1328
0
size_t TabletSchema::row_size() const {
1329
0
    size_t size = 0;
1330
0
    for (const auto& column : _cols) {
1331
0
        size += column->length();
1332
0
    }
1333
0
    size += (_num_columns + 7) / 8;
1334
1335
0
    return size;
1336
0
}
1337
1338
1.21k
int32_t TabletSchema::field_index(const std::string& field_name) const {
1339
1.21k
    const auto& found = _field_name_to_index.find(StringRef(field_name));
1340
1.21k
    return (found == _field_name_to_index.end()) ? -1 : found->second;
1341
1.21k
}
1342
1343
0
int32_t TabletSchema::field_index(const vectorized::PathInData& path) const {
1344
0
    const auto& found = _field_path_to_index.find(vectorized::PathInDataRef(&path));
1345
0
    return (found == _field_path_to_index.end()) ? -1 : found->second;
1346
0
}
1347
1348
178
int32_t TabletSchema::field_index(int32_t col_unique_id) const {
1349
178
    const auto& found = _field_id_to_index.find(col_unique_id);
1350
178
    return (found == _field_id_to_index.end()) ? -1 : found->second;
1351
178
}
1352
1353
22.3k
const std::vector<TabletColumnPtr>& TabletSchema::columns() const {
1354
22.3k
    return _cols;
1355
22.3k
}
1356
1357
0
const std::vector<TabletColumnPtr>& TabletColumn::sparse_columns() const {
1358
0
    return _sparse_cols;
1359
0
}
1360
1361
145k
const TabletColumn& TabletSchema::column(size_t ordinal) const {
1362
145k
    DCHECK(ordinal < _num_columns) << "ordinal:" << ordinal << ", _num_columns:" << _num_columns;
1363
145k
    return *_cols[ordinal];
1364
145k
}
1365
1366
0
const TabletColumn& TabletColumn::sparse_column_at(size_t ordinal) const {
1367
0
    DCHECK(ordinal < _sparse_cols.size())
1368
0
            << "ordinal:" << ordinal << ", _num_columns:" << _sparse_cols.size();
1369
0
    return *_sparse_cols[ordinal];
1370
0
}
1371
1372
928
const TabletColumn& TabletSchema::column_by_uid(int32_t col_unique_id) const {
1373
928
    return *_cols.at(_field_id_to_index.at(col_unique_id));
1374
928
}
1375
1376
0
TabletColumn& TabletSchema::mutable_column_by_uid(int32_t col_unique_id) {
1377
0
    return *_cols.at(_field_id_to_index.at(col_unique_id));
1378
0
}
1379
1380
8
TabletColumn& TabletSchema::mutable_column(size_t ordinal) {
1381
8
    return *_cols.at(ordinal);
1382
8
}
1383
1384
0
void TabletSchema::update_indexes_from_thrift(const std::vector<doris::TOlapTableIndex>& tindexes) {
1385
0
    std::vector<TabletIndexPtr> indexes;
1386
0
    for (const auto& tindex : tindexes) {
1387
0
        TabletIndex index;
1388
0
        index.init_from_thrift(tindex, *this);
1389
0
        indexes.emplace_back(std::make_shared<TabletIndex>(std::move(index)));
1390
0
    }
1391
0
    _indexes = std::move(indexes);
1392
0
    std::unordered_map<IndexKey, int32_t, IndexKeyHash> col_id_suffix_to_index;
1393
0
    for (size_t i = 0; i < _indexes.size(); i++) {
1394
0
        for (int32_t col_uid : _indexes[i]->col_unique_ids()) {
1395
0
            col_id_suffix_to_index.emplace(std::make_tuple(_indexes[i]->index_type(), col_uid,
1396
0
                                                           _indexes[i]->get_index_suffix()),
1397
0
                                           i);
1398
0
        }
1399
0
    }
1400
0
    _col_id_suffix_to_index = std::move(col_id_suffix_to_index);
1401
0
}
1402
1403
0
bool TabletSchema::exist_column(const std::string& field_name) const {
1404
0
    return _field_name_to_index.contains(StringRef {field_name});
1405
0
}
1406
1407
414
bool TabletSchema::has_column_unique_id(int32_t col_unique_id) const {
1408
414
    return _field_id_to_index.contains(col_unique_id);
1409
414
}
1410
1411
0
Status TabletSchema::have_column(const std::string& field_name) const {
1412
0
    if (!_field_name_to_index.contains(StringRef(field_name))) {
1413
0
        return Status::Error<ErrorCode::INTERNAL_ERROR>(
1414
0
                "Not found field_name, field_name:{}, schema:{}", field_name,
1415
0
                get_all_field_names());
1416
0
    }
1417
0
    return Status::OK();
1418
0
}
1419
1420
172
Result<const TabletColumn*> TabletSchema::column(const std::string& field_name) const {
1421
172
    auto it = _field_name_to_index.find(StringRef {field_name});
1422
172
    if (it == _field_name_to_index.end()) {
1423
0
        DCHECK(false) << "field_name=" << field_name << ", table_id=" << _table_id
1424
0
                      << ", field_name_to_index=" << get_all_field_names();
1425
0
        return ResultError(
1426
0
                Status::InternalError("column not found, name={}, table_id={}, schema_version={}",
1427
0
                                      field_name, _table_id, _schema_version));
1428
0
    }
1429
172
    return _cols[it->second].get();
1430
172
}
1431
1432
void TabletSchema::update_tablet_columns(const TabletSchema& tablet_schema,
1433
0
                                         const std::vector<TColumn>& t_columns) {
1434
0
    copy_from(tablet_schema);
1435
0
    if (!t_columns.empty() && t_columns[0].col_unique_id >= 0) {
1436
0
        clear_columns();
1437
0
        for (const auto& column : t_columns) {
1438
0
            append_column(TabletColumn(column));
1439
0
        }
1440
0
    }
1441
0
}
1442
1443
52
bool TabletSchema::has_inverted_index_with_index_id(int64_t index_id) const {
1444
66
    for (size_t i = 0; i < _indexes.size(); i++) {
1445
33
        if (_indexes[i]->index_type() == IndexType::INVERTED &&
1446
33
            _indexes[i]->index_id() == index_id) {
1447
19
            return true;
1448
19
        }
1449
33
    }
1450
33
    return false;
1451
52
}
1452
1453
const TabletIndex* TabletSchema::inverted_index(int32_t col_unique_id,
1454
28.5k
                                                const std::string& suffix_path) const {
1455
28.5k
    const std::string escaped_suffix = escape_for_path_name(suffix_path);
1456
28.5k
    auto it = _col_id_suffix_to_index.find(
1457
28.5k
            std::make_tuple(IndexType::INVERTED, col_unique_id, escaped_suffix));
1458
28.5k
    if (it != _col_id_suffix_to_index.end()) {
1459
7.97k
        return _indexes[it->second].get();
1460
7.97k
    }
1461
20.5k
    return nullptr;
1462
28.5k
}
1463
1464
13.6k
const TabletIndex* TabletSchema::inverted_index(const TabletColumn& col) const {
1465
    // Some columns(Float, Double, JSONB ...) from the variant do not support inverted index
1466
13.6k
    if (!segment_v2::InvertedIndexColumnWriter::check_support_inverted_index(col)) {
1467
2
        return nullptr;
1468
2
    }
1469
    // TODO use more efficient impl
1470
    // Use parent id if unique not assigned, this could happend when accessing subcolumns of variants
1471
13.6k
    int32_t col_unique_id = col.is_extracted_column() ? col.parent_unique_id() : col.unique_id();
1472
13.6k
    return inverted_index(col_unique_id, escape_for_path_name(col.suffix_path()));
1473
13.6k
}
1474
1475
0
bool TabletSchema::has_ngram_bf_index(int32_t col_unique_id) const {
1476
0
    IndexKey index_key(IndexType::NGRAM_BF, col_unique_id, "");
1477
0
    auto it = _col_id_suffix_to_index.find(index_key);
1478
0
    return it != _col_id_suffix_to_index.end();
1479
0
}
1480
1481
12.8k
const TabletIndex* TabletSchema::get_ngram_bf_index(int32_t col_unique_id) const {
1482
    // Get the ngram bf index for the given column unique id
1483
12.8k
    IndexKey index_key(IndexType::NGRAM_BF, col_unique_id, "");
1484
12.8k
    auto it = _col_id_suffix_to_index.find(index_key);
1485
12.8k
    if (it != _col_id_suffix_to_index.end()) {
1486
1
        return _indexes[it->second].get();
1487
1
    }
1488
12.8k
    return nullptr;
1489
12.8k
}
1490
1491
vectorized::Block TabletSchema::create_block(
1492
        const std::vector<uint32_t>& return_columns,
1493
715
        const std::unordered_set<uint32_t>* tablet_columns_need_convert_null) const {
1494
715
    vectorized::Block block;
1495
2.82k
    for (int i = 0; i < return_columns.size(); ++i) {
1496
2.11k
        const auto& col = *_cols[return_columns[i]];
1497
2.11k
        bool is_nullable = (tablet_columns_need_convert_null != nullptr &&
1498
2.11k
                            tablet_columns_need_convert_null->find(return_columns[i]) !=
1499
0
                                    tablet_columns_need_convert_null->end());
1500
2.11k
        auto data_type = vectorized::DataTypeFactory::instance().create_data_type(col, is_nullable);
1501
2.11k
        auto column = data_type->create_column();
1502
2.11k
        block.insert({std::move(column), data_type, col.name()});
1503
2.11k
    }
1504
715
    return block;
1505
715
}
1506
1507
1.81k
vectorized::Block TabletSchema::create_block(bool ignore_dropped_col) const {
1508
1.81k
    vectorized::Block block;
1509
5.90k
    for (const auto& col : _cols) {
1510
5.90k
        if (ignore_dropped_col && is_dropped_column(*col)) {
1511
0
            continue;
1512
0
        }
1513
5.90k
        auto data_type = vectorized::DataTypeFactory::instance().create_data_type(*col);
1514
5.90k
        block.insert({data_type->create_column(), data_type, col->name()});
1515
5.90k
    }
1516
1.81k
    return block;
1517
1.81k
}
1518
1519
0
vectorized::Block TabletSchema::create_block_by_cids(const std::vector<uint32_t>& cids) const {
1520
0
    vectorized::Block block;
1521
0
    for (const auto& cid : cids) {
1522
0
        const auto& col = *_cols[cid];
1523
0
        auto data_type = vectorized::DataTypeFactory::instance().create_data_type(col);
1524
0
        block.insert({data_type->create_column(), data_type, col.name()});
1525
0
    }
1526
0
    return block;
1527
0
}
1528
1529
0
bool operator==(const TabletColumn& a, const TabletColumn& b) {
1530
0
    if (a._unique_id != b._unique_id) return false;
1531
0
    if (a._col_name != b._col_name) return false;
1532
0
    if (a._type != b._type) return false;
1533
0
    if (a._is_key != b._is_key) return false;
1534
0
    if (a._aggregation != b._aggregation) return false;
1535
0
    if (a._is_nullable != b._is_nullable) return false;
1536
0
    if (a._has_default_value != b._has_default_value) return false;
1537
0
    if (a._has_default_value) {
1538
0
        if (a._default_value != b._default_value) return false;
1539
0
    }
1540
0
    if (a._is_decimal != b._is_decimal) return false;
1541
0
    if (a._is_decimal) {
1542
0
        if (a._precision != b._precision) return false;
1543
0
        if (a._frac != b._frac) return false;
1544
0
    }
1545
0
    if (a._length != b._length) return false;
1546
0
    if (a._index_length != b._index_length) return false;
1547
0
    if (a._is_bf_column != b._is_bf_column) return false;
1548
0
    if (a._has_bitmap_index != b._has_bitmap_index) return false;
1549
0
    if (a._column_path == nullptr && a._column_path != nullptr) return false;
1550
0
    if (b._column_path == nullptr && a._column_path != nullptr) return false;
1551
0
    if (b._column_path != nullptr && a._column_path != nullptr &&
1552
0
        *a._column_path != *b._column_path)
1553
0
        return false;
1554
0
    return true;
1555
0
}
1556
1557
0
bool operator!=(const TabletColumn& a, const TabletColumn& b) {
1558
0
    return !(a == b);
1559
0
}
1560
1561
1
bool operator==(const TabletSchema& a, const TabletSchema& b) {
1562
1
    if (a._keys_type != b._keys_type) return false;
1563
1
    if (a._cols.size() != b._cols.size()) return false;
1564
1
    for (int i = 0; i < a._cols.size(); ++i) {
1565
0
        if (*a._cols[i] != *b._cols[i]) return false;
1566
0
    }
1567
1
    if (a._num_columns != b._num_columns) return false;
1568
1
    if (a._num_key_columns != b._num_key_columns) return false;
1569
1
    if (a._num_null_columns != b._num_null_columns) return false;
1570
1
    if (a._num_short_key_columns != b._num_short_key_columns) return false;
1571
1
    if (a._num_rows_per_row_block != b._num_rows_per_row_block) return false;
1572
1
    if (a._compress_kind != b._compress_kind) return false;
1573
1
    if (a._next_column_unique_id != b._next_column_unique_id) return false;
1574
1
    if (a._has_bf_fpp != b._has_bf_fpp) return false;
1575
1
    if (a._has_bf_fpp) {
1576
0
        if (std::abs(a._bf_fpp - b._bf_fpp) > 1e-6) return false;
1577
0
    }
1578
1
    if (a._is_in_memory != b._is_in_memory) return false;
1579
1
    if (a._delete_sign_idx != b._delete_sign_idx) return false;
1580
1
    if (a._disable_auto_compaction != b._disable_auto_compaction) return false;
1581
1
    if (a._enable_single_replica_compaction != b._enable_single_replica_compaction) return false;
1582
1
    if (a._store_row_column != b._store_row_column) return false;
1583
1
    if (a._row_store_page_size != b._row_store_page_size) return false;
1584
1
    if (a._storage_page_size != b._storage_page_size) return false;
1585
1
    if (a._skip_write_index_on_load != b._skip_write_index_on_load) return false;
1586
1
    if (a._enable_variant_flatten_nested != b._enable_variant_flatten_nested) return false;
1587
1
    return true;
1588
1
}
1589
1590
1
bool operator!=(const TabletSchema& a, const TabletSchema& b) {
1591
1
    return !(a == b);
1592
1
}
1593
1594
} // namespace doris