Coverage Report

Created: 2026-05-29 11:01

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/AgentService_types.h>
21
#include <gen_cpp/Types_types.h>
22
#include <gen_cpp/olap_common.pb.h>
23
#include <gen_cpp/olap_file.pb.h>
24
#include <gen_cpp/segment_v2.pb.h>
25
#include <parallel_hashmap/phmap.h>
26
27
#include <algorithm>
28
#include <cstdint>
29
#include <map>
30
#include <memory>
31
#include <string>
32
#include <unordered_map>
33
#include <unordered_set>
34
#include <utility>
35
#include <vector>
36
37
#include "common/consts.h"
38
#include "common/status.h"
39
#include "core/data_type/define_primitive_type.h"
40
#include "core/string_ref.h"
41
#include "core/types.h"
42
#include "exec/common/string_utils/string_utils.h"
43
#include "exprs/aggregate/aggregate_function.h"
44
#include "runtime/descriptors.h"
45
#include "runtime/memory/lru_cache_policy.h"
46
#include "storage/index/inverted/inverted_index_parser.h"
47
#include "storage/metadata_adder.h"
48
#include "storage/olap_common.h"
49
#include "storage/segment/options.h"
50
#include "util/debug_points.h"
51
#include "util/json/path_in_data.h"
52
#include "util/string_parser.hpp"
53
#include "util/string_util.h"
54
55
namespace doris {
56
class Block;
57
class PathInData;
58
class IDataType;
59
60
struct OlapTableIndexSchema;
61
class TColumn;
62
class TOlapTableIndex;
63
class TabletColumn;
64
65
using TabletColumnPtr = std::shared_ptr<TabletColumn>;
66
67
class TabletColumn : public MetadataAdder<TabletColumn> {
68
public:
69
    struct VariantParams {
70
        int32_t max_subcolumns_count = 0;
71
        bool enable_typed_paths_to_sparse = false;
72
        int32_t max_sparse_column_statistics_size =
73
                BeConsts::DEFAULT_VARIANT_MAX_SPARSE_COLUMN_STATS_SIZE;
74
        // default to 0, no shard
75
        int32_t sparse_hash_shard_count = 0;
76
77
        bool enable_doc_mode = false;
78
        int64_t doc_materialization_min_rows = 0;
79
        int32_t doc_hash_shard_count = 64;
80
81
        bool enable_nested_group = false;
82
    };
83
84
    TabletColumn();
85
    TabletColumn(const ColumnPB& column);
86
    TabletColumn(const TColumn& column);
87
    TabletColumn(FieldAggregationMethod agg, FieldType type);
88
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable);
89
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable,
90
                 int32_t unique_id, size_t length);
91
92
#ifdef BE_TEST
93
    virtual ~TabletColumn() = default;
94
#endif
95
96
    void init_from_pb(const ColumnPB& column);
97
    void init_from_thrift(const TColumn& column);
98
    void to_schema_pb(ColumnPB* column) const;
99
100
3.87M
    int32_t unique_id() const { return _unique_id; }
101
2.14k
    void set_unique_id(int32_t id) { _unique_id = id; }
102
5.16M
    const std::string& name() const { return _col_name; }
103
22.4k
    const std::string& name_lower_case() const { return _col_name_lower_case; }
104
5.34k
    void set_name(std::string col_name) {
105
5.34k
        _col_name = col_name;
106
5.34k
        _col_name_lower_case = to_lower(_col_name);
107
5.34k
    }
108
4.81M
    MOCK_FUNCTION FieldType type() const { return _type; }
109
5.50k
    void set_type(FieldType type) { _type = type; }
110
3.16M
    bool is_key() const { return _is_key; }
111
3.26M
    bool is_nullable() const { return _is_nullable; }
112
0
    bool is_auto_increment() const { return _is_auto_increment; }
113
0
    bool is_seqeunce_col() const { return _col_name == SEQUENCE_COL; }
114
0
    bool is_on_update_current_timestamp() const { return _is_on_update_current_timestamp; }
115
6.14M
    bool is_variant_type() const { return _type == FieldType::OLAP_FIELD_TYPE_VARIANT; }
116
17.6k
    bool is_bf_column() const { return _is_bf_column; }
117
187k
    bool is_array_type() const { return _type == FieldType::OLAP_FIELD_TYPE_ARRAY; }
