/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 | 10.1M | Schema::Schema(const Schema& other) { |
45 | 10.1M | _copy_from(other); |
46 | 10.1M | } |
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 | 10.2M | void Schema::_copy_from(const Schema& other) { |
56 | 10.2M | _col_ids = other._col_ids; |
57 | 10.2M | _col_offsets = other._col_offsets; |
58 | | |
59 | 10.2M | _num_key_columns = other._num_key_columns; |
60 | 10.2M | _schema_size = other._schema_size; |
61 | | |
62 | | // Deep copy _cols |
63 | | // TODO(lingbin): really need clone? |
64 | 10.2M | _cols.resize(other._cols.size(), nullptr); |
65 | 15.1M | for (auto cid : _col_ids) { |
66 | 15.1M | _cols[cid] = other._cols[cid]->clone(); |
67 | 15.1M | } |
68 | 10.2M | } |
69 | | |
70 | | void Schema::_init(const std::vector<TabletColumnPtr>& cols, const std::vector<ColumnId>& col_ids, |
71 | 16.4M | size_t num_key_columns) { |
72 | 16.4M | _col_ids = col_ids; |
73 | 16.4M | _num_key_columns = num_key_columns; |
74 | | |
75 | 16.4M | _cols.resize(cols.size(), nullptr); |
76 | 16.4M | _col_offsets.resize(_cols.size(), -1); |
77 | | |
78 | 16.4M | size_t offset = 0; |
79 | 16.4M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
80 | 250M | for (int cid = 0; cid < cols.size(); ++cid) { |
81 | 233M | if (col_id_set.find(cid) == col_id_set.end()) { |
82 | 140M | continue; |
83 | 140M | } |
84 | 93.2M | _cols[cid] = FieldFactory::create(*cols[cid]); |
85 | | |
86 | 93.2M | _col_offsets[cid] = offset; |
87 | | // Plus 1 byte for null byte |
88 | 93.2M | offset += _cols[cid]->size() + 1; |
89 | 93.2M | } |
90 | | |
91 | 16.4M | _schema_size = offset; |
92 | 16.4M | } |
93 | | |
94 | | void Schema::_init(const std::vector<const Field*>& cols, const std::vector<ColumnId>& col_ids, |
95 | 2.78M | size_t num_key_columns) { |
96 | 2.78M | _col_ids = col_ids; |
97 | 2.78M | _num_key_columns = num_key_columns; |
98 | | |
99 | 2.78M | _cols.resize(cols.size(), nullptr); |
100 | 2.78M | _col_offsets.resize(_cols.size(), -1); |
101 | | |
102 | 2.78M | size_t offset = 0; |
103 | 2.78M | std::unordered_set<uint32_t> col_id_set(col_ids.begin(), col_ids.end()); |
104 | 12.6M | for (int cid = 0; cid < cols.size(); ++cid) { |
105 | 9.89M | 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 | 9.89M | _cols[cid] = cols[cid]->clone(); |
111 | | |
112 | 9.89M | _col_offsets[cid] = offset; |
113 | | // Plus 1 byte for null byte |
114 | 9.89M | offset += _cols[cid]->size() + 1; |
115 | 9.89M | } |
116 | | |
117 | 2.78M | _schema_size = offset; |
118 | 2.78M | } |
119 | | |
120 | 29.4M | Schema::~Schema() { |
121 | 360M | for (auto col : _cols) { |
122 | 360M | delete col; |
123 | 360M | } |
124 | 29.4M | } |
125 | | |
126 | 105M | vectorized::DataTypePtr Schema::get_data_type_ptr(const Field& field) { |
127 | 105M | return vectorized::DataTypeFactory::instance().create_data_type(field); |
128 | 105M | } |
129 | | |
130 | 10.2M | vectorized::IColumn::MutablePtr Schema::get_column_by_field(const Field& field) { |
131 | 10.2M | return get_data_type_ptr(field)->create_column(); |
132 | 10.2M | } |
133 | | |
134 | | vectorized::IColumn::MutablePtr Schema::get_predicate_column_ptr(const FieldType& type, |
135 | | bool is_nullable, |
136 | 2.54M | const ReaderType reader_type) { |
137 | 2.54M | vectorized::IColumn::MutablePtr ptr = nullptr; |
138 | 2.54M | switch (type) { |
139 | 306 | case FieldType::OLAP_FIELD_TYPE_BOOL: |
140 | 306 | ptr = doris::vectorized::PredicateColumnType<TYPE_BOOLEAN>::create(); |
141 | 306 | break; |
142 | 2.46M | case FieldType::OLAP_FIELD_TYPE_TINYINT: |
143 | 2.46M | ptr = doris::vectorized::PredicateColumnType<TYPE_TINYINT>::create(); |
144 | 2.46M | break; |
145 | 1.76k | case FieldType::OLAP_FIELD_TYPE_SMALLINT: |
146 | 1.76k | ptr = doris::vectorized::PredicateColumnType<TYPE_SMALLINT>::create(); |
147 | 1.76k | break; |
148 | 31.2k | case FieldType::OLAP_FIELD_TYPE_INT: |
149 | 31.2k | ptr = doris::vectorized::PredicateColumnType<TYPE_INT>::create(); |
150 | 31.2k | 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 | 7.86k | case FieldType::OLAP_FIELD_TYPE_BIGINT: |
158 | 7.86k | ptr = doris::vectorized::PredicateColumnType<TYPE_BIGINT>::create(); |
159 | 7.86k | break; |
160 | 1.03k | case FieldType::OLAP_FIELD_TYPE_LARGEINT: |
161 | 1.03k | ptr = doris::vectorized::PredicateColumnType<TYPE_LARGEINT>::create(); |
162 | 1.03k | break; |
163 | 728 | case FieldType::OLAP_FIELD_TYPE_DATE: |
164 | 728 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATE>::create(); |
165 | 728 | break; |
166 | 8.41k | case FieldType::OLAP_FIELD_TYPE_DATEV2: |
167 | 8.41k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATEV2>::create(); |
168 | 8.41k | break; |
169 | 3.64k | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: |
170 | 3.64k | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIMEV2>::create(); |
171 | 3.64k | break; |
172 | 656 | case FieldType::OLAP_FIELD_TYPE_DATETIME: |
173 | 656 | ptr = doris::vectorized::PredicateColumnType<TYPE_DATETIME>::create(); |
174 | 656 | break; |
175 | 2.32k | case FieldType::OLAP_FIELD_TYPE_CHAR: |
176 | 2.32k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
177 | 2.31k | ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type); |
178 | 2.31k | } else { |
179 | 6 | ptr = doris::vectorized::PredicateColumnType<TYPE_CHAR>::create(); |
180 | 6 | } |
181 | 2.32k | break; |
182 | 0 | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
183 | 21.2k | case FieldType::OLAP_FIELD_TYPE_STRING: |
184 | 21.3k | case FieldType::OLAP_FIELD_TYPE_JSONB: |
185 | 21.3k | if (config::enable_low_cardinality_optimize && reader_type == ReaderType::READER_QUERY) { |
186 | 19.4k | ptr = doris::vectorized::ColumnDictionary<doris::vectorized::Int32>::create(type); |
187 | 19.4k | } else { |
188 | 1.89k | ptr = doris::vectorized::PredicateColumnType<TYPE_STRING>::create(); |
189 | 1.89k | } |
190 | 21.3k | break; |
191 | 10 | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
192 | 10 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMALV2>::create(); |
193 | 10 | break; |
194 | 426 | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
195 | 426 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL32>::create(); |
196 | 426 | break; |
197 | 376 | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
198 | 376 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL64>::create(); |
199 | 376 | break; |
200 | 854 | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
201 | 854 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL128I>::create(); |
202 | 854 | break; |
203 | 394 | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
204 | 394 | ptr = doris::vectorized::PredicateColumnType<TYPE_DECIMAL256>::create(); |
205 | 394 | break; |
206 | 288 | case FieldType::OLAP_FIELD_TYPE_IPV4: |
207 | 288 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV4>::create(); |
208 | 288 | break; |
209 | 262 | case FieldType::OLAP_FIELD_TYPE_IPV6: |
210 | 262 | ptr = doris::vectorized::PredicateColumnType<TYPE_IPV6>::create(); |
211 | 262 | 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 | 2.54M | } |
217 | | |
218 | 2.54M | if (is_nullable) { |
219 | 46.0k | return doris::vectorized::ColumnNullable::create(std::move(ptr), |
220 | 46.0k | doris::vectorized::ColumnUInt8::create()); |
221 | 46.0k | } |
222 | 2.50M | return ptr; |
223 | 2.54M | } |
224 | | |
225 | | } // namespace doris |