Coverage Report

Created: 2026-03-27 10:51

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/field.h
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
#include "common/compile_check_begin.h"
39
// A Field is used to represent a column in memory format.
40
// User can use this class to access or deal with column data in memory.
41
class StorageField {
42
public:
43
    StorageField(const TabletColumn& column)
44
38.6k
            : _type_info(get_type_info(&column)),
45
38.6k
              _desc(column),
46
38.6k
              _length(column.length()),
47
38.6k
              _key_coder(get_key_coder(column.type())),
48
38.6k
              _name(column.name()),
49
38.6k
              _index_size(column.index_length()),
50
38.6k
              _is_nullable(column.is_nullable()),
51
38.6k
              _unique_id(column.unique_id()),
52
38.6k
              _parent_unique_id(column.parent_unique_id()),
53
38.6k
              _is_extracted_column(column.is_extracted_column()),
54
38.6k
              _path(column.path_info_ptr()) {}
55
56
38.6k
    virtual ~StorageField() = default;
57
58
732k
    size_t size() const { return _type_info->size(); }
59
5
    size_t length() const { return _length; }
60
0
    size_t field_size() const { return size() + 1; }
61
0
    size_t index_size() const { return _index_size; }
62
17.6k
    int32_t unique_id() const { return _unique_id; }
63
0
    int32_t parent_unique_id() const { return _parent_unique_id; }
64
17.6k
    bool is_extracted_column() const { return _is_extracted_column; }
65
65.4k
    const std::string& name() const { return _name; }
66
0
    const PathInDataPtr& path() const { return _path; }
67
68
0
    virtual void set_to_max(char* buf) const { return _type_info->set_to_max(buf); }
69
70
2
    virtual void set_to_min(char* buf) const { return _type_info->set_to_min(buf); }
71
72
12
    virtual StorageField* clone() const {
73
12
        auto* local = new StorageField(_desc);
74
12
        this->clone(local);
75
12
        return local;
76
12
    }
77
78
1.03M
    FieldType type() const { return _type_info->type(); }
79
2.29k
    const TypeInfo* type_info() const { return _type_info.get(); }
80
97.1k
    bool is_nullable() const { return _is_nullable; }
81
82
    // similar to `full_encode_ascending`, but only encode part (the first `index_size` bytes) of the value.
83
    // only applicable to string type
84
50
    void encode_ascending(const void* value, std::string* buf) const {
85
50
        _key_coder->encode_ascending(value, _index_size, buf);
86
50
    }
87
88
    // encode the provided `value` into `buf`.
89
322k
    void full_encode_ascending(const void* value, std::string* buf) const {
90
322k
        _key_coder->full_encode_ascending(value, buf);
91
322k
    }
92
697
    void add_sub_field(std::unique_ptr<StorageField> sub_field) {
93
697
        _sub_fields.emplace_back(std::move(sub_field));
94
697
    }
95
15
    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
27
    void set_precision(int32_t precision) { _precision = precision; }
99
27
    void set_scale(int32_t scale) { _scale = scale; }
100
0
    int32_t get_precision() const { return _precision; }
101
0
    int32_t get_scale() const { return _scale; }
102
112k
    const TabletColumn& get_desc() const { return _desc; }
103
104
12.6k
    int32_t get_unique_id() const {
105
12.6k
        return is_extracted_column() ? parent_unique_id() : unique_id();
106
12.6k
    }
107
108
protected:
109
    TypeInfoPtr _type_info;
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
12
    void clone(StorageField* other) const {
119
12
        other->_type_info = clone_type_info(this->_type_info.get());
120
12
        other->_key_coder = this->_key_coder;
121
12
        other->_name = this->_name;
122
12
        other->_index_size = this->_index_size;
123
12
        other->_is_nullable = this->_is_nullable;
124
12
        other->_sub_fields.clear();
125
12
        other->_precision = this->_precision;
126
12
        other->_scale = this->_scale;
127
12
        other->_unique_id = this->_unique_id;
128
12
        other->_parent_unique_id = this->_parent_unique_id;
129
12
        other->_is_extracted_column = this->_is_extracted_column;
130
12
        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
12
    }
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
328
    MapField(const TabletColumn& column) : StorageField(column) {}
155
};
156
157
class StructField : public StorageField {
158
public:
159
0
    StructField(const TabletColumn& column) : StorageField(column) {}
160
};
161
162
class ArrayField : public StorageField {
163
public:
164
41
    ArrayField(const TabletColumn& column) : StorageField(column) {}
165
};
166
167
class CharField : public StorageField {
168
public:
169
11
    CharField(const TabletColumn& column) : StorageField(column) {}
170
171
0
    CharField* clone() const override {
172
0
        auto* local = new CharField(_desc);
173
0
        StorageField::clone(local);
174
0
        return local;
175
0
    }
176
177
0
    void set_to_max(char* ch) const override {
178
0
        auto slice = reinterpret_cast<Slice*>(ch);
179
0
        slice->size = _length;
180
0
        memset(slice->data, 0xFF, slice->size);
181
0
    }
182
};
183
184
class VarcharField : public StorageField {
185
public:
186
60
    VarcharField(const TabletColumn& column) : StorageField(column) {}
187
188
0
    VarcharField* clone() const override {
189
0
        auto* local = new VarcharField(_desc);
190
0
        StorageField::clone(local);
191
0
        return local;
192
0
    }
193
194
2
    void set_to_max(char* ch) const override {
195
2
        auto slice = reinterpret_cast<Slice*>(ch);
196
2
        slice->size = _length - OLAP_VARCHAR_MAX_BYTES;
197
2
        memset(slice->data, 0xFF, slice->size);
198
2
    }
199
};
200
class StringField : public StorageField {
201
public:
202
9.58k
    StringField(const TabletColumn& column) : StorageField(column) {}
203
204
0
    StringField* clone() const override {
205
0
        auto* local = new StringField(_desc);
206
0
        StorageField::clone(local);
207
0
        return local;
208
0
    }
209
210
0
    void set_to_max(char* ch) const override {
211
0
        auto slice = reinterpret_cast<Slice*>(ch);
212
0
        memset(slice->data, 0xFF, slice->size);
213
0
    }
214
};
215
216
class BitmapAggField : public StorageField {
217
public:
218
0
    BitmapAggField(const TabletColumn& column) : StorageField(column) {}
219
220
0
    BitmapAggField* clone() const override {
221
0
        auto* local = new BitmapAggField(_desc);
222
0
        StorageField::clone(local);
223
0
        return local;
224
0
    }
225
};
226
227
class QuantileStateAggField : public StorageField {
228
public:
229
0
    QuantileStateAggField(const TabletColumn& column) : StorageField(column) {}
230
231
0
    QuantileStateAggField* clone() const override {
232
0
        auto* local = new QuantileStateAggField(_desc);
233
0
        StorageField::clone(local);
234
0
        return local;
235
0
    }
236
};
237
238
class AggStateField : public StorageField {
239
public:
240
0
    AggStateField(const TabletColumn& column) : StorageField(column) {}
241
242
0
    AggStateField* clone() const override {
243
0
        auto* local = new AggStateField(_desc);
244
0
        StorageField::clone(local);
245
0
        return local;
246
0
    }
247
};
248
249
class HllAggField : public StorageField {
250
public:
251
0
    HllAggField(const TabletColumn& column) : StorageField(column) {}
252
253
0
    HllAggField* clone() const override {
254
0
        auto* local = new HllAggField(_desc);
255
0
        StorageField::clone(local);
256
0
        return local;
257
0
    }
258
};
259
260
class StorageFieldFactory {
261
public:
262
38.6k
    static StorageField* create(const TabletColumn& column) {
263
        // for key column
264
38.6k
        if (column.is_key()) {
265
7.51k
            switch (column.type()) {
266
10
            case FieldType::OLAP_FIELD_TYPE_CHAR:
267
10
                return new CharField(column);
268
142
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
269
429
            case FieldType::OLAP_FIELD_TYPE_STRING:
270
429
                return new StringField(column);
271
0
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
272
0
                auto* local = new StructField(column);
273
0
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
274
0
                    std::unique_ptr<StorageField> sub_field(
275
0
                            StorageFieldFactory::create(column.get_sub_column(i)));
276
0
                    local->add_sub_field(std::move(sub_field));
277
0
                }
278
0
                return local;
279
142
            }
280
0
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
281
0
                std::unique_ptr<StorageField> item_field(
282
0
                        StorageFieldFactory::create(column.get_sub_column(0)));
283
0
                auto* local = new ArrayField(column);
284
0
                local->add_sub_field(std::move(item_field));
285
0
                return local;
286
142
            }
287
0
            case FieldType::OLAP_FIELD_TYPE_MAP: {
288
0
                std::unique_ptr<StorageField> key_field(
289
0
                        StorageFieldFactory::create(column.get_sub_column(0)));
290
0
                std::unique_ptr<StorageField> val_field(
291
0
                        StorageFieldFactory::create(column.get_sub_column(1)));
292
0
                auto* local = new MapField(column);
293
0
                local->add_sub_field(std::move(key_field));
294
0
                local->add_sub_field(std::move(val_field));
295
0
                return local;
296
142
            }
297
5
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
298
5
                [[fallthrough]];
299
9
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
300
9
                [[fallthrough]];
301
11
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
302
11
                [[fallthrough]];
303
14
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
304
14
                [[fallthrough]];
305
16
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
306
16
                [[fallthrough]];
307
17
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
308
17
                [[fallthrough]];
309
22
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
310
22
                StorageField* field = new StorageField(column);
311
22
                field->set_precision(column.precision());
312
22
                field->set_scale(column.frac());
313
22
                return field;
314
17
            }
