Coverage Report

Created: 2026-07-27 11:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "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
215k
    uint64_t get_elapse_time_us() const {
51
215k
        struct timeval now;
52
215k
        gettimeofday(&now, nullptr);
53
215k
        return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 +
54
215k
                          (now.tv_usec - _begin_time.tv_usec));
55
215k
    }
56
57
422
    double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; }
58
59
127k
    void reset() { gettimeofday(&_begin_time, nullptr); }
60
61
127k
    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
321k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
73
321k
    if (!result) {
74
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
75
0
    }
76
77
    // 处理base为空的情况
78
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
79
321k
    if (base.size() == 0) {
80
0
        result->push_back("");
81
0
        return Status::OK();
82
0
    }
83
84
321k
    size_t offset = 0;
85
963k
    while (offset < base.length()) {
86
963k
        size_t next = base.find(separator, offset);
87
963k
        if (next == std::string::npos) {
88
321k
            result->emplace_back(base.substr(offset));
89
321k
            break;
90
642k
        } else {
91
642k
            result->emplace_back(base.substr(offset, next - offset));
92
642k
            offset = next + 1;
93
642k
        }
94
963k
    }
95
96
321k
    return Status::OK();
97
321k
}
_ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE
Line
Count
Source
72
320k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
73
320k
    if (!result) {
74
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
75
0
    }
76
77
    // 处理base为空的情况
78
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
79
320k
    if (base.size() == 0) {
80
0
        result->push_back("");
81
0
        return Status::OK();
82
0
    }
83
84
320k
    size_t offset = 0;
85
962k
    while (offset < base.length()) {
86
962k
        size_t next = base.find(separator, offset);
87
962k
        if (next == std::string::npos) {
88
320k
            result->emplace_back(base.substr(offset));
89
320k
            break;
90
641k
        } else {
91
641k
            result->emplace_back(base.substr(offset, next - offset));
92
641k
            offset = next + 1;
93
641k
        }
94
962k
    }
95
96
320k
    return Status::OK();
97
320k
}
_ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE
Line
Count
Source
72
360
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
73
360
    if (!result) {
74
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
75
0
    }
76
77
    // 处理base为空的情况
78
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
79
360
    if (base.size() == 0) {
80
0
        result->push_back("");
81
0
        return Status::OK();
82
0
    }
83
84
360
    size_t offset = 0;
85
720
    while (offset < base.length()) {
86
720
        size_t next = base.find(separator, offset);
87
720
        if (next == std::string::npos) {
88
360
            result->emplace_back(base.substr(offset));
89
360
            break;
90
360
        } else {
91
360
            result->emplace_back(base.substr(offset, next - offset));
92
360
            offset = next + 1;
93
360
        }
94
720
    }
95
96
360
    return Status::OK();
97
360
}
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
889
bool valid_signed_number(const std::string& value_str) {
125
889
    char* endptr = nullptr;
126
889
    errno = 0;
127
889
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
889
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
893
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
2
        return false;
132
2
    }
133
134
889
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
6
        return false;
136
6
    }
137
138
881
    return true;
139
887
}
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
26
bool valid_signed_number(const std::string& value_str) {
125
26
    char* endptr = nullptr;
126
26
    errno = 0;
127
26
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
26
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
26
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
26
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
24
    return true;
139
26
}
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
17
bool valid_signed_number(const std::string& value_str) {
125
17
    char* endptr = nullptr;
126
17
    errno = 0;
127
17
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
17
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
17
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
17
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
15
    return true;
139
17
}
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
785
bool valid_signed_number(const std::string& value_str) {
125
785
    char* endptr = nullptr;
126
785
    errno = 0;
127
785
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
785
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
790
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
0
        return false;
132
0
    }
133
134
789
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
2
        return false;
136
2
    }
137
138
783
    return true;
139
785
}
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
124
61
bool valid_signed_number(const std::string& value_str) {
125
61
    char* endptr = nullptr;
126
61
    errno = 0;
127
61
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
128
129
61
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
130
61
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
131
2
        return false;
132
2
    }
133
134
59
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
135
0
        return false;
136
0
    }
137
138
59
    return true;
139
59
}
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
77.9k
constexpr bool is_string_type(const FieldType& field_type) {
180
77.9k
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
181
77.9k
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
182
77.9k
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
183
77.9k
}
184
185
// Util used to get string name of thrift enum item
186
#define EnumToString(enum_type, index, out)                   \
187
36.6M
    do {                                                      \
188
36.6M
        auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \
189
36.6M
        if (it == _##enum_type##_VALUES_TO_NAMES.end()) {     \
190
0
            out = "NULL";                                     \
191
36.6M
        } else {                                              \
192
36.6M
            out = it->second;                                 \
193
36.6M
        }                                                     \
194
36.6M
    } while (0)
195
196
struct RowLocation {
197
26.1M
    RowLocation() : segment_id(0), row_id(0) {}
198
9.90M
    RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {}
199
    RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid)
200
11.3M
            : rowset_id(rsid), segment_id(sid), row_id(rid) {}
201
    RowsetId rowset_id;
202
    uint32_t segment_id;
203
    uint32_t row_id;
204
205
0
    bool operator==(const RowLocation& rhs) const {
206
0
        return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id;
207
0
    }
208
209
377
    bool operator<(const RowLocation& rhs) const {
210
377
        if (rowset_id != rhs.rowset_id) {
211
43
            return rowset_id < rhs.rowset_id;
212
334
        } else if (segment_id != rhs.segment_id) {
213
0
            return segment_id < rhs.segment_id;
214
334
        } else {
215
334
            return row_id < rhs.row_id;
216
334
        }
217
377
    }
218
};
219
using RowLocationSet = std::set<RowLocation>;
220
using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>;
221
222
struct GlobalRowLoacation {
223
    GlobalRowLoacation(int64_t tid, RowsetId rsid, uint32_t sid, uint32_t rid)
224
93
            : tablet_id(tid), row_location(rsid, sid, rid) {}
225
    int64_t tablet_id;
226
    RowLocation row_location;
227
228
0
    bool operator==(const GlobalRowLoacation& rhs) const {
229
0
        return tablet_id == rhs.tablet_id && row_location == rhs.row_location;
230
0
    }
231
232
160
    bool operator<(const GlobalRowLoacation& rhs) const {
233
160
        if (tablet_id != rhs.tablet_id) {
234
0
            return tablet_id < rhs.tablet_id;
235
160
        } else {
236
160
            return row_location < rhs.row_location;
237
160
        }
238
160
    }
239
};
240
241
struct GlobalRowLoacationV2 {
242
    GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid)
243
25.3M
            : version(ver), backend_id(bid), file_id(fid), row_id(rid) {}
244
    uint8_t version;
245
    int64_t backend_id;
246
    uint32_t file_id;
247
    uint32_t row_id;
248
249
    auto operator<=>(const GlobalRowLoacationV2&) const = default;
250
};
251
252
} // namespace doris