Coverage Report

Created: 2026-06-18 05:40

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