Coverage Report

Created: 2026-06-18 20:09

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