Coverage Report

Created: 2026-03-16 12:03

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