Coverage Report

Created: 2026-04-10 04:10

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
// 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
    Schema(TabletSchemaSPtr tablet_schema) {
53
        size_t num_columns = tablet_schema->num_columns();
54
        // ignore this column
55
        if (tablet_schema->columns().back()->name() == BeConsts::ROW_STORE_COL) {
56
            --num_columns;
57
        }
58
        std::vector<ColumnId> col_ids(num_columns);
59
        _unique_ids.resize(num_columns);
60
        std::vector<TabletColumnPtr> columns;
61
        columns.reserve(num_columns);
62
63
        size_t num_key_columns = 0;
64
        for (uint32_t cid = 0; cid < num_columns; ++cid) {
65
            col_ids[cid] = cid;
66
            const TabletColumn& column = tablet_schema->column(cid);
67
            _unique_ids[cid] = column.unique_id();
68
            if (column.is_key()) {
69
                ++num_key_columns;
70
            }
71
            if (column.name() == BeConsts::ROWID_COL ||
72
                column.name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
73
                _rowid_col_idx = cid;
74
            }
75
            if (column.name() == VERSION_COL) {
76
                _version_col_idx = cid;
77
            }
78
            columns.push_back(std::make_shared<TabletColumn>(column));
79
        }
80
        _delete_sign_idx = tablet_schema->delete_sign_idx();
81
        if (tablet_schema->has_sequence_col() || tablet_schema->has_seq_map()) {
82
            _has_sequence_col = true;
83
        }
84
        _init(columns, col_ids, num_key_columns);
85
    }
86
87
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
88
6.73M
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
89
6.73M
        size_t num_key_columns = 0;
90
6.73M
        _unique_ids.resize(columns.size());
91
113M
        for (int i = 0; i < columns.size(); ++i) {
92
106M
            if (columns[i]->is_key()) {
93
36.9M
                ++num_key_columns;
94
36.9M
            }
95
106M
            if (columns[i]->name() == DELETE_SIGN) {
96
4.81M
                _delete_sign_idx = i;
97
4.81M
            }
98
106M
            if (columns[i]->name() == BeConsts::ROWID_COL ||
99
106M
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
100
29.4k
                _rowid_col_idx = i;
101
29.4k
            }
102
106M
            if (columns[i]->name() == VERSION_COL) {
103
4.80M
                _version_col_idx = i;
104
4.80M
            }
105
106M
            _unique_ids[i] = columns[i]->unique_id();
106
106M
        }
107
6.73M
        _init(columns, col_ids, num_key_columns);
108
6.73M
    }
109
110
    // Only for UT
111
1.20M
    Schema(const std::vector<TabletColumnPtr>& columns, size_t num_key_columns) {
112
1.20M
        std::vector<ColumnId> col_ids(columns.size());
113
1.20M
        _unique_ids.resize(columns.size());
114
5.51M
        for (uint32_t cid = 0; cid < columns.size(); ++cid) {
115
4.30M
            col_ids[cid] = cid;
116
4.30M
            _unique_ids[cid] = columns[cid]->unique_id();
117
4.30M
        }
118
119
1.20M
        _init(columns, col_ids, num_key_columns);
120
1.20M
    }
121
122
    Schema(const Schema&);
123
    Schema& operator=(const Schema& other);
124
125
    ~Schema();
126
127
    static DataTypePtr get_data_type_ptr(const doris::StorageField& field);
128
129
    static IColumn::MutablePtr get_column_by_field(const doris::StorageField& field);
130
131
    static IColumn::MutablePtr get_predicate_column_ptr(const FieldType& type, bool is_nullable,
132
                                                        const ReaderType reader_type);
133
134
56.6M
    const std::vector<doris::StorageField*>& columns() const { return _cols; }
135
136
200M
    const doris::StorageField* column(ColumnId cid) const { return _cols[cid]; }
137
138
71.5k
    size_t num_key_columns() const { return _num_key_columns; }
139
140
2.84M
    size_t num_columns() const { return _cols.size(); }
141
70.0M
    size_t num_column_ids() const { return _col_ids.size(); }
142
57.8M
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
143
76.7M
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
144
0
    int32_t unique_id(size_t index) const { return _unique_ids[index]; }
145
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
146
0
    bool has_sequence_col() const { return _has_sequence_col; }
147
1.42M
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
148
1.10M
    int32_t version_col_idx() const { return _version_col_idx; }
149
    // Don't use.
150
    // TODO: memory size of Schema cannot be accurately tracked.
151
    // In some places, temporarily use num_columns() as Schema size.
152
0
    int64_t mem_size() const { return _mem_size; }
153
154
private:
155
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
156
               size_t num_key_columns);
157
158
    void _copy_from(const Schema& other);
159
160
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
161
    // a column in current row, not the unique id-identifier of each column
162
    std::vector<ColumnId> _col_ids;
163
    std::vector<int32_t> _unique_ids;
164
    // NOTE: _cols[cid] can only be accessed when the cid is
165
    // contained in _col_ids
166
    std::vector<doris::StorageField*> _cols;
167
168
    size_t _num_key_columns;
169
    int32_t _delete_sign_idx = -1;
170
    bool _has_sequence_col = false;
171
    int32_t _rowid_col_idx = -1;
172
    int32_t _version_col_idx = -1;
173
    int64_t _mem_size = 0;
174
};
175
176
} // namespace doris