Coverage Report

Created: 2025-06-13 06:59

/root/doris/be/src/olap/schema.cpp
Line
Count
Source (jump to first uncovered line)
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
173
Schema::Schema(const Schema& other) {
44
173
    _copy_from(other);
45
173
}
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
173
void Schema::_copy_from(const Schema& other) {
55
173
    _col_ids = other._col_ids;
56
173
    _col_offsets = other._col_offsets;
57
58
173
    _num_key_columns = other._num_key_columns;
59
173
    _schema_size = other._schema_size;
60
61
    // Deep copy _cols
62
    // TODO(lingbin): really need clone?
63
173
    _cols.resize(other._cols.size(), nullptr);
64
666
    for (auto cid : _col_ids) {
65
666
        _cols[cid] = other._cols[cid]->clone();
66
666
    }
67
173
}
68
69
void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids,
70
7.32k
                   size_t num_key_columns) {
71
7.32k
    _col_ids = col_ids;
72
7.32k
    _num_key_columns = num_key_columns;
73
74
7.32k
    _cols.resize(cols.size(), nullptr);
75
7.32k
    _col_offsets.resize(_cols.size(), -1);
76
77
7.32k
    size_t offset = 0;
78
7.32k
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
79
65.3k
    for (int cid = 0; cid < cols.size(); ++cid) {
80
58.0k
        if (col_id_set.find(cid) == col_id_set.end()) {
81
33.6k
            continue;
82
33.6k
        }
83
24.4k
        _cols[cid] = FieldFactory::create(*cols[cid]);
84
85
24.4k
        _col_offsets[cid] = offset;
86
        // Plus 1 byte for null byte
87
24.4k
        offset += _cols[cid]->size() + 1;
88
24.4k
    }
89
90
7.32k
    _schema_size = offset;
91
7.32k
}
92
93
void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids,
94
262
                   size_t num_key_columns) {
95
262
    _col_ids = col_ids;
96
262
    _num_key_columns = num_key_columns;
97
98
262
    _cols.resize(cols.size(), nullptr);
99
262
    _col_offsets.resize(_cols.size(), -1);
100
101
262
    size_t offset = 0;
102
262
    std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end());
103
1.30k
    for (int cid = 0; cid < cols.size(); ++cid) {
104
1.04k
        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
1.04k
        _cols[cid] = cols[cid]->clone();
110
111
1.04k
        _col_offsets[cid] = offset;
112
        // Plus 1 byte for null byte
113
1.04k
        offset += _cols[cid]->size() + 1;
114
1.04k
    }
115
116
262
    _schema_size = offset;
117
262
}
118
119
7.76k
Schema::~Schema() {
120
61.9k
    for (auto col : _cols) {
121
61.9k
        delete col;
122
61.9k
    }
123
7.76k
}
124
125
128k
vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) {
126
128k
    return vectorized::DataTypeFactory::instance().create_data_type(field);
127
128k
}
128
129
1.05k
vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) {
130
1.05k
    return get_data_type_ptr(field)->create_column();
131
1.05k
}
132
133
vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type,
134
                                                                 bool is_nullable,
135
871
                                                                 const ReaderType reader_type) {
136
871
    vectorized::IColumn::MutablePtr ptr = nullptr;
137
871
    switch (type) {
138
0
    case FieldType::OLAP_FIELD_TYPE_BOOL:
139
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create();
140
0
        break;
141
334
    case FieldType::OLAP_FIELD_TYPE_TINYINT:
142
334
        ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create();
143
334
        break;
144
0
    case FieldType::OLAP_FIELD_TYPE_SMALLINT:
145
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create();
146
0
        break;
147
327
    case FieldType::OLAP_FIELD_TYPE_INT:
148
327
        ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create();
149
327
        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
0
    case FieldType::OLAP_FIELD_TYPE_BIGINT:
157
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create();
158
0
        break;
159
0
    case FieldType::OLAP_FIELD_TYPE_LARGEINT:
160
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create();
161
0
        break;
162
0
    case FieldType::OLAP_FIELD_TYPE_DATE:
163
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create();
164
0
        break;
165
0
    case FieldType::OLAP_FIELD_TYPE_DATEV2:
166
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create();
167
0
        break;
168
0
    case FieldType::OLAP_FIELD_TYPE_DATETIMEV2:
169
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create();
170
0
        break;
171
0
    case FieldType::OLAP_FIELD_TYPE_DATETIME:
172
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create();
173
0
        break;
174
0
    case FieldType::OLAP_FIELD_TYPE_CHAR:
175
0
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
176
0
            ptr = doris::vectorized::ColumnDictI32::create(type);
177
0
        } else {
178
0
            ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create();
179
0
        }
180
0
        break;
181
0
    case FieldType::OLAP_FIELD_TYPE_VARCHAR:
182
210
    case FieldType::OLAP_FIELD_TYPE_STRING:
183
210
    case FieldType::OLAP_FIELD_TYPE_JSONB:
184
210
        if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) {
185
70
            ptr = doris::vectorized::ColumnDictI32::create(type);
186
140
        } else {
187
140
            ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create();
188
140
        }
189
210
        break;
190
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL:
191
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create();
192
0
        break;
193
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
194
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create();
195
0
        break;
196
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
197
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create();
198
0
        break;
199
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
200
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create();
201
0
        break;
202
0
    case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
203
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create();
204
0
        break;
205
0
    case FieldType::OLAP_FIELD_TYPE_IPV4:
206
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create();
207
0
        break;
208
0
    case FieldType::OLAP_FIELD_TYPE_IPV6:
209
0
        ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create();
210
0
        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
871
    }
216
217
871
    if (is_nullable) {
218
210
        return doris::vectorized::ColumnNullable::create(std::move(ptr),
219
210
                                                         doris::vectorized::ColumnUInt8::create());
220
210
    }
221
661
    return ptr;
222
871
}
223
224
} // namespace doris