Coverage Report

Created: 2026-03-12 14:13

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