Coverage Report

Created: 2025-09-09 10:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "io/io_common.h"
31
#include "olap/field.h"
32
#include "olap/olap_common.h"
33
#include "olap/tablet_schema.h"
34
#include "olap/utils.h"
35
#include "runtime/thread_context.h"
36
#include "vec/aggregate_functions/aggregate_function.h"
37
#include "vec/columns/column.h"
38
39
namespace doris {
40
41
#include "common/compile_check_begin.h"
42
43
// The class is used to represent row's format in memory.  Each row contains
44
// multiple columns, some of which are key-columns (the rest are value-columns).
45
// NOTE: If both key-columns and value-columns exist, then the key-columns
46
// must be placed before value-columns.
47
//
48
// To compare two rows whose schemas are different, but they are from the same origin
49
// we store all column schema maybe accessed here. And default access through column id
50
class Schema;
51
using SchemaSPtr = std::shared_ptr<const Schema>;
52
class Schema {
53
public:
54
4.15k
    Schema(TabletSchemaSPtr tablet_schema) {
55
4.15k
        size_t num_columns = tablet_schema->num_columns();
56
        // ignore this column
57
4.15k
        if (tablet_schema->columns().back()->name() == BeConsts::ROW_STORE_COL) {
58
0
            --num_columns;
59
0
        }
60
4.15k
        std::vector<ColumnId> col_ids(num_columns);
61
4.15k
        _unique_ids.resize(num_columns);
62
4.15k
        std::vector<TabletColumnPtr> columns;
63
4.15k
        columns.reserve(num_columns);
64
65
4.15k
        size_t num_key_columns = 0;
66
12.8k
        for (uint32_t cid = 0; cid < num_columns; ++cid) {
67
8.73k
            col_ids[cid] = cid;
68
8.73k
            const TabletColumn& column = tablet_schema->column(cid);
69
8.73k
            _unique_ids[cid] = column.unique_id();
70
8.73k
            if (column.is_key()) {
71
2.50k
                ++num_key_columns;
72
2.50k
            }
73
8.73k
            if (column.name() == BeConsts::ROWID_COL ||
74
8.73k
                column.name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
75
0
                _rowid_col_idx = cid;
76
0
            }
77
8.73k
            if (column.name() == VERSION_COL) {
78
0
                _version_col_idx = cid;
79
0
            }
80
8.73k
            columns.push_back(std::make_shared<TabletColumn>(column));
81
8.73k
        }
82
4.15k
        _delete_sign_idx = tablet_schema->delete_sign_idx();
83
4.15k
        if (tablet_schema->has_sequence_col()) {
84
5
            _has_sequence_col = true;
85
5
        }
86
4.15k
        _init(columns, col_ids, num_key_columns);
87
4.15k
    }
88
89
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
90
7.88M
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
91
7.88M
        size_t num_key_columns = 0;
92
7.88M
        _unique_ids.resize(columns.size());
93
118M
        for (int i = 0; i < columns.size(); ++i) {
94
110M
            if (columns[i]->is_key()) {
95
34.3M
                ++num_key_columns;
96
34.3M
            }
97
110M
            if (columns[i]->name() == DELETE_SIGN) {
98
3.01M
                _delete_sign_idx = i;
99
3.01M
            }
100
110M
            if (columns[i]->name() == BeConsts::ROWID_COL ||
101
110M
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
102
20.3k
                _rowid_col_idx = i;
103
20.3k
            }
104
110M
            if (columns[i]->name() == VERSION_COL) {
105
3.01M
                _version_col_idx = i;
106
3.01M
            }
107
110M
            _unique_ids[i] = columns[i]->unique_id();
108
110M
        }
109
7.88M
        _init(columns, col_ids, num_key_columns);
110
7.88M
    }
111
112
    // Only for UT
113
    Schema(const std::vector<TabletColumnPtr>& columns, size_t num_key_columns) {
114
        std::vector<ColumnId> col_ids(columns.size());
115
        _unique_ids.resize(columns.size());
116
        for (uint32_t cid = 0; cid < columns.size(); ++cid) {
117
            col_ids[cid] = cid;
118
            _unique_ids[cid] = columns[cid]->unique_id();
119
        }
120
121
        _init(columns, col_ids, num_key_columns);
122
    }
123
124
1.66M
    Schema(const std::vector<const doris::Field*>& cols, size_t num_key_columns) {
125
1.66M
        std::vector<ColumnId> col_ids(cols.size());
126
1.66M
        _unique_ids.resize(cols.size());
127
7.63M
        for (uint32_t cid = 0; cid < cols.size(); ++cid) {
128
5.97M
            col_ids[cid] = cid;
129
5.97M
            if (cols.at(cid)->name() == DELETE_SIGN) {
130
0
                _delete_sign_idx = cid;
131
0
            }
132
5.97M
            if (cols.at(cid)->name() == VERSION_COL) {
133
0
                _version_col_idx = cid;
134
0
            }
135
5.97M
            _unique_ids[cid] = cols[cid]->unique_id();
136
5.97M
        }
137
138
1.66M
        _init(cols, col_ids, num_key_columns);
139
1.66M
    }
140
141
    Schema(const Schema&);
142
    Schema& operator=(const Schema& other);
143
144
    ~Schema();
145
146
    static vectorized::DataTypePtr get_data_type_ptr(const doris::Field& field);
147
148
    static vectorized::IColumn::MutablePtr get_column_by_field(const doris::Field& field);
149
150
    static vectorized::IColumn::MutablePtr get_predicate_column_ptr(const FieldType& type,
151
                                                                    bool is_nullable,
152
                                                                    const ReaderType reader_type);
153
154
77.6M
    const std::vector<doris::Field*>& columns() const { return _cols; }
155
156
331M
    const doris::Field* column(ColumnId cid) const { return _cols[cid]; }
157
158
0
    doris::Field* mutable_column(ColumnId cid) const { return _cols[cid]; }
159
160
90.3k
    size_t num_key_columns() const { return _num_key_columns; }
161
5.63M
    size_t schema_size() const { return _schema_size; }
162
163
50.0M
    size_t column_offset(ColumnId cid) const { return _col_offsets[cid]; }
164
165
0
    size_t column_size(ColumnId cid) const { return _cols[cid]->size(); }
166
167
0
    bool is_null(const char* row, int index) const {
168
0
        return *reinterpret_cast<const bool*>(row + _col_offsets[index]);
169
0
    }
170
0
    void set_is_null(void* row, uint32_t cid, bool is_null) const {
171
0
        *reinterpret_cast<bool*>((char*)row + _col_offsets[cid]) = is_null;
172
0
    }
173
174
5.63M
    size_t num_columns() const { return _cols.size(); }
175
109M
    size_t num_column_ids() const { return _col_ids.size(); }
176
100M
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
177
0
    const std::vector<int32_t>& unique_ids() const { return _unique_ids; }
178
107M
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
179
0
    int32_t unique_id(size_t index) const { return _unique_ids[index]; }
180
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
181
0
    bool has_sequence_col() const { return _has_sequence_col; }
182
1.87M
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
183
4.25M
    int32_t version_col_idx() const { return _version_col_idx; }
184
    // Don't use.
185
    // TODO: memory size of Schema cannot be accurately tracked.
186
    // In some places, temporarily use num_columns() as Schema size.
187
0
    int64_t mem_size() const { return _mem_size; }
188
189
private:
190
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
191
               size_t num_key_columns);
192
    void _init(const std::vector<const doris::Field*>& cols, const std::vector<ColumnId>& col_ids,
193
               size_t num_key_columns);
194
195
    void _copy_from(const Schema& other);
196
197
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
198
    // a column in current row, not the unique id-identifier of each column
199
    std::vector<ColumnId> _col_ids;
200
    std::vector<int32_t> _unique_ids;
201
    // NOTE: Both _cols[cid] and _col_offsets[cid] can only be accessed when the cid is
202
    // contained in _col_ids
203
    std::vector<doris::Field*> _cols;
204
    // The value of each item indicates the starting offset of the corresponding column in
205
    // current row. e.g. _col_offsets[idx] is the offset of _cols[idx] (idx must in _col_ids)
206
    std::vector<size_t> _col_offsets;
207
208
    size_t _num_key_columns;
209
    size_t _schema_size;
210
    int32_t _delete_sign_idx = -1;
211
    bool _has_sequence_col = false;
212
    int32_t _rowid_col_idx = -1;
213
    int32_t _version_col_idx = -1;
214
    int64_t _mem_size = 0;
215
};
216
217
#include "common/compile_check_end.h"
218
219
} // namespace doris