Coverage Report

Created: 2024-11-20 15:52

/root/doris/be/src/olap/utils.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
// IWYU pragma: no_include <bthread/errno.h>
21
#include <errno.h> // IWYU pragma: keep
22
#include <limits.h>
23
#include <stdint.h>
24
#include <sys/time.h>
25
26
#include <cstdio>
27
#include <cstdlib>
28
#include <iterator>
29
#include <limits>
30
#include <string>
31
#include <vector>
32
33
#include "common/status.h"
34
#include "olap/olap_common.h"
35
36
namespace doris {
37
static const std::string DELETE_SIGN = "__DORIS_DELETE_SIGN__";
38
static const std::string WHERE_SIGN = "__DORIS_WHERE_SIGN__";
39
static const std::string VERSION_COL = "__DORIS_VERSION_COL__";
40
static const std::string SKIP_BITMAP_COL = "__DORIS_SKIP_BITMAP_COL__";
41
static const std::string SEQUENCE_COL = "__DORIS_SEQUENCE_COL__";
42
43
// 用来加速运算
44
const static int32_t g_power_table[] = {1,      10,      100,      1000,      10000,
45
                                        100000, 1000000, 10000000, 100000000, 1000000000};
46
47
// 计时工具,用于确定一段代码执行的时间,用于性能调优
48
class OlapStopWatch {
49
public:
50
84
    uint64_t get_elapse_time_us() const {
51
84
        struct timeval now;
52
84
        gettimeofday(&now, nullptr);
53
84
        return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 +
54
84
                          (now.tv_usec - _begin_time.tv_usec));
55
84
    }
56
57
2
    double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; }
58
59
70
    void reset() { gettimeofday(&_begin_time, nullptr); }
60
61
70
    OlapStopWatch() { reset(); }
62
63
private:
64
    struct timeval _begin_time; // 起始时间戳
65
};
66
67
// @brief 切分字符串
68
// @param base 原串
69
// @param separator 分隔符
70
// @param result 切分结果
71
template <typename Str, typename T>
72
0
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
73
0
    if (!result) {
74
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
75
0
    }
76
77
    // 处理base为空的情况
78
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
79
0
    if (base.size() == 0) {
80
0
        result->push_back("");
81
0
        return Status::OK();
82
0
    }
83
84
0
    size_t offset = 0;
85
0
    while (offset < base.length()) {
86
0
        size_t next = base.find(separator, offset);
87
0
        if (next == std::string::npos) {
88
0
            result->emplace_back(base.substr(offset));
89
0
            break;
90
0
        } else {
91
0
            result->emplace_back(base.substr(offset, next - offset));
92
0
            offset = next + 1;
93
0
        }
94
0
    }
95
96
0
    return Status::OK();
97
0
}
Unexecuted instantiation: _ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE
Unexecuted instantiation: _ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE
98
99
uint32_t olap_adler32_init();
100
uint32_t olap_adler32(uint32_t adler, const char* buf, size_t len);
101
102
// 获取系统当前时间,并将时间转换为字符串
103
Status gen_timestamp_string(std::string* out_string);
104
105
Status check_datapath_rw(const std::string& path);
106
107
Status read_write_test_file(const std::string& test_file_path);
108
109
// 打印Errno
110
class Errno {
111
public:
112
    // 返回Errno对应的错误信息,线程安全
113
    static const char* str();
114
    static const char* str(int no);
115
    static int no();
116
117
private:
118
    static const int BUF_SIZE = 256;
119
    static __thread char _buf[BUF_SIZE];
120
};
121
122
// 检查int8_t, int16_t, int32_t, int64_t的值是否溢出
123
template <typename T>
124
89
bool valid_signed_number(const std::string& value_str) {
125
89
    char* endptr = nullptr;
126
89
    errno = 0;
127
89
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
89
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
89
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
2
        return false;
132
2
    }
133
134
87
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
6
        return false;
136
6
    }
137
138
81
    return true;
139
87
}
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
11
bool valid_signed_number(const std::string& value_str) {
125
11
    char* endptr = nullptr;
126
11
    errno = 0;
127
11
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
11
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
11
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
11
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
9
    return true;
139
11
}
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
13
bool valid_signed_number(const std::string& value_str) {
125
13
    char* endptr = nullptr;
126
13
    errno = 0;
127
13
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
13
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
13
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
13
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
11
    return true;
139
13
}
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
54
bool valid_signed_number(const std::string& value_str) {
125
54
    char* endptr = nullptr;
126
54
    errno = 0;
127
54
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
54
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
54
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
54
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
52
    return true;
139
54
}
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
11
bool valid_signed_number(const std::string& value_str) {
125
11
    char* endptr = nullptr;
126
11
    errno = 0;
127
11
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
11
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
11
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
2
        return false;
132
2
    }
133
134
9
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
0
        return false;
136
0
    }
137
138
9
    return true;