118
69.6k
    bool is_agg_state_type() const { return _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE; }
119
0
    bool is_jsonb_type() const { return _type == FieldType::OLAP_FIELD_TYPE_JSONB; }
120
596
    bool is_length_variable_type() const {
121
596
        return _type == FieldType::OLAP_FIELD_TYPE_CHAR ||
122
596
               _type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
123
596
               _type == FieldType::OLAP_FIELD_TYPE_STRING ||
124
596
               _type == FieldType::OLAP_FIELD_TYPE_HLL ||
125
596
               _type == FieldType::OLAP_FIELD_TYPE_BITMAP ||
126
596
               _type == FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE ||
127
596
               _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE;
128
596
    }
129
    // Such columns are not exist in frontend schema info, so we need to
130
    // add them into tablet_schema for later column indexing.
131
    static TabletColumn create_materialized_variant_column(const std::string& root,
132
                                                           const std::vector<std::string>& paths,
133
                                                           int32_t parent_unique_id,
134
                                                           int32_t max_subcolumns_count,
135
                                                           bool enable_doc_mode = false);
136
4.13k
    bool has_default_value() const { return _has_default_value; }
137
14.6k
    std::string default_value() const { return _default_value; }
138
398k
    int32_t length() const { return _length; }
139
2.86k
    void set_length(int32_t length) { _length = length; }
140
647
    void set_default_value(const std::string& default_value) {
141
647
        _default_value = default_value;
142
647
        _has_default_value = true;
143
647
    }
144
2.80k
    int32_t index_length() const { return _index_length; }
145
2.30k
    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
2.93k
    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
3.99k
    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
398k
    int precision() const { return _precision; }
159
434k
    int frac() const { return _frac; }
160
22
    inline bool visible() const { return _visible; }
161
    bool has_char_type() const;
162
163
1.72k
    void set_aggregation_method(FieldAggregationMethod agg) {
164
1.72k
        _aggregation = agg;
165
1.72k
        _aggregation_name = get_string_by_aggregation_type(agg);
166
1.72k
    }
167
168
    /**
169
     * Add a sub column.
170
     */
171
    void add_sub_column(TabletColumn& sub_column);
172
173
17.7k
    uint32_t get_subtype_count() const { return _sub_column_count; }
174
4.19k
    MOCK_FUNCTION const TabletColumn& get_sub_column(uint64_t i) const { return *_sub_columns[i]; }
175
2.03k
    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
12.5k
    std::string get_aggregation_name() const { return _aggregation_name; }
189
12.5k
    bool get_result_is_nullable() const { return _result_is_nullable; }
190
12.6k
    int get_be_exec_version() const { return _be_exec_version; }
191
3.30M
    bool has_path_info() const { return _column_path != nullptr && !_column_path->empty(); }
192
87.2k
    const PathInDataPtr& path_info_ptr() const { return _column_path; }
193
    // If it is an extracted column from variant column
194
3.54M
    bool is_extracted_column() const {
195
3.54M
        return _column_path != nullptr && !_column_path->empty() && _parent_col_unique_id >= 0;
196
3.54M
    };
197
104k
    std::string suffix_path() const {
198
104k
        return is_extracted_column() ? _column_path->get_path() : "";
199
104k
    }
200
11
    bool is_nested_subcolumn() const {
201
11
        return _column_path != nullptr && _column_path->has_nested_part();
202
11
    }
203
2.97k
    int32_t parent_unique_id() const { return _parent_col_unique_id; }
204
1.82k
    void set_parent_unique_id(int32_t col_unique_id) { _parent_col_unique_id = col_unique_id; }
205
1.02k
    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
768
    Status check_valid() const {
209
768
        if (type() != FieldType::OLAP_FIELD_TYPE_ARRAY &&
210
768
            type() != FieldType::OLAP_FIELD_TYPE_STRUCT &&
211
768
            type() != FieldType::OLAP_FIELD_TYPE_MAP) {
212
768
            return Status::OK();
213
768
        }
214
0
        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
0
        return Status::OK();
219
0
    }
220
221
3
    void set_precision(int precision) {
222
3
        _precision = precision;
223
3
        _is_decimal = true;
224
3
    }
225
226
38
    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
2.16k
    int32_t variant_max_subcolumns_count() const { return _variant.max_subcolumns_count; }
