Coverage Report

Created: 2025-12-21 13:42

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