Coverage Report

Created: 2026-08-20 19:08

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