Coverage Report

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