Coverage Report

Created: 2026-05-13 17:40

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