Coverage Report

Created: 2026-03-17 02:30

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