Coverage Report

Created: 2026-06-01 15:56

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/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
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
53
10.5M
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
54
10.5M
        size_t num_key_columns = 0;
55
152M
        for (int i = 0; i < columns.size(); ++i) {
56
141M
            if (columns[i]->is_key()) {
57
53.8M
                ++num_key_columns;
58
53.8M
            }
59
141M
            if (columns[i]->name() == DELETE_SIGN) {
60
5.84M
                _delete_sign_idx = i;
61
5.84M
            }
62
141M
            if (columns[i]->name() == BeConsts::ROWID_COL ||
63
141M
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
64
31.1k
                _rowid_col_idx = i;
65
31.1k
            }
66
141M
            if (columns[i]->name() == VERSION_COL) {
67
5.84M
                _version_col_idx = i;
68
5.84M
            }
69
141M
            if (columns[i]->name() == std::string(kRowBinlogLsnColName)) {
70
0
                _lsn_col_idx = i;
71
0
            }
72
141M
            if (columns[i]->name() == std::string(kRowBinlogTimestampColName)) {
73
0
                _tso_col_idx = i;
74
0
            }
75
141M
        }
76
10.5M
        _init(columns, col_ids, num_key_columns);
77
10.5M
    }
78
79
    Schema(const Schema&);
80
    Schema& operator=(const Schema& other);
81
82
    ~Schema();
83
84
    static DataTypePtr get_data_type_ptr(const TabletColumn& column);
85
86
    static IColumn::MutablePtr get_predicate_column_ptr(const FieldType& type, bool is_nullable,
87
                                                        const ReaderType reader_type);
88
89
57.6M
    const std::vector<TabletColumnPtr>& columns() const { return _cols; }
90
91
205M
    const TabletColumn* column(ColumnId cid) const { return _cols[cid].get(); }
92
93
71.5k
    size_t num_key_columns() const { return _num_key_columns; }
94
95
2.92M
    size_t num_columns() const { return _cols.size(); }
96
71.7M
    size_t num_column_ids() const { return _col_ids.size(); }
97
60.9M
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
98
78.8M
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
99
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
100
0
    bool has_sequence_col() const { return _has_sequence_col; }
101
1.45M
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
102
1.12M
    int32_t version_col_idx() const { return _version_col_idx; }
103
0
    int32_t lsn_col_idx() const { return _lsn_col_idx; }
104
0
    int32_t tso_col_idx() const { return _tso_col_idx; }
105
    // Don't use.
106
    // TODO: memory size of Schema cannot be accurately tracked.
107
    // In some places, temporarily use num_columns() as Schema size.
108
0
    int64_t mem_size() const { return _mem_size; }
109
110
private:
111
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
112
               size_t num_key_columns);
113
114
    void _copy_from(const Schema& other);
115
116
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
117
    // a column in current row, not the unique id-identifier of each column
118
    std::vector<ColumnId> _col_ids;
119
    // NOTE: _cols[cid] can only be accessed when the cid is
120
    // contained in _col_ids
121
    std::vector<TabletColumnPtr> _cols;
122
123
    size_t _num_key_columns;
124
    int32_t _delete_sign_idx = -1;
125
    bool _has_sequence_col = false;
126
    int32_t _rowid_col_idx = -1;
127
    int32_t _version_col_idx = -1;
128
    int32_t _lsn_col_idx = -1;
129
    int32_t _tso_col_idx = -1;
130
    int64_t _mem_size = 0;
131
};
132
133
} // namespace doris