Coverage Report

Created: 2026-07-14 19:01

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
    // All the columns of one table may exist in the columns param, but col_ids is only a subset.
52
7.01k
    Schema(const std::vector<TabletColumnPtr>& columns, const std::vector<ColumnId>& col_ids) {
53
7.01k
        size_t num_key_columns = 0;
54
68.8k
        for (int i = 0; i < columns.size(); ++i) {
55
61.8k
            if (columns[i]->is_key()) {
56
5.45k
                ++num_key_columns;
57
5.45k
            }
58
61.8k
            if (columns[i]->name() == DELETE_SIGN) {
59
1.73k
                _delete_sign_idx = i;
60
1.73k
            }
61
61.8k
            if (columns[i]->name() == BeConsts::ROWID_COL ||
62
61.8k
                columns[i]->name().starts_with(BeConsts::GLOBAL_ROWID_COL)) {
63
0
                _rowid_col_idx = i;
64
0
            }
65
61.8k
            if (columns[i]->name() == VERSION_COL) {
66
0
                _version_col_idx = i;
67
0
            }
68
61.8k
            if (columns[i]->name() == BINLOG_TSO_COL) {
69
0
                _tso_col_idx = i;
70
0
            }
71
61.8k
            if (columns[i]->name() == BINLOG_LSN_COL) {
72
0
                _lsn_col_idx = i;
73
0
            }
74
61.8k
            if (columns[i]->name() == BINLOG_OP_COL) {
75
0
                _op_col_idx = i;
76
0
            }
77
61.8k
            if (columns[i]->name() == COMMIT_TSO_COL) {
78
4
                _commit_tso_col_idx = i;
79
4
            }
80
61.8k
        }
81
7.01k
        _init(columns, col_ids, num_key_columns);
82
7.01k
    }
83
84
    Schema(const Schema&);
85
    Schema& operator=(const Schema& other);
86
87
    ~Schema();
88
89
    static DataTypePtr get_data_type_ptr(const TabletColumn& column);
90
91
    static IColumn::MutablePtr get_predicate_column_ptr(const DataTypePtr& data_type,
92
                                                        const ReaderType reader_type);
93
94
59.8k
    const std::vector<TabletColumnPtr>& columns() const { return _cols; }
95
96
1.20M
    const TabletColumn* column(ColumnId cid) const { return _cols[cid].get(); }
97
98
1.48k
    size_t num_key_columns() const { return _num_key_columns; }
99
100
13.4k
    size_t num_columns() const { return _cols.size(); }
101
977k
    size_t num_column_ids() const { return _col_ids.size(); }
102
47.8k
    const std::vector<ColumnId>& column_ids() const { return _col_ids; }
103
42.4k
    ColumnId column_id(size_t index) const { return _col_ids[index]; }
104
21.4k
    int column_index(ColumnId cid) const { return _column_id_to_index[cid]; }
105
0
    const std::vector<int>& column_id_to_index() const { return _column_id_to_index; }
106
0
    int32_t delete_sign_idx() const { return _delete_sign_idx; }
107
0
    bool has_sequence_col() const { return _has_sequence_col; }
108
3.03k
    int32_t rowid_col_idx() const { return _rowid_col_idx; }
109
8.21k
    int32_t version_col_idx() const { return _version_col_idx; }
110
63
    int32_t commit_tso_col_idx() const { return _commit_tso_col_idx; }
111
0
    int32_t tso_col_idx() const { return _tso_col_idx; }
112
0
    int32_t lsn_col_idx() const { return _lsn_col_idx; }
113
0
    int32_t op_col_idx() const { return _op_col_idx; }
114
    // Don't use.
115
    // TODO: memory size of Schema cannot be accurately tracked.
116
    // In some places, temporarily use num_columns() as Schema size.
117
0
    int64_t mem_size() const { return _mem_size; }
118
119
private:
120
    void _init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
121
               size_t num_key_columns);
122
123
    void _copy_from(const Schema& other);
124
125
    // NOTE: The ColumnId here represents the sequential index number (starting from 0) of
126
    // a column in current row, not the unique id-identifier of each column
127
    std::vector<ColumnId> _col_ids;
128
    // NOTE: _cols[cid] can only be accessed when the cid is
129
    // contained in _col_ids
130
    std::vector<TabletColumnPtr> _cols;
131
    // Tablet column id -> slot index in this Schema.
132
    std::vector<int> _column_id_to_index;
133
134
    size_t _num_key_columns;
135
    int32_t _delete_sign_idx = -1;
136
    bool _has_sequence_col = false;
137
    int32_t _rowid_col_idx = -1;
138
    int32_t _version_col_idx = -1;
139
    int32_t _commit_tso_col_idx = -1;
140
    int32_t _tso_col_idx = -1;
141
    int32_t _lsn_col_idx = -1;
142
    int32_t _op_col_idx = -1;
143
    int64_t _mem_size = 0;
144
};
145
146
} // namespace doris