Coverage Report

Created: 2024-11-20 21:05

/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
22
#include <algorithm>
23
#include <cstring>
24
#include <ostream>
25
26
#include "common/config.h"
27
#include "common/status.h"
28
#include "olap/olap_common.h"
29
#include "olap/olap_define.h"
30
#include "olap/row_cursor.h"
31
#include "util/expected.hpp"
32
33
namespace doris {
34
35
const size_t DEFAULT_STRING_LENGTH = 50;
36
37
0
Result<WrapperField*> WrapperField::create(const TabletColumn& column, uint32_t len) {
38
0
    bool is_string_type = (column.type() == FieldType::OLAP_FIELD_TYPE_CHAR ||
39
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
40
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_HLL ||
41
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_OBJECT ||
42
0
                           column.type() == FieldType::OLAP_FIELD_TYPE_STRING);
43
0
    size_t max_length = column.type() == FieldType::OLAP_FIELD_TYPE_STRING
44
0
                                ? config::string_type_length_soft_limit_bytes
45
0
                                : OLAP_VARCHAR_MAX_LENGTH;
46
0
    if (is_string_type && len > max_length) {
47
0
        LOG(WARNING) << "length of string parameter is too long[len=" << len
48
0
                     << ", max_len=" << max_length << "].";
49
0
        return unexpected {Status::Error<ErrorCode::EXCEEDED_LIMIT>(
50
0
                "length of string parameter is too long[len={}, max_len={}].", len, max_length)};
51
0
    }
52
53
0
    Field* rep = FieldFactory::create(column);
54
0
    if (rep == nullptr) {
55
0
        return unexpected {Status::Uninitialized("Unsupport field creation of {}", column.name())};
56
0
    }
57
58
0
    size_t variable_len = 0;
59
0
    if (column.type() == FieldType::OLAP_FIELD_TYPE_CHAR) {
60
0
        variable_len = std::max(len, (uint32_t)(column.length()));
61
0
    } else if (column.type() == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
62
0
               column.type() == FieldType::OLAP_FIELD_TYPE_HLL) {
63
        // column.length is the serialized varchar length
64
        // the first sizeof(VarcharLengthType) bytes is the length of varchar
65
        // variable_len is the real length of varchar
66
0
        variable_len =
67
0
                std::max(len, static_cast<uint32_t>(column.length() - sizeof(VarcharLengthType)));
68
0
    } else if (column.type() == FieldType::OLAP_FIELD_TYPE_STRING) {
69
0
        variable_len = len;
70
0
    } else {
71
0
        variable_len = column.length();
72
0
    }
73
0
    return new WrapperField(rep, variable_len, is_string_type);
74
0
}
75
76
116
WrapperField* WrapperField::create_by_type(const FieldType& type, int32_t var_length) {
77
116
    Field* rep = FieldFactory::create_by_type(type);
78
116
    if (rep == nullptr) {
79
0
        return nullptr;
80
0
    }
81
116
    bool is_string_type =
82
116
            (type == FieldType::OLAP_FIELD_TYPE_CHAR ||
83
116
             type == FieldType::OLAP_FIELD_TYPE_VARCHAR || type == FieldType::OLAP_FIELD_TYPE_HLL ||
84
116
             type == FieldType::OLAP_FIELD_TYPE_OBJECT ||
85
116
             type == FieldType::OLAP_FIELD_TYPE_STRING ||
86
116
             type == FieldType::OLAP_FIELD_TYPE_QUANTILE_STATE);
87
116
    return new WrapperField(rep, var_length, is_string_type);
88
116
}
89
90
WrapperField::WrapperField(Field* rep, size_t variable_len, bool is_string_type)
91
116
        : _rep(rep), _is_string_type(is_string_type), _var_length(0) {
92
116
    size_t fixed_len = _rep->size();
93
116
    _length = fixed_len + 1;
94
116
    _field_buf = new char[_length];
95
116
    memset(_field_buf, 0, _length);
96
116
    _owned_buf = _field_buf;
97
116
    char* buf = _field_buf + 1;
98
99
116
    if (_is_string_type) {
100
6
        _var_length = variable_len > DEFAULT_STRING_LENGTH ? DEFAULT_STRING_LENGTH : variable_len;
101
6
        auto* 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
116
    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
116
}
111
} // namespace doris