Coverage Report

Created: 2026-03-31 20:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_schema.h
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
#pragma once
19
20
#include <gen_cpp/Types_types.h>
21
#include <gen_cpp/olap_common.pb.h>
22
#include <gen_cpp/olap_file.pb.h>
23
#include <gen_cpp/segment_v2.pb.h>
24
#include <parallel_hashmap/phmap.h>
25
26
#include <algorithm>
27
#include <cstdint>
28
#include <map>
29
#include <memory>
30
#include <string>
31
#include <unordered_map>
32
#include <unordered_set>
33
#include <utility>
34
#include <vector>
35
36
#include "common/consts.h"
37
#include "common/status.h"
38
#include "core/data_type/define_primitive_type.h"
39
#include "core/string_ref.h"
40
#include "core/types.h"
41
#include "exec/common/string_utils/string_utils.h"
42
#include "exprs/aggregate/aggregate_function.h"
43
#include "runtime/descriptors.h"
44
#include "runtime/memory/lru_cache_policy.h"
45
#include "storage/index/inverted/inverted_index_parser.h"
46
#include "storage/metadata_adder.h"
47
#include "storage/olap_common.h"
48
#include "storage/segment/options.h"
49
#include "util/debug_points.h"
50
#include "util/json/path_in_data.h"
51
#include "util/string_parser.hpp"
52
#include "util/string_util.h"
53
54
namespace doris {
55
class Block;
56
class PathInData;
57
class IDataType;
58
59
#include "common/compile_check_begin.h"
60
61
struct OlapTableIndexSchema;
62
class TColumn;
63
class TOlapTableIndex;
64
class TabletColumn;
65
66
using TabletColumnPtr = std::shared_ptr<TabletColumn>;
67
68
class TabletColumn : public MetadataAdder<TabletColumn> {
69
public:
70
    struct VariantParams {
71
        int32_t max_subcolumns_count = 0;
72
        bool enable_typed_paths_to_sparse = false;
73
        int32_t max_sparse_column_statistics_size =
74
                BeConsts::DEFAULT_VARIANT_MAX_SPARSE_COLUMN_STATS_SIZE;
75
        // default to 0, no shard
76
        int32_t sparse_hash_shard_count = 0;
77
78
        bool enable_doc_mode = false;
79
        int64_t doc_materialization_min_rows = 0;
80
        int32_t doc_hash_shard_count = 64;
81
82
        bool enable_nested_group = false;
83
    };
84
85
    TabletColumn();
86
    TabletColumn(const ColumnPB& column);
87
    TabletColumn(const TColumn& column);
88
    TabletColumn(FieldAggregationMethod agg, FieldType type);
89
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable);
90
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable,
91
                 int32_t unique_id, size_t length);
92
93
#ifdef BE_TEST
94
76.9k
    virtual ~TabletColumn() = default;
95
#endif
96
97
    void init_from_pb(const ColumnPB& column);
98
    void init_from_thrift(const TColumn& column);
99
    void to_schema_pb(ColumnPB* column) const;
100
101
305k
    int32_t unique_id() const { return _unique_id; }
102
1.52k
    void set_unique_id(int32_t id) { _unique_id = id; }
103
336k
    const std::string& name() const { return _col_name; }
104
22.4k
    const std::string& name_lower_case() const { return _col_name_lower_case; }
105
4.67k
    void set_name(std::string col_name) {
106
4.67k
        _col_name = col_name;
107
4.67k
        _col_name_lower_case = to_lower(_col_name);
108
4.67k
    }
109
1.11M
    MOCK_FUNCTION FieldType type() const { return _type; }
110
4.99k
    void set_type(FieldType type) { _type = type; }
111
132k
    bool is_key() const { return _is_key; }
112
181k
    bool is_nullable() const { return _is_nullable; }
113
0
    bool is_auto_increment() const { return _is_auto_increment; }
114
0
    bool is_seqeunce_col() const { return _col_name == SEQUENCE_COL; }
115
0
    bool is_on_update_current_timestamp() const { return _is_on_update_current_timestamp; }
116
82.8k
    bool is_variant_type() const { return _type == FieldType::OLAP_FIELD_TYPE_VARIANT; }
117
18.7k
    bool is_bf_column() const { return _is_bf_column; }
118
56.9k
    bool is_array_type() const { return _type == FieldType::OLAP_FIELD_TYPE_ARRAY; }
119
12.5k
    bool is_agg_state_type() const { return _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE; }
120
0
    bool is_jsonb_type() const { return _type == FieldType::OLAP_FIELD_TYPE_JSONB; }
121
0
    bool is_length_variable_type() const {
122
0
        return _type == FieldType::OLAP_FIELD_TYPE_CHAR ||
123
0
               _type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
124
0
               _type == FieldType::OLAP_FIELD_TYPE_STRING ||
125
0
               _type == FieldType::OLAP_FIELD_TYPE_HLL ||
126
0
               _type == FieldType::OLAP_FIELD_TYPE_BITMAP ||
127
0
               _type == FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE ||
128
0
               _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE;
129
0
    }
130
    // Such columns are not exist in frontend schema info, so we need to
131
    // add them into tablet_schema for later column indexing.
132
    static TabletColumn create_materialized_variant_column(const std::string& root,
133
                                                           const std::vector<std::string>& paths,
134
                                                           int32_t parent_unique_id,
135
                                                           int32_t max_subcolumns_count,
136
                                                           bool enable_doc_mode = false);
137
9
    bool has_default_value() const { return _has_default_value; }
138
18.0k
    std::string default_value() const { return _default_value; }
