Coverage Report

Created: 2026-05-18 13:00

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