Coverage Report

Created: 2026-03-17 01:09

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
3.71M
Schema::Schema(const Schema& other) {
44
3.71M
    _copy_from(other);
45
3.71M
}
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
3.72M
void Schema::_copy_from(const Schema& other) {
55
3.72M
    _col_ids = other._col_ids;
56
3.72M
    _col_offsets = other._col_offsets;
57
58
3.72M
    _num_key_columns = other._num_key_columns;
59
3.72M
    _schema_size = other._schema_size;
60
61
    // Deep copy _cols
62
    // TODO(lingbin): really need clone?
63
3.72M
    _cols.resize(other._cols.size(), nullptr);
64
8.11M
    for (auto cid : _col_ids) {
65
8.11M
        _cols[cid] = other._cols[cid]->clone();
66
8.11M
    }
67
3.72M
}
68
69
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
70
8.53M
                   size_t num_key_columns) {
71
8.53M
    _col_ids = col_ids;
72
8.53M
    _num_key_columns = num_key_columns;
73
74
8.53M
    _cols.resize(cols.size(), nullptr);
75
8.53M
    _col_offsets.resize(_cols.size(), -1);
76
77
8.53M
    size_t offset = 0;
78
8.53M
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
79
143M
    for (int cid = 0; cid < cols.size(); ++cid) {
80
135M
        if (col_id_set.find(cid) == col_id_set.end()) {
81
51.9M
            continue;
82
51.9M
        }
83
83.0M
        _cols[cid] = StorageFieldFactory::create(*cols[cid]);
84
85
83.0M
        _col_offsets[cid] = offset;
86
        // Plus 1 byte for null byte
87
83.0M
        offset += _cols[cid]->size() + 1;
88
83.0M
    }
89
90
8.53M
    _schema_size = offset;
91
8.53M
}
92
93
void Schema::_init(const std::vector<const StorageField*>& cols,
94
1.76M
                   const std::vector<ColumnId>& col_ids, size_t num_key_columns) {
95
1.76M
    _col_ids = col_ids;
96
1.76M
    _num_key_columns = num_key_columns;
97
98
1.76M
    _cols.resize(cols.size(), nullptr);
99
1.76M
    _col_offsets.resize(_cols.size(), -1);
100
101
1.76M
    size_t offset = 0;
102
1.76M
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
103
8.28M
    for (int cid = 0; cid < cols.size(); ++cid) {
104
6.52M
        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
6.52M
        _cols[cid] = cols[cid]->clone();
110
111
6.52M
        _col_offsets[cid] = offset;
112
        // Plus 1 byte for null byte
113
6.52M
        offset += _cols[cid]->size() + 1;
114
6.52M
    }
115
116
1.76M
    _schema_size = offset;
117
1.76M
}
118
119
14.0M
Schema::~Schema() {
120
195M
    for (auto col : _cols) {
121
195M
        delete col;
122
195M
    }
123
14.0M
}
124
125
63.6M
DataTypePtr Schema::get_data_type_ptr(const StorageField& field) {
126
63.6M
    return DataTypeFactory::instance().create_data_type(field);
127
63.6M
}
128
129
6.65M
IColumn::MutablePtr Schema::get_column_by_field(const StorageField& field) {
130
6.65M
    return get_data_type_ptr(field)->create_column();
131
6.65M
}
132
133
IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, bool is_nullable,
134
1.75M
                                                     const ReaderType reader_type) {
135
1.75M
    IColumn::MutablePtr ptr = nullptr;
136
1.75M
    switch (type) {
137
191
    case FieldType::OLAP_FIELD_TYPE_BOOL:
138
191
        ptr = doris::PredicateColumnType<TYPE_BOOLEAN>::create();
139
191
        break;
140
1.69M
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
141
1.69M
        ptr = doris::PredicateColumnType<TYPE_TINYINT>::create();
142
1.69M
        break;
143
1.09k
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
144
1.09k
        ptr = doris::PredicateColumnType<TYPE_SMALLINT>::create();
145
1.09k
        break;
146
19.4k
    case FieldType::OLAP_FIELD_TYPE_INT:
147
19.4k
        ptr = doris::PredicateColumnType<TYPE_INT>::create();
148
19.4k
        break;
149
27
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
150
27
        ptr = doris::PredicateColumnType<TYPE_FLOAT>::create();
151
27
        break;
152
280
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
153
280
        ptr = doris::PredicateColumnType<TYPE_DOUBLE>::create();
154
280
        break;
155
2.89k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
156
2.89k
        ptr = doris::PredicateColumnType<TYPE_BIGINT>::create();
157
2.89k
        break;
158
667
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
159
667
        ptr = doris::PredicateColumnType<TYPE_LARGEINT>::create();
160
667
        break;
161
422
    case FieldType::OLAP_FIELD_TYPE_DATE:
162
422
        ptr = doris::PredicateColumnType<TYPE_DATE>::create();
163
422
        break;
164
2.55k
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
165
2.55k
        ptr = doris::PredicateColumnType<TYPE_DATEV2>::create();
166
2.55k
        break;
167
2.71k
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
168
2.71k
        ptr = doris::PredicateColumnType<TYPE_DATETIMEV2>::create();
169
2.71k
        break;
170
390
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
171
390
        ptr = doris::PredicateColumnType<TYPE_DATETIME>::create();
172
390
        break;
173
3.59k
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
174
3.59k
        ptr = doris::PredicateColumnType<TYPE_TIMESTAMPTZ>::create();
175
3.59k
        break;
176
1.10k
    case FieldType::OLAP_FIELD_TYPE_CHAR:
177
1.10k
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
178
1.10k
            ptr = doris::ColumnDictI32::create(type);
179
1.10k
        } else {
180
3
            ptr = doris::PredicateColumnType<TYPE_CHAR>::create();
181
3
        }
182
1.10k
        break;
183
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
184
15.6k
    case FieldType::OLAP_FIELD_TYPE_STRING:
185
15.7k
    case FieldType::OLAP_FIELD_TYPE_JSONB:
186
15.7k
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
187
15.3k
            ptr = doris::ColumnDictI32::create(type);
188
15.3k
        } else {
189
408
            ptr = doris::PredicateColumnType<TYPE_STRING>::create();
190
408
        }
191
15.7k
        break;
192
5
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
193
5
        ptr = doris::PredicateColumnType<TYPE_DECIMALV2>::create();
194
5
        break;
195
91
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
196
91
        ptr = doris::PredicateColumnType<TYPE_DECIMAL32>::create();
197
91
        break;
198
240
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
199
240
        ptr = doris::PredicateColumnType<TYPE_DECIMAL64>::create();
200
240
        break;
201
697
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
202
697
        ptr = doris::PredicateColumnType<TYPE_DECIMAL128I>::create();
203
697
        break;
204
180
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
205
180
        ptr = doris::PredicateColumnType<TYPE_DECIMAL256>::create();
206
180
        break;
207
85
    case FieldType::OLAP_FIELD_TYPE_IPV4:
208
85
        ptr = doris::PredicateColumnType<TYPE_IPV4>::create();
209
85
        break;
210
85
    case FieldType::OLAP_FIELD_TYPE_IPV6:
211
85
        ptr = doris::PredicateColumnType<TYPE_IPV6>::create();
212
85
        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
1.75M
    }
218
219
1.75M
    if (is_nullable) {
220
27.1k
        return doris::ColumnNullable::create(std::move(ptr), doris::ColumnUInt8::create());
221
27.1k
    }
222
1.72M
    return ptr;
223
1.75M
}
224
225
} // namespace doris