Coverage Report

Created: 2026-07-14 14:34

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