Coverage Report

Created: 2026-04-02 10:23

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