Coverage Report

Created: 2026-03-27 10:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/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 <stddef.h>
21
#include <stdint.h>
22
23
#include <algorithm>
24
#include <cstdint>
25
#include <memory>
26
#include <string>
27
#include <vector>
28
29
#include "common/consts.h"
30
#include "core/column/column.h"
31
#include "exprs/aggregate/aggregate_function.h"
32
#include "io/io_common.h"
33
#include "runtime/thread_context.h"
34
#include "storage/field.h"
35
#include "storage/olap_common.h"
36
#include "storage/tablet/tablet_schema.h"
37
#include "storage/utils.h"
38
39
namespace doris {
40
41
#include "common/compile_check_begin.h"
42
43
// The class is used to represent row's format in memory.  Each row contains
44
// multiple columns, some of which are key-columns (the rest are value-columns).
45
// NOTE: If both key-columns and value-columns exist, then the key-columns
46
// must be placed before value-columns.
47
//
48
// To compare two rows whose schemas are different, but they are from the same origin
49
// we store all column schema maybe accessed here. And default access through column id
50
class Schema;
51
using SchemaSPtr = std::shared_ptr<const Schema>;
52
class Schema {
53
public:
54
    Schema(TabletSchemaSPtr tablet_schema) {
55
        size_t num_columns = tablet_schema->num_columns();
56
        // ignore this column
57
        if (tablet_schema->columns().back()->name() == BeConsts::ROW_STORE_COL) {
58
            --num_columns;
59
        }
60
        std::vector<ColumnId> col_ids(num_columns);
61
        _unique_ids.resize(num_columns);
62
        std::vector<TabletColumnPtr> columns;
63
        columns.reserve(num_columns);
64
65
        size_t num_key_columns = 0;
66
        for (uint32_t cid = 0; cid < num_columns; ++cid) {
67
            col_ids[cid] = cid;
68
            const TabletColumn& column = tablet_schema->column(cid);
69
            _unique_ids[cid] = column.unique_id();
70
            if (column.is_key()) {
71
                ++num_key_columns;
72
            }
73
            if (column.name() == BeConsts::ROWID_COL ||
74
                column.name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
75
                _rowid_col_idx = cid;
76
            }
77
            if (column.name() == VERSION_COL) {
78
                _version_col_idx = cid;
79
            }
80
            columns.push_back(std::make_shared<TabletColumn>(column));
81
        }
82
        _delete_sign_idx = tablet_schema->delete_sign_idx();
83
        if (tablet_schema->has_sequence_col() || tablet_schema->has_seq_map()) {
84
            _has_sequence_col = true;
85
        }
86
        _init(columns, col_ids, num_key_columns);
87
    }
88
89
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
90
8.26M
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
91
8.26M
        size_t num_key_columns = 0;
92
8.26M
        _unique_ids.resize(columns.size());
93
137M
        for (int i = 0; i < columns.size(); ++i) {
94
129M
            if (columns[i]->is_key()) {
95
43.1M
                ++num_key_columns;
96
43.1M
            }
97
129M
            if (columns[i]->name() == DELETE_SIGN) {
98
5.44M
                _delete_sign_idx = i;
99
5.44M
            }
100
129M
            if (columns[i]->name() == BeConsts::ROWID_COL ||
101
129M
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
102
32.4k
                _rowid_col_idx = i;
103
32.4k
            }
104
129M
            if (columns[i]->name() == VERSION_COL) {
105
5.44M
                _version_col_idx = i;
106
5.44M
            }
107
129M
            _unique_ids[i] = columns[i]->unique_id();
108
129M
        }
109
8.26M
        _init(columns, col_ids, num_key_columns);
110
8.26M
    }
111
112
    // Only for UT
113
1.37M
    Schema(const std::vector<TabletColumnPtr>& columns, size_t num_key_columns) {
114
1.37M
        std::vector<ColumnId> col_ids(columns.size());
115
1.37M
        _unique_ids.resize(columns.size());
116
6.33M
        for (uint32_t cid = 0; cid < columns.size(); ++cid) {
117
4.96M
            col_ids[cid] = cid;
118
4.96M
            _unique_ids[cid] = columns[cid]->unique_id();
119
4.96M
        }
120
121
1.37M
        _init(columns, col_ids, num_key_columns);
122
1.37M
    }
123
124
    Schema(const Schema&);
125
    Schema& operator=(const Schema& other);
126
127
    ~Schema();
128
129
    static DataTypePtr get_data_type_ptr(const doris::StorageField& field);
130
131
    static IColumn::MutablePtr get_column_by_field(const doris::StorageField& field);
132
133
    static IColumn::MutablePtr get_predicate_column_ptr(const FieldType& type, bool is_nullable,
134
                                                        const ReaderType reader_type);
135
136
64.3M
    const std::vector<doris::StorageField*>& columns() const { return _cols; }
137
138
228M
    const doris::StorageField* column(ColumnId cid) const { return _cols[cid]; }
139
140
79.5k
    size_t num_key_columns() const { return _num_key_columns; }
141
142
3.21M
    size_t num_columns() const { return _cols.size(); }
143
79.8M
    size_t num_column_ids() const { return _col_ids.size(); }
144
65.8M
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
145
87.8M
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
146
0
    int32_t unique_id(size_t index) const { return _unique_ids[index]; }
147
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
148
0
    bool has_sequence_col() const { return _has_sequence_col; }
149
1.61M
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
150
1.19M
    int32_t version_col_idx() const { return _version_col_idx; }
151
    // Don't use.
152
    // TODO: memory size of Schema cannot be accurately tracked.
153
    // In some places, temporarily use num_columns() as Schema size.
154
0
    int64_t mem_size() const { return _mem_size; }
155
156
private:
157
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
158
               size_t num_key_columns);
159
160
    void _copy_from(const Schema& other);
161
162
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
163
    // a column in current row, not the unique id-identifier of each column
164
    std::vector<ColumnId> _col_ids;
165
    std::vector<int32_t> _unique_ids;
166
    // NOTE: _cols[cid] can only be accessed when the cid is
167
    // contained in _col_ids
168
    std::vector<doris::StorageField*> _cols;
169
170
    size_t _num_key_columns;
171
    int32_t _delete_sign_idx = -1;
172
    bool _has_sequence_col = false;
173
    int32_t _rowid_col_idx = -1;
174
    int32_t _version_col_idx = -1;
175
    int64_t _mem_size = 0;
176
};
177
178
#include "common/compile_check_end.h"
179
180
} // namespace doris