139
178k
    int32_t length() const { return _length; }
140
2.33k
    void set_length(int32_t length) { _length = length; }
141
680
    void set_default_value(const std::string& default_value) {
142
680
        _default_value = default_value;
143
680
        _has_default_value = true;
144
680
    }
145
42.3k
    int32_t index_length() const { return _index_length; }
146
1.61k
    void set_index_length(int32_t index_length) { _index_length = index_length; }
147
1.31k
    void set_is_key(bool is_key) { _is_key = is_key; }
148
2.38k
    void set_is_nullable(bool is_nullable) { _is_nullable = is_nullable; }
149
0
    void set_is_auto_increment(bool is_auto_increment) { _is_auto_increment = is_auto_increment; }
150
0
    void set_is_on_update_current_timestamp(bool is_on_update_current_timestamp) {
151
0
        _is_on_update_current_timestamp = is_on_update_current_timestamp;
152
0
    }
153
    void set_path_info(const PathInData& path);
154
32.9k
    FieldAggregationMethod aggregation() const { return _aggregation; }
155
    AggregateFunctionPtr get_aggregate_function_union(DataTypePtr type,
156
                                                      int current_be_exec_version) const;
157
    AggregateFunctionPtr get_aggregate_function(std::string suffix,
158
                                                int current_be_exec_version) const;
159
139k
    int precision() const { return _precision; }
160
139k
    int frac() const { return _frac; }
161
2
    inline bool visible() const { return _visible; }
162
    bool has_char_type() const;
163
164
1.77k
    void set_aggregation_method(FieldAggregationMethod agg) {
165
1.77k
        _aggregation = agg;
166
1.77k
        _aggregation_name = get_string_by_aggregation_type(agg);
167
1.77k
    }
168
169
    /**
170
     * Add a sub column.
171
     */
172
    void add_sub_column(TabletColumn& sub_column);
173
174
20.7k
    uint32_t get_subtype_count() const { return _sub_column_count; }
175
3.56k
    MOCK_FUNCTION const TabletColumn& get_sub_column(uint64_t i) const { return *_sub_columns[i]; }
176
2.07k
    const std::vector<TabletColumnPtr>& get_sub_columns() const { return _sub_columns; }
177
178
    friend bool operator==(const TabletColumn& a, const TabletColumn& b);
179
    friend bool operator!=(const TabletColumn& a, const TabletColumn& b);
180
181
    static std::string get_string_by_field_type(FieldType type);
182
    static std::string get_string_by_aggregation_type(FieldAggregationMethod aggregation_type);
183
    static FieldType get_field_type_by_string(const std::string& str);
184
    static FieldType get_field_type_by_type(PrimitiveType type);
185
    static PrimitiveType get_primitive_type_by_field_type(FieldType type);
186
    static FieldAggregationMethod get_aggregation_type_by_string(const std::string& str);
187
    static uint32_t get_field_length_by_type(TPrimitiveType::type type, uint32_t string_length);
188
    bool is_row_store_column() const;
189
16.0k
    std::string get_aggregation_name() const { return _aggregation_name; }
190
16.0k
    bool get_result_is_nullable() const { return _result_is_nullable; }
191
16.0k
    int get_be_exec_version() const { return _be_exec_version; }
192
77.1k
    bool has_path_info() const { return _column_path != nullptr && !_column_path->empty(); }
193
59.3k
    const PathInDataPtr& path_info_ptr() const { return _column_path; }
194
    // If it is an extracted column from variant column
195
204k
    bool is_extracted_column() const {
196
204k
        return _column_path != nullptr && !_column_path->empty() && _parent_col_unique_id > 0;
197
204k
    };
198
28.6k
    std::string suffix_path() const {
199
28.6k
        return is_extracted_column() ? _column_path->get_path() : "";
200
28.6k
    }
201
5
    bool is_nested_subcolumn() const {
202
5
        return _column_path != nullptr && _column_path->has_nested_part();
203
5
    }
204
41.5k
    int32_t parent_unique_id() const { return _parent_col_unique_id; }
205
1.85k
    void set_parent_unique_id(int32_t col_unique_id) { _parent_col_unique_id = col_unique_id; }
206
1.04k
    void set_is_bf_column(bool is_bf_column) { _is_bf_column = is_bf_column; }
207
    std::shared_ptr<const IDataType> get_vec_type() const;
208
209
680
    Status check_valid() const {
210
680
        if (type() != FieldType::OLAP_FIELD_TYPE_ARRAY &&
211
680
            type() != FieldType::OLAP_FIELD_TYPE_STRUCT &&
212
680
            type() != FieldType::OLAP_FIELD_TYPE_MAP) {
213
680
            return Status::OK();
214
680
        }
215
0
        if (is_bf_column()) {
216
0
            return Status::NotSupported("Do not support bloom filter index, type={}",
217
0
                                        get_string_by_field_type(type()));
218
0
        }
219
0
        return Status::OK();
220
0
    }
221
222
2
    void set_precision(int precision) {
223
2
        _precision = precision;
224
2
        _is_decimal = true;
225
2
    }
226
227
33
    void set_frac(int frac) { _frac = frac; }
228
229
0
    const VariantParams& variant_params() const { return _variant; }
230
0
    VariantParams* mutable_variant_params() { return &_variant; }
231
232
2.17k
    int32_t variant_max_subcolumns_count() const { return _variant.max_subcolumns_count; }
233
234
70
    void set_variant_max_subcolumns_count(int32_t variant_max_subcolumns_count) {
235
70
        _variant.max_subcolumns_count = variant_max_subcolumns_count;
236
70
    }