232
233
72
    void set_variant_max_subcolumns_count(int32_t variant_max_subcolumns_count) {
234
72
        _variant.max_subcolumns_count = variant_max_subcolumns_count;
235
72
    }
236
237
122
    PatternTypePB pattern_type() const { return _pattern_type; }
238
239
403
    bool variant_enable_typed_paths_to_sparse() const {
240
403
        return _variant.enable_typed_paths_to_sparse;
241
403
    }
242
243
20.7k
    int32_t variant_max_sparse_column_statistics_size() const {
244
20.7k
        return _variant.max_sparse_column_statistics_size;
245
20.7k
    }
246
247
305
    int32_t variant_sparse_hash_shard_count() const { return _variant.sparse_hash_shard_count; }
248
249
2.61k
    bool variant_enable_doc_mode() const { return _variant.enable_doc_mode; }
250
251
7
    int64_t variant_doc_materialization_min_rows() const {
252
7
        return _variant.doc_materialization_min_rows;
253
7
    }
254
255
5
    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
13
    void set_variant_enable_doc_mode(bool variant_enable_doc_mode) {
275
13
        _variant.enable_doc_mode = variant_enable_doc_mode;
276
13
    }
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
2.86k
    bool variant_enable_nested_group() const { return _variant.enable_nested_group; }
283
284
    void set_variant_enable_nested_group(bool val) { _variant.enable_nested_group = val; }
285
286
35
    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
218k
    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
21.1k
    int64_t index_id() const { return _index_id; }
344
    const std::string& index_name() const { return _index_name; }
345
223k
    MOCK_FUNCTION IndexType index_type() const { return _index_type; }
346
221k
    const std::vector<int32_t>& col_unique_ids() const { return _col_unique_ids; }
347
26.5k
    MOCK_FUNCTION const std::map<std::string, std::string>& properties() const {
348
26.5k
        return _properties;
349
26.5k
    }
350
1
    int32_t get_gram_size() const {
351
1
        if (_properties.contains("gram_size")) {
352
1
            return std::stoi(_properties.at("gram_size"));
353
1
        }
354
355
0
        return 0;
356
1
    }
357
1
    int32_t get_gram_bf_size() const {
358
1
        if (_properties.contains("bf_size")) {
359
1
            return std::stoi(_properties.at("bf_size"));
360
1
        }
361
362
0
        return 0;
363
1
    }
364
365
228k
    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
2.30k
    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
10
    void remove_parser_and_analyzer() {
374
10
        _properties.erase(INVERTED_INDEX_PARSER_KEY);
375
10
        _properties.erase(INVERTED_INDEX_PARSER_KEY_ALIAS);
376
10
        _properties.erase(INVERTED_INDEX_ANALYZER_NAME_KEY);
377
10
        _properties.erase(INVERTED_INDEX_NORMALIZER_NAME_KEY);
378
10
    }
379
380
219k
    std::string field_pattern() const {
381
219k
        if (_properties.contains("field_pattern")) {
382
3.30k
            return _properties.at("field_pattern");
383
3.30k
        }
384
215k
        return "";
385
219k
    }