315
7.05k
            default:
316
7.05k
                return new StorageField(column);
317
7.51k
            }
318
7.51k
        }
319
320
        // for value column
321
31.1k
        switch (column.aggregation()) {
322
30.0k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE:
323
31.0k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM:
324
31.0k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN:
325
31.0k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX:
326
31.1k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE:
327
31.1k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL:
328
31.1k
            switch (column.type()) {
329
1
            case FieldType::OLAP_FIELD_TYPE_CHAR:
330
1
                return new CharField(column);
331
60
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
332
60
                return new VarcharField(column);
333
9.15k
            case FieldType::OLAP_FIELD_TYPE_STRING:
334
9.15k
                return new StringField(column);
335
0
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
336
0
                auto* local = new StructField(column);
337
0
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
338
0
                    std::unique_ptr<StorageField> sub_field(
339
0
                            StorageFieldFactory::create(column.get_sub_column(i)));
340
0
                    local->add_sub_field(std::move(sub_field));
341
0
                }
342
0
                return local;
343
0
            }
344
41
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
345
41
                std::unique_ptr<StorageField> item_field(
346
41
                        StorageFieldFactory::create(column.get_sub_column(0)));
347
41
                auto* local = new ArrayField(column);
348
41
                local->add_sub_field(std::move(item_field));
