Coverage Report

Created: 2026-03-13 03:47

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