Coverage Report

Created: 2026-05-14 06:37

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