237
238
116
    PatternTypePB pattern_type() const { return _pattern_type; }
239
240
404
    bool variant_enable_typed_paths_to_sparse() const {
241
404
        return _variant.enable_typed_paths_to_sparse;
242
404
    }
243
244
20.7k
    int32_t variant_max_sparse_column_statistics_size() const {
245
20.7k
        return _variant.max_sparse_column_statistics_size;
246
20.7k
    }
247
248
315
    int32_t variant_sparse_hash_shard_count() const { return _variant.sparse_hash_shard_count; }
249
250
2.60k
    bool variant_enable_doc_mode() const { return _variant.enable_doc_mode; }
251
252
6
    int64_t variant_doc_materialization_min_rows() const {
253
6
        return _variant.doc_materialization_min_rows;
254
6
    }
255
256
4
    int32_t variant_doc_hash_shard_count() const { return _variant.doc_hash_shard_count; }
257
258
0
    void set_variant_doc_materialization_min_rows(int64_t variant_doc_materialization_min_rows) {
259
0
        _variant.doc_materialization_min_rows = variant_doc_materialization_min_rows;
260
0
    }
261
262
0
    void set_variant_doc_hash_shard_count(int32_t variant_doc_hash_shard_count) {
263
0
        _variant.doc_hash_shard_count = variant_doc_hash_shard_count;
264
0
    }
265
266
    void set_variant_max_sparse_column_statistics_size(
267
2
            int32_t variant_max_sparse_column_statistics_size) {
268
2
        _variant.max_sparse_column_statistics_size = variant_max_sparse_column_statistics_size;
269
2
    }
270
271
0
    void set_variant_sparse_hash_shard_count(int32_t variant_sparse_hash_shard_count) {
272
0
        _variant.sparse_hash_shard_count = variant_sparse_hash_shard_count;
273
0
    }
274
275
13
    void set_variant_enable_doc_mode(bool variant_enable_doc_mode) {
276
13
        _variant.enable_doc_mode = variant_enable_doc_mode;
277
13
    }
278
279
1
    void set_variant_enable_typed_paths_to_sparse(bool variant_enable_typed_paths_to_sparse) {
280
1
        _variant.enable_typed_paths_to_sparse = variant_enable_typed_paths_to_sparse;
281
1
    }
282
283
2.83k
    bool variant_enable_nested_group() const { return _variant.enable_nested_group; }
284
285
0
    void set_variant_enable_nested_group(bool val) { _variant.enable_nested_group = val; }
286
287
31
    bool is_decimal() const { return _is_decimal; }
288
289
private:
290
    int32_t _unique_id = -1;
291
    std::string _col_name;
292
    std::string _col_name_lower_case;
293
    // the field _type will change from TPrimitiveType
294
    // to string by 'EnumToString(TPrimitiveType, tcolumn.column_type.type, data_type);' (reference: TabletMeta::init_column_from_tcolumn)
295
    // to FieldType by 'TabletColumn::get_field_type_by_string' (reference: TabletColumn::init_from_pb).
296
    // And the _type in columnPB is string and it changed from FieldType by 'get_string_by_field_type' (reference: TabletColumn::to_schema_pb).
297
    FieldType _type;
298
    bool _is_key = false;
299
    FieldAggregationMethod _aggregation;
300
    std::string _aggregation_name;
301
    bool _is_nullable = false;
302
    bool _is_auto_increment = false;
303
    bool _is_on_update_current_timestamp {false};
304
305
    bool _has_default_value = false;
306
    std::string _default_value;
307
308
    bool _is_decimal = false;
309
    int32_t _precision = -1;
310
    int32_t _frac = -1;
311
312
    int32_t _length = -1;
313
    int32_t _index_length = -1;
314
315
    bool _is_bf_column = false;
316
317
    bool _visible = true;
318
319
    std::vector<TabletColumnPtr> _sub_columns;
320
    uint32_t _sub_column_count = 0;
321
322
    bool _result_is_nullable = false;
323
    int _be_exec_version = -1;
324
325
    // The extracted sub-columns from "variant" contain the following information:
326
    int32_t _parent_col_unique_id = -1; // "variant" -> col_unique_id
327
    PathInDataPtr _column_path;         // the path of the sub-columns themselves
328
    PatternTypePB _pattern_type = PatternTypePB::MATCH_NAME_GLOB;
329
330
    VariantParams _variant;
