Coverage Report

Created: 2026-03-23 17:10

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
1.43M
Schema::Schema(const Schema& other) {
44
1.43M
    _copy_from(other);
45
1.43M
}
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
1.44M
void Schema::_copy_from(const Schema& other) {
55
1.44M
    _col_ids = other._col_ids;
56
1.44M
    _col_offsets = other._col_offsets;
57
58
1.44M
    _num_key_columns = other._num_key_columns;
59
1.44M
    _schema_size = other._schema_size;
60
61
    // Deep copy _cols
62
    // TODO(lingbin): really need clone?
63
1.44M
    _cols.resize(other._cols.size(), nullptr);
64
2.63M
    for (auto cid : _col_ids) {
65
2.63M
        _cols[cid] = other._cols[cid]->clone();
66
2.63M
    }
67
1.44M
}
68
69
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
70
2.70M
                   size_t num_key_columns) {
71
2.70M
    _col_ids = col_ids;
72
2.70M
    _num_key_columns = num_key_columns;
73
74
2.70M
    _cols.resize(cols.size(), nullptr);
75
2.70M
    _col_offsets.resize(_cols.size(), -1);
76
77
2.70M
    size_t offset = 0;
78
2.70M
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
79
43.1M
    for (int cid = 0; cid < cols.size(); ++cid) {
80
40.4M
        if (col_id_set.find(cid) == col_id_set.end()) {
81
9.61M
            continue;
82
9.61M
        }
83
30.8M
        _cols[cid] = StorageFieldFactory::create(*cols[cid]);
84
85
30.8M
        _col_offsets[cid] = offset;
86
        // Plus 1 byte for null byte
87
30.8M
        offset += _cols[cid]->size() + 1;
88
30.8M
    }
89
90
2.70M
    _schema_size = offset;
91
2.70M
}
92
93
void Schema::_init(const std::vector<const StorageField*>& cols,
94
412k
                   const std::vector<ColumnId>& col_ids, size_t num_key_columns) {
95
412k
    _col_ids = col_ids;
96
412k
    _num_key_columns = num_key_columns;
97
98
412k
    _cols.resize(cols.size(), nullptr);
99
412k
    _col_offsets.resize(_cols.size(), -1);
100
101
412k
    size_t offset = 0;
102
412k
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
103
1.90M
    for (int cid = 0; cid < cols.size(); ++cid) {
104
1.49M
        if (col_id_set.find(cid) == col_id_set.end()) {
105
0
            continue;
106
0
        }
107
        // TODO(lingbin): is it necessary to clone StorageField? each SegmentIterator will
108
        // use this func, can we avoid clone?
109
1.49M
        _cols[cid] = cols[cid]->clone();
110
111
1.49M
        _col_offsets[cid] = offset;
112
        // Plus 1 byte for null byte
113
1.49M
        offset += _cols[cid]->size() + 1;
114
1.49M
    }
115
116
412k
    _schema_size = offset;
117
412k
}
118
119
4.56M
Schema::~Schema() {
120
62.6M
    for (auto col : _cols) {
121
62.6M
        delete col;
122
62.6M
    }
123
4.56M
}
124
125
15.3M
DataTypePtr Schema::get_data_type_ptr(const StorageField& field) {
126
15.3M
    return DataTypeFactory::instance().create_data_type(field);
127
15.3M
}
128
129
1.52M
IColumn::MutablePtr Schema::get_column_by_field(const StorageField& field) {
130
1.52M
    return get_data_type_ptr(field)->create_column();
131
1.52M
}
132
133
IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, bool is_nullable,
134
449k
                                                     const ReaderType reader_type) {
135
449k
    IColumn::MutablePtr ptr = nullptr;
136
449k
    switch (type) {
137
383
    case FieldType::OLAP_FIELD_TYPE_BOOL:
138
383
        ptr = doris::PredicateColumnType<TYPE_BOOLEAN>::create();
139
383
        break;
140
414k
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
141
414k
        ptr = doris::PredicateColumnType<TYPE_TINYINT>::create();
142
414k
        break;
143
254
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
144
254
        ptr = doris::PredicateColumnType<TYPE_SMALLINT>::create();
145
254
        break;
146
18.1k
    case FieldType::OLAP_FIELD_TYPE_INT:
147
18.1k
        ptr = doris::PredicateColumnType<TYPE_INT>::create();
148
18.1k
        break;
149
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
150
0
        ptr = doris::PredicateColumnType<TYPE_FLOAT>::create();
151
0
        break;
152
128
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
153
128
        ptr = doris::PredicateColumnType<TYPE_DOUBLE>::create();
154
128
        break;
155
1.47k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
156
1.47k
        ptr = doris::PredicateColumnType<TYPE_BIGINT>::create();
157
1.47k
        break;
158
528
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
159
528
        ptr = doris::PredicateColumnType<TYPE_LARGEINT>::create();
160
528
        break;
161
155
    case FieldType::OLAP_FIELD_TYPE_DATE:
162
155
        ptr = doris::PredicateColumnType<TYPE_DATE>::create();
163
155
        break;
164
1.19k
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
165
1.19k
        ptr = doris::PredicateColumnType<TYPE_DATEV2>::create();
166
1.19k
        break;
167
2.28k
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
168
2.28k
        ptr = doris::PredicateColumnType<TYPE_DATETIMEV2>::create();
169
2.28k
        break;
170
333
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
171
333
        ptr = doris::PredicateColumnType<TYPE_DATETIME>::create();
172
333
        break;
173
0
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
174
0
        ptr = doris::PredicateColumnType<TYPE_TIMESTAMPTZ>::create();
175
0
        break;
176
798
    case FieldType::OLAP_FIELD_TYPE_CHAR:
177
798
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
178
797
            ptr = doris::ColumnDictI32::create(type);
179
797
        } else {
180
1
            ptr = doris::PredicateColumnType<TYPE_CHAR>::create();
181
1
        }
182
798
        break;
183
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
184
7.99k
    case FieldType::OLAP_FIELD_TYPE_STRING:
185
8.02k
    case FieldType::OLAP_FIELD_TYPE_JSONB:
186
8.02k
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
187
7.87k
            ptr = doris::ColumnDictI32::create(type);
188
7.87k
        } else {
189
148
            ptr = doris::PredicateColumnType<TYPE_STRING>::create();
190
148
        }
