Coverage Report

Created: 2025-08-27 03:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "olap/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 "olap/olap_common.h"
29
#include "runtime/define_primitive_type.h"
30
#include "util/trace.h"
31
#include "vec/columns/column_array.h"
32
#include "vec/columns/column_dictionary.h"
33
#include "vec/columns/column_map.h"
34
#include "vec/columns/column_nullable.h"
35
#include "vec/columns/column_struct.h"
36
#include "vec/columns/predicate_column.h"
37
#include "vec/core/types.h"
38
#include "vec/data_types/data_type.h"
39
#include "vec/data_types/data_type_factory.hpp"
40
41
namespace doris {
42
43
10.4M
Schema::Schema(const Schema& other) {
44
10.4M
    _copy_from(other);
45
10.4M
}
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
10.4M
void Schema::_copy_from(const Schema& other) {
55
10.4M
    _col_ids = other._col_ids;
56
10.4M
    _col_offsets = other._col_offsets;
57
58
10.4M
    _num_key_columns = other._num_key_columns;
59
10.4M
    _schema_size = other._schema_size;
60
61
    // Deep copy _cols
62
    // TODO(lingbin): really need clone?
63
10.4M
    _cols.resize(other._cols.size(), nullptr);
64
17.0M
    for (auto cid : _col_ids) {
65
17.0M
        _cols[cid] = other._cols[cid]->clone();
66
17.0M
    }
67
10.4M
}
68
69
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
70
15.2M
                   size_t num_key_columns) {
71
15.2M
    _col_ids = col_ids;
72
15.2M
    _num_key_columns = num_key_columns;
73
74
15.2M
    _cols.resize(cols.size(), nullptr);
75
15.2M
    _col_offsets.resize(_cols.size(), -1);
76
77
15.2M
    size_t offset = 0;
78
15.2M
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
79
231M
    for (int cid = 0; cid < cols.size(); ++cid) {
80
215M
        if (col_id_set.find(cid) == col_id_set.end()) {
81
124M
            continue;
82
124M
        }
83
91.4M
        _cols[cid] = FieldFactory::create(*cols[cid]);
84
85
91.4M
        _col_offsets[cid] = offset;
86
        // Plus 1 byte for null byte
87
91.4M
        offset += _cols[cid]->size() + 1;
88
91.4M
    }
89
90
15.2M
    _schema_size = offset;
91
15.2M
}
92
93
void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids,
94
3.29M
                   size_t num_key_columns) {
95
3.29M
    _col_ids = col_ids;
96
3.29M
    _num_key_columns = num_key_columns;
97
98
3.29M
    _cols.resize(cols.size(), nullptr);
99
3.29M
    _col_offsets.resize(_cols.size(), -1);
100
101
3.29M
    size_t offset = 0;
102
3.29M
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
103
15.1M
    for (int cid = 0; cid < cols.size(); ++cid) {
104
11.8M
        if (col_id_set.find(cid) == col_id_set.end()) {
105
0
            continue;
106
0
        }
107
        // TODO(lingbin): is it necessary to clone Field? each SegmentIterator will
108
        // use this func, can we avoid clone?
109
11.8M
        _cols[cid] = cols[cid]->clone();
110
111
11.8M
        _col_offsets[cid] = offset;
112
        // Plus 1 byte for null byte
113
11.8M
        offset += _cols[cid]->size() + 1;
114
11.8M
    }
115
116
3.29M
    _schema_size = offset;
117
3.29M
}
118
119
28.9M
Schema::~Schema() {
120
357M
    for (auto col : _cols) {
121
357M
        delete col;
122
357M
    }
123
28.9M
}
124
125
144M
vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) {
126
144M
    return vectorized::DataTypeFactory::instance().create_data_type(field);
127
144M
}
128
129
12.1M
vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) {
130
12.1M
    return get_data_type_ptr(field)->create_column();
131
12.1M
}
132
133
vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type,
134
                                                                 bool is_nullable,
135
3.04M
                                                                 const ReaderType reader_type) {
136
3.04M
    vectorized::IColumn::MutablePtr ptr = nullptr;
137
3.04M
    switch (type) {
138
348
    case FieldType::OLAP_FIELD_TYPE_BOOL:
139
348
        ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create();
140
348
        break;
141
2.95M
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
142
2.95M
        ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create();
143
2.95M
        break;
144
1.18k
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
145
1.18k
        ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create();
146
1.18k
        break;
147
33.3k
    case FieldType::OLAP_FIELD_TYPE_INT:
148
33.3k
        ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create();
149
33.3k
        break;
150
0
    case FieldType::OLAP_FIELD_TYPE_FLOAT:
151
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_FLOAT>::create();
152
0
        break;
153
0
    case FieldType::OLAP_FIELD_TYPE_DOUBLE:
154
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DOUBLE>::create();
155
0
        break;
156
7.60k
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
157
7.60k
        ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create();
158
7.60k
        break;
159
930
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
160
930
        ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create();
161
930
        break;
162
328
    case FieldType::OLAP_FIELD_TYPE_DATE:
163
328
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create();
164
328
        break;
165
8.48k
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
166
8.48k
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create();
167
8.48k
        break;
168
4.91k
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
169
4.91k
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create();
170
4.91k
        break;
171
540
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
172
540
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create();
173
540
        break;
174
2.83k
    case FieldType::OLAP_FIELD_TYPE_CHAR:
175
2.83k
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
176
2.82k
            ptr = doris::vectorized::ColumnDictI32::create(type);
177
2.82k
        } else {
178
12
            ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create();
179
12
        }
180
2.83k
        break;
181
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
182
25.5k
    case FieldType::OLAP_FIELD_TYPE_STRING:
183
25.7k
    case FieldType::OLAP_FIELD_TYPE_JSONB:
184
25.7k
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
185
25.1k
            ptr = doris::vectorized::ColumnDictI32::create(type);
186
25.1k
        } else {
187
608
            ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create();
188
608
        }
189
25.7k
        break;
190
46
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
191
46
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create();
192
46
        break;
193
214
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
194
214
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create();
195
214
        break;
196
410
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
197
410
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create();
198
410
        break;
199
1.10k
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
200
1.10k
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create();
201
1.10k
        break;
202
376
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
203
376
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create();
204
376
        break;
205
250
    case FieldType::OLAP_FIELD_TYPE_IPV4:
206
250
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create();
207
250
        break;
208
244
    case FieldType::OLAP_FIELD_TYPE_IPV6:
209
244
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create();
210
244
        break;
211
0
    default:
212
0
        throw Exception(
213
0
                ErrorCode::SCHEMA_SCHEMA_FIELD_INVALID,
214
0
                fmt::format("Unexpected type when choosing predicate column, type={}", int(type)));
215
3.04M
    }
216
217
3.05M
    if (is_nullable) {
218
42.3k
        return doris::vectorized::ColumnNullable::create(std::move(ptr),
219
42.3k
                                                         doris::vectorized::ColumnUInt8::create());
220
42.3k
    }
221
3.01M
    return ptr;
222
3.05M
}
223
224
} // namespace doris