349
41
                return local;
350
0
            }
351
328
            case FieldType::OLAP_FIELD_TYPE_MAP: {
352
328
                DCHECK(column.get_subtype_count() == 2);
353
328
                auto* local = new MapField(column);
354
328
                std::unique_ptr<StorageField> key_field(
355
328
                        StorageFieldFactory::create(column.get_sub_column(0)));
356
328
                std::unique_ptr<StorageField> value_field(
357
328
                        StorageFieldFactory::create(column.get_sub_column(1)));
358
328
                local->add_sub_field(std::move(key_field));
359
328
                local->add_sub_field(std::move(value_field));
360
328
                return local;
361
0
            }
362
1
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
363
1
                [[fallthrough]];
364
1
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
365
1
                [[fallthrough]];
366
1
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
367
1
                [[fallthrough]];
368
1
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
369
1
                [[fallthrough]];
370
1
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
371
1
                [[fallthrough]];
372
4
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
373
4
                [[fallthrough]];
374
5
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
375
5
                StorageField* field = new StorageField(column);
376
5
                field->set_precision(column.precision());
377
5
                field->set_scale(column.frac());
378
5
                return field;
379
4
            }
380
21.5k
            default:
381
21.5k
                return new StorageField(column);
382
31.1k
            }
383
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION:
384
0
            return new HllAggField(column);
385
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION:
386
0
            return new BitmapAggField(column);
387
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION:
388
0
            return new QuantileStateAggField(column);
389
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC:
390
0
            return new AggStateField(column);
391
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN:
392
0
            CHECK(false) << ", value column no agg type";
393
0
            return nullptr;
394
31.1k
        }
395
0
        return nullptr;
396
31.1k
    }
397
398
2
    static StorageField* create_by_type(const FieldType& type) {
399
2
        TabletColumn column(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE, type);
400
2
        return create(column);
401
2
    }
402
};
403
#include "common/compile_check_end.h"
404
} // namespace doris