Coverage Report

Created: 2025-07-28 13:40

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/metadata_adder.h"
39
#include "olap/olap_common.h"
40
#include "olap/rowset/segment_v2/options.h"
41
#include "runtime/define_primitive_type.h"
42
#include "runtime/descriptors.h"
43
#include "runtime/memory/lru_cache_policy.h"
44
#include "util/debug_points.h"
45
#include "util/string_parser.hpp"
46
#include "util/string_util.h"
47
#include "vec/aggregate_functions/aggregate_function.h"
48
#include "vec/common/string_ref.h"
49
#include "vec/common/string_utils/string_utils.h"
50
#include "vec/core/types.h"
51
#include "vec/json/path_in_data.h"
52
53
namespace doris {
54
namespace vectorized {
55
class Block;
56
class PathInData;
57
class IDataType;
58
} // namespace vectorized
59
60
#include "common/compile_check_begin.h"
61
62
struct OlapTableIndexSchema;
63
class TColumn;
64
class TOlapTableIndex;
65
class TabletColumn;
66
67
using TabletColumnPtr = std::shared_ptr<TabletColumn>;
68
69
class TabletColumn : public MetadataAdder<TabletColumn> {
70
public:
71
    TabletColumn();
72
    TabletColumn(const ColumnPB& column);
73
    TabletColumn(const TColumn& column);
74
    TabletColumn(FieldAggregationMethod agg, FieldType type);
75
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable);
76
    TabletColumn(FieldAggregationMethod agg, FieldType filed_type, bool is_nullable,
77
                 int32_t unique_id, size_t length);
78
    void init_from_pb(const ColumnPB& column);
79
    void init_from_thrift(const TColumn& column);
80
    void to_schema_pb(ColumnPB* column) const;
81
82
322k
    int32_t unique_id() const { return _unique_id; }
83
262
    void set_unique_id(int32_t id) { _unique_id = id; }
84
185k
    const std::string& name() const { return _col_name; }
85
0
    const std::string& name_lower_case() const { return _col_name_lower_case; }
86
313
    void set_name(std::string col_name) {
87
313
        _col_name = col_name;
88
313
        _col_name_lower_case = to_lower(_col_name);
89
313
    }
90
945k
    FieldType type() const { return _type; }
91
310
    void set_type(FieldType type) { _type = type; }
92
87.1k
    bool is_key() const { return _is_key; }
93
168k
    bool is_nullable() const { return _is_nullable; }
94
0
    bool is_auto_increment() const { return _is_auto_increment; }
95
0
    bool is_seqeunce_col() const { return _col_name == SEQUENCE_COL; }
96
0
    bool is_on_update_current_timestamp() const { return _is_on_update_current_timestamp; }
97
76.4k
    bool is_variant_type() const { return _type == FieldType::OLAP_FIELD_TYPE_VARIANT; }
98
13.2k
    bool is_bf_column() const { return _is_bf_column; }
99
13.2k
    bool has_bitmap_index() const { return _has_bitmap_index; }
100
14.0k
    bool is_array_type() const { return _type == FieldType::OLAP_FIELD_TYPE_ARRAY; }
101
12.4k
    bool is_agg_state_type() const { return _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE; }
102
0
    bool is_jsonb_type() const { return _type == FieldType::OLAP_FIELD_TYPE_JSONB; }
103
0
    bool is_length_variable_type() const {
104
0
        return _type == FieldType::OLAP_FIELD_TYPE_CHAR ||
105
0
               _type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
106
0
               _type == FieldType::OLAP_FIELD_TYPE_STRING ||
107
0
               _type == FieldType::OLAP_FIELD_TYPE_HLL ||
108
0
               _type == FieldType::OLAP_FIELD_TYPE_BITMAP ||
109
0
               _type == FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE ||
110
0
               _type == FieldType::OLAP_FIELD_TYPE_AGG_STATE;
111
0
    }
112
    // Such columns are not exist in frontend schema info, so we need to
113
    // add them into tablet_schema for later column indexing.
114
    static TabletColumn create_materialized_variant_column(const std::string& root,
115
                                                           const std::vector<std::string>& paths,
116
                                                           int32_t parent_unique_id);
117
188
    bool has_default_value() const { return _has_default_value; }
118
13.2k
    std::string default_value() const { return _default_value; }
119
39.6k
    int32_t length() const { return _length; }
120
225
    void set_length(int32_t length) { _length = length; }
121
0
    void set_default_value(const std::string& default_value) {
122
0
        _default_value = default_value;
123
0
        _has_default_value = true;
124
0
    }
125
29.8k
    int32_t index_length() const { return _index_length; }
126
183
    void set_index_length(int32_t index_length) { _index_length = index_length; }
127
252
    void set_is_key(bool is_key) { _is_key = is_key; }
128
224
    void set_is_nullable(bool is_nullable) { _is_nullable = is_nullable; }
129
0
    void set_is_auto_increment(bool is_auto_increment) { _is_auto_increment = is_auto_increment; }
130
0
    void set_is_on_update_current_timestamp(bool is_on_update_current_timestamp) {
131
0
        _is_on_update_current_timestamp = is_on_update_current_timestamp;
132
0
    }
133
    void set_path_info(const vectorized::PathInData& path);
134
19.5k
    FieldAggregationMethod aggregation() const { return _aggregation; }
135
    vectorized::AggregateFunctionPtr get_aggregate_function_union(
136
            vectorized::DataTypePtr type, int current_be_exec_version) const;
137
    vectorized::AggregateFunctionPtr get_aggregate_function(std::string suffix,
138
                                                            int current_be_exec_version) const;
139
148k
    int precision() const { return _precision; }
140
148k
    int frac() const { return _frac; }
141
0
    inline bool visible() const { return _visible; }
142
    bool has_char_type() const;
143
144
7
    void set_aggregation_method(FieldAggregationMethod agg) {
145
7
        _aggregation = agg;
146
7
        _aggregation_name = get_string_by_aggregation_type(agg);
147
7
    }
148
149
    /**
150
     * Add a sub column.
151
     */
152
    void add_sub_column(TabletColumn& sub_column);
153
154
13.2k
    uint32_t get_subtype_count() const { return _sub_column_count; }
155
66
    const TabletColumn& get_sub_column(uint64_t i) const { return *_sub_columns[i]; }
156
0
    const std::vector<TabletColumnPtr>& get_sub_columns() const { return _sub_columns; }
157
158
    friend bool operator==(const TabletColumn& a, const TabletColumn& b);
159
    friend bool operator!=(const TabletColumn& a, const TabletColumn& b);
160
161
    static std::string get_string_by_field_type(FieldType type);
162
    static std::string get_string_by_aggregation_type(FieldAggregationMethod aggregation_type);
163
    static FieldType get_field_type_by_string(const std::string& str);
164
    static FieldType get_field_type_by_type(PrimitiveType type);
165
    static FieldAggregationMethod get_aggregation_type_by_string(const std::string& str);
166
    static uint32_t get_field_length_by_type(TPrimitiveType::type type, uint32_t string_length);
167
    bool is_row_store_column() const;
168
13.2k
    std::string get_aggregation_name() const { return _aggregation_name; }
169
13.2k
    bool get_result_is_nullable() const { return _result_is_nullable; }
170
13.2k
    int get_be_exec_version() const { return _be_exec_version; }
171
109k
    bool has_path_info() const { return _column_path != nullptr && !_column_path->empty(); }
172
26.4k
    const vectorized::PathInDataPtr& path_info_ptr() const { return _column_path; }
173
    // If it is an extracted column from variant column
174
91.9k
    bool is_extracted_column() const {
175
91.9k
        return _column_path != nullptr && !_column_path->empty() && _parent_col_unique_id > 0;
176
91.9k
    };
177
26.4k
    std::string suffix_path() const {
178
26.4k
        return is_extracted_column() ? _column_path->get_path() : "";
179
26.4k
    }
180
0
    bool is_nested_subcolumn() const {
181
0
        return _column_path != nullptr && _column_path->has_nested_part();
182
0
    }
183
26.4k
    int32_t parent_unique_id() const { return _parent_col_unique_id; }
184
13
    void set_parent_unique_id(int32_t col_unique_id) { _parent_col_unique_id = col_unique_id; }
185
14
    void set_is_bf_column(bool is_bf_column) { _is_bf_column = is_bf_column; }
186
0
    void set_has_bitmap_index(bool has_bitmap_index) { _has_bitmap_index = has_bitmap_index; }
187
    std::shared_ptr<const vectorized::IDataType> get_vec_type() const;
188
189
    void append_sparse_column(TabletColumn column);
190
    const TabletColumn& sparse_column_at(size_t oridinal) const;
191
    const std::vector<TabletColumnPtr>& sparse_columns() const;
192
13.2k
    size_t num_sparse_columns() const { return _num_sparse_columns; }
193
194
1
    Status check_valid() const {
195
1
        if (type() != FieldType::OLAP_FIELD_TYPE_ARRAY &&
196
1
            type() != FieldType::OLAP_FIELD_TYPE_STRUCT &&
197
1
            type() != FieldType::OLAP_FIELD_TYPE_MAP) {
198
1
            return Status::OK();
199
1
        }
200
0
        if (is_bf_column()) {
201
0
            return Status::NotSupported("Do not support bloom filter index, type={}",
202
0
                                        get_string_by_field_type(type()));
203
0
        }
204
0
        if (has_bitmap_index()) {
205
0
            return Status::NotSupported("Do not support bitmap index, type={}",
206
0
                                        get_string_by_field_type(type()));
207
0
        }
208
0
        return Status::OK();
209
0
    }
210
211
private:
212
    int32_t _unique_id = -1;
213
    std::string _col_name;
214
    std::string _col_name_lower_case;
215
    // the field _type will change from TPrimitiveType
216
    // to string by 'EnumToString(TPrimitiveType, tcolumn.column_type.type, data_type);' (reference: TabletMeta::init_column_from_tcolumn)
217
    // to FieldType by 'TabletColumn::get_field_type_by_string' (reference: TabletColumn::init_from_pb).
218
    // And the _type in columnPB is string and it changed from FieldType by 'get_string_by_field_type' (reference: TabletColumn::to_schema_pb).
219
    FieldType _type;
220
    bool _is_key = false;
221
    FieldAggregationMethod _aggregation;
222
    std::string _aggregation_name;
223
    bool _is_nullable = false;
224
    bool _is_auto_increment = false;
225
    bool _is_on_update_current_timestamp {false};
226
227
    bool _has_default_value = false;
228
    std::string _default_value;
229
230
    bool _is_decimal = false;
231
    int32_t _precision = -1;
232
    int32_t _frac = -1;
233
234
    int32_t _length = -1;
235
    int32_t _index_length = -1;
236
237
    bool _is_bf_column = false;
238
239
    bool _has_bitmap_index = false;
240
    bool _visible = true;
241
242
    std::vector<TabletColumnPtr> _sub_columns;
243
    uint32_t _sub_column_count = 0;
244
245
    bool _result_is_nullable = false;
246
    int _be_exec_version = -1;
247
248
    // The extracted sub-columns from "variant" contain the following information:
249
    int32_t _parent_col_unique_id = -1;     // "variant" -> col_unique_id
250
    vectorized::PathInDataPtr _column_path; // the path of the sub-columns themselves
251
252
    // Record information about columns merged into a sparse column within a variant
253
    // `{"id": 100, "name" : "jack", "point" : 3.9}`
254
    // If the information mentioned above is inserted into the variant column,
255
    // 'id' and 'name' are correctly extracted, while 'point' is merged into the sparse column due to its sparsity.
256
    // The path_info and type of 'point' will be recorded using the TabletColumn.
257
    // Use shared_ptr for reuse and reducing column memory usage
258
    std::vector<TabletColumnPtr> _sparse_cols;
259
    size_t _num_sparse_columns = 0;
260
};
261
262
bool operator==(const TabletColumn& a, const TabletColumn& b);
263
bool operator!=(const TabletColumn& a, const TabletColumn& b);
264
265
class TabletIndex : public MetadataAdder<TabletIndex> {
266
public:
267
7.42k
    TabletIndex() = default;
268
    void init_from_thrift(const TOlapTableIndex& index, const TabletSchema& tablet_schema);
269
    void init_from_thrift(const TOlapTableIndex& index, const std::vector<int32_t>& column_uids);
270
    void init_from_pb(const TabletIndexPB& index);
271
    void to_schema_pb(TabletIndexPB* index) const;
272
273
11.2k
    int64_t index_id() const { return _index_id; }
274
0
    const std::string& index_name() const { return _index_name; }
275
9.36k
    IndexType index_type() const { return _index_type; }
276
9.20k
    const std::vector<int32_t>& col_unique_ids() const { return _col_unique_ids; }
277
25.5k
    const std::map<std::string, std::string>& properties() const { return _properties; }
278
0
    int32_t get_gram_size() const {
279
0
        if (_properties.contains("gram_size")) {
280
0
            return std::stoi(_properties.at("gram_size"));
281
0
        }
282
283
0
        return 0;
284
0
    }
285
0
    int32_t get_gram_bf_size() const {
286
0
        if (_properties.contains("bf_size")) {
287
0
            return std::stoi(_properties.at("bf_size"));
288
0
        }
289
290
0
        return 0;
291
0
    }
292
293
15.7k
    const std::string& get_index_suffix() const { return _escaped_index_suffix_path; }
294
295
    void set_escaped_escaped_index_suffix_path(const std::string& name);
296
297
private:
298
    int64_t _index_id = -1;
299
    // Identify the different index with the same _index_id
300
    std::string _escaped_index_suffix_path;
301
    std::string _index_name;
302
    IndexType _index_type;
303
    std::vector<int32_t> _col_unique_ids;
304
    std::map<std::string, std::string> _properties;
305
};
306
307
using TabletIndexPtr = std::shared_ptr<TabletIndex>;
308
309
class TabletSchema : public MetadataAdder<TabletSchema> {
310
public:
311
    enum class ColumnType { NORMAL = 0, DROPPED = 1, VARIANT = 2 };
312
    // TODO(yingchun): better to make constructor as private to avoid
313
    // manually init members incorrectly, and define a new function like
314
    // void create_from_pb(const TabletSchemaPB& schema, TabletSchema* tablet_schema).
315
    TabletSchema();
316
    ~TabletSchema() override;
317
318
    // Init from pb
319
    // ignore_extracted_columns: ignore the extracted columns from variant column
320
    // reuse_cached_column: reuse the cached column in the schema if they are the same, to reduce memory usage
321
    void init_from_pb(const TabletSchemaPB& schema, bool ignore_extracted_columns = false,
322
                      bool reuse_cached_column = false);
323
    // Notice: Use deterministic way to serialize protobuf,
324
    // since serialize Map in protobuf may could lead to un-deterministic by default
325
    template <class PbType>
326
4.01k
    static std::string deterministic_string_serialize(const PbType& pb) {
327
4.01k
        std::string output;
328
4.01k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
329
4.01k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
330
4.01k
        output_stream.SetSerializationDeterministic(true);
331
4.01k
        pb.SerializeToCodedStream(&output_stream);
332
4.01k
        return output;
333
4.01k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_14TabletSchemaPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
326
3.64k
    static std::string deterministic_string_serialize(const PbType& pb) {
327
3.64k
        std::string output;
328
3.64k
        google::protobuf::io::StringOutputStream string_output_stream(&output);
329
3.64k
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
330
3.64k
        output_stream.SetSerializationDeterministic(true);
331
3.64k
        pb.SerializeToCodedStream(&output_stream);
332
3.64k
        return output;
333
3.64k
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_8ColumnPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
326
258
    static std::string deterministic_string_serialize(const PbType& pb) {
327
258
        std::string output;
328
258
        google::protobuf::io::StringOutputStream string_output_stream(&output);
329
258
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
330
258
        output_stream.SetSerializationDeterministic(true);
331
258
        pb.SerializeToCodedStream(&output_stream);
332
258
        return output;
333
258
    }
_ZN5doris12TabletSchema30deterministic_string_serializeINS_13TabletIndexPBEEENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKT_
Line
Count
Source
326
112
    static std::string deterministic_string_serialize(const PbType& pb) {
327
112
        std::string output;
328
112
        google::protobuf::io::StringOutputStream string_output_stream(&output);
329
112
        google::protobuf::io::CodedOutputStream output_stream(&string_output_stream);
330
112
        output_stream.SetSerializationDeterministic(true);
331
112
        pb.SerializeToCodedStream(&output_stream);
332
112
        return output;
333
112
    }
334
    void to_schema_pb(TabletSchemaPB* tablet_meta_pb) const;
335
    void append_column(TabletColumn column, ColumnType col_type = ColumnType::NORMAL);
336
    void append_index(TabletIndex&& index);
337
    void update_index(const TabletColumn& column, const IndexType& index_type, TabletIndex&& index);
338
    void remove_index(int64_t index_id);
339
    void clear_index();
340
    // Must make sure the row column is always the last column
341
    void add_row_column();
342
    void copy_from(const TabletSchema& tablet_schema);
343
    // lightweight copy, take care of lifecycle of TabletColumn
344
    void shawdow_copy_without_columns(const TabletSchema& tablet_schema);
345
    void update_index_info_from(const TabletSchema& tablet_schema);
346
    std::string to_key() const;
347
    // get_metadata_size is only the memory of the TabletSchema itself, not include child objects.
348
52
    int64_t mem_size() const { return get_metadata_size(); }
349
    size_t row_size() const;
350
    int32_t field_index(const std::string& field_name) const;
351
    int32_t field_index(const vectorized::PathInData& path) const;
352
    int32_t field_index(int32_t col_unique_id) const;
353
    const TabletColumn& column(size_t ordinal) const;
354
    Result<const TabletColumn*> column(const std::string& field_name) const;
355
    Status have_column(const std::string& field_name) const;
356
    bool exist_column(const std::string& field_name) const;
357
    bool has_column_unique_id(int32_t col_unique_id) const;
358
    const TabletColumn& column_by_uid(int32_t col_unique_id) const;
359
    TabletColumn& mutable_column_by_uid(int32_t col_unique_id);
360
    TabletColumn& mutable_column(size_t ordinal);
361
    void replace_column(size_t pos, TabletColumn new_col);
362
    const std::vector<TabletColumnPtr>& columns() const;
363
660k
    size_t num_columns() const { return _num_columns; }
364
1.08M
    size_t num_key_columns() const { return _num_key_columns; }
365
136k
    const std::vector<uint32_t>& cluster_key_uids() const { return _cluster_key_uids; }
366
0
    size_t num_null_columns() const { return _num_null_columns; }
367
5.21k
    size_t num_short_key_columns() const { return _num_short_key_columns; }
368
0
    size_t num_rows_per_row_block() const { return _num_rows_per_row_block; }
369
1.28k
    size_t num_variant_columns() const { return _num_variant_columns; };
370
0
    size_t num_virtual_columns() const { return _num_virtual_columns; }
371
7.11M
    KeysType keys_type() const { return _keys_type; }
372
5.52k
    SortType sort_type() const { return _sort_type; }
373
0
    size_t sort_col_num() const { return _sort_col_num; }
374
0
    CompressKind compress_kind() const { return _compress_kind; }
375
0
    size_t next_column_unique_id() const { return _next_column_unique_id; }
376
4
    bool has_bf_fpp() const { return _has_bf_fpp; }
377
4
    double bloom_filter_fpp() const { return _bf_fpp; }
378
27.9k
    bool is_in_memory() const { return _is_in_memory; }
379
0
    void set_is_in_memory(bool is_in_memory) { _is_in_memory = is_in_memory; }
380
2
    void set_disable_auto_compaction(bool disable_auto_compaction) {
381
2
        _disable_auto_compaction = disable_auto_compaction;
382
2
    }
383
288
    bool disable_auto_compaction() const { return _disable_auto_compaction; }
384
0
    void set_enable_variant_flatten_nested(bool flatten_nested) {
385
0
        _enable_variant_flatten_nested = flatten_nested;
386
0
    }
387
0
    bool variant_flatten_nested() const { return _enable_variant_flatten_nested; }
388
0
    void set_enable_single_replica_compaction(bool enable_single_replica_compaction) {
389
0
        _enable_single_replica_compaction = enable_single_replica_compaction;
390
0
    }
391
351
    bool enable_single_replica_compaction() const { return _enable_single_replica_compaction; }
392
    // indicate if full row store column(all the columns encodes as row) exists
393
0
    bool has_row_store_for_all_columns() const {
394
0
        return _store_row_column && row_columns_uids().empty();
395
0
    }
396
0
    void set_skip_write_index_on_load(bool skip) { _skip_write_index_on_load = skip; }
397
73
    bool skip_write_index_on_load() const { return _skip_write_index_on_load; }
398
8.20k
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
399
0
    void set_delete_sign_idx(int32_t delete_sign_idx) { _delete_sign_idx = delete_sign_idx; }
400
142k
    bool has_sequence_col() const { return _sequence_col_idx != -1; }
401
64.7k
    int32_t sequence_col_idx() const { return _sequence_col_idx; }
402
0
    void set_version_col_idx(int32_t version_col_idx) { _version_col_idx = version_col_idx; }
403
0
    int32_t version_col_idx() const { return _version_col_idx; }
404
0
    bool has_skip_bitmap_col() const { return _skip_bitmap_col_idx != -1; }
405
0
    int32_t skip_bitmap_col_idx() const { return _skip_bitmap_col_idx; }
406
5.09k
    segment_v2::CompressionTypePB compression_type() const { return _compression_type; }
407
0
    void set_row_store_page_size(long page_size) { _row_store_page_size = page_size; }
408
0
    long row_store_page_size() const { return _row_store_page_size; }
409
0
    void set_storage_page_size(long storage_page_size) { _storage_page_size = storage_page_size; }
410
13.2k
    long storage_page_size() const { return _storage_page_size; }
411
0
    void set_storage_dict_page_size(long storage_dict_page_size) {
412
0
        _storage_dict_page_size = storage_dict_page_size;
413
0
    }
414
13.2k
    long storage_dict_page_size() const { return _storage_dict_page_size; }
415
0
    bool has_global_row_id() const {
416
0
        for (auto [col_name, _] : _field_name_to_index) {
417
0
            if (col_name.start_with(StringRef(BeConsts::GLOBAL_ROWID_COL.data(),
418
0
                                              BeConsts::GLOBAL_ROWID_COL.size()))) {
419
0
                return true;
420
0
            }
421
0
        }
422
0
        return false;
423
0
    }
424
425
112
    const std::vector<const TabletIndex*> inverted_indexes() const {
426
112
        std::vector<const TabletIndex*> inverted_indexes;
427
1.25k
        for (const auto& index : _indexes) {
428
1.25k
            if (index->index_type() == IndexType::INVERTED) {
429
1.24k
                inverted_indexes.emplace_back(index.get());
430
1.24k
            }
431
1.25k
        }
432
112
        return inverted_indexes;
433
112
    }
434
11.1k
    bool has_inverted_index() const {
435
11.1k
        for (const auto& index : _indexes) {
436
699
            DBUG_EXECUTE_IF("tablet_schema::has_inverted_index", {
437
699
                if (index->col_unique_ids().empty()) {
438
699
                    throw Exception(Status::InternalError("col unique ids cannot be empty"));
439
699
                }
440
699
            });
441
442
699
            if (index->index_type() == IndexType::INVERTED) {
443
                //if index_id == -1, ignore it.
444
699
                if (!index->col_unique_ids().empty() && index->col_unique_ids()[0] >= 0) {
445
699
                    return true;
446
699
                }
447
699
            }
448
699
        }
449
10.4k
        return false;
450
11.1k
    }
451
    bool has_inverted_index_with_index_id(int64_t index_id) const;
452
    // Check whether this column supports inverted index
453
    // Some columns (Float, Double, JSONB ...) from the variant do not support index, but they are listed in TabletIndex.
454
    const TabletIndex* inverted_index(const TabletColumn& col) const;
455
456
    // Regardless of whether this column supports inverted index
457
    // TabletIndex information will be returned as long as it exists.
458
    const TabletIndex* inverted_index(int32_t col_unique_id,
459
                                      const std::string& suffix_path = "") const;
460
    bool has_ngram_bf_index(int32_t col_unique_id) const;
461
    const TabletIndex* get_ngram_bf_index(int32_t col_unique_id) const;
462
    void update_indexes_from_thrift(const std::vector<doris::TOlapTableIndex>& indexes);
463
    // If schema version is not set, it should be -1
464
1.54k
    int32_t schema_version() const { return _schema_version; }
465
    void clear_columns();
466
    vectorized::Block create_block(
467
            const std::vector<uint32_t>& return_columns,
468
            const std::unordered_set<uint32_t>* tablet_columns_need_convert_null = nullptr) const;
469
    vectorized::Block create_block(bool ignore_dropped_col = true) const;
470
0
    void set_schema_version(int32_t version) { _schema_version = version; }
471
0
    void set_auto_increment_column(const std::string& auto_increment_column) {
472
0
        _auto_increment_column = auto_increment_column;
473
0
    }
474
0
    std::string auto_increment_column() const { return _auto_increment_column; }
475
476
28
    void set_table_id(int64_t table_id) { _table_id = table_id; }
477
465
    int64_t table_id() const { return _table_id; }
478
28
    void set_db_id(int64_t db_id) { _db_id = db_id; }
479
0
    int64_t db_id() const { return _db_id; }
480
    void build_current_tablet_schema(int64_t index_id, int32_t version,
481
                                     const OlapTableIndexSchema* index,
482
                                     const TabletSchema& out_tablet_schema);
483
484
    // Merge columns that not exit in current schema, these column is dropped in current schema
485
    // but they are useful in some cases. For example,
486
    // 1. origin schema is  ColA, ColB
487
    // 2. insert values     1, 2
488
    // 3. delete where ColB = 2
489
    // 4. drop ColB
490
    // 5. insert values  3
491
    // 6. add column ColB, although it is name ColB, but it is different with previous ColB, the new ColB we name could call ColB'
492
    // 7. insert value  4, 5
493
    // Then the read schema should be ColA, ColB, ColB' because the delete predicate need ColB to remove related data.
494
    // Because they have same name, so that the dropped column should not be added to the map, only with unique id.
495
    void merge_dropped_columns(const TabletSchema& src_schema);
496
497
    bool is_dropped_column(const TabletColumn& col) const;
498
499
    // copy extracted columns from src_schema
500
    void copy_extracted_columns(const TabletSchema& src_schema);
501
502
    // only reserve extracted columns
503
    void reserve_extracted_columns();
504
505
4.04k
    std::string get_all_field_names() const {
506
4.04k
        std::string str = "[";
507
4.04k
        for (auto p : _field_name_to_index) {
508
4.04k
            if (str.size() > 1) {
509
0
                str += ", ";
510
0
            }
511
4.04k
            str += p.first.to_string() + "(" + std::to_string(_cols[p.second]->unique_id()) + ")";
512
4.04k
        }
513
4.04k
        str += "]";
514
4.04k
        return str;
515
4.04k
    }
516
517
    // Dump [(name, type, is_nullable), ...]
518
0
    std::string dump_structure() const {
519
0
        std::string str = "[";
520
0
        for (auto p : _cols) {
521
0
            if (str.size() > 1) {
522
0
                str += ", ";
523
0
            }
524
0
            str += "(";
525
0
            str += p->name();
526
0
            str += ", ";
527
0
            str += TabletColumn::get_string_by_field_type(p->type());
528
0
            str += ", ";
529
0
            str += "is_nullable:";
530
0
            str += (p->is_nullable() ? "true" : "false");
531
0
            str += ")";
532
0
        }
533
0
        str += "]";
534
0
        return str;
535
0
    }
536
537
1
    std::string dump_full_schema() const {
538
1
        std::string str = "[";
539
4
        for (auto p : _cols) {
540
4
            if (str.size() > 1) {
541
3
                str += ", ";
542
3
            }
543
4
            ColumnPB col_pb;
544
4
            p->to_schema_pb(&col_pb);
545
4
            str += "(";
546
4
            str += col_pb.ShortDebugString();
547
4
            str += ")";
548
4
        }
549
1
        str += "]";
550
1
        return str;
551
1
    }
552
553
    vectorized::Block create_block_by_cids(const std::vector<uint32_t>& cids) const;
554
555
    std::shared_ptr<TabletSchema> copy_without_variant_extracted_columns();
556
9.21k
    InvertedIndexStorageFormatPB get_inverted_index_storage_format() const {
557
9.21k
        return _inverted_index_storage_format;
558
9.21k
    }
559
560
    void update_tablet_columns(const TabletSchema& tablet_schema,
561
                               const std::vector<TColumn>& t_columns);
562
563
0
    const std::vector<int32_t>& row_columns_uids() const { return _row_store_column_unique_ids; }
564
565
    int64_t get_metadata_size() const override;
566
567
private:
568
    friend bool operator==(const TabletSchema& a, const TabletSchema& b);
569
    friend bool operator!=(const TabletSchema& a, const TabletSchema& b);
570
0
    TabletSchema(const TabletSchema&) = default;
571
572
    KeysType _keys_type = DUP_KEYS;
573
    SortType _sort_type = SortType::LEXICAL;
574
    size_t _sort_col_num = 0;
575
    std::vector<TabletColumnPtr> _cols;
576
577
    std::vector<TabletIndexPtr> _indexes;
578
    std::unordered_map<StringRef, int32_t, StringRefHash> _field_name_to_index;
579
    std::unordered_map<int32_t, int32_t> _field_uniqueid_to_index;
580
    std::unordered_map<vectorized::PathInDataRef, int32_t, vectorized::PathInDataRef::Hash>
581
            _field_path_to_index;
582
583
    // index_type/col_unique_id/suffix -> idx in _indexes
584
    using IndexKey = std::tuple<IndexType, int32_t, std::string>;
585
    struct IndexKeyHash {
586
49.6k
        size_t operator()(const IndexKey& t) const {
587
49.6k
            uint32_t seed = 0;
588
49.6k
            seed = doris::HashUtil::hash((const char*)&std::get<0>(t), sizeof(std::get<0>(t)),
589
49.6k
                                         seed);
590
49.6k
            seed = doris::HashUtil::hash((const char*)&std::get<1>(t), sizeof(std::get<1>(t)),
591
49.6k
                                         seed);
592
49.6k
            seed = doris::HashUtil::hash((const char*)std::get<2>(t).c_str(),
593
49.6k
                                         static_cast<uint32_t>(std::get<2>(t).size()), seed);
594
49.6k
            return seed;
595
49.6k
        }
596
    };
597
    std::unordered_map<IndexKey, int32_t, IndexKeyHash> _col_id_suffix_to_index;
598
599
    size_t _num_columns = 0;
600
    size_t _num_variant_columns = 0;
601
    size_t _num_virtual_columns = 0;
602
    size_t _num_key_columns = 0;
603
    std::vector<uint32_t> _cluster_key_uids;
604
    size_t _num_null_columns = 0;
605
    size_t _num_short_key_columns = 0;
606
    size_t _num_rows_per_row_block = 0;
607
    CompressKind _compress_kind = COMPRESS_NONE;
608
    segment_v2::CompressionTypePB _compression_type = segment_v2::CompressionTypePB::LZ4F;
609
    long _row_store_page_size = segment_v2::ROW_STORE_PAGE_SIZE_DEFAULT_VALUE;
610
    long _storage_page_size = segment_v2::STORAGE_PAGE_SIZE_DEFAULT_VALUE;
611
    long _storage_dict_page_size = segment_v2::STORAGE_DICT_PAGE_SIZE_DEFAULT_VALUE;
612
    size_t _next_column_unique_id = 0;
613
    std::string _auto_increment_column;
614
615
    bool _has_bf_fpp = false;
616
    double _bf_fpp = 0;
617
    bool _is_in_memory = false;
618
    int32_t _delete_sign_idx = -1;
619
    int32_t _sequence_col_idx = -1;
620
    int32_t _version_col_idx = -1;
621
    int32_t _skip_bitmap_col_idx = -1;
622
    int32_t _schema_version = -1;
623
    int64_t _table_id = -1;
624
    int64_t _db_id = -1;
625
    bool _disable_auto_compaction = false;
626
    bool _enable_single_replica_compaction = false;
627
    bool _store_row_column = false;
628
    bool _skip_write_index_on_load = false;
629
    InvertedIndexStorageFormatPB _inverted_index_storage_format = InvertedIndexStorageFormatPB::V1;
630
631
    // Contains column ids of which columns should be encoded into row store.
632
    // ATTN: For compability reason empty cids means all columns of tablet schema are encoded to row column
633
    std::vector<int32_t> _row_store_column_unique_ids;
634
    bool _enable_variant_flatten_nested = false;
635
636
    std::map<size_t, int32_t> _vir_col_idx_to_unique_id;
637
};
638
639
bool operator==(const TabletSchema& a, const TabletSchema& b);
640
bool operator!=(const TabletSchema& a, const TabletSchema& b);
641
642
using TabletSchemaSPtr = std::shared_ptr<TabletSchema>;
643
644
#include "common/compile_check_end.h"
645
} // namespace doris