Coverage Report

Created: 2024-11-20 12:56

/root/doris/be/src/olap/wrapper_field.cpp
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
#include "olap/wrapper_field.h"
19
20
#include <glog/logging.h>
21
#include <string.h>
22
23
#include <algorithm>
24
#include <ostream>
25
26
#include "common/config.h"
27
#include "olap/olap_common.h"
28
#include "olap/olap_define.h"
29
#include "olap/row_cursor.h"
30
31
namespace doris {
32
33
const size_t DEFAULT_STRING_LENGTH = 50;
34
35
0
WrapperField* WrapperField::create(const TabletColumn& column, uint32_t len) {
36
0
    bool is_string_type = (column.type() == FieldType::OLAP_FIELD_TYPE_CHAR ||
37
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
38
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_HLL ||
39
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_OBJECT ||
40
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_STRING);
41
0
    size_t max_length = column.type() == FieldType::OLAP_FIELD_TYPE_STRING
42
0
                                ? config::string_type_length_soft_limit_bytes
43
0
                                : OLAP_VARCHAR_MAX_LENGTH;
44
0
    if (is_string_type && len > max_length) {
45
0
        LOG(WARNING) << "length of string parameter is too long[len=" << len
46
0
                     << ", max_len=" << max_length << "].";
47
0
        return nullptr;
48
0
    }
49
50
0
    Field* rep = FieldFactory::create(column);
51
0
    if (rep == nullptr) {
52
0
        return nullptr;
53
0
    }
54
55
0
    size_t variable_len = 0;
56
0
    if (column.type() == FieldType::OLAP_FIELD_TYPE_CHAR) {
57
0
        variable_len = std::max(len, (uint32_t)(column.length()));
58
0
    } else if (column.type() == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
59
0
               column.type() == FieldType::OLAP_FIELD_TYPE_HLL) {
60
        // column.length is the serialized varchar length
61
        // the first sizeof(VarcharLengthType) bytes is the length of varchar
62
        // variable_len is the real length of varchar
63
0
        variable_len =
64
0
                std::max(len, static_cast<uint32_t>(column.length() - sizeof(VarcharLengthType)));
65
0
    } else if (column.type() == FieldType::OLAP_FIELD_TYPE_STRING) {
66
0
        variable_len = len;
67
0
    } else {
68
0
        variable_len = column.length();
69
0
    }
70
71
0
    WrapperField* wrapper = new WrapperField(rep, variable_len, is_string_type);
72
0
    return wrapper;
73
0
}
74
75
92
WrapperField* WrapperField::create_by_type(const FieldType& type, int32_t var_length) {
76
92
    Field* rep = FieldFactory::create_by_type(type);
77
92
    if (rep == nullptr) {
78
0
        return nullptr;
79
0
    }
80
92
    bool is_string_type =
81
92
            (type == FieldType::OLAP_FIELD_TYPE_CHAR ||
82
92
             type == FieldType::OLAP_FIELD_TYPE_VARCHAR || type == FieldType::OLAP_FIELD_TYPE_HLL ||
83
92
             type == FieldType::OLAP_FIELD_TYPE_OBJECT ||
84
92
             type == FieldType::OLAP_FIELD_TYPE_STRING ||
85
92
             type == FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE);
86
92
    auto wrapper = new WrapperField(rep, var_length, is_string_type);
87
92
    return wrapper;
88
92
}
89
90
WrapperField::WrapperField(Field* rep, size_t variable_len, bool is_string_type)
91
92
        : _rep(rep), _is_string_type(is_string_type), _var_length(0) {
92
92
    size_t fixed_len = _rep->size();
93
92
    _length = fixed_len + 1;
94
92
    _field_buf = new char[_length];
95
92
    memset(_field_buf, 0, _length);
96
92
    _owned_buf = _field_buf;
97
92
    char* buf = _field_buf + 1;
98
99
92
    if (_is_string_type) {
100
6
        _var_length = variable_len > DEFAULT_STRING_LENGTH ? DEFAULT_STRING_LENGTH : variable_len;
101
6
        Slice* slice = reinterpret_cast<Slice*>(buf);
102
6
        slice->size = _var_length;
103
6
        _string_content.reset(new char[slice->size]);
104
6
        slice->data = _string_content.get();
105
6
    }
106
92
    if (_rep->type() == FieldType::OLAP_FIELD_TYPE_STRING) {
107
4
        _long_text_buf = (char*)malloc(RowCursor::DEFAULT_TEXT_LENGTH * sizeof(char));
108
4
        rep->set_long_text_buf(&_long_text_buf);
109
4
    }
110
92
}
111
} // namespace doris