331
};
332
333
bool operator==(const TabletColumn& a, const TabletColumn& b);
334
bool operator!=(const TabletColumn& a, const TabletColumn& b);
335
336
class TabletIndex : public MetadataAdder<TabletIndex> {
337
public:
338
7.75k
    TabletIndex() = default;
339
    void init_from_thrift(const TOlapTableIndex& index, const TabletSchema& tablet_schema);
340
    void init_from_thrift(const TOlapTableIndex& index, const std::vector<int32_t>& column_uids);
341
    void init_from_pb(const TabletIndexPB& index);
342
    void to_schema_pb(TabletIndexPB* index) const;
343
344
19.2k
    int64_t index_id() const { return _index_id; }
345
25
    const std::string& index_name() const { return _index_name; }
346
12.2k
    MOCK_FUNCTION IndexType index_type() const { return _index_type; }
347
9.60k
    const std::vector<int32_t>& col_unique_ids() const { return _col_unique_ids; }
348
26.5k
    MOCK_FUNCTION const std::map<std::string, std::string>& properties() const {
349
26.5k
        return _properties;
350
26.5k
    }
351
1
    int32_t get_gram_size() const {
352
1
        if (_properties.contains("gram_size")) {
353
1
            return std::stoi(_properties.at("gram_size"));
354
1
        }
355
356
0
        return 0;
357
1
    }
358
1
    int32_t get_gram_bf_size() const {
359
1
        if (_properties.contains("bf_size")) {
360
1
            return std::stoi(_properties.at("bf_size"));
361
1
        }
362
363
0
        return 0;
364
1
    }
365
366
16.0k
    const std::string& get_index_suffix() const { return _escaped_index_suffix_path; }
367
368
    void set_escaped_escaped_index_suffix_path(const std::string& name);
369
370
2.28k
    bool is_inverted_index() const { return _index_type == IndexType::INVERTED; }
371
372
2
    bool is_ann_index() const { return _index_type == IndexType::ANN; }
373
374
8
    void remove_parser_and_analyzer() {
375
8
        _properties.erase(INVERTED_INDEX_PARSER_KEY);
376
8
        _properties.erase(INVERTED_INDEX_PARSER_KEY_ALIAS);
377
8
        _properties.erase(INVERTED_INDEX_ANALYZER_NAME_KEY);
378
8
        _properties.erase(INVERTED_INDEX_NORMALIZER_NAME_KEY);
379
8
    }
380
381
7.49k
    std::string field_pattern() const {
382
7.49k
        if (_properties.contains("field_pattern")) {
383
8
            return _properties.at("field_pattern");
384
8
        }
385
7.48k
        return "";
386
7.49k
    }
387
388
8
    bool is_same_except_id(const TabletIndex* other) const {
389
8
        return _escaped_index_suffix_path == other->_escaped_index_suffix_path &&
390
8
               _index_name == other->_index_name && _index_type == other->_index_type &&
391
8
               _col_unique_ids == other->_col_unique_ids && _properties == other->_properties;
392
8
    }
393
394
private:
395
    int64_t _index_id = -1;
396
    // Identify the different index with the same _index_id
397
    std::string _escaped_index_suffix_path;
398
    std::string _index_name;
399
    IndexType _index_type;
400
    std::vector<int32_t> _col_unique_ids;
401
    std::map<std::string, std::string> _properties;
402
};
403
404
using TabletIndexPtr = std::shared_ptr<TabletIndex>;
405
using TabletIndexes = std::vector<std::shared_ptr<TabletIndex>>;
406
using PathSet = phmap::flat_hash_set<std::string>;
407
408
class TabletSchema : public MetadataAdder<TabletSchema> {
409
public:
410
    enum class ColumnType { NORMAL = 0, DROPPED = 1, VARIANT = 2 };
411
    // TODO(yingchun): better to make constructor as private to avoid
412
    // manually init members incorrectly, and define a new function like
413
    // void create_from_pb(const TabletSchemaPB& schema, TabletSchema* tablet_schema).
414
    TabletSchema();
415
    ~TabletSchema() override;
416
417
    // Init from pb
418
    // ignore_extracted_columns: ignore the extracted columns from variant column
419
    // reuse_cached_column: reuse the cached column in the schema if they are the same, to reduce memory usage
420
    void init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns = false,
421
                      bool reuse_cached_column = false);
422
    // Notice: Use deterministic way to serialize protobuf,
423
    // since serialize Map in protobuf may could lead to un-deterministic by default
424
    template <class PbType>
