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 | | #pragma once |
19 | | |
20 | | #include <cstddef> |
21 | | #include <sstream> |
22 | | #include <string> |
23 | | |
24 | | #include "core/arena.h" |
25 | | #include "core/value/map_value.h" |
26 | | #include "runtime/collection_value.h" |
27 | | #include "storage/key_coder.h" |
28 | | #include "storage/olap_common.h" |
29 | | #include "storage/olap_define.h" |
30 | | #include "storage/tablet/tablet_schema.h" |
31 | | #include "storage/types.h" |
32 | | #include "storage/utils.h" |
33 | | #include "util/hash_util.hpp" |
34 | | #include "util/json/path_in_data.h" |
35 | | #include "util/slice.h" |
36 | | |
37 | | namespace doris { |
38 | | // A Field is used to represent a column in memory format. |
39 | | // User can use this class to access or deal with column data in memory. |
40 | | class StorageField { |
41 | | public: |
42 | | StorageField(const TabletColumn& column) |
43 | 109M | : _type(column.type()), |
44 | 109M | _desc(column), |
45 | 109M | _length(column.length()), |
46 | 109M | _key_coder(get_key_coder(column.type())), |
47 | 109M | _name(column.name()), |
48 | 109M | _index_size(column.index_length()), |
49 | 109M | _is_nullable(column.is_nullable()), |
50 | 109M | _unique_id(column.unique_id()), |
51 | 109M | _parent_unique_id(column.parent_unique_id()), |
52 | 109M | _is_extracted_column(column.is_extracted_column()), |
53 | 109M | _path(column.path_info_ptr()) {} |
54 | | |
55 | 109M | virtual ~StorageField() = default; |
56 | | |
57 | 6.11M | size_t size() const { return field_type_size(_type); } |
58 | 633 | size_t length() const { return _length; } |
59 | 0 | size_t field_size() const { return size() + 1; } |
60 | 0 | size_t index_size() const { return _index_size; } |
61 | 56.6M | int32_t unique_id() const { return _unique_id; } |
62 | 59.2k | int32_t parent_unique_id() const { return _parent_unique_id; } |
63 | 54.9M | bool is_extracted_column() const { return _is_extracted_column; } |
64 | 84.6M | const std::string& name() const { return _name; } |
65 | 0 | const PathInDataPtr& path() const { return _path; } |
66 | | |
67 | 1.82M | virtual StorageField* clone() const { |
68 | 1.82M | auto* local = new StorageField(_desc); |
69 | 1.82M | this->clone(local); |
70 | 1.82M | return local; |
71 | 1.82M | } |
72 | | |
73 | 48.3M | FieldType type() const { return _type; } |
74 | 62.5M | bool is_nullable() const { return _is_nullable; } |
75 | | |
76 | | // similar to `full_encode_ascending`, but only encode part (the first `index_size` bytes) of the value. |
77 | | // only applicable to string type |
78 | 11.9k | void encode_ascending(const void* value, std::string* buf) const { |
79 | 11.9k | _key_coder->encode_ascending(value, _index_size, buf); |
80 | 11.9k | } |
81 | | |
82 | | // encode the provided `value` into `buf`. |
83 | | // `schema_length` is the CHAR(N) declared length so KeyCoder for CHAR can |
84 | | // pad the (possibly unpadded) source slice up to the canonical fixed-width |
85 | | // encoding form. Ignored for non-CHAR types. |
86 | | void full_encode_ascending(const void* value, std::string* buf, |
87 | 11.5M | size_t schema_length = 0) const { |
88 | 11.5M | _key_coder->full_encode_ascending(value, buf, schema_length); |
89 | 11.5M | } |
90 | | |
91 | 1.11M | const KeyCoder* key_coder() const { return _key_coder; } |
92 | 716k | void add_sub_field(std::unique_ptr<StorageField> sub_field) { |
93 | 716k | _sub_fields.emplace_back(std::move(sub_field)); |
94 | 716k | } |
95 | 13 | StorageField* get_sub_field(size_t i) const { return _sub_fields[i].get(); } |
96 | 2 | size_t get_sub_field_count() const { return _sub_fields.size(); } |
97 | | |
98 | 6.62M | void set_precision(int32_t precision) { _precision = precision; } |
99 | 6.62M | void set_scale(int32_t scale) { _scale = scale; } |
100 | 0 | int32_t get_precision() const { return _precision; } |
101 | 23.7M | int32_t get_scale() const { return _scale; } |
102 | 100M | const TabletColumn& get_desc() const { return _desc; } |
103 | | |
104 | 27.5M | int32_t get_unique_id() const { |
105 | 27.5M | return is_extracted_column() ? parent_unique_id() : unique_id(); |
106 | 27.5M | } |
107 | | |
108 | | protected: |
109 | | FieldType _type; |
110 | | TabletColumn _desc; |
111 | | // unit : byte |
112 | | // except for strings, other types have fixed lengths |
113 | | // Note that, the struct type itself has fixed length, but due to |
114 | | // its number of subfields is a variable, so the actual length of |
115 | | // a struct field is not fixed. |
116 | | size_t _length; |
117 | | |
118 | 7.47M | void clone(StorageField* other) const { |
119 | 7.47M | other->_type = this->_type; |
120 | 7.47M | other->_key_coder = this->_key_coder; |
121 | 7.47M | other->_name = this->_name; |
122 | 7.47M | other->_index_size = this->_index_size; |
123 | 7.47M | other->_is_nullable = this->_is_nullable; |
124 | 7.47M | other->_sub_fields.clear(); |
125 | 7.47M | other->_precision = this->_precision; |
126 | 7.47M | other->_scale = this->_scale; |
127 | 7.47M | other->_unique_id = this->_unique_id; |
128 | 7.47M | other->_parent_unique_id = this->_parent_unique_id; |
129 | 7.47M | other->_is_extracted_column = this->_is_extracted_column; |
130 | 7.47M | for (const auto& f : _sub_fields) { |
131 | 0 | StorageField* item = f->clone(); |
132 | 0 | other->add_sub_field(std::unique_ptr<StorageField>(item)); |
133 | 0 | } |
134 | 7.47M | } |
135 | | |
136 | | private: |
137 | | // maximum length of Field, unit : bytes |
138 | | // usually equal to length, except for variable-length strings |
139 | | const KeyCoder* _key_coder; |
140 | | std::string _name; |
141 | | size_t _index_size; |
142 | | bool _is_nullable; |
143 | | std::vector<std::unique_ptr<StorageField>> _sub_fields; |
144 | | int32_t _precision; |
145 | | int32_t _scale; |
146 | | int32_t _unique_id; |
147 | | int32_t _parent_unique_id; |
148 | | bool _is_extracted_column = false; |
149 | | PathInDataPtr _path; |
150 | | }; |
151 | | |
152 | | class MapField : public StorageField { |
153 | | public: |
154 | 115k | MapField(const TabletColumn& column) : StorageField(column) {} |
155 | | }; |
156 | | |
157 | | class StructField : public StorageField { |
158 | | public: |
159 | 43.6k | StructField(const TabletColumn& column) : StorageField(column) {} |
160 | | }; |
161 | | |
162 | | class ArrayField : public StorageField { |
163 | | public: |
164 | 362k | ArrayField(const TabletColumn& column) : StorageField(column) {} |
165 | | }; |
166 | | |
167 | | class CharField : public StorageField { |
168 | | public: |
169 | 232k | CharField(const TabletColumn& column) : StorageField(column) {} |
170 | | |
171 | 1.09k | CharField* clone() const override { |
172 | 1.09k | auto* local = new CharField(_desc); |
173 | 1.09k | StorageField::clone(local); |
174 | 1.09k | return local; |
175 | 1.09k | } |
176 | | }; |
177 | | |
178 | | class VarcharField : public StorageField { |
179 | | public: |
180 | 10.9M | VarcharField(const TabletColumn& column) : StorageField(column) {} |
181 | | |
182 | 0 | VarcharField* clone() const override { |
183 | 0 | auto* local = new VarcharField(_desc); |
184 | 0 | StorageField::clone(local); |
185 | 0 | return local; |
186 | 0 | } |
187 | | }; |
188 | | class StringField : public StorageField { |
189 | | public: |
190 | 57.4M | StringField(const TabletColumn& column) : StorageField(column) {} |
191 | | |
192 | 5.65M | StringField* clone() const override { |
193 | 5.65M | auto* local = new StringField(_desc); |
194 | 5.65M | StorageField::clone(local); |
195 | 5.65M | return local; |
196 | 5.65M | } |
197 | | }; |
198 | | |
199 | | class BitmapAggField : public StorageField { |
200 | | public: |
201 | 25.8k | BitmapAggField(const TabletColumn& column) : StorageField(column) {} |
202 | | |
203 | 0 | BitmapAggField* clone() const override { |
204 | 0 | auto* local = new BitmapAggField(_desc); |
205 | 0 | StorageField::clone(local); |
206 | 0 | return local; |
207 | 0 | } |
208 | | }; |
209 | | |
210 | | class QuantileStateAggField : public StorageField { |
211 | | public: |
212 | 10.7k | QuantileStateAggField(const TabletColumn& column) : StorageField(column) {} |
213 | | |
214 | 0 | QuantileStateAggField* clone() const override { |
215 | 0 | auto* local = new QuantileStateAggField(_desc); |
216 | 0 | StorageField::clone(local); |
217 | 0 | return local; |
218 | 0 | } |
219 | | }; |
220 | | |
221 | | class AggStateField : public StorageField { |
222 | | public: |
223 | 6.97k | AggStateField(const TabletColumn& column) : StorageField(column) {} |
224 | | |
225 | 0 | AggStateField* clone() const override { |
226 | 0 | auto* local = new AggStateField(_desc); |
227 | 0 | StorageField::clone(local); |
228 | 0 | return local; |
229 | 0 | } |
230 | | }; |
231 | | |
232 | | class HllAggField : public StorageField { |
233 | | public: |
234 | 16.0k | HllAggField(const TabletColumn& column) : StorageField(column) {} |
235 | | |
236 | 0 | HllAggField* clone() const override { |
237 | 0 | auto* local = new HllAggField(_desc); |
238 | 0 | StorageField::clone(local); |
239 | 0 | return local; |
240 | 0 | } |
241 | | }; |
242 | | |
243 | | class StorageFieldFactory { |
244 | | public: |
245 | 102M | static StorageField* create(const TabletColumn& column) { |
246 | | // for key column |
247 | 102M | if (column.is_key()) { |
248 | 49.2M | switch (column.type()) { |
249 | 39.5k | case FieldType::OLAP_FIELD_TYPE_CHAR: |
250 | 39.5k | return new CharField(column); |
251 | 46.1M | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
252 | 46.1M | case FieldType::OLAP_FIELD_TYPE_STRING: |
253 | 46.1M | return new StringField(column); |
254 | 0 | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
255 | 0 | auto* local = new StructField(column); |
256 | 0 | for (uint32_t i = 0; i < column.get_subtype_count(); i++) { |
257 | 0 | std::unique_ptr<StorageField> sub_field( |
258 | 0 | StorageFieldFactory::create(column.get_sub_column(i))); |
259 | 0 | local->add_sub_field(std::move(sub_field)); |
260 | 0 | } |
261 | 0 | return local; |
262 | 46.1M | } |
263 | 0 | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
264 | 0 | std::unique_ptr<StorageField> item_field( |
265 | 0 | StorageFieldFactory::create(column.get_sub_column(0))); |
266 | 0 | auto* local = new ArrayField(column); |
267 | 0 | local->add_sub_field(std::move(item_field)); |
268 | 0 | return local; |
269 | 46.1M | } |
270 | 0 | case FieldType::OLAP_FIELD_TYPE_MAP: { |
271 | 0 | std::unique_ptr<StorageField> key_field( |
272 | 0 | StorageFieldFactory::create(column.get_sub_column(0))); |
273 | 0 | std::unique_ptr<StorageField> val_field( |
274 | 0 | StorageFieldFactory::create(column.get_sub_column(1))); |
275 | 0 | auto* local = new MapField(column); |
276 | 0 | local->add_sub_field(std::move(key_field)); |
277 | 0 | local->add_sub_field(std::move(val_field)); |
278 | 0 | return local; |
279 | 46.1M | } |
280 | 3.11k | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
281 | 3.11k | [[fallthrough]]; |
282 | 37.6k | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
283 | 37.6k | [[fallthrough]]; |
284 | 45.3k | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
285 | 45.3k | [[fallthrough]]; |
286 | 90.2k | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
287 | 90.2k | [[fallthrough]]; |
288 | 98.5k | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
289 | 98.5k | [[fallthrough]]; |
290 | 190k | case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ: |
291 | 190k | [[fallthrough]]; |
292 | 304k | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: { |
293 | 304k | StorageField* field = new StorageField(column); |
294 | 304k | field->set_precision(column.precision()); |
295 | 304k | field->set_scale(column.frac()); |
296 | 304k | return field; |
297 | 190k | } |
298 | 2.62M | default: |
299 | 2.62M | return new StorageField(column); |
300 | 49.2M | } |
301 | 49.2M | } |
302 | | |
303 | | // for value column |
304 | 53.2M | switch (column.aggregation()) { |
305 | 52.2M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE: |
306 | 52.4M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM: |
307 | 52.4M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN: |
308 | 52.5M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX: |
309 | 52.9M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE: |
310 | 53.0M | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL: |
311 | 53.0M | switch (column.type()) { |
312 | 192k | case FieldType::OLAP_FIELD_TYPE_CHAR: |
313 | 192k | return new CharField(column); |
314 | 10.9M | case FieldType::OLAP_FIELD_TYPE_VARCHAR: |
315 | 10.9M | return new VarcharField(column); |
316 | 5.89M | case FieldType::OLAP_FIELD_TYPE_STRING: |
317 | 5.89M | return new StringField(column); |
318 | 43.7k | case FieldType::OLAP_FIELD_TYPE_STRUCT: { |
319 | 43.7k | auto* local = new StructField(column); |
320 | 167k | for (uint32_t i = 0; i < column.get_subtype_count(); i++) { |
321 | 123k | std::unique_ptr<StorageField> sub_field( |
322 | 123k | StorageFieldFactory::create(column.get_sub_column(i))); |
323 | 123k | local->add_sub_field(std::move(sub_field)); |
324 | 123k | } |
325 | 43.7k | return local; |
326 | 0 | } |
327 | 362k | case FieldType::OLAP_FIELD_TYPE_ARRAY: { |
328 | 362k | std::unique_ptr<StorageField> item_field( |
329 | 362k | StorageFieldFactory::create(column.get_sub_column(0))); |
330 | 362k | auto* local = new ArrayField(column); |
331 | 362k | local->add_sub_field(std::move(item_field)); |
332 | 362k | return local; |
333 | 0 | } |
334 | 115k | case FieldType::OLAP_FIELD_TYPE_MAP: { |
335 | 115k | DCHECK(column.get_subtype_count() == 2); |
336 | 115k | auto* local = new MapField(column); |
337 | 115k | std::unique_ptr<StorageField> key_field( |
338 | 115k | StorageFieldFactory::create(column.get_sub_column(0))); |
339 | 115k | std::unique_ptr<StorageField> value_field( |
340 | 115k | StorageFieldFactory::create(column.get_sub_column(1))); |
341 | 115k | local->add_sub_field(std::move(key_field)); |
342 | 115k | local->add_sub_field(std::move(value_field)); |
343 | 115k | return local; |
344 | 0 | } |
345 | 5.55k | case FieldType::OLAP_FIELD_TYPE_DECIMAL: |
346 | 5.55k | [[fallthrough]]; |
347 | 82.1k | case FieldType::OLAP_FIELD_TYPE_DECIMAL32: |
348 | 82.1k | [[fallthrough]]; |
349 | 345k | case FieldType::OLAP_FIELD_TYPE_DECIMAL64: |
350 | 345k | [[fallthrough]]; |
351 | 530k | case FieldType::OLAP_FIELD_TYPE_DECIMAL128I: |
352 | 530k | [[fallthrough]]; |
353 | 540k | case FieldType::OLAP_FIELD_TYPE_DECIMAL256: |
354 | 540k | [[fallthrough]]; |
355 | 685k | case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ: |
356 | 685k | [[fallthrough]]; |
357 | 6.32M | case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: { |
358 | 6.32M | StorageField* field = new StorageField(column); |
359 | 6.32M | field->set_precision(column.precision()); |
360 | 6.32M | field->set_scale(column.frac()); |
361 | 6.32M | return field; |
362 | 685k | } |
363 | 29.2M | default: |
364 | 29.2M | return new StorageField(column); |
365 | 53.0M | } |
366 | 16.1k | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION: |
367 | 16.1k | return new HllAggField(column); |
368 | 25.8k | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION: |
369 | 25.8k | return new BitmapAggField(column); |
370 | 10.7k | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION: |
371 | 10.7k | return new QuantileStateAggField(column); |
372 | 7.00k | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC: |
373 | 7.00k | return new AggStateField(column); |
374 | 0 | case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN: |
375 | 0 | CHECK(false) << ", value column no agg type"; |
376 | 0 | return nullptr; |
377 | 53.2M | } |
378 | 0 | return nullptr; |
379 | 53.2M | } |
380 | | |
381 | | static StorageField* create_by_type(const FieldType& type) { |
382 | | TabletColumn column(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE, type); |
383 | | return create(column); |
384 | | } |
385 | | }; |
386 | | } // namespace doris |