Coverage Report

Created: 2026-02-27 19:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "olap/key_coder.h"
25
#include "olap/olap_common.h"
26
#include "olap/olap_define.h"
27
#include "olap/row_cursor_cell.h"
28
#include "olap/tablet_schema.h"
29
#include "olap/types.h"
30
#include "olap/utils.h"
31
#include "runtime/collection_value.h"
32
#include "runtime/map_value.h"
33
#include "util/hash_util.hpp"
34
#include "util/slice.h"
35
#include "vec/common/arena.h"
36
#include "vec/json/path_in_data.h"
37
38
namespace doris {
39
#include "common/compile_check_begin.h"
40
// A Field is used to represent a column in memory format.
41
// User can use this class to access or deal with column data in memory.
42
class Field {
43
public:
44
    Field(const TabletColumn& column)
45
76.2M
            : _type_info(get_type_info(&column)),
46
76.2M
              _desc(column),
47
76.2M
              _length(column.length()),
48
76.2M
              _key_coder(get_key_coder(column.type())),
49
76.2M
              _name(column.name()),
50
76.2M
              _index_size(column.index_length()),
51
76.2M
              _is_nullable(column.is_nullable()),
52
76.2M
              _unique_id(column.unique_id()),
53
76.2M
              _parent_unique_id(column.parent_unique_id()),
54
76.2M
              _is_extracted_column(column.is_extracted_column()),
55
76.2M
              _path(column.path_info_ptr()) {}
56
57
77.0M
    virtual ~Field() = default;
58
59
73.9M
    size_t size() const { return _type_info->size(); }
60
0
    size_t length() const { return _length; }
61
0
    size_t field_size() const { return size() + 1; }
62
0
    size_t index_size() const { return _index_size; }
63
44.9M
    int32_t unique_id() const { return _unique_id; }
64
71.6k
    int32_t parent_unique_id() const { return _parent_unique_id; }
65
39.1M
    bool is_extracted_column() const { return _is_extracted_column; }
66
68.7M
    const std::string& name() const { return _name; }
67
0
    const vectorized::PathInDataPtr& path() const { return _path; }
68
69
0
    virtual void set_to_max(char* buf) const { return _type_info->set_to_max(buf); }
70
71
2
    virtual void set_to_min(char* buf) const { return _type_info->set_to_min(buf); }
72
73
0
    void set_long_text_buf(char** buf) { _long_text_buf = buf; }
74
75
5.71M
    virtual size_t get_variable_len() const { return 0; }
76
77
2.43M
    virtual Field* clone() const {
78
2.43M
        auto* local = new Field(_desc);
79
2.43M
        this->clone(local);
80
2.43M
        return local;
81
2.43M
    }
82
83
    // Only compare column content, without considering nullptr condition.
84
    // RETURNS:
85
    //      0 means equal,
86
    //      -1 means left less than right,
87
    //      1 means left bigger than right
88
0
    int compare(const void* left, const void* right) const { return _type_info->cmp(left, right); }
89
90
    // Compare two types of cell.
91
    // This function differs compare in that this function compare cell which
92
    // will consider the condition which cell may be nullptr. While compare only
93
    // compare column content without considering nullptr condition.
94
    // Only compare column content, without considering nullptr condition.
95
    // RETURNS:
96
    //      0 means equal,
97
    //      -1 means left less than right,
98
    //      1 means left bigger than right
99
    template <typename LhsCellType, typename RhsCellType>
100
2.84M
    int compare_cell(const LhsCellType& lhs, const RhsCellType& rhs) const {
101
2.84M
        bool l_null = lhs.is_null();
102
2.84M
        bool r_null = rhs.is_null();
103
2.84M
        if (l_null != r_null) {
104
18.4E
            return l_null ? -1 : 1;
105
235k
        }
106
2.60M
        return l_null ? 0 : _type_info->cmp(lhs.cell_ptr(), rhs.cell_ptr());
107
2.84M
    }
108
109
    // deep copy source cell' content to destination cell.
110
    // For string type, this will allocate data form arena,
111
    // and copy source's content.
112
    template <typename DstCellType, typename SrcCellType>
113
    void deep_copy(DstCellType* dst, const SrcCellType& src, vectorized::Arena& arena) const {
114
        bool is_null = src.is_null();
115
        dst->set_is_null(is_null);
116
        if (is_null) {
117
            return;
118
        }
119
        _type_info->deep_copy(dst->mutable_cell_ptr(), src.cell_ptr(), arena);
120
    }
121
122
    // used by init scan key stored in string format
123
    // value_string should end with '\0'
124
    Status from_string(char* buf, const std::string& value_string, const int precision = 0,
125
5.45M
                       const int scale = 0) const {
126
5.45M
        if (type() == FieldType::OLAP_FIELD_TYPE_STRING && !value_string.empty()) {
127
0
            auto slice = reinterpret_cast<Slice*>(buf);
128
0
            if (slice->size < value_string.size()) {
129
0
                *_long_text_buf = static_cast<char*>(realloc(*_long_text_buf, value_string.size()));
130
0
                slice->data = *_long_text_buf;
131
0
                slice->size = value_string.size();
132
0
            }
133
0
        }
134
5.45M
        return _type_info->from_string(buf, value_string, precision, scale);
135
5.45M
    }
136
137
58.3M
    FieldType type() const { return _type_info->type(); }
138
43.8k
    const TypeInfo* type_info() const { return _type_info.get(); }
139
45.7M
    bool is_nullable() const { return _is_nullable; }
140
141
    // similar to `full_encode_ascending`, but only encode part (the first `index_size` bytes) of the value.
142
    // only applicable to string type
143
722k
    void encode_ascending(const void* value, std::string* buf) const {
144
722k
        _key_coder->encode_ascending(value, _index_size, buf);
145
722k
    }
146
147
    // encode the provided `value` into `buf`.
148
8.79M
    void full_encode_ascending(const void* value, std::string* buf) const {
149
8.79M
        _key_coder->full_encode_ascending(value, buf);
150
8.79M
    }
151
826k
    void add_sub_field(std::unique_ptr<Field> sub_field) {
152
826k
        _sub_fields.emplace_back(std::move(sub_field));
153
826k
    }
154
141k
    Field* get_sub_field(size_t i) const { return _sub_fields[i].get(); }
155
43.1k
    size_t get_sub_field_count() const { return _sub_fields.size(); }
156
157
4.72M
    void set_precision(int32_t precision) { _precision = precision; }
158
4.72M
    void set_scale(int32_t scale) { _scale = scale; }
159
5.44M
    int32_t get_precision() const { return _precision; }
160
5.45M
    int32_t get_scale() const { return _scale; }
161
68.2M
    const TabletColumn& get_desc() const { return _desc; }
162
163
19.6M
    int32_t get_unique_id() const {
164
19.6M
        return is_extracted_column() ? parent_unique_id() : unique_id();
165
19.6M
    }
166
167
protected:
168
    TypeInfoPtr _type_info;
169
    TabletColumn _desc;
170
    // unit : byte
171
    // except for strings, other types have fixed lengths
172
    // Note that, the struct type itself has fixed length, but due to
173
    // its number of subfields is a variable, so the actual length of
174
    // a struct field is not fixed.
175
    size_t _length;
176
    // Since the length of the STRING type cannot be determined,
177
    // only dynamic memory can be used. Arena cannot realize realloc.
178
    // The schema information is shared globally. Therefore,
179
    // dynamic memory can only be managed in thread local mode.
180
    // The memory will be created and released in rowcursor.
181
    char** _long_text_buf = nullptr;
182
183
0
    char* allocate_string_value(vectorized::Arena& arena) const {
184
0
        char* type_value = arena.alloc(sizeof(Slice));
185
0
        auto slice = reinterpret_cast<Slice*>(type_value);
186
0
        slice->size = _length;
187
0
        slice->data = arena.alloc(slice->size);
188
0
        return type_value;
189
0
    }
190
191
10.2M
    void clone(Field* other) const {
192
10.2M
        other->_type_info = clone_type_info(this->_type_info.get());
193
10.2M
        other->_key_coder = this->_key_coder;
194
10.2M
        other->_name = this->_name;
195
10.2M
        other->_index_size = this->_index_size;
196
10.2M
        other->_is_nullable = this->_is_nullable;
197
10.2M
        other->_sub_fields.clear();
198
10.2M
        other->_precision = this->_precision;
199
10.2M
        other->_scale = this->_scale;
200
10.2M
        other->_unique_id = this->_unique_id;
201
10.2M
        other->_parent_unique_id = this->_parent_unique_id;
202
10.2M
        other->_is_extracted_column = this->_is_extracted_column;
203
10.2M
        for (const auto& f : _sub_fields) {
204
0
            Field* item = f->clone();
205
0
            other->add_sub_field(std::unique_ptr<Field>(item));
206
0
        }
207
10.2M
    }
208
209
private:
210
    // maximum length of Field, unit : bytes
211
    // usually equal to length, except for variable-length strings
212
    const KeyCoder* _key_coder;
213
    std::string _name;
214
    size_t _index_size;
215
    bool _is_nullable;
216
    std::vector<std::unique_ptr<Field>> _sub_fields;
217
    int32_t _precision;
218
    int32_t _scale;
219
    int32_t _unique_id;
220
    int32_t _parent_unique_id;
221
    bool _is_extracted_column = false;
222
    vectorized::PathInDataPtr _path;
223
};
224
225
class MapField : public Field {
226
public:
227
141k
    MapField(const TabletColumn& column) : Field(column) {}
228
229
0
    size_t get_variable_len() const override { return _length; }
230
};
231
232
class StructField : public Field {
233
public:
234
50.5k
    StructField(const TabletColumn& column) : Field(column) {}
235
236
0
    size_t get_variable_len() const override {
237
0
        size_t variable_len = _length;
238
0
        for (size_t i = 0; i < get_sub_field_count(); i++) {
239
0
            variable_len += get_sub_field(i)->get_variable_len();
240
0
        }
241
0
        return variable_len;
242
0
    }
243
};
244
245
class ArrayField : public Field {
246
public:
247
382k
    ArrayField(const TabletColumn& column) : Field(column) {}
248
249
0
    size_t get_variable_len() const override { return _length; }
250
};
251
252
class CharField : public Field {
253
public:
254
244k
    CharField(const TabletColumn& column) : Field(column) {}
255
256
1.42k
    size_t get_variable_len() const override { return _length; }
257
258
1.71k
    CharField* clone() const override {
259
1.71k
        auto* local = new CharField(_desc);
260
1.71k
        Field::clone(local);
261
1.71k
        return local;
262
1.71k
    }
263
264
0
    void set_to_max(char* ch) const override {
265
0
        auto slice = reinterpret_cast<Slice*>(ch);
266
0
        slice->size = _length;
267
0
        memset(slice->data, 0xFF, slice->size);
268
0
    }
269
};
270
271
class VarcharField : public Field {
272
public:
273
7.48M
    VarcharField(const TabletColumn& column) : Field(column) {}
274
275
2
    size_t get_variable_len() const override { return _length - OLAP_VARCHAR_MAX_BYTES; }
276
277
0
    VarcharField* clone() const override {
278
0
        auto* local = new VarcharField(_desc);
279
0
        Field::clone(local);
280
0
        return local;
281
0
    }
282
283
2
    void set_to_max(char* ch) const override {
284
2
        auto slice = reinterpret_cast<Slice*>(ch);
285
2
        slice->size = _length - OLAP_VARCHAR_MAX_BYTES;
286
2
        memset(slice->data, 0xFF, slice->size);
287
2
    }
288
};
289
class StringField : public Field {
290
public:
291
37.5M
    StringField(const TabletColumn& column) : Field(column) {}
292
293
7.79M
    StringField* clone() const override {
294
7.79M
        auto* local = new StringField(_desc);
295
7.79M
        Field::clone(local);
296
7.79M
        return local;
297
7.79M
    }
298
299
0
    void set_to_max(char* ch) const override {
300
0
        auto slice = reinterpret_cast<Slice*>(ch);
301
0
        memset(slice->data, 0xFF, slice->size);
302
0
    }
303
};
304
305
class BitmapAggField : public Field {
306
public:
307
24.8k
    BitmapAggField(const TabletColumn& column) : Field(column) {}
308
309
0
    BitmapAggField* clone() const override {
310
0
        auto* local = new BitmapAggField(_desc);
311
0
        Field::clone(local);
312
0
        return local;
313
0
    }
314
};
315
316
class QuantileStateAggField : public Field {
317
public:
318
10.3k
    QuantileStateAggField(const TabletColumn& column) : Field(column) {}
319
320
0
    QuantileStateAggField* clone() const override {
321
0
        auto* local = new QuantileStateAggField(_desc);
322
0
        Field::clone(local);
323
0
        return local;
324
0
    }
325
};
326
327
class AggStateField : public Field {
328
public:
329
6.33k
    AggStateField(const TabletColumn& column) : Field(column) {}
330
331
0
    AggStateField* clone() const override {
332
0
        auto* local = new AggStateField(_desc);
333
0
        Field::clone(local);
334
0
        return local;
335
0
    }
336
};
337
338
class HllAggField : public Field {
339
public:
340
14.6k
    HllAggField(const TabletColumn& column) : Field(column) {}
341
342
0
    HllAggField* clone() const override {
343
0
        auto* local = new HllAggField(_desc);
344
0
        Field::clone(local);
345
0
        return local;
346
0
    }
347
};
348
349
class FieldFactory {
350
public:
351
66.7M
    static Field* create(const TabletColumn& column) {
352
        // for key column
353
66.7M
        if (column.is_key()) {
354
29.4M
            switch (column.type()) {
355
41.9k
            case FieldType::OLAP_FIELD_TYPE_CHAR:
356
41.9k
                return new CharField(column);
357
26.2M
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
358
26.2M
            case FieldType::OLAP_FIELD_TYPE_STRING:
359
26.2M
                return new StringField(column);
360
0
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
361
0
                auto* local = new StructField(column);
362
0
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
363
0
                    std::unique_ptr<Field> sub_field(
364
0
                            FieldFactory::create(column.get_sub_column(i)));
365
0
                    local->add_sub_field(std::move(sub_field));
366
0
                }
367
0
                return local;
368
26.2M
            }
369
0
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
370
0
                std::unique_ptr<Field> item_field(FieldFactory::create(column.get_sub_column(0)));
371
0
                auto* local = new ArrayField(column);
372
0
                local->add_sub_field(std::move(item_field));
373
0
                return local;
374
26.2M
            }
375
0
            case FieldType::OLAP_FIELD_TYPE_MAP: {
376
0
                std::unique_ptr<Field> key_field(FieldFactory::create(column.get_sub_column(0)));
377
0
                std::unique_ptr<Field> val_field(FieldFactory::create(column.get_sub_column(1)));
378
0
                auto* local = new MapField(column);
379
0
                local->add_sub_field(std::move(key_field));
380
0
                local->add_sub_field(std::move(val_field));
381
0
                return local;
382
26.2M
            }
383
1.81k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
384
1.81k
                [[fallthrough]];
385
41.6k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
386
41.6k
                [[fallthrough]];
387
54.0k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
388
54.0k
                [[fallthrough]];
389
104k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
390
104k
                [[fallthrough]];
391
109k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
392
109k
                [[fallthrough]];
393
165k
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
394
165k
                [[fallthrough]];
395
282k
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
396
282k
                Field* field = new Field(column);
397
282k
                field->set_precision(column.precision());
398
282k
                field->set_scale(column.frac());
399
282k
                return field;
400
165k
            }
401
2.85M
            default:
402
2.85M
                return new Field(column);
403
29.4M
            }
