Coverage Report

Created: 2026-01-23 15:41

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