Coverage Report

Created: 2026-05-21 14:12

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/binlog.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
// The class is used to represent row's format in memory.  Each row contains
42
// multiple columns, some of which are key-columns (the rest are value-columns).
43
// NOTE: If both key-columns and value-columns exist, then the key-columns
44
// must be placed before value-columns.
45
//
46
// To compare two rows whose schemas are different, but they are from the same origin
47
// we store all column schema maybe accessed here. And default access through column id
48
class Schema;
49
using SchemaSPtr = std::shared_ptr<const Schema>;
50
class Schema {
51
public:
52
0
    Schema(TabletSchemaSPtr tablet_schema) {
53
0
        size_t num_columns = tablet_schema->num_columns();
54
0
        // ignore this column
55
0
        if (tablet_schema->columns().back()->name() == BeConsts::ROW_STORE_COL) {
56
0
            --num_columns;
57
0
        }
58
0
        std::vector<ColumnId> col_ids(num_columns);
59
0
        _unique_ids.resize(num_columns);
60
0
        std::vector<TabletColumnPtr> columns;
61
0
        columns.reserve(num_columns);
62
0
63
0
        size_t num_key_columns = 0;
64
0
        for (uint32_t cid = 0; cid < num_columns; ++cid) {
65
0
            col_ids[cid] = cid;
66
0
            const TabletColumn& column = tablet_schema->column(cid);
67
0
            _unique_ids[cid] = column.unique_id();
68
0
            if (column.is_key()) {
69
0
                ++num_key_columns;
70
0
            }
71
0
            if (column.name() == BeConsts::ROWID_COL ||
72
0
                column.name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
73
0
                _rowid_col_idx = cid;
74
0
            }
75
0
            if (column.name() == VERSION_COL) {
76
0
                _version_col_idx = cid;
77
0
            }
78
0
            if (column.name() == std::string(kRowBinlogLsnColName)) {
79
0
                _lsn_col_idx = cid;
80
0
            }
81
0
            if (column.name() == std::string(kRowBinlogTimestampColName)) {
82
0
                _tso_col_idx = cid;
83
0
            }
84
0
            columns.push_back(std::make_shared<TabletColumn>(column));
85
0
        }
86
0
        _delete_sign_idx = tablet_schema->delete_sign_idx();
87
0
        if (tablet_schema->has_sequence_col() || tablet_schema->has_seq_map()) {
88
0
            _has_sequence_col = true;
89
0
        }
90
0
        _init(columns, col_ids, num_key_columns);
91
0
    }
92
93
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
94
8.28M
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
95
8.28M
        size_t num_key_columns = 0;
96
8.28M
        _unique_ids.resize(columns.size());
97
138M
        for (int i = 0; i < columns.size(); ++i) {
98
129M
            if (columns[i]->is_key()) {
99
46.0M
                ++num_key_columns;
100
46.0M
            }
101
129M
            if (columns[i]->name() == DELETE_SIGN) {
102
6.16M
                _delete_sign_idx = i;
103
6.16M
            }
104
129M
            if (columns[i]->name() == BeConsts::ROWID_COL ||
105
129M
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
106
29.1k
                _rowid_col_idx = i;
107
29.1k
            }
108
129M
            if (columns[i]->name() == VERSION_COL) {
109
6.16M
                _version_col_idx = i;
110
6.16M
            }
111
129M
            if (columns[i]->name() == std::string(kRowBinlogLsnColName)) {
112
0
                _lsn_col_idx = i;
113
0
            }
114
129M
            if (columns[i]->name() == std::string(kRowBinlogTimestampColName)) {
115
0
                _tso_col_idx = i;
116
0
            }
117
129M
            _unique_ids[i] = columns[i]->unique_id();
118
129M
        }
119
8.28M
        _init(columns, col_ids, num_key_columns);
120
8.28M
    }
121
122
    // Only for UT
123
1.64M
    Schema(const std::vector<TabletColumnPtr>& columns, size_t num_key_columns) {
124
1.64M
        std::vector<ColumnId> col_ids(columns.size());
125
1.64M
        _unique_ids.resize(columns.size());
126
7.68M
        for (uint32_t cid = 0; cid < columns.size(); ++cid) {
127
6.03M
            col_ids[cid] = cid;
128
6.03M
            _unique_ids[cid] = columns[cid]->unique_id();
129
6.03M
        }
130
131
1.64M
        _init(columns, col_ids, num_key_columns);
132
1.64M
    }
133
134
    Schema(const Schema&);
135
    Schema& operator=(const Schema& other);
136
137
    ~Schema();
138
139
    static DataTypePtr get_data_type_ptr(const TabletColumn& column);
140
141
    static IColumn::MutablePtr get_predicate_column_ptr(const FieldType& type, bool is_nullable,
142
                                                        const ReaderType reader_type);
143
144
77.4M
    const std::vector<TabletColumnPtr>& columns() const { return _cols; }
145
146
275M
    const TabletColumn* column(ColumnId cid) const { return _cols[cid].get(); }
147
148
98.5k
    size_t num_key_columns() const { return _num_key_columns; }
149
150
3.90M
    size_t num_columns() const { return _cols.size(); }
151
96.6M
    size_t num_column_ids() const { return _col_ids.size(); }
152
81.9M
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
153
107M
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
154
0
    int32_t unique_id(size_t index) const { return _unique_ids[index]; }
155
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
156
0
    bool has_sequence_col() const { return _has_sequence_col; }
157
1.95M
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
158
1.25M
    int32_t version_col_idx() const { return _version_col_idx; }
159
0
    int32_t lsn_col_idx() const { return _lsn_col_idx; }
160
0
    int32_t tso_col_idx() const { return _tso_col_idx; }
161
    // Don't use.
162
    // TODO: memory size of Schema cannot be accurately tracked.
163
    // In some places, temporarily use num_columns() as Schema size.
164
0
    int64_t mem_size() const { return _mem_size; }
165
166
private:
167
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
168
               size_t num_key_columns);
169
170
    void _copy_from(const Schema& other);
171
172
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
173
    // a column in current row, not the unique id-identifier of each column
174
    std::vector<ColumnId> _col_ids;
175
    std::vector<int32_t> _unique_ids;
176
    // NOTE: _cols[cid] can only be accessed when the cid is
177
    // contained in _col_ids
178
    std::vector<TabletColumnPtr> _cols;
179
180
    size_t _num_key_columns;
181
    int32_t _delete_sign_idx = -1;
182
    bool _has_sequence_col = false;
183
    int32_t _rowid_col_idx = -1;
184
    int32_t _version_col_idx = -1;
185
    int32_t _lsn_col_idx = -1;
186
    int32_t _tso_col_idx = -1;
187
    int64_t _mem_size = 0;
188
};
189
190
} // namespace doris