Coverage Report

Created: 2026-08-21 18:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/utils.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
// 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 "storage/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
static const std::string COMMIT_TSO_COL = "__DORIS_COMMIT_TSO_COL__";
43
static const std::string ROW_LSN_COL = "__DORIS_ROW_LSN_COL__";
44
static const std::string BINLOG_TSO_COL = "__DORIS_BINLOG_TSO__";
45
static const std::string BINLOG_LSN_COL = "__DORIS_BINLOG_LSN__";
46
static const std::string BINLOG_OP_COL = "__DORIS_BINLOG_OP__";
47
48
// 用来加速运算
49
const static int32_t g_power_table[] = {1,      10,      100,      1000,      10000,
50
                                        100000, 1000000, 10000000, 100000000, 1000000000};
51
52
// 计时工具,用于确定一段代码执行的时间,用于性能调优
53
class OlapStopWatch {
54
public:
55
183
    uint64_t get_elapse_time_us() const {
56
183
        struct timeval now;
57
183
        gettimeofday(&now, nullptr);
58
183
        return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 +
59
183
                          (now.tv_usec - _begin_time.tv_usec));
60
183
    }
61
62
101
    double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; }
63
64
149
    void reset() { gettimeofday(&_begin_time, nullptr); }
65
66
149
    OlapStopWatch() { reset(); }
67
68
private:
69
    struct timeval _begin_time; // 起始时间戳
70
};
71
72
// @brief 切分字符串
73
// @param base 原串
74
// @param separator 分隔符
75
// @param result 切分结果
76
template <typename Str, typename T>
77
7
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
78
7
    if (!result) {
79
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
80
0
    }
81
82
    // 处理base为空的情况
83
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
84
7
    if (base.size() == 0) {
85
0
        result->push_back("");
86
0
        return Status::OK();
87
0
    }
88
89
7
    size_t offset = 0;
90
21
    while (offset < base.length()) {
91
21
        size_t next = base.find(separator, offset);
92
21
        if (next == std::string::npos) {
93
7
            result->emplace_back(base.substr(offset));
94
7
            break;
95
14
        } else {
96
14
            result->emplace_back(base.substr(offset, next - offset));
97
14
            offset = next + 1;
98
14
        }
99
21
    }
100
101
7
    return Status::OK();
102
7
}
_ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE
Line
Count
Source
77
7
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
78
7
    if (!result) {
79
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
80
0
    }
81
82
    // 处理base为空的情况
83
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
84
7
    if (base.size() == 0) {
85
0
        result->push_back("");
86
0
        return Status::OK();
87
0
    }
88
89
7
    size_t offset = 0;
90
21
    while (offset < base.length()) {
91
21
        size_t next = base.find(separator, offset);
92
21
        if (next == std::string::npos) {
93
7
            result->emplace_back(base.substr(offset));
94
7
            break;
95
14
        } else {
96
14
            result->emplace_back(base.substr(offset, next - offset));
97
14
            offset = next + 1;
98
14
        }
99
21
    }
100
101
7
    return Status::OK();
102
7
}
Unexecuted instantiation: _ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE
103
104
uint32_t olap_adler32_init();
105
uint32_t olap_adler32(uint32_t adler, const char* buf, size_t len);
106
107
// 获取系统当前时间,并将时间转换为字符串
108
Status gen_timestamp_string(std::string* out_string);
109
110
Status check_datapath_rw(const std::string& path);
111
112
Status read_write_test_file(const std::string& test_file_path);
113
114
// 打印Errno
115
class Errno {
116
public:
117
    // 返回Errno对应的错误信息,线程安全
118
    static const char* str();
119
    static const char* str(int no);
120
    static int no();
121
122
private:
123
    static const int BUF_SIZE = 256;
124
    static __thread char _buf[BUF_SIZE];
125
};
126
127
// 检查int8_t, int16_t, int32_t, int64_t的值是否溢出
128
template <typename T>
129
89
bool valid_signed_number(const std::string& value_str) {
130
89
    char* endptr = nullptr;
131
89
    errno = 0;
132
89
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
89
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
89
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
2
        return false;
137
2
    }
138
139
87
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
6
        return false;
141
6
    }
142
143
81
    return true;