425
7.52k
    static std::string deterministic_string_serialize(const PbType& pb) {
426
7.52k
        std::string output;
427
7.52k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
428
7.52k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
429
7.52k
        output_stream.SetSerializationDeterministic(true);
430
7.52k
        pb.SerializeToCodedStream(&output_stream);
431
7.52k
        return output;
432
7.52k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_14TabletSchemaPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
425
7.00k
    static std::string deterministic_string_serialize(const PbType& pb) {
426
7.00k
        std::string output;
427
7.00k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
428
7.00k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
429
7.00k
        output_stream.SetSerializationDeterministic(true);
430
7.00k
        pb.SerializeToCodedStream(&output_stream);
431
7.00k
        return output;
432
7.00k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_8ColumnPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
425
360
    static std::string deterministic_string_serialize(const PbType& pb) {
426
360
        std::string output;
427
360
        google::protobuf::io::StringOutputStream string_output_stream(&output);
428
360
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
429
360
        output_stream.SetSerializationDeterministic(true);
430
360
        pb.SerializeToCodedStream(&output_stream);
431
360
        return output;
432
360
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_13TabletIndexPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
425
163
    static std::string deterministic_string_serialize(const PbType& pb) {
426
163
        std::string output;
427
163
        google::protobuf::io::StringOutputStream string_output_stream(&output);
428
163
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
429
163
        output_stream.SetSerializationDeterministic(true);
430
163
        pb.SerializeToCodedStream(&output_stream);
431
163
        return output;
432
163
    }
433
    void to_schema_pb(TabletSchemaPB* tablet_meta_pb) const;
434
    void append_column(TabletColumn column, ColumnType col_type = ColumnType::NORMAL);
435
    void append_index(TabletIndex&& index);
436
    void remove_index(int64_t index_id);
437
    void clear_index();
438
    // Must make sure the row column is always the last column
439
    void add_row_column();
440
    void copy_from(const TabletSchema& tablet_schema);
441
    // lightweight copy, take care of lifecycle of TabletColumn
442
    void shawdow_copy_without_columns(const TabletSchema& tablet_schema);
443
    void update_index_info_from(const TabletSchema& tablet_schema);
444
    std::string to_key() const;
445
    // get_metadata_size is only the memory of the TabletSchema itself, not include child objects.
446
83
    int64_t mem_size() const { return get_metadata_size(); }
447
    size_t row_size() const;
448
    int32_t field_index(const std::string& field_name) const;
449
    int32_t field_index(const PathInData& path) const;
450
    int32_t field_index(int32_t col_unique_id) const;
451
    const TabletColumn& column(size_t ordinal) const;
452
    Result<const TabletColumn*> column(const std::string& field_name) const;
453
    Status have_column(const std::string& field_name) const;
454
    bool exist_column(const std::string& field_name) const;
455
    bool has_column_unique_id(int32_t col_unique_id) const;
456
    const TabletColumn& column_by_uid(int32_t col_unique_id) const;
457
    TabletColumn& mutable_column_by_uid(int32_t col_unique_id);
458
    TabletColumn& mutable_column(size_t ordinal);
459
    void replace_column(size_t pos, TabletColumn new_col);
460
    const std::vector<TabletColumnPtr>& columns() const;
461
793k
    size_t num_columns() const { return _num_columns; }
462
1.08M
    size_t num_key_columns() const { return _num_key_columns; }
463
136k
    const std::vector<uint32_t>& cluster_key_uids() const { return _cluster_key_uids; }
464
0
    size_t num_null_columns() const { return _num_null_columns; }
465
5.38k
    size_t num_short_key_columns() const { return _num_short_key_columns; }
466
0
    size_t num_rows_per_row_block() const { return _num_rows_per_row_block; }
467
10.4k
    size_t num_variant_columns() const { return _num_variant_columns; };
468
0
    size_t num_virtual_columns() const { return _num_virtual_columns; }
469
7.12M
    KeysType keys_type() const { return _keys_type; }
470
5.63k
    SortType sort_type() const { return _sort_type; }
471
0
    size_t sort_col_num() const { return _sort_col_num; }
472
0
    CompressKind compress_kind() const { return _compress_kind; }
473
0
    size_t next_column_unique_id() const { return _next_column_unique_id; }
474
4
    bool has_bf_fpp() const { return _has_bf_fpp; }
475
4
    double bloom_filter_fpp() const { return _bf_fpp; }
476
13.4k
    bool is_in_memory() const { return _is_in_memory; }
477
0
    void set_is_in_memory(bool is_in_memory) { _is_in_memory = is_in_memory; }
478
5
    void set_disable_auto_compaction(bool disable_auto_compaction) {
479
5
        _disable_auto_compaction = disable_auto_compaction;
480
5
    }
481
306
    bool disable_auto_compaction() const { return _disable_auto_compaction; }
482
    // Deprecated legacy switch for flatten-nested variant behavior.
483
    // It is distinct from variant_enable_nested_group.
484
0
    void set_deprecated_variant_flatten_nested(bool flatten_nested) {
485
0
        _deprecated_enable_variant_flatten_nested = flatten_nested;
486
0
    }
487
275
    bool deprecated_variant_flatten_nested() const {
488
275
        return _deprecated_enable_variant_flatten_nested;
489
275
    }
490
0
    void set_enable_single_replica_compaction(bool enable_single_replica_compaction) {
491
0
        _enable_single_replica_compaction = enable_single_replica_compaction;
492
0
    }
493
356
    bool enable_single_replica_compaction() const { return _enable_single_replica_compaction; }
494
    // indicate if full row store column(all the columns encodes as row) exists
495
0
    bool has_row_store_for_all_columns() const {
496
0
        return _store_row_column && row_columns_uids().empty();
497
0
    }
498
0
    void set_skip_write_index_on_load(bool skip) { _skip_write_index_on_load = skip; }
499
77
    bool skip_write_index_on_load() const { return _skip_write_index_on_load; }
500
8.56k
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
501
143k
    bool has_sequence_col() const { return _sequence_col_idx != -1; }
502
64.7k
    int32_t sequence_col_idx() const { return _sequence_col_idx; }
503
0
    void set_version_col_idx(int32_t version_col_idx) { _version_col_idx = version_col_idx; }
504
0
    int32_t version_col_idx() const { return _version_col_idx; }
505
0
    bool has_skip_bitmap_col() const { return _skip_bitmap_col_idx != -1; }
506
0
    int32_t skip_bitmap_col_idx() const { return _skip_bitmap_col_idx; }
507
5.26k
    segment_v2::CompressionTypePB compression_type() const { return _compression_type; }
508
0
    void set_row_store_page_size(long page_size) { _row_store_page_size = page_size; }
509
0
    long row_store_page_size() const { return _row_store_page_size; }
510
0
    void set_storage_page_size(long storage_page_size) { _storage_page_size = storage_page_size; }
511
15.9k
    long storage_page_size() const { return _storage_page_size; }
512
0
    void set_storage_dict_page_size(long storage_dict_page_size) {
513
0
        _storage_dict_page_size = storage_dict_page_size;
514
0
    }
515
15.9k
    long storage_dict_page_size() const { return _storage_dict_page_size; }
516
0
    bool has_global_row_id() const {
517
0
        for (auto [col_name, _] : _field_name_to_index) {
518
0
            if (col_name.start_with(StringRef(BeConsts::GLOBAL_ROWID_COL.data(),
519
0
                                              BeConsts::GLOBAL_ROWID_COL.size()))) {
520
0
                return true;
521
0
            }
522
0
        }
523
0
        return false;
524
0
    }
525
526
122
    const std::vector<const TabletIndex*> inverted_indexes() const {
527
122
        std::vector<const TabletIndex*> inverted_indexes;
528
1.26k
        for (const auto& index : _indexes) {
529
1.26k
            if (index->index_type() == IndexType::INVERTED) {
530
1.26k
                inverted_indexes.emplace_back(index.get());
531
1.26k
            }
532
1.26k
        }
533
122
        return inverted_indexes;
534
122
    }
535
12.1k
    bool has_inverted_index() const {
536
12.1k
        for (const auto& index : _indexes) {
537
837
            DBUG_EXECUTE_IF("tablet_schema::has_inverted_index", {
538
837
                if (index->col_unique_ids().empty()) {
539
837
                    throw Exception(Status::InternalError("col unique ids cannot be empty"));
540
837
                }
541
837
            });
542
543
837
            if (index->index_type() == IndexType::INVERTED) {
544
                //if index_id == -1, ignore it.
545
829
                if (!index->col_unique_ids().empty() && index->col_unique_ids()[0] >= 0) {
546
829
                    return true;
547
829
                }
548
829
            }
549
837
        }
550
11.3k
        return false;
551
12.1k
    }
552
553
10.7k
    bool has_ann_index() const {
554
10.7k
        for (const auto& index : _indexes) {
555
7
            if (index->index_type() == IndexType::ANN) {
556
5
                if (!index->col_unique_ids().empty() && index->col_unique_ids()[0] >= 0) {
557
5
                    return true;
558
5
                }
559
5
            }
560
7
        }
561
10.7k
        return false;
562
10.7k
    }
563
564
    bool has_inverted_index_with_index_id(int64_t index_id) const;
565
566
    std::vector<const TabletIndex*> inverted_indexs(const TabletColumn& col) const;
567
568
    std::vector<const TabletIndex*> inverted_indexs(int32_t col_unique_id,
569
                                                    const std::string& suffix_path = "") const;
570
    const TabletIndex* ann_index(const TabletColumn& col) const;
571
572
    // Regardless of whether this column supports inverted index
573
    // TabletIndex information will be returned as long as it exists.
574
    const TabletIndex* ann_index(int32_t col_unique_id, const std::string& suffix_path = "") const;
575
576
    std::vector<TabletIndexPtr> inverted_index_by_field_pattern(
577
            int32_t col_unique_id, const std::string& field_pattern) const;
578
579
    bool has_ngram_bf_index(int32_t col_unique_id) const;
580
    const TabletIndex* get_ngram_bf_index(int32_t col_unique_id) const;
581
    const TabletIndex* get_index(int32_t col_unique_id, IndexType index_type,
582
                                 const std::string& suffix_path) const;
583
    void update_indexes_from_thrift(const std::vector<doris::TOlapTableIndex>& indexes);
584
    // If schema version is not set, it should be -1
585
7.45k
    int32_t schema_version() const { return _schema_version; }
586
    void clear_columns();
587
    Block create_block(
588
            const std::vector<uint32_t>& return_columns,
589
            const std::unordered_set<uint32_t>* tablet_columns_need_convert_null = nullptr) const;
590
    Block create_block() const;
591
2
    void set_schema_version(int32_t version) { _schema_version = version; }
592
0
    void set_auto_increment_column(const std::string& auto_increment_column) {
593
0
        _auto_increment_column = auto_increment_column;
594
0
    }
595
0
    std::string auto_increment_column() const { return _auto_increment_column; }
596
597
28
    void set_table_id(int64_t table_id) { _table_id = table_id; }
598
498
    int64_t table_id() const { return _table_id; }
599
28
    void set_db_id(int64_t db_id) { _db_id = db_id; }
600
0
    int64_t db_id() const { return _db_id; }
601
    void build_current_tablet_schema(int64_t index_id, int32_t version,
602
                                     const OlapTableIndexSchema* index,
603
                                     const TabletSchema& out_tablet_schema);
604
605
    // Merge columns that not exit in current schema, these column is dropped in current schema
606
    // but they are useful in some cases. For example,
607
    // 1. origin schema is  ColA, ColB
608
    // 2. insert values     1, 2
609
    // 3. delete where ColB = 2
610
    // 4. drop ColB
611
    // 5. insert values  3
612
    // 6. add column ColB, although it is name ColB, but it is different with previous ColB, the new ColB we name could call ColB'
613
    // 7. insert value  4, 5
614
    // Then the read schema should be ColA, ColB, ColB' because the delete predicate need ColB to remove related data.
615
    // Because they have same name, so that the dropped column should not be added to the map, only with unique id.
616
    void merge_dropped_columns(const TabletSchema& src_schema);
617
618
    bool is_dropped_column(const TabletColumn& col) const;
619
620
    // copy extracted columns from src_schema
621
    void copy_extracted_columns(const TabletSchema& src_schema);
622
623
    // only reserve extracted columns
624
    void reserve_extracted_columns();
625
626
4.04k
    std::string get_all_field_names() const {
627
4.04k
        std::string str = "[";
628
4.04k
        for (auto p : _field_name_to_index) {
629
4.04k
            if (str.size() > 1) {
630
0
                str += ", ";
631
0
            }
632
4.04k
            str += p.first.to_string() + "(" + std::to_string(_cols[p.second]->unique_id()) + ")";
633
4.04k
        }
634
4.04k
        str += "]";
635
4.04k
        return str;
636
4.04k
    }
637
638
    // Dump [(name, type, is_nullable), ...]
639
1
    std::string dump_structure() const {
640
1
        std::string str = "[";
641
15
        for (auto p : _cols) {
642
15
            if (str.size() > 1) {
643
14
                str += ", ";
644
14
            }
645
15
            str += "(";
646
15
            str += p->name();
647
15
            str += ", ";
648
15
            str += TabletColumn::get_string_by_field_type(p->type());
649
15
            str += ", ";
650
15
            str += "is_nullable:";
651
15
            str += (p->is_nullable() ? "true" : "false");
652
15
            str += ")";
653
15
        }
654
1
        str += "]";
655
1
        return str;
656
1
    }
657
658
1
    std::string dump_full_schema() const {
659
1
        std::string str = "[";
660
4
        for (auto p : _cols) {
661
4
            if (str.size() > 1) {
662
3
                str += ", ";
663
3
            }
664
4
            ColumnPB col_pb;
665
4
            p->to_schema_pb(&col_pb);
666
4
            str += "(";
667
4
            str += col_pb.ShortDebugString();
668
4
            str += ")";
669
4
        }
670
1
        str += "]";
671
1
        return str;
672
1
    }
673
674
    Block create_block_by_cids(const std::vector<uint32_t>& cids) const;
675
676
    std::shared_ptr<TabletSchema> copy_without_variant_extracted_columns();
677
9.49k
    InvertedIndexStorageFormatPB get_inverted_index_storage_format() const {
678
9.49k
        return _inverted_index_storage_format;
679
9.49k
    }
680
681
    void update_tablet_columns(const TabletSchema& tablet_schema,
682
                               const std::vector<TColumn>& t_columns);
683
684
0
    const std::vector<int32_t>& row_columns_uids() const { return _row_store_column_unique_ids; }
685
686
    int64_t get_metadata_size() const override;
687
688
    struct SubColumnInfo {
689
        TabletColumn column;
690
        TabletIndexes indexes;
691
    };
692
693
    // all path in path_set_info are relative to the parent column
694
    struct PathsSetInfo {
695
        std::unordered_map<std::string, SubColumnInfo> typed_path_set;    // typed columns
696
        std::unordered_map<std::string, TabletIndexes> subcolumn_indexes; // subcolumns indexes
697
        PathSet sub_path_set;                                             // extracted columns
698
        PathSet sparse_path_set;                                          // sparse columns
699
    };
700
701
42
    void set_path_set_info(std::unordered_map<int32_t, PathsSetInfo>&& path_set_info_map) {
702
42
        _path_set_info_map = std::move(path_set_info_map);
703
42
    }
704
705
2
    const PathsSetInfo& path_set_info(int32_t unique_id) const {
706
2
        return _path_set_info_map.at(unique_id);
707
2
    }
708
709
6
    bool need_record_variant_extended_schema() const { return variant_max_subcolumns_count() == 0; }
710
711
11
    int32_t variant_max_subcolumns_count() const {
712
12
        for (const auto& col : _cols) {
713
12
            if (col->is_variant_type()) {
714
7
                return col->variant_max_subcolumns_count();
715
7
            }
716
12
        }
717
4
        return 0;
718
11
    }
719
    const std::unordered_map<uint32_t, std::vector<uint32_t>>& seq_col_idx_to_value_cols_idx()
720
0
            const {
721
0
        return _seq_col_idx_to_value_cols_idx;
722
0
    }
723
724
4.76k
    bool has_seq_map() const { return !_seq_col_idx_to_value_cols_idx.empty(); }
725
726
0
    const std::unordered_map<uint32_t, uint32_t>& value_col_idx_to_seq_col_idx() const {
727
0
        return _value_col_idx_to_seq_col_idx;
728
0
    }
729
730
0
    void add_pruned_columns_data_type(int32_t col_unique_id, DataTypePtr data_type) {
731
0
        _pruned_columns_data_type[col_unique_id] = std::move(data_type);
732
0
    }
733
734
0
    void clear_pruned_columns_data_type() { _pruned_columns_data_type.clear(); }
735
736
0
    bool has_pruned_columns() const { return !_pruned_columns_data_type.empty(); }
737
738
    // Whether new segments use externalized ColumnMetaPB layout (CMO) by default
739
5.27k
    bool is_external_segment_column_meta_used() const {
740
5.27k
        return _is_external_segment_column_meta_used;
741
5.27k
    }
742
743
33
    void set_external_segment_meta_used_default(bool v) {
744
33
        _is_external_segment_column_meta_used = v;
745
33
    }
746
747
15.9k
    bool integer_type_default_use_plain_encoding() const {
748
15.9k
        return _integer_type_default_use_plain_encoding;
749
15.9k
    }
750
751
0
    void set_integer_type_default_use_plain_encoding(bool v) {
752
0
        _integer_type_default_use_plain_encoding = v;
753
0
    }
754
755
15.9k
    BinaryPlainEncodingTypePB binary_plain_encoding_default_impl() const {
756
15.9k
        return _binary_plain_encoding_default_impl;
757
15.9k
    }
758
759
0
    void set_binary_plain_encoding_default_impl(BinaryPlainEncodingTypePB impl) {
760
0
        _binary_plain_encoding_default_impl = impl;
761
0
    }
762
763
private:
764
    friend bool operator==(const TabletSchema& a, const TabletSchema& b);
765
    friend bool operator!=(const TabletSchema& a, const TabletSchema& b);
766
0
    TabletSchema(const TabletSchema&) = default;
767
768
    KeysType _keys_type = DUP_KEYS;
769
    SortType _sort_type = SortType::LEXICAL;
770
    size_t _sort_col_num = 0;
771
    std::vector<TabletColumnPtr> _cols;
772
773
    std::vector<TabletIndexPtr> _indexes;
774
    std::unordered_map<StringRef, int32_t, StringRefHash> _field_name_to_index;
775
    std::unordered_map<int32_t, int32_t> _field_uniqueid_to_index;
776
    std::unordered_map<PathInDataRef, int32_t, PathInDataRef::Hash> _field_path_to_index;
777
778
    // index_type/col_unique_id/suffix -> idxs in _indexes
779
    using IndexKey = std::tuple<IndexType, int32_t, std::string>;
780
    struct IndexKeyHash {
781
57.2k
        size_t operator()(const IndexKey& t) const {
782
57.2k
            uint32_t seed = 0;
783
57.2k
            seed = doris::HashUtil::hash((const char*)&std::get<0>(t), sizeof(std::get<0>(t)),
784
57.2k
                                         seed);
785
57.2k
            seed = doris::HashUtil::hash((const char*)&std::get<1>(t), sizeof(std::get<1>(t)),
786
57.2k
                                         seed);
787
57.2k
            seed = doris::HashUtil::hash((const char*)std::get<2>(t).c_str(),
788
57.2k
                                         static_cast<uint32_t>(std::get<2>(t).size()), seed);
789
57.2k
            return seed;
790
57.2k
        }
791
    };
792
    std::unordered_map<IndexKey, std::vector<size_t>, IndexKeyHash> _col_id_suffix_to_index;
793
794
    int32_t _num_columns = 0;
795
    size_t _num_variant_columns = 0;
796
    size_t _num_virtual_columns = 0;
797
    size_t _num_key_columns = 0;
798
    std::vector<uint32_t> _cluster_key_uids;
799
    size_t _num_null_columns = 0;
800
    size_t _num_short_key_columns = 0;
801
    size_t _num_rows_per_row_block = 0;
802
    CompressKind _compress_kind = COMPRESS_NONE;
803
    segment_v2::CompressionTypePB _compression_type = segment_v2::CompressionTypePB::LZ4F;
804
    long _row_store_page_size = segment_v2::ROW_STORE_PAGE_SIZE_DEFAULT_VALUE;
805
    long _storage_page_size = segment_v2::STORAGE_PAGE_SIZE_DEFAULT_VALUE;
806
    long _storage_dict_page_size = segment_v2::STORAGE_DICT_PAGE_SIZE_DEFAULT_VALUE;
807
    size_t _next_column_unique_id = 0;
808
    std::string _auto_increment_column;
809
810
    bool _has_bf_fpp = false;
811
    double _bf_fpp = 0;
812
    bool _is_in_memory = false;
813
    int32_t _delete_sign_idx = -1;
814
    int32_t _sequence_col_idx = -1;
815
    int32_t _version_col_idx = -1;
816
    int32_t _skip_bitmap_col_idx = -1;
817
    int32_t _schema_version = -1;
818
    int64_t _table_id = -1;
819
    int64_t _db_id = -1;
820
    bool _disable_auto_compaction = false;
821
    bool _enable_single_replica_compaction = false;
822
    bool _store_row_column = false;
823
    bool _skip_write_index_on_load = false;
824
    InvertedIndexStorageFormatPB _inverted_index_storage_format = InvertedIndexStorageFormatPB::V1;
825
826
    // Contains column ids of which columns should be encoded into row store.
827
    // ATTN: For compability reason empty cids means all columns of tablet schema are encoded to row column
828
    std::vector<int32_t> _row_store_column_unique_ids;
829
    bool _deprecated_enable_variant_flatten_nested = false;
830
831
    std::map<size_t, int32_t> _vir_col_idx_to_unique_id;
832
    std::map<int32_t, DataTypePtr> _pruned_columns_data_type;
833
834
    // value: extracted path set and sparse path set
835
    std::unordered_map<int32_t, PathsSetInfo> _path_set_info_map;
836
837
    // key: field_pattern
838
    // value: indexes
839
    using PatternToIndex = std::unordered_map<std::string, std::vector<TabletIndexPtr>>;
840
    std::unordered_map<int32_t, PatternToIndex> _index_by_unique_id_with_pattern;
841
842
    // Default behavior for new segments: use external ColumnMeta region + CMO table if true
843
    bool _is_external_segment_column_meta_used = false;
844
845
    bool _integer_type_default_use_plain_encoding {false};
846
    BinaryPlainEncodingTypePB _binary_plain_encoding_default_impl {
847
            BinaryPlainEncodingTypePB::BINARY_PLAIN_ENCODING_V1};
848
    // Sequence column unique id mapping to value columns unique id
849
    std::unordered_map<uint32_t, std::vector<uint32_t>> _seq_col_uid_to_value_cols_uid;
850
    // Value column unique id mapping to sequence column unique id(also map sequence column it self)
851
    std::unordered_map<uint32_t, uint32_t> _value_col_uid_to_seq_col_uid;
852
    // Sequence column index mapping to value column index
853
    std::unordered_map<uint32_t, std::vector<uint32_t>> _seq_col_idx_to_value_cols_idx;
854
    // Value column index mapping to sequence column index(also map sequence column it self)
855
    std::unordered_map<uint32_t, uint32_t> _value_col_idx_to_seq_col_idx;
856
};
857
858
bool operator==(const TabletSchema& a, const TabletSchema& b);
859
bool operator!=(const TabletSchema& a, const TabletSchema& b);
860
861
using TabletSchemaSPtr = std::shared_ptr<TabletSchema>;
862
863
#include "common/compile_check_end.h"
864
} // namespace doris