Coverage Report

Created: 2026-03-28 02:37

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
    _num_key_columns = other._num_key_columns;
57
58
    // Deep copy _cols
59
    // TODO(lingbin): really need clone?
60
4
    _cols.resize(other._cols.size(), nullptr);
61
12
    for (auto cid : _col_ids) {
62
12
        _cols[cid] = other._cols[cid]->clone();
63
12
    }
64
4
}
65
66
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
67
6.66k
                   size_t num_key_columns) {
68
6.66k
    _col_ids = col_ids;
69
6.66k
    _num_key_columns = num_key_columns;
70
71
6.66k
    _cols.resize(cols.size(), nullptr);
72
73
6.66k
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
74
66.9k
    for (int cid = 0; cid < cols.size(); ++cid) {
75
60.3k
        if (col_id_set.find(cid) == col_id_set.end()) {
76
41.5k
            continue;
77
41.5k
        }
78
18.7k
        _cols[cid] = StorageFieldFactory::create(*cols[cid]);
79
18.7k
    }
80
6.66k
}
81
82
6.66k
Schema::~Schema() {
83
60.3k
    for (auto col : _cols) {
84
60.3k
        delete col;
85
60.3k
    }
86
6.66k
}
87
88
97.1k
DataTypePtr Schema::get_data_type_ptr(const StorageField& field) {
89
97.1k
    return DataTypeFactory::instance().create_data_type(field);
90
97.1k
}
91
92
0
IColumn::MutablePtr Schema::get_column_by_field(const StorageField& field) {
93
0
    return get_data_type_ptr(field)->create_column();
94
0
}
95
96
IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, bool is_nullable,
97
467
                                                     const ReaderType reader_type) {
98
467
    IColumn::MutablePtr ptr = nullptr;
99
467
    switch (type) {
100
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
101
0
        ptr = doris::PredicateColumnType<TYPE_BOOLEAN>::create();
102
0
        break;
103
0
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
104
0
        ptr = doris::PredicateColumnType<TYPE_TINYINT>::create();
105
0
        break;
106
0
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
107
0
        ptr = doris::PredicateColumnType<TYPE_SMALLINT>::create();
108
0
        break;
109
327
    case FieldType::OLAP_FIELD_TYPE_INT:
110
327
        ptr = doris::PredicateColumnType<TYPE_INT>::create();
111
327
        break;
112
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
113
0
        ptr = doris::PredicateColumnType<TYPE_FLOAT>::create();
114
0
        break;
115
0
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
116
0
        ptr = doris::PredicateColumnType<TYPE_DOUBLE>::create();
117
0
        break;
118
0
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
119
0
        ptr = doris::PredicateColumnType<TYPE_BIGINT>::create();
120
0
        break;
121
0
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
122
0
        ptr = doris::PredicateColumnType<TYPE_LARGEINT>::create();
123
0
        break;
124
0
    case FieldType::OLAP_FIELD_TYPE_DATE:
125
0
        ptr = doris::PredicateColumnType<TYPE_DATE>::create();
126
0
        break;
127
0
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
128
0
        ptr = doris::PredicateColumnType<TYPE_DATEV2>::create();
129
0
        break;
130
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
131
0
        ptr = doris::PredicateColumnType<TYPE_DATETIMEV2>::create();
132
0
        break;
133
0
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
134
0
        ptr = doris::PredicateColumnType<TYPE_DATETIME>::create();
135
0
        break;
136
0
    case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
137
0
        ptr = doris::PredicateColumnType<TYPE_TIMESTAMPTZ>::create();
138
0
        break;
139
0
    case FieldType::OLAP_FIELD_TYPE_CHAR:
140
0
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
141
0
            ptr = doris::ColumnDictI32::create(type);
142
0
        } else {
143
0
            ptr = doris::PredicateColumnType<TYPE_CHAR>::create();
144
0
        }
145
0
        break;
146
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
147
140
    case FieldType::OLAP_FIELD_TYPE_STRING:
148
140
    case FieldType::OLAP_FIELD_TYPE_JSONB:
149
140
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
150
0
            ptr = doris::ColumnDictI32::create(type);
151
140
        } else {
152
140
            ptr = doris::PredicateColumnType<TYPE_STRING>::create();
153
140
        }
154
140
        break;
155
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
156
0
        ptr = doris::PredicateColumnType<TYPE_DECIMALV2>::create();
157
0
        break;
158
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
159
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL32>::create();
160
0
        break;
161
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
162
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL64>::create();
163
0
        break;
164
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
165
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL128I>::create();
166
0
        break;
167
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
168
0
        ptr = doris::PredicateColumnType<TYPE_DECIMAL256>::create();
169
0
        break;
170
0
    case FieldType::OLAP_FIELD_TYPE_IPV4:
171
0
        ptr = doris::PredicateColumnType<TYPE_IPV4>::create();
172
0
        break;
173
0
    case FieldType::OLAP_FIELD_TYPE_IPV6:
174
0
        ptr = doris::PredicateColumnType<TYPE_IPV6>::create();
175
0
        break;
176
0
    default:
177
0
        throw Exception(
178
0
                ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID,
179
0
                fmt::format("Unexpected type when choosing predicate column, type={}", int(type)));
180
467
    }
181
182
467
    if (is_nullable) {
183
140
        return doris::ColumnNullable::create(std::move(ptr), doris::ColumnUInt8::create());
184
140
    }
185
327
    return ptr;
186
467
}
187
188
} // namespace doris