144
87
}
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
11
bool valid_signed_number(const std::string& value_str) {
130
11
    char* endptr = nullptr;
131
11
    errno = 0;
132
11
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
11
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
11
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
2
        return false;
137
2
    }
138
139
9
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
0
        return false;
141
0
    }
142
143
9
    return true;
144
9
}
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
11
bool valid_signed_number(const std::string& value_str) {
130
11
    char* endptr = nullptr;
131
11
    errno = 0;
132
11
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
11
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
11
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
11
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
9
    return true;
144
11
}
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
13
bool valid_signed_number(const std::string& value_str) {
130
13
    char* endptr = nullptr;
131
13
    errno = 0;
132
13
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
13
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
13
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
13
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
11
    return true;
144
13
}
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
54
bool valid_signed_number(const std::string& value_str) {
130
54
    char* endptr = nullptr;
131
54
    errno = 0;
132
54
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
54
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
54
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
54
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
52
    return true;
144
54
}
145
146
template <>
147
bool valid_signed_number<int128_t>(const std::string& value_str);
148
149
// 检查uint8_t, uint16_t, uint32_t, uint64_t的值是否溢出
150
template <typename T>
151
0
bool valid_unsigned_number(const std::string& value_str) {
152
0
    if (value_str[0] == '-') {
153
0
        return false;
154
0
    }
155
156
0
    char* endptr = nullptr;
157
0
    errno = 0;
158
0
    uint64_t value = strtoul(value_str.c_str(), &endptr, 10);
159
160
0
    if ((errno == ERANGE && (value == ULONG_MAX)) || (errno != 0 && value == 0) ||
161
0
        endptr == value_str || *endptr != '\0') {
162
0
        return false;
163
0
    }
164
165
0
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
166
0
        return false;
167
0
    }
168
169
0
    return true;
170
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
171
172
bool valid_decimal(const std::string& value_str, const uint32_t precision, const uint32_t frac);
173
174
// Validate for date/datetime roughly. The format is 'yyyy-MM-dd HH:mm:ss'
175
// TODO: support 'yyyy-MM-dd HH:mm:ss.SSS'
176
bool valid_datetime(const std::string& value_str, const uint32_t scale);
177
178
bool valid_bool(const std::string& value_str);
179
180
bool valid_ipv4(const std::string& value_str);
181
182
bool valid_ipv6(const std::string& value_str);
183
184
15.6k
constexpr bool is_string_type(const FieldType& field_type) {
185
15.6k
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
186
15.6k
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
187
15.6k
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
188
15.6k
}
189
190
// Util used to get string name of thrift enum item
191
#define EnumToString(enum_type, index, out)                   \
192
3.62k
    do {                                                      \
193
3.62k
        auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \
194
3.62k
        if (it == _##enum_type##_VALUES_TO_NAMES.end()) {     \
195
0
            out = "NULL";                                     \
196
3.62k
        } else {                                              \
197
3.62k
            out = it->second;                                 \
198
3.62k
        }                                                     \
199
3.62k
    } while (0)
200
201
struct RowLocation {
202
6.22M
    RowLocation() : segment_id(0), row_id(0) {}
203
6.71M
    RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {}
204
    RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid)
205
8.15M
            : rowset_id(rsid), segment_id(sid), row_id(rid) {}
206
    RowsetId rowset_id;
207
    uint32_t segment_id;
208
    uint32_t row_id;
209
210
0
    bool operator==(const RowLocation& rhs) const {
211
0
        return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id;
212
0
    }
213
214
0
    bool operator<(const RowLocation& rhs) const {
215
0
        if (rowset_id != rhs.rowset_id) {
216
0
            return rowset_id < rhs.rowset_id;
217
0
        } else if (segment_id != rhs.segment_id) {
218
0
            return segment_id < rhs.segment_id;
219
0
        } else {
220
0
            return row_id < rhs.row_id;
221
0
        }
222
0
    }
223
};
224
using RowLocationSet = std::set<RowLocation>;
225
using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>;
226
227
struct GlobalRowLoacationV2 {
228
    GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid)
229
68
            : version(ver), backend_id(bid), file_id(fid), row_id(rid) {}
230
    uint8_t version;
231
    int64_t backend_id;
232
    uint32_t file_id;
233
    uint32_t row_id;
234
235
    auto operator<=>(const GlobalRowLoacationV2&) const = default;
236
};
237
238
} // namespace doris