404
29.4M
        }
405
406
        // for value column
407
37.3M
        switch (column.aggregation()) {
408
36.4M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE:
409
36.6M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_SUM:
410
36.6M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MIN:
411
36.7M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_MAX:
412
37.1M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE:
413
37.3M
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL:
414
37.3M
            switch (column.type()) {
415
201k
            case FieldType::OLAP_FIELD_TYPE_CHAR:
416
201k
                return new CharField(column);
417
7.51M
            case FieldType::OLAP_FIELD_TYPE_VARCHAR:
418
7.51M
                return new VarcharField(column);
419
3.99M
            case FieldType::OLAP_FIELD_TYPE_STRING:
420
3.99M
                return new StringField(column);
421
50.6k
            case FieldType::OLAP_FIELD_TYPE_STRUCT: {
422
50.6k
                auto* local = new StructField(column);
423
209k
                for (uint32_t i = 0; i < column.get_subtype_count(); i++) {
424
158k
                    std::unique_ptr<Field> sub_field(
425
158k
                            FieldFactory::create(column.get_sub_column(i)));
426
158k
                    local->add_sub_field(std::move(sub_field));
427
158k
                }
428
50.6k
                return local;
429
0
            }
430
383k
            case FieldType::OLAP_FIELD_TYPE_ARRAY: {
431
383k
                std::unique_ptr<Field> item_field(FieldFactory::create(column.get_sub_column(0)));
432
383k
                auto* local = new ArrayField(column);
433
383k
                local->add_sub_field(std::move(item_field));
434
383k
                return local;
435
0
            }
436
142k
            case FieldType::OLAP_FIELD_TYPE_MAP: {
437
142k
                DCHECK(column.get_subtype_count() == 2);
438
142k
                auto* local = new MapField(column);
439
142k
                std::unique_ptr<Field> key_field(FieldFactory::create(column.get_sub_column(0)));
440
142k
                std::unique_ptr<Field> value_field(FieldFactory::create(column.get_sub_column(1)));
441
142k
                local->add_sub_field(std::move(key_field));
442
142k
                local->add_sub_field(std::move(value_field));
443
142k
                return local;
444
0
            }
445
4.94k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL:
446
4.94k
                [[fallthrough]];
447
86.4k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL32:
448
86.4k
                [[fallthrough]];
449
334k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL64:
450
334k
                [[fallthrough]];
451
528k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL128I:
452
528k
                [[fallthrough]];
453
540k
            case FieldType::OLAP_FIELD_TYPE_DECIMAL256:
454
540k
                [[fallthrough]];
455
675k
            case FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ:
456
675k
                [[fallthrough]];
457
4.44M
            case FieldType::OLAP_FIELD_TYPE_DATETIMEV2: {
458
4.44M
                Field* field = new Field(column);
459
4.44M
                field->set_precision(column.precision());
460
4.44M
                field->set_scale(column.frac());
461
4.44M
                return field;
462
675k
            }
463
20.6M
            default:
464
20.6M
                return new Field(column);
465
37.3M
            }
466
14.7k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_HLL_UNION:
467
14.7k
            return new HllAggField(column);
468
24.9k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_BITMAP_UNION:
469
24.9k
            return new BitmapAggField(column);
470
10.3k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_QUANTILE_UNION:
471
10.3k
            return new QuantileStateAggField(column);
472
6.39k
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_GENERIC:
473
6.39k
            return new AggStateField(column);
474
0
        case FieldAggregationMethod::OLAP_FIELD_AGGREGATION_UNKNOWN:
475
0
            CHECK(false) << ", value column no agg type";
476
0
            return nullptr;
477
37.3M
        }
478
0
        return nullptr;
479
37.3M
    }
480
481
    static Field* create_by_type(const FieldType& type) {
482
        TabletColumn column(FieldAggregationMethod::OLAP_FIELD_AGGREGATION_NONE, type);
483
        return create(column);
484
    }
485
};
486
#include "common/compile_check_end.h"
487
} // namespace doris