Coverage Report

Created: 2024-11-21 12:31

/root/doris/be/src/olap/wrapper_field.h
Line
Count
Source (jump to first uncovered line)
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 <stdint.h>
21
#include <stdlib.h>
22
23
#include <memory>
24
#include <string>
25
26
#include "common/status.h"
27
#include "olap/field.h"
28
#include "olap/row_cursor_cell.h"
29
#include "olap/tablet_schema.h"
30
#include "util/slice.h"
31
32
namespace doris {
33
enum class FieldType;
34
35
class WrapperField {
36
public:
37
    static Result<WrapperField*> create(const TabletColumn& column, uint32_t len = 0);
38
116
    static WrapperField* create_by_type(const FieldType& type) { return create_by_type(type, 0); }
39
    static WrapperField* create_by_type(const FieldType& type, int32_t var_length);
40
41
    WrapperField(Field* rep, size_t variable_len, bool is_string_type);
42
43
116
    virtual ~WrapperField() {
44
116
        delete _rep;
45
116
        delete[] _owned_buf;
46
116
        if (_long_text_buf) {
47
4
            free(_long_text_buf);
48
4
        }
49
116
    }
50
51
    // Convert the internal value to string output.
52
    //
53
    // NOTE: it only for DEBUG use. Do not include the null flag.
54
110
    std::string to_string() const { return _rep->to_string(_field_buf + 1); }
55
56
    // Deserialize field value from incoming string.
57
    //
58
    // NOTE: the parameter must be a '\0' terminated string. It do not include the null flag.
59
    Status from_string(const std::string& value_string, const int precision = 0,
60
80
                       const int scale = 0) {
61
80
        if (_is_string_type) {
62
6
            if (value_string.size() > _var_length) {
63
6
                Slice* slice = reinterpret_cast<Slice*>(cell_ptr());
64
6
                slice->size = value_string.size();
65
6
                _var_length = slice->size;
66
6
                _string_content.reset(new char[slice->size]);
67
6
                slice->data = _string_content.get();
68
6
            }
69
6
        }
70
80
        return _rep->from_string(_field_buf + 1, value_string, precision, scale);
71
80
    }
72
73
0
    bool is_string_type() const { return _is_string_type; }
74
0
    char* ptr() const { return _field_buf + 1; }
75
0
    size_t size() const { return _rep->size(); }
76
0
    size_t field_size() const { return _rep->field_size(); }
77
6
    bool is_null() const { return *reinterpret_cast<bool*>(_field_buf); }
78
0
    void set_is_null(bool is_null) { *reinterpret_cast<bool*>(_field_buf) = is_null; }
79
3
    void set_null() { *reinterpret_cast<bool*>(_field_buf) = true; }
80
2
    void set_not_null() { *reinterpret_cast<bool*>(_field_buf) = false; }
81
0
    char* nullable_cell_ptr() const { return _field_buf; }
82
18
    void set_to_max() { _rep->set_to_max(_field_buf + 1); }
83
18
    void set_to_min() { _rep->set_to_min(_field_buf + 1); }
84
10
    void* cell_ptr() const { return _field_buf + 1; }
85
0
    void* mutable_cell_ptr() const { return _field_buf + 1; }
86
0
    const Field* field() const { return _rep; }
87
88
0
    int cmp(const WrapperField* field) const { return _rep->compare_cell(*this, *field); }
89
90
0
    void copy(const WrapperField* field) { _rep->direct_copy(this, *field); }
91
92
private:
93
    Field* _rep = nullptr;
94
    bool _is_string_type;
95
    char* _field_buf = nullptr;
96
    char* _owned_buf = nullptr;
97
    char* _long_text_buf = nullptr;
98
99
    // Include fixed and variable length and null bytes.
100
    size_t _length;
101
    size_t _var_length;
102
    // Memory for string type field.
103
    std::unique_ptr<char[]> _string_content;
104
};
105
106
} // namespace doris