386
387
8
    bool is_same_except_id(const TabletIndex* other) const {
388
8
        return _escaped_index_suffix_path == other->_escaped_index_suffix_path &&
389
8
               _index_name == other->_index_name && _index_type == other->_index_type &&
390
8
               _col_unique_ids == other->_col_unique_ids && _properties == other->_properties;
391
8
    }
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
859k
    static std::string deterministic_string_serialize(const PbType& pb) {
425
859k
        std::string output;
426
859k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
427
859k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
428
859k
        output_stream.SetSerializationDeterministic(true);
429
859k
        pb.SerializeToCodedStream(&output_stream);
430
859k
        return output;
431
859k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_14TabletSchemaPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
424
779k
    static std::string deterministic_string_serialize(const PbType& pb) {
425
779k
        std::string output;
426
779k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
427
779k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
428
779k
        output_stream.SetSerializationDeterministic(true);
429
779k
        pb.SerializeToCodedStream(&output_stream);
430
779k
        return output;
431
779k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_8ColumnPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
424
74.5k
    static std::string deterministic_string_serialize(const PbType& pb) {
425
74.5k
        std::string output;
426
74.5k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
427
74.5k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
428
74.5k
        output_stream.SetSerializationDeterministic(true);
429
74.5k
        pb.SerializeToCodedStream(&output_stream);
430
74.5k
        return output;
431
74.5k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_13TabletIndexPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
424
5.52k
    static std::string deterministic_string_serialize(const PbType& pb) {
425
5.52k
        std::string output;
426
5.52k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
427
5.52k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
428
5.52k
        output_stream.SetSerializationDeterministic(true);
429
5.52k
        pb.SerializeToCodedStream(&output_stream);
430
5.52k
        return output;
431
5.52k
    }
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
10.5k
    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
1.18M
    size_t num_columns() const { return _num_columns; }
461
10
    size_t num_visible_columns() const {
462
10
        return std::count_if(_cols.begin(), _cols.end(),
463
20
                             [](const TabletColumnPtr& column) { return column->visible(); });
464
10
    }
465
0
    size_t num_visible_value_columns() const {
466
0
        return std::count_if(_cols.begin(), _cols.end(), [](const TabletColumnPtr& column) {
467
0
            return column->visible() && !column->is_key();
468
0
        });
469
0
    }
470
1.11M
    size_t num_key_columns() const { return _num_key_columns; }
471
145k
    const std::vector<uint32_t>& cluster_key_uids() const { return _cluster_key_uids; }
472
0
    size_t num_null_columns() const { return _num_null_columns; }
473
3.01k
    size_t num_short_key_columns() const { return _num_short_key_columns; }
474
122
    size_t num_rows_per_row_block() const { return _num_rows_per_row_block; }
475
20.9k
    size_t num_variant_columns() const { return _num_variant_columns; };
476
0
    size_t num_virtual_columns() const { return _num_virtual_columns; }
477
7.19M
    KeysType keys_type() const { return _keys_type; }
478
7.37k
    SortType sort_type() const { return _sort_type; }
479
122
    size_t sort_col_num() const { return _sort_col_num; }
480
122
    CompressKind compress_kind() const { return _compress_kind; }
481
122
    size_t next_column_unique_id() const { return _next_column_unique_id; }
482
4
    bool has_bf_fpp() const { return _has_bf_fpp; }
483
4
    double bloom_filter_fpp() const { return _bf_fpp; }
484
1.19M
    bool is_in_memory() const { return _is_in_memory; }
485
0
    void set_is_in_memory(bool is_in_memory) { _is_in_memory = is_in_memory; }
486
5
    void set_disable_auto_compaction(bool disable_auto_compaction) {
487
5
        _disable_auto_compaction = disable_auto_compaction;
488
5
    }
489
352M
    bool disable_auto_compaction() const { return _disable_auto_compaction; }
490
    // Deprecated legacy switch for flatten-nested variant behavior.
491
    // It is distinct from variant_enable_nested_group.
492
0
    void set_deprecated_variant_flatten_nested(bool flatten_nested) {
493
0
        _deprecated_enable_variant_flatten_nested = flatten_nested;
494
0
    }
495
399
    bool deprecated_variant_flatten_nested() const {
496
399
        return _deprecated_enable_variant_flatten_nested;
497
399
    }
498
0
    void set_enable_single_replica_compaction(bool enable_single_replica_compaction) {
499
0
        _enable_single_replica_compaction = enable_single_replica_compaction;
500
0
    }
501
27.7M
    bool enable_single_replica_compaction() const { return _enable_single_replica_compaction; }
502
    // indicate if full row store column(all the columns encodes as row) exists
503
60
    bool has_row_store_for_all_columns() const {
504
60
        return _store_row_column && row_columns_uids().empty();
505
60
    }
506
0
    void set_skip_write_index_on_load(bool skip) { _skip_write_index_on_load = skip; }
507
1.85k
    bool skip_write_index_on_load() const { return _skip_write_index_on_load; }
508
11.1k
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
509
152k
    bool has_sequence_col() const { return _sequence_col_idx != -1; }
510
64.7k
    int32_t sequence_col_idx() const { return _sequence_col_idx; }
511
0
    void set_version_col_idx(int32_t version_col_idx) { _version_col_idx = version_col_idx; }
512
0
    int32_t version_col_idx() const { return _version_col_idx; }
513
0
    bool has_skip_bitmap_col() const { return _skip_bitmap_col_idx != -1; }
514
0
    int32_t skip_bitmap_col_idx() const { return _skip_bitmap_col_idx; }
515
2.47k
    segment_v2::CompressionTypePB compression_type() const { return _compression_type; }
516
0
    void set_row_store_page_size(long page_size) { _row_store_page_size = page_size; }
517
123
    long row_store_page_size() const { return _row_store_page_size; }
518
0
    void set_storage_page_size(long storage_page_size) { _storage_page_size = storage_page_size; }
519
12.5k
    long storage_page_size() const { return _storage_page_size; }
520
0
    void set_storage_dict_page_size(long storage_dict_page_size) {
521
0
        _storage_dict_page_size = storage_dict_page_size;
522
0
    }
523
12.5k
    long storage_dict_page_size() const { return _storage_dict_page_size; }
524
2.56k
    bool has_global_row_id() const {
525
38.0k
        for (auto [col_name, _] : _field_name_to_index) {
526
38.0k
            if (col_name.start_with(StringRef(BeConsts::GLOBAL_ROWID_COL.data(),
527
38.0k
                                              BeConsts::GLOBAL_ROWID_COL.size()))) {
528
0
                return true;
529
0
            }
530
38.0k
        }
531
2.56k
        return false;
532
2.56k
    }
533
534
148
    const std::vector<const TabletIndex*> inverted_indexes() const {
535
148
        std::vector<const TabletIndex*> inverted_indexes;
536
1.26k
        for (const auto& index : _indexes) {
537
1.26k
            if (index->index_type() == IndexType::INVERTED) {
538
1.26k
                inverted_indexes.emplace_back(index.get());
539
1.26k
            }
540
1.26k
        }
541
148
        return inverted_indexes;
542
148
    }
543
7.29k
    bool has_inverted_index() const {
544
7.29k
        for (const auto& index : _indexes) {
545
937
            DBUG_EXECUTE_IF("tablet_schema::has_inverted_index", {
546
937
                if (index->col_unique_ids().empty()) {
547
937
                    throw Exception(Status::InternalError("col unique ids cannot be empty"));
548
937
                }
549
937
            });
550
551
937
            if (index->index_type() == IndexType::INVERTED) {
552
                //if index_id == -1, ignore it.
553
929
                if (!index->col_unique_ids().empty() && index->col_unique_ids()[0] >= 0) {
554
929
                    return true;
555
929
                }
556
929
            }
557
937
        }
558
6.36k
        return false;
559
7.29k
    }
560
561
5.70k
    bool has_ann_index() const {
562
5.70k
        for (const auto& index : _indexes) {
563
7
            if (index->index_type() == IndexType::ANN) {
564
5
                if (!index->col_unique_ids().empty() && index->col_unique_ids()[0] >= 0) {
565
5
                    return true;
566
5
                }
567
5
            }
568
7
        }
569
5.70k
        return false;
570
5.70k
    }
571
572
    bool has_inverted_index_with_index_id(int64_t index_id) const;
573
574
    std::vector<const TabletIndex*> inverted_indexs(const TabletColumn& col) const;
575
576
    std::vector<const TabletIndex*> inverted_indexs(int32_t col_unique_id,
577
                                                    const std::string& suffix_path = "") const;
578
    const TabletIndex* ann_index(const TabletColumn& col) const;
579
580
    // Regardless of whether this column supports inverted index
581
    // TabletIndex information will be returned as long as it exists.
582
    const TabletIndex* ann_index(int32_t col_unique_id, const std::string& suffix_path = "") const;
583
584
    std::vector<TabletIndexPtr> inverted_index_by_field_pattern(
585
            int32_t col_unique_id, const std::string& field_pattern) const;
586
587
    bool has_ngram_bf_index(int32_t col_unique_id) const;
588
    const TabletIndex* get_ngram_bf_index(int32_t col_unique_id) const;
589
    const TabletIndex* get_index(int32_t col_unique_id, IndexType index_type,
590
                                 const std::string& suffix_path) const;
591
    void update_indexes_from_thrift(const std::vector<doris::TOlapTableIndex>& indexes);
592
    // If schema version is not set, it should be -1
593
416k
    int32_t schema_version() const { return _schema_version; }
594
    void clear_columns();
595
    Block create_block(
596
            const std::vector<uint32_t>& return_columns,
597
            const std::unordered_set<uint32_t>* tablet_columns_need_convert_null = nullptr) const;
598
    Block create_block() const;
599
2.88k
    void set_schema_version(int32_t version) { _schema_version = version; }
600
70
    void set_auto_increment_column(const std::string& auto_increment_column) {
601
70
        _auto_increment_column = auto_increment_column;
602
70
    }
603
334
    std::string auto_increment_column() const { return _auto_increment_column; }
604
605
152
    void set_table_id(int64_t table_id) { _table_id = table_id; }
606
3.61k
    int64_t table_id() const { return _table_id; }
607
152
    void set_db_id(int64_t db_id) { _db_id = db_id; }
608
436
    int64_t db_id() const { return _db_id; }
609
    void build_current_tablet_schema(int64_t index_id, int32_t version,
610
                                     const OlapTableIndexSchema* index,
611
                                     const TabletSchema& out_tablet_schema);
612
613
    // Merge columns that not exit in current schema, these column is dropped in current schema
614
    // but they are useful in some cases. For example,
615
    // 1. origin schema is  ColA, ColB
616
    // 2. insert values     1, 2
617
    // 3. delete where ColB = 2
618
    // 4. drop ColB
619
    // 5. insert values  3
620
    // 6. add column ColB, although it is name ColB, but it is different with previous ColB, the new ColB we name could call ColB'
621
    // 7. insert value  4, 5
622
    // Then the read schema should be ColA, ColB, ColB' because the delete predicate need ColB to remove related data.
623
    // Because they have same name, so that the dropped column should not be added to the map, only with unique id.
624
    void merge_dropped_columns(const TabletSchema& src_schema);
625
626
    bool is_dropped_column(const TabletColumn& col) const;
627
628
    // copy extracted columns from src_schema
629
    void copy_extracted_columns(const TabletSchema& src_schema);
630
631
    // only reserve extracted columns
632
    void reserve_extracted_columns();
633
634
4.04k
    std::string get_all_field_names() const {
635
4.04k
        std::string str = "[";
636
4.04k
        for (auto p : _field_name_to_index) {
637
4.04k
            if (str.size() > 1) {
638
0
                str += ", ";
639
0
            }
640
4.04k
            str += p.first.to_string() + "(" + std::to_string(_cols[p.second]->unique_id()) + ")";
641
4.04k
        }
642
4.04k
        str += "]";
643
4.04k
        return str;
644
4.04k
    }
645
646
    // Dump [(name, type, is_nullable), ...]
647
1
    std::string dump_structure() const {
648
1
        std::string str = "[";
649
15
        for (auto p : _cols) {
650
15
            if (str.size() > 1) {
651
14
                str += ", ";
652
14
            }
653
15
            str += "(";
654
15
            str += p->name();
655
15
            str += ", ";
656
15
            str += TabletColumn::get_string_by_field_type(p->type());
657
15
            str += ", ";
658
15
            str += "is_nullable:";
659
15
            str += (p->is_nullable() ? "true" : "false");
660
15
            str += ")";
661
15
        }
662
1
        str += "]";
663
1
        return str;
664
1
    }
665
666
1
    std::string dump_full_schema() const {
667
1
        std::string str = "[";
668
4
        for (auto p : _cols) {
669
4
            if (str.size() > 1) {
670
3
                str += ", ";
671
3
            }
672
4
            ColumnPB col_pb;
673
4
            p->to_schema_pb(&col_pb);
674
4
            str += "(";
675
4
            str += col_pb.ShortDebugString();
676
4
            str += ")";
677
4
        }
678
1
        str += "]";
679
1
        return str;
680
1
    }
681
682
    Block create_block_by_cids(const std::vector<uint32_t>& cids) const;
683
684
    std::shared_ptr<TabletSchema> copy_without_variant_extracted_columns();
685
15.3k
    InvertedIndexStorageFormatPB get_inverted_index_storage_format() const {
686
15.3k
        return _inverted_index_storage_format;
687
15.3k
    }
688
689
    void update_tablet_columns(const TabletSchema& tablet_schema,
690
                               const std::vector<TColumn>& t_columns);
691
692
4
    const std::vector<int32_t>& row_columns_uids() const { return _row_store_column_unique_ids; }
693
694
    int64_t get_metadata_size() const override;
695
696
    struct SubColumnInfo {
697
        TabletColumn column;
698
        TabletIndexes indexes;
699
    };
700
701
    // all path in path_set_info are relative to the parent column
702
    struct PathsSetInfo {
703
        std::unordered_map<std::string, SubColumnInfo> typed_path_set;    // typed columns
704
        std::unordered_map<std::string, TabletIndexes> subcolumn_indexes; // subcolumns indexes
705
        PathSet sub_path_set;                                             // extracted columns
706
        PathSet sparse_path_set;                                          // sparse columns
707
708
        // "Materialized regular path" means compaction chose to store this path as a dedicated
709
        // column in the schema, either typed or extracted, instead of re-emitting it dynamically.
710
0
        bool contains_materialized_regular_path(const std::string& path) const {
711
0
            return typed_path_set.contains(path) || sub_path_set.contains(path);
712
0
        }
713
    };
714
715
71
    void set_path_set_info(std::unordered_map<int32_t, PathsSetInfo>&& path_set_info_map) {
716
71
        _path_set_info_map = std::move(path_set_info_map);
717
71
    }
718
719
4
    const PathsSetInfo& path_set_info(int32_t unique_id) const {
720
4
        return _path_set_info_map.at(unique_id);
721
4
    }
722
723
2
    const PathsSetInfo* try_path_set_info(int32_t unique_id) const {
724
2
        auto it = _path_set_info_map.find(unique_id);
725
2
        return it == _path_set_info_map.end() ? nullptr : &it->second;
726
2
    }
727
728
    bool need_record_variant_extended_schema() const { return variant_max_subcolumns_count() == 0; }
729
730
    int32_t variant_max_subcolumns_count() const {
731
        for (const auto& col : _cols) {
732
            if (col->is_variant_type()) {
733
                return col->variant_max_subcolumns_count();
734
            }
735
        }
736
        return 0;
737
    }
738
    const std::unordered_map<uint32_t, std::vector<uint32_t>>& seq_col_idx_to_value_cols_idx()
739
0
            const {
740
0
        return _seq_col_idx_to_value_cols_idx;
741
0
    }
742
743
14.6k
    bool has_seq_map() const { return !_seq_col_idx_to_value_cols_idx.empty(); }
744
745
0
    const std::unordered_map<uint32_t, uint32_t>& value_col_idx_to_seq_col_idx() const {
746
0
        return _value_col_idx_to_seq_col_idx;
747
0
    }
748
749
0
    void add_pruned_columns_data_type(int32_t col_unique_id, DataTypePtr data_type) {
750
0
        _pruned_columns_data_type[col_unique_id] = std::move(data_type);
751
0
    }
752
753
0
    void clear_pruned_columns_data_type() { _pruned_columns_data_type.clear(); }
754
755
0
    bool has_pruned_columns() const { return !_pruned_columns_data_type.empty(); }
756
757
15.0k
    TabletStorageFormatPB storage_format() const { return _storage_format; }
758
38
    void set_storage_format(TabletStorageFormatPB v) { _storage_format = v; }
759
760
private:
761
    friend bool operator==(const TabletSchema& a, const TabletSchema& b);
762
    friend bool operator!=(const TabletSchema& a, const TabletSchema& b);
763
    TabletSchema(const TabletSchema&) = default;
764
765
    KeysType _keys_type = DUP_KEYS;
766
    SortType _sort_type = SortType::LEXICAL;
767
    size_t _sort_col_num = 0;
768
    std::vector<TabletColumnPtr> _cols;
769
770
    std::vector<TabletIndexPtr> _indexes;
771
    std::unordered_map<StringRef, int32_t, StringRefHash> _field_name_to_index;
772
    std::unordered_map<int32_t, int32_t> _field_uniqueid_to_index;
773
    std::unordered_map<PathInDataRef, int32_t, PathInDataRef::Hash> _field_path_to_index;
774
775
    // index_type/col_unique_id/suffix -> idxs in _indexes
776
    using IndexKey = std::tuple<IndexType, int32_t, std::string>;
777
    struct IndexKeyHash {
778
337k
        size_t operator()(const IndexKey& t) const {
779
337k
            uint32_t seed = 0;
780
337k
            seed = doris::HashUtil::hash((const char*)&std::get<0>(t), sizeof(std::get<0>(t)),
781
337k
                                         seed);
782
337k
            seed = doris::HashUtil::hash((const char*)&std::get<1>(t), sizeof(std::get<1>(t)),
783
337k
                                         seed);
784
337k
            seed = doris::HashUtil::hash((const char*)std::get<2>(t).c_str(),
785
337k
                                         static_cast<uint32_t>(std::get<2>(t).size()), seed);
786
337k
            return seed;
787
337k
        }
788
    };
789
    std::unordered_map<IndexKey, std::vector<size_t>, IndexKeyHash> _col_id_suffix_to_index;
790
791
    int32_t _num_columns = 0;
792
    size_t _num_variant_columns = 0;
793
    size_t _num_virtual_columns = 0;
794
    size_t _num_key_columns = 0;
795
    std::vector<uint32_t> _cluster_key_uids;
796
    size_t _num_null_columns = 0;
797
    size_t _num_short_key_columns = 0;
798
    size_t _num_rows_per_row_block = 0;
799
    CompressKind _compress_kind = COMPRESS_NONE;
800
    segment_v2::CompressionTypePB _compression_type = segment_v2::CompressionTypePB::LZ4F;
801
    long _row_store_page_size = segment_v2::ROW_STORE_PAGE_SIZE_DEFAULT_VALUE;
802
    long _storage_page_size = segment_v2::STORAGE_PAGE_SIZE_DEFAULT_VALUE;
803
    long _storage_dict_page_size = segment_v2::STORAGE_DICT_PAGE_SIZE_DEFAULT_VALUE;
804
    size_t _next_column_unique_id = 0;
805
    std::string _auto_increment_column;
806
807
    bool _has_bf_fpp = false;
808
    double _bf_fpp = 0;
809
    bool _is_in_memory = false;
810
    int32_t _delete_sign_idx = -1;
811
    int32_t _sequence_col_idx = -1;
812
    int32_t _version_col_idx = -1;
813
    int32_t _skip_bitmap_col_idx = -1;
814
    int32_t _schema_version = -1;
815
    int64_t _table_id = -1;
816
    int64_t _db_id = -1;
817
    bool _disable_auto_compaction = false;
818
    bool _enable_single_replica_compaction = false;
819
    bool _store_row_column = false;
820
    bool _skip_write_index_on_load = false;
821
    InvertedIndexStorageFormatPB _inverted_index_storage_format = InvertedIndexStorageFormatPB::V1;
822
823
    // Contains column ids of which columns should be encoded into row store.
824
    // ATTN: For compability reason empty cids means all columns of tablet schema are encoded to row column
825
    std::vector<int32_t> _row_store_column_unique_ids;
826
    bool _deprecated_enable_variant_flatten_nested = false;
827
828
    std::map<size_t, int32_t> _vir_col_idx_to_unique_id;
829
    std::map<int32_t, DataTypePtr> _pruned_columns_data_type;
830
831
    // value: extracted path set and sparse path set
832
    std::unordered_map<int32_t, PathsSetInfo> _path_set_info_map;
833
834
    // key: field_pattern
835
    // value: indexes
836
    using PatternToIndex = std::unordered_map<std::string, std::vector<TabletIndexPtr>>;
837
    std::unordered_map<int32_t, PatternToIndex> _index_by_unique_id_with_pattern;
838
839
    // Default behavior for new segments: use external ColumnMeta region + CMO table if true
840
    // Persisted tablet storage format. Authoritative source for "is this tablet V3?"
841
    // decisions in the segment write paths. Old PBs without this field are upgraded in
842
    // init_from_pb() by deriving V3 from any of the three legacy V3-flavor flags.
843
    TabletStorageFormatPB _storage_format {TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V2};
844
    // Sequence column unique id mapping to value columns unique id
845
    std::unordered_map<uint32_t, std::vector<uint32_t>> _seq_col_uid_to_value_cols_uid;
846
    // Value column unique id mapping to sequence column unique id(also map sequence column it self)
847
    std::unordered_map<uint32_t, uint32_t> _value_col_uid_to_seq_col_uid;
848
    // Sequence column index mapping to value column index
849
    std::unordered_map<uint32_t, std::vector<uint32_t>> _seq_col_idx_to_value_cols_idx;
850
    // Value column index mapping to sequence column index(also map sequence column it self)
851
    std::unordered_map<uint32_t, uint32_t> _value_col_idx_to_seq_col_idx;
852
};
853
854
bool operator==(const TabletSchema& a, const TabletSchema& b);
855
bool operator!=(const TabletSchema& a, const TabletSchema& b);
856
857
using TabletSchemaSPtr = std::shared_ptr<TabletSchema>;
858
859
} // namespace doris