Coverage Report

Created: 2024-11-20 12:30

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