Coverage Report

Created: 2026-01-21 02:58

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