Coverage Report

Created: 2026-03-31 15:05

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