Coverage Report

Created: 2026-05-09 13:49

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
// 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
32.7k
            : _type_info(get_type_info(&column)),
44
32.7k
              _desc(column),
45
32.7k
              _length(column.length()),
46
32.7k
              _key_coder(get_key_coder(column.type())),
47
32.7k
              _name(column.name()),
48
32.7k
              _index_size(column.index_length()),
49
32.7k
              _is_nullable(column.is_nullable()),
50
32.7k
              _unique_id(column.unique_id()),
51
32.7k
              _parent_unique_id(column.parent_unique_id()),
52
32.7k
              _is_extracted_column(column.is_extracted_column()),
53
32.7k
              _path(column.path_info_ptr()) {}
54
55
32.7k
    virtual ~StorageField() = default;
56
57
724k
    size_t size() const { return _type_info->size(); }
58
5
    size_t length() const { return _length; }
59
0
    size_t field_size() const { return size() + 1; }
60
44
    size_t index_size() const { return _index_size; }
61
12.0k
    int32_t unique_id() const { return _unique_id; }
62
6
    int32_t parent_unique_id() const { return _parent_unique_id; }
63
12.0k
    bool is_extracted_column() const { return _is_extracted_column; }
64
42.8k
    const std::string& name() const { return _name; }
65
0
    const PathInDataPtr& path() const { return _path; }
66
67
12
    virtual StorageField* clone() const {
68
12
        auto* local = new StorageField(_desc);
69
12
        this->clone(local);
70
12
        return local;
71
12
    }
72
73
1.01M
    FieldType type() const { return _type_info->type(); }
74
2.31k
    const TypeInfo* type_info() const { return _type_info.get(); }
75
66.7k
    bool is_nullable() const { return _is_nullable; }
76
77
    // similar to `full_encode_ascending`, but only encode part (the first `index_size` bytes) of the value.
78
    // only applicable to string type
79
6
    void encode_ascending(const void* value, std::string* buf) const {
80
6
        _key_coder->encode_ascending(value, _index_size, buf);
81
6
    }
82
83
    // encode the provided `value` into `buf`.
84
5
    void full_encode_ascending(const void* value, std::string* buf) const {
85
5
        _key_coder->full_encode_ascending(value, buf);
86
5
    }
87
88
322k
    const KeyCoder* key_coder() const { return _key_coder; }
89
693
    void add_sub_field(std::unique_ptr<StorageField> sub_field) {
90
693
        _sub_fields.emplace_back(std::move(sub_field));
91
693
    }
92
19
    StorageField* get_sub_field(size_t i) const { return _sub_fields[i].get(); }
93
2
    size_t get_sub_field_count() const { return _sub_fields.size(); }
94
95
34
    void set_precision(int32_t precision) { _precision = precision; }
96
34
    void set_scale(int32_t scale) { _scale = scale; }
97
0
    int32_t get_precision() const { return _precision; }
98
0
    int32_t get_scale() const { return _scale; }
99
75.1k
    const TabletColumn& get_desc() const { return _desc; }
100
101
7.05k
    int32_t get_unique_id() const {
102
7.05k
        return is_extracted_column() ? parent_unique_id() : unique_id();
103
7.05k
    }
104
105
protected:
106
    TypeInfoPtr _type_info;
107
    TabletColumn _desc;
108
    // unit : byte
109
    // except for strings, other types have fixed lengths
110
    // Note that, the struct type itself has fixed length, but due to
111
    // its number of subfields is a variable, so the actual length of
112
    // a struct field is not fixed.
113
    size_t _length;
114
115
12
    void clone(StorageField* other) const {
116
12
        other->_type_info = clone_type_info(this->_type_info.get());
117
12
        other->_key_coder = this->_key_coder;
118
12
        other->_name = this->_name;
119
12
        other->_index_size = this->_index_size;
120
12
        other->_is_nullable = this->_is_nullable;
121
12
        other->_sub_fields.clear();
122
12
        other->_precision = this->_precision;
123
12
        other->_scale = this->_scale;
124
12
        other->_unique_id = this->_unique_id;
125
12
        other->_parent_unique_id = this->_parent_unique_id;
126
12
        other->_is_extracted_column = this->_is_extracted_column;
127
12
        for (const auto& f : _sub_fields) {
128
0
            StorageField* item = f->clone();
129
0
            other->add_sub_field(std::unique_ptr<StorageField>(item));
130
0
        }
131
12
    }
