Coverage Report

Created: 2026-07-23 18:52

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 TTL_COL = "__DORIS_TTL_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
437k
    uint64_t get_elapse_time_us() const {
56
437k
        struct timeval now;
57
437k
        gettimeofday(&now, nullptr);
58
437k
        return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 +
59
437k
                          (now.tv_usec - _begin_time.tv_usec));
60
437k
    }
61
62
8.35k
    double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; }
63
64
198k
    void reset() { gettimeofday(&_begin_time, nullptr); }
65
66
198k
    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
436k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
78
436k
    if (!result) {
79
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
80
0
    }
81
82
    // 处理base为空的情况
83
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
84
436k
    if (base.size() == 0) {
85
0
        result->push_back("");
86
0
        return Status::OK();
87
0
    }
88
89
436k
    size_t offset = 0;
90
1.22M
    while (offset < base.length()) {
91
1.22M
        size_t next = base.find(separator, offset);
92
1.22M
        if (next == std::string::npos) {
93
436k
            result->emplace_back(base.substr(offset));
94
436k
            break;
95
792k
        } else {
96
792k
            result->emplace_back(base.substr(offset, next - offset));
97
792k
            offset = next + 1;
98
792k
        }
99
1.22M
    }
100
101
436k
    return Status::OK();
102
436k
}
_ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE
Line
Count
Source
77
356k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
78
356k
    if (!result) {
79
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
80
0
    }
81
82
    // 处理base为空的情况
83
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
84
356k
    if (base.size() == 0) {
85
0
        result->push_back("");
86
0
        return Status::OK();
87
0
    }
88
89
356k
    size_t offset = 0;
90
1.06M
    while (offset < base.length()) {
91
1.06M
        size_t next = base.find(separator, offset);
92
1.06M
        if (next == std::string::npos) {
93
356k
            result->emplace_back(base.substr(offset));
94
356k
            break;
95
712k
        } else {
96
712k
            result->emplace_back(base.substr(offset, next - offset));
97
712k
            offset = next + 1;
98
712k
        }
99
1.06M
    }
100
101
356k
    return Status::OK();
102
356k
}
_ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE
Line
Count
Source
77
79.7k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
78
79.7k
    if (!result) {
79
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
80
0
    }
81
82
    // 处理base为空的情况
83
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
84
79.7k
    if (base.size() == 0) {
85
0
        result->push_back("");
86
0
        return Status::OK();
87
0
    }
88
89
79.7k
    size_t offset = 0;
90
159k
    while (offset < base.length()) {
91
159k
        size_t next = base.find(separator, offset);
92
159k
        if (next == std::string::npos) {
93
79.7k
            result->emplace_back(base.substr(offset));
94
79.7k
            break;
95
79.7k
        } else {
96
79.7k
            result->emplace_back(base.substr(offset, next - offset));
97
79.7k
            offset = next + 1;
98
79.7k
        }
99
159k
    }
100
101
79.7k
    return Status::OK();
102
79.7k
}
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
925
bool valid_signed_number(const std::string& value_str) {
130
925
    char* endptr = nullptr;
131
925
    errno = 0;
132
925
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
925
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
930
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
2
        return false;
137
2
    }
138
139
926
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
6
        return false;
141
6
    }
142
143
917
    return true;
144
923
}
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
25
bool valid_signed_number(const std::string& value_str) {
130
25
    char* endptr = nullptr;
131
25
    errno = 0;
132
25
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
25
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
25
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
25
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
23
    return true;
144
25
}
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
17
bool valid_signed_number(const std::string& value_str) {
130
17
    char* endptr = nullptr;
131
17
    errno = 0;
132
17
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
17
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
17
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
17
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
15
    return true;
144
17
}
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
818
bool valid_signed_number(const std::string& value_str) {
130
818
    char* endptr = nullptr;
131
818
    errno = 0;
132
818
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
818
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
825
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
0
        return false;
137
0
    }
138
139
823
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
2
        return false;
141
2
    }
142
143
816
    return true;
144
818
}
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
129
65
bool valid_signed_number(const std::string& value_str) {
130
65
    char* endptr = nullptr;
131
65
    errno = 0;
132
65
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
133
134
65
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
135
65
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
136
2
        return false;
137
2
    }
138
139
63
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
140
0
        return false;
141
0
    }
142
143
63
    return true;
144
63
}
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
27.8M
constexpr bool is_string_type(const FieldType& field_type) {
185
27.8M
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
186
27.8M
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
187
27.8M
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
188
27.8M
}
189
190
// Util used to get string name of thrift enum item
191
#define EnumToString(enum_type, index, out)                   \
192
45.4M
    do {                                                      \
193
45.4M
        auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \
194
45.4M
        if (it == _##enum_type##_VALUES_TO_NAMES.end()) {     \
195
0
            out = "NULL";                                     \
196
45.4M
        } else {                                              \
197
45.4M
            out = it->second;                                 \
198
45.4M
        }                                                     \
199
45.4M
    } while (0)
200
201
struct RowLocation {
202
36.1M
    RowLocation() : segment_id(0), row_id(0) {}
203
9.89M
    RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {}
204
    RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid)
205
11.3M
            : 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
    bool operator==(const RowLocation& rhs) const {
211
        return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id;
212
    }
213
214
5.31k
    bool operator<(const RowLocation& rhs) const {
215
5.31k
        if (rowset_id != rhs.rowset_id) {
216
1.12k
            return rowset_id < rhs.rowset_id;
217
4.19k
        } else if (segment_id != rhs.segment_id) {
218
0
            return segment_id < rhs.segment_id;
219
4.19k
        } else {
220
4.19k
            return row_id < rhs.row_id;
221
4.19k
        }
222
5.31k
    }
223
};
224
using RowLocationSet = std::set<RowLocation>;
225
using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>;
226
227
struct GlobalRowLoacation {
228
    GlobalRowLoacation(int64_t tid, RowsetId rsid, uint32_t sid, uint32_t rid)
229
0
            : tablet_id(tid), row_location(rsid, sid, rid) {}
230
    int64_t tablet_id;
231
    RowLocation row_location;
232
233
0
    bool operator==(const GlobalRowLoacation& rhs) const {
234
0
        return tablet_id == rhs.tablet_id && row_location == rhs.row_location;
235
0
    }
236
237
0
    bool operator<(const GlobalRowLoacation& rhs) const {
238
0
        if (tablet_id != rhs.tablet_id) {
239
0
            return tablet_id < rhs.tablet_id;
240
0
        } else {
241
0
            return row_location < rhs.row_location;
242
0
        }
243
0
    }
244
};
245
246
struct GlobalRowLoacationV2 {
247
    GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid)
248
41.3M
            : version(ver), backend_id(bid), file_id(fid), row_id(rid) {}
249
    uint8_t version;
250
    int64_t backend_id;
251
    uint32_t file_id;
252
    uint32_t row_id;
253
254
    auto operator<=>(const GlobalRowLoacationV2&) const = default;
255
};
256
257
} // namespace doris