139
9
}
140
141
template <>
142
bool valid_signed_number<int128_t>(const std::string& value_str);
143
144
// 检查uint8_t, uint16_t, uint32_t, uint64_t的值是否溢出
145
template <typename T>
146
0
bool valid_unsigned_number(const std::string& value_str) {
147
0
    if (value_str[0] == '-') {
148
0
        return false;
149
0
    }
150
151
0
    char* endptr = nullptr;
152
0
    errno = 0;
153
0
    uint64_t value = strtoul(value_str.c_str(), &endptr, 10);
154
155
0
    if ((errno == ERANGE && (value == ULONG_MAX)) || (errno != 0 && value == 0) ||
156
0
        endptr == value_str || *endptr != '\0') {
157
0
        return false;
158
0
    }
159
160
0
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
161
0
        return false;
162
0
    }
163
164
0
    return true;
165
0
}
Unexecuted instantiation: _ZN5doris21valid_unsigned_numberIhEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris21valid_unsigned_numberItEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris21valid_unsigned_numberIjEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Unexecuted instantiation: _ZN5doris21valid_unsigned_numberImEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
166
167
bool valid_decimal(const std::string& value_str, const uint32_t precision, const uint32_t frac);
168
169
// Validate for date/datetime roughly. The format is 'yyyy-MM-dd HH:mm:ss'
170
// TODO: support 'yyyy-MM-dd HH:mm:ss.SSS'
171
bool valid_datetime(const std::string& value_str, const uint32_t scale);
172
173
bool valid_bool(const std::string& value_str);
174
175
bool valid_ipv4(const std::string& value_str);
176
177
bool valid_ipv6(const std::string& value_str);
178
179
208
constexpr bool is_string_type(const FieldType& field_type) {
180
208
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
181
208
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
182
208
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
183
208
}
184
185
148
constexpr bool is_numeric_type(const FieldType& field_type) {
186
148
    return field_type == FieldType::OLAP_FIELD_TYPE_INT ||
187
148
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT ||
188
148
           field_type == FieldType::OLAP_FIELD_TYPE_BIGINT ||
189
148
           field_type == FieldType::OLAP_FIELD_TYPE_SMALLINT ||
190
148
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT ||
191
148
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT ||
192
148
           field_type == FieldType::OLAP_FIELD_TYPE_TINYINT ||
193
148
           field_type == FieldType::OLAP_FIELD_TYPE_DOUBLE ||
194
148
           field_type == FieldType::OLAP_FIELD_TYPE_FLOAT ||
195
148
           field_type == FieldType::OLAP_FIELD_TYPE_DATE ||
196
148
           field_type == FieldType::OLAP_FIELD_TYPE_DATEV2 ||
197
148
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIME ||
198
148
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2 ||
199
148
           field_type == FieldType::OLAP_FIELD_TYPE_LARGEINT ||
200
148
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL ||
201
148
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL32 ||
202
148
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL64 ||
203
148
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL128I ||
204
148
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL256 ||
205
148
           field_type == FieldType::OLAP_FIELD_TYPE_BOOL ||
206
148
           field_type == FieldType::OLAP_FIELD_TYPE_IPV4 ||
207
148
           field_type == FieldType::OLAP_FIELD_TYPE_IPV6;
208
148
}
209
210
// Util used to get string name of thrift enum item
211
#define EnumToString(enum_type, index, out)                   \
212
2.30k
    do {                                                      \
213
2.30k
        auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \
214
2.30k
        if (it == _##enum_type##_VALUES_TO_NAMES.end()) {     \
215
0
            out = "NULL";                                     \
216
2.30k
        } else {                                              \
217
2.30k
            out = it->second;                                 \
218
2.30k
        }                                                     \
219
2.30k
    } while (0)
220
221
struct RowLocation {
222
2.36M
    RowLocation() : segment_id(0), row_id(0) {}
223
6.66M
    RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {}
224
    RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid)
225
8.10M
            : rowset_id(rsid), segment_id(sid), row_id(rid) {}
226
    RowsetId rowset_id;
227
    uint32_t segment_id;
228
    uint32_t row_id;
229
230
0
    bool operator==(const RowLocation& rhs) const {
231
0
        return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id;
232
0
    }
233
234
0
    bool operator<(const RowLocation& rhs) const {
235
0
        if (rowset_id != rhs.rowset_id) {
236
0
            return rowset_id < rhs.rowset_id;
237
0
        } else if (segment_id != rhs.segment_id) {
238
0
            return segment_id < rhs.segment_id;
239
0
        } else {
240
0
            return row_id < rhs.row_id;
241
0
        }
242
0
    }
243
};
244
using RowLocationSet = std::set<RowLocation>;
245
using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>;
246
247
struct GlobalRowLoacation {
248
    GlobalRowLoacation(int64_t tid, RowsetId rsid, uint32_t sid, uint32_t rid)
249
0
            : tablet_id(tid), row_location(rsid, sid, rid) {}
250
    int64_t tablet_id;
251
    RowLocation row_location;
252
253
0
    bool operator==(const GlobalRowLoacation& rhs) const {
254
0
        return tablet_id == rhs.tablet_id && row_location == rhs.row_location;
255
0
    }
256
257
0
    bool operator<(const GlobalRowLoacation& rhs) const {
258
0
        if (tablet_id != rhs.tablet_id) {
259
0
            return tablet_id < rhs.tablet_id;
260
0
        } else {
261
0
            return row_location < rhs.row_location;
262
0
        }
263
0
    }
264
};
265
266
} // namespace doris