132
133
private:
134
    // maximum length of Field, unit : bytes
135
    // usually equal to length, except for variable-length strings
136
    const KeyCoder* _key_coder;
137
    std::string _name;
138
    size_t _index_size;
139
    bool _is_nullable;
140
    std::vector<std::unique_ptr<StorageField>> _sub_fields;
141
    int32_t _precision;
142
    int32_t _scale;
143
    int32_t _unique_id;
144
    int32_t _parent_unique_id;
145
    bool _is_extracted_column = false;
146
    PathInDataPtr _path;
147
};
148
149
class MapField : public StorageField {
150
public:
151
326
    MapField(const TabletColumn& column) : StorageField(column) {}
152
};
153
154
class StructField : public StorageField {
155
public:
156
0
    StructField(const TabletColumn& column) : StorageField(column) {}
157
};
158
159
class ArrayField : public StorageField {
160
public:
161
41
    ArrayField(const TabletColumn& column) : StorageField(column) {}
162
};
163
164
class CharField : public StorageField {
165
public:
166
11
    CharField(const TabletColumn& column) : StorageField(column) {}
167
168
0
    CharField* clone() const override {
169
0
        auto* local = new CharField(_desc);
170
0
        StorageField::clone(local);
171
0
        return local;
172
0
    }
173
};
174
175
class VarcharField : public StorageField {
176
public:
177
61
    VarcharField(const TabletColumn& column) : StorageField(column) {}
178
179
0
    VarcharField* clone() const override {
180
0
        auto* local = new VarcharField(_desc);
181
0
        StorageField::clone(local);
182
0
        return local;
183
0
    }
184
};
185
class StringField : public StorageField {
186
public:
187
9.55k
    StringField(const TabletColumn& column) : StorageField(column) {}
188
189
0
    StringField* clone() const override {
190
0
        auto* local = new StringField(_desc);
191
0
        StorageField::clone(local);
192
0
        return local;
193
0
    }
194
};
195
196
class BitmapAggField : public StorageField {
197
public:
198
0
    BitmapAggField(const TabletColumn& column) : StorageField(column) {}
199
200
0
    BitmapAggField* clone() const override {
201
0
        auto* local = new BitmapAggField(_desc);
202
0
        StorageField::clone(local);
203
0
        return local;
204
0
    }
205
};
206
207
class QuantileStateAggField : public StorageField {
208
public:
209
0
    QuantileStateAggField(const TabletColumn& column) : StorageField(column) {}
210
211
0
    QuantileStateAggField* clone() const override {
212
0
        auto* local = new QuantileStateAggField(_desc);
213
0
        StorageField::clone(local);
214
0
        return local;
215
0
    }
216
};
217
218
class AggStateField : public StorageField {
219
public:
220
0
    AggStateField(const TabletColumn& column) : StorageField(column) {}
221
222
0
    AggStateField* clone() const override {
223
0
        auto* local = new AggStateField(_desc);
224
0
        StorageField::clone(local);
225
0
        return local;
226
0
    }
227
};
228
229
class HllAggField : public StorageField {
230
public:
231
0
    HllAggField(const TabletColumn& column) : StorageField(column) {}
232
233
0
    HllAggField* clone() const override {
234
0
        auto* local = new HllAggField(_desc);
235
0
        StorageField::clone(local);
236
0
        return local;
237
0
    }
238
};
239
240
class StorageFieldFactory {
241
public:
242
32.7k
    static StorageField* create(const TabletColumn& column) {
243
        // for key column
244
32.7k
        if (column.is_key()) {
245
5.89k
            switch (column.type()) {
246
10
            case FieldType::OLAP_FIELD_TYPE_CHAR:
247
10
                return new CharField(column);
248
142
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
249
430
            case FieldType::OLAP_FIELD_TYPE_STRING:
250
430
                return new StringField(column);
251
0
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
252
0
                auto* local = new StructField(column);
253
0
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
254
0
                    std::unique_ptr<StorageField> sub_field(
255
0
                            StorageFieldFactory::create(column.get_sub_column(i)));
256
0
                    local->add_sub_field(std::move(sub_field));
257
0
                }
258
0
                return local;
259
142
            }
260
0
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
261
0
                std::unique_ptr<StorageField> item_field(
262
0
                        StorageFieldFactory::create(column.get_sub_column(0)));
263
0
                auto* local = new ArrayField(column);
264
0
                local->add_sub_field(std::move(item_field));
265
0
                return local;
266
142
            }
267
0
            case FieldType::OLAP_FIELD_TYPE_MAP: {
268
0
                std::unique_ptr<StorageField> key_field(
269
0
                        StorageFieldFactory::create(column.get_sub_column(0)));
270
0
                std::unique_ptr<StorageField> val_field(
271
0
                        StorageFieldFactory::create(column.get_sub_column(1)));
272
0
                auto* local = new MapField(column);
273
0
                local->add_sub_field(std::move(key_field));
274
0
                local->add_sub_field(std::move(val_field));
275
0
                return local;
276
142
            }
277
5
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
278
5
                [[fallthrough]];
279
9
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
280
9
                [[fallthrough]];
281
11
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
282
11
                [[fallthrough]];
283
14
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
284
14
                [[fallthrough]];
285
16
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
286
16
                [[fallthrough]];
287
17
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
288
17
                [[fallthrough]];
289
22
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
290
22
                StorageField* field = new StorageField(column);
291
22
                field->set_precision(column.precision());
292
22
                field->set_scale(column.frac());
293
22
                return field;
294
17
            }