191
8.02k
        break;
192
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
193
0
        ptr = doris::PredicateColumnType<TYPE_DECIMALV2>::create();
194
0
        break;
195
22
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
196
22
        ptr = doris::PredicateColumnType<TYPE_DECIMAL32>::create();
197
22
        break;
198
72
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
199
72
        ptr = doris::PredicateColumnType<TYPE_DECIMAL64>::create();
200
72
        break;
201
674
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
202
674
        ptr = doris::PredicateColumnType<TYPE_DECIMAL128I>::create();
203
674
        break;
204
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
205
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL256>::create();
206
0
        break;
207
8
    case FieldType::OLAP_FIELD_TYPE_IPV4:
208
8
        ptr = doris::PredicateColumnType<TYPE_IPV4>::create();
209
8
        break;
210
2
    case FieldType::OLAP_FIELD_TYPE_IPV6:
211
2
        ptr = doris::PredicateColumnType<TYPE_IPV6>::create();
212
2
        break;
213
0
    default:
214
0
        throw Exception(
215
0
                ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID,
216
0
                fmt::format("Unexpected type when choosing predicate column, type={}", int(type)));
217
449k
    }
218
219
450k
    if (is_nullable) {
220
14.2k
        return doris::ColumnNullable::create(std::move(ptr), doris::ColumnUInt8::create());
221
14.2k
    }
222
435k
    return ptr;
223
450k
}
224
225
} // namespace doris