Coverage Report

Created: 2026-08-01 07:28

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