295
5.43k
            default:
296
5.43k
                return new StorageField(column);
297
5.89k
            }
298
5.89k
        }
299
300
        // for value column
301
26.8k
        switch (column.aggregation()) {
302
26.0k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE:
303
26.7k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM:
304
26.7k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN:
305
26.7k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX:
306
26.8k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE:
307
26.8k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL:
308
26.8k
            switch (column.type()) {
309
1
            case FieldType::OLAP_FIELD_TYPE_CHAR:
310
1
                return new CharField(column);
311
61
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
312
61
                return new VarcharField(column);
313
9.12k
            case FieldType::OLAP_FIELD_TYPE_STRING:
314
9.12k
                return new StringField(column);
315
0
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
316
0
                auto* local = new StructField(column);
317
0
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
318
0
                    std::unique_ptr<StorageField> sub_field(
319
0
                            StorageFieldFactory::create(column.get_sub_column(i)));
320
0
                    local->add_sub_field(std::move(sub_field));
321
0
                }
322
0
                return local;
323
0
            }
324
41
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
325
41
                std::unique_ptr<StorageField> item_field(
326
41
                        StorageFieldFactory::create(column.get_sub_column(0)));
327
41
                auto* local = new ArrayField(column);
328
41
                local->add_sub_field(std::move(item_field));
329
41
                return local;
330
0
            }
331
326
            case FieldType::OLAP_FIELD_TYPE_MAP: {
332
326
                DCHECK(column.get_subtype_count() == 2);
333
326
                auto* local = new MapField(column);
334
326
                std::unique_ptr<StorageField> key_field(
335
326
                        StorageFieldFactory::create(column.get_sub_column(0)));
336
326
                std::unique_ptr<StorageField> value_field(
337
326
                        StorageFieldFactory::create(column.get_sub_column(1)));
338
326
                local->add_sub_field(std::move(key_field));
339
326
                local->add_sub_field(std::move(value_field));
340
326
                return local;
341
0
            }
342
2
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
343
2
                [[fallthrough]];
344
3
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
345
3
                [[fallthrough]];
346
4
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
347
4
                [[fallthrough]];
348
5
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
349
5
                [[fallthrough]];
350
6
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
351
6
                [[fallthrough]];
352
10
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
353
10
                [[fallthrough]];
354
12
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
355
12
                StorageField* field = new StorageField(column);
356
12
                field->set_precision(column.precision());
357
12
                field->set_scale(column.frac());
358
12
                return field;
359
10
            }
360
17.2k
            default:
361
17.2k
                return new StorageField(column);
362
26.8k
            }
363
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION:
364
0
            return new HllAggField(column);
365
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION:
366
0
            return new BitmapAggField(column);
367
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION:
368
0
            return new QuantileStateAggField(column);
369
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC:
370
0
            return new AggStateField(column);
371
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN:
372
0
            CHECK(false) << ", value column no agg type";
373
0
            return nullptr;
374
26.8k
        }
375
0
        return nullptr;
376
26.8k
    }
377
378
2
    static StorageField* create_by_type(const FieldType& type) {
379
2
        TabletColumn column(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE, type);
380
2
        return create(column);
381
2
    }
382
};
383
} // namespace doris