Coverage Report

Created: 2026-05-15 08:39

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