Coverage Report

Created: 2026-05-12 22:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/schema.cpp
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
#include "storage/schema.h"
19
20
#include <glog/logging.h>
21
22
#include <boost/iterator/iterator_facade.hpp>
23
#include <ostream>
24
#include <unordered_set>
25
#include <utility>
26
27
#include "common/config.h"
28
#include "core/column/column_array.h"
29
#include "core/column/column_dictionary.h"
30
#include "core/column/column_map.h"
31
#include "core/column/column_nullable.h"
32
#include "core/column/column_struct.h"
33
#include "core/column/predicate_column.h"
34
#include "core/data_type/data_type.h"
35
#include "core/data_type/data_type_factory.hpp"
36
#include "core/data_type/define_primitive_type.h"
37
#include "core/types.h"
38
#include "storage/olap_common.h"
39
#include "util/trace.h"
40
41
namespace doris {
42
43
4
Schema::Schema(const Schema& other) {
44
4
    _copy_from(other);
45
4
}
46
47
0
Schema& Schema::operator=(const Schema& other) {
48
0
    if (this != &other) {
49
0
        _copy_from(other);
50
0
    }
51
0
    return *this;
52
0
}
53
54
4
void Schema::_copy_from(const Schema& other) {
55
4
    _col_ids = other._col_ids;
56
4
    _unique_ids = other._unique_ids;
57
4
    _num_key_columns = other._num_key_columns;
58
4
    _delete_sign_idx = other._delete_sign_idx;
59
4
    _has_sequence_col = other._has_sequence_col;
60
4
    _rowid_col_idx = other._rowid_col_idx;
61
4
    _version_col_idx = other._version_col_idx;
62
4
    _lsn_col_idx = other._lsn_col_idx;
63
4
    _tso_col_idx = other._tso_col_idx;
64
4
    _mem_size = other._mem_size;
65
66
    // Deep copy _cols
67
    // TODO(lingbin): really need clone?
68
4
    _cols.resize(other._cols.size(), nullptr);
69
12
    for (auto cid : _col_ids) {
70
12
        _cols[cid] = other._cols[cid]->clone();
71
12
    }
72
4
}
73
74
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
75
6.58k
                   size_t num_key_columns) {
76
6.58k
    _col_ids = col_ids;
77
6.58k
    _num_key_columns = num_key_columns;
78
79
6.58k
    _cols.resize(cols.size(), nullptr);
80
81
6.58k
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
82
66.7k
    for (int cid = 0; cid < cols.size(); ++cid) {
83
60.1k
        if (col_id_set.find(cid) == col_id_set.end()) {
84
41.5k
            continue;
85
41.5k
        }
86
18.5k
        _cols[cid] = StorageFieldFactory::create(*cols[cid]);
87
18.5k
    }
88
6.58k
}
89
90
6.59k
Schema::~Schema() {
91
60.1k
    for (auto col : _cols) {
92
60.1k
        delete col;
93
60.1k
    }
94
6.59k
}
95
96
66.7k
DataTypePtr Schema::get_data_type_ptr(const StorageField& field) {
97
66.7k
    return DataTypeFactory::instance().create_data_type(field);
98
66.7k
}
99
100
0
IColumn::MutablePtr Schema::get_column_by_field(const StorageField& field) {
101
0
    return get_data_type_ptr(field)->create_column();
102
0
}
103
104
IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, bool is_nullable,
105
467
                                                     const ReaderType reader_type) {
106
467
    IColumn::MutablePtr ptr = nullptr;
107
467
    switch (type) {
108
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
109
0
        ptr = doris::PredicateColumnType<TYPE_BOOLEAN>::create();
110
0
        break;
111
0
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
112
0
        ptr = doris::PredicateColumnType<TYPE_TINYINT>::create();
113
0
        break;
114
0
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
115
0
        ptr = doris::PredicateColumnType<TYPE_SMALLINT>::create();
116
0
        break;
117
327
    case FieldType::OLAP_FIELD_TYPE_INT:
118
327
        ptr = doris::PredicateColumnType<TYPE_INT>::create();
119
327
        break;
120
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
121
0
        ptr = doris::PredicateColumnType<TYPE_FLOAT>::create();
122
0
        break;
123
0
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
124
0
        ptr = doris::PredicateColumnType<TYPE_DOUBLE>::create();
125
0
        break;
126
0
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
127
0
        ptr = doris::PredicateColumnType<TYPE_BIGINT>::create();
128
0
        break;
129
0
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
130
0
        ptr = doris::PredicateColumnType<TYPE_LARGEINT>::create();
131
0
        break;
132
0
    case FieldType::OLAP_FIELD_TYPE_DATE:
133
0
        ptr = doris::PredicateColumnType<TYPE_DATE>::create();
134
0
        break;
135
0
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
136
0
        ptr = doris::PredicateColumnType<TYPE_DATEV2>::create();
137
0
        break;
138
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
139
0
        ptr = doris::PredicateColumnType<TYPE_DATETIMEV2>::create();
140
0
        break;
141
0
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
142
0
        ptr = doris::PredicateColumnType<TYPE_DATETIME>::create();
143
0
        break;
144
0
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
145
0
        ptr = doris::PredicateColumnType<TYPE_TIMESTAMPTZ>::create();
146
0
        break;
147
0
    case FieldType::OLAP_FIELD_TYPE_CHAR:
148
0
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
149
0
            ptr = doris::ColumnDictI32::create(type);
150
0
        } else {
151
0
            ptr = doris::PredicateColumnType<TYPE_CHAR>::create();
152
0
        }
153
0
        break;
154
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
155
140
    case FieldType::OLAP_FIELD_TYPE_STRING:
156
140
    case FieldType::OLAP_FIELD_TYPE_JSONB:
157
140
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
158
0
            ptr = doris::ColumnDictI32::create(type);
159
140
        } else {
160
140
            ptr = doris::PredicateColumnType<TYPE_STRING>::create();
161
140
        }
162
140
        break;
163
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
164
0
        ptr = doris::PredicateColumnType<TYPE_DECIMALV2>::create();
165
0
        break;
166
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
167
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL32>::create();
168
0
        break;
169
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
170
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL64>::create();
171
0
        break;
172
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
173
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL128I>::create();
174
0
        break;
175
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
176
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL256>::create();
177
0
        break;
178
0
    case FieldType::OLAP_FIELD_TYPE_IPV4:
179
0
        ptr = doris::PredicateColumnType<TYPE_IPV4>::create();
180
0
        break;
181
0
    case FieldType::OLAP_FIELD_TYPE_IPV6:
182
0
        ptr = doris::PredicateColumnType<TYPE_IPV6>::create();
183
0
        break;
184
0
    default:
185
0
        throw Exception(
186
0
                ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID,
187
0
                fmt::format("Unexpected type when choosing predicate column, type={}", int(type)));
188
467
    }
189
190
467
    if (is_nullable) {
191
140
        return doris::ColumnNullable::create(std::move(ptr), doris::ColumnUInt8::create());
192
140
    }
193
327
    return ptr;
194
467
}
195
196
} // namespace doris