Coverage Report

Created: 2024-11-21 17:33

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