Coverage Report

Created: 2026-07-14 21:58

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