Coverage Report

Created: 2026-10-27 19:19

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 <cstddef>
27
#include <cstdio>
28
#include <cstdlib>
29
#include <iterator>
30
#include <limits>
31
#include <string>
32
#include <vector>
33
34
#include "common/status.h"
35
#include "storage/olap_common.h"
36
37
namespace doris {
38
static const std::string DELETE_SIGN = "__DORIS_DELETE_SIGN__";
39
static const std::string WHERE_SIGN = "__DORIS_WHERE_SIGN__";
40
static const std::string VERSION_COL = "__DORIS_VERSION_COL__";
41
static const std::string SKIP_BITMAP_COL = "__DORIS_SKIP_BITMAP_COL__";
42
static const std::string SEQUENCE_COL = "__DORIS_SEQUENCE_COL__";
43
static const std::string COMMIT_TSO_COL = "__DORIS_COMMIT_TSO_COL__";
44
static const std::string ROW_LSN_COL = "__DORIS_ROW_LSN_COL__";
45
// ATTN(changelog-rename): formerly "__DORIS_BINLOG_TSO__"
46
static const std::string CHANGELOG_TSO_COL = "__DORIS_CHANGELOG_TSO__";
47
// ATTN(changelog-rename): formerly "__DORIS_BINLOG_LSN__"
48
static const std::string CHANGELOG_LSN_COL = "__DORIS_CHANGELOG_LSN__";
49
// ATTN(changelog-rename): formerly "__DORIS_BINLOG_OP__"
50
static const std::string CHANGELOG_OP_COL = "__DORIS_CHANGELOG_OP__";
51
52
// 用来加速运算
53
const static int32_t g_power_table[] = {1,      10,      100,      1000,      10000,
54
                                        100000, 1000000, 10000000, 100000000, 1000000000};
55
56
// 计时工具,用于确定一段代码执行的时间,用于性能调优
57
class OlapStopWatch {
58
public:
59
224k
    uint64_t get_elapse_time_us() const {
60
224k
        struct timeval now;
61
224k
        gettimeofday(&now, nullptr);
62
224k
        return (uint64_t)((now.tv_sec - _begin_time.tv_sec) * 1e6 +
63
224k
                          (now.tv_usec - _begin_time.tv_usec));
64
224k
    }
65
66
373
    double get_elapse_second() const { return get_elapse_time_us() / 1000000.0; }
67
68
120k
    void reset() { gettimeofday(&_begin_time, nullptr); }
69
70
120k
    OlapStopWatch() { reset(); }
71
72
private:
73
    struct timeval _begin_time; // 起始时间戳
74
};
75
76
// @brief 切分字符串
77
// @param base 原串
78
// @param separator 分隔符
79
// @param result 切分结果
80
template <typename Str, typename T>
81
302k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
82
302k
    if (!result) {
83
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
84
0
    }
85
86
    // 处理base为空的情况
87
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
88
302k
    if (base.size() == 0) {
89
0
        result->push_back("");
90
0
        return Status::OK();
91
0
    }
92
93
302k
    size_t offset = 0;
94
903k
    while (offset < base.length()) {
95
903k
        size_t next = base.find(separator, offset);
96
903k
        if (next == std::string::npos) {
97
302k
            result->emplace_back(base.substr(offset));
98
302k
            break;
99
601k
        } else {
100
601k
            result->emplace_back(base.substr(offset, next - offset));
101
601k
            offset = next + 1;
102
601k
        }
103
903k
    }
104
105
302k
    return Status::OK();
106
302k
}
_ZN5doris12split_stringISt17basic_string_viewIcSt11char_traitsIcEEcEENS_6StatusERKT_T0_PSt6vectorINSt7__cxx1112basic_stringIcS3_SaIcEEESaISE_EE
Line
Count
Source
81
299k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
82
299k
    if (!result) {
83
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
84
0
    }
85
86
    // 处理base为空的情况
87
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
88
299k
    if (base.size() == 0) {
89
0
        result->push_back("");
90
0
        return Status::OK();
91
0
    }
92
93
299k
    size_t offset = 0;
94
898k
    while (offset < base.length()) {
95
898k
        size_t next = base.find(separator, offset);
96
898k
        if (next == std::string::npos) {
97
299k
            result->emplace_back(base.substr(offset));
98
299k
            break;
99
598k
        } else {
100
598k
            result->emplace_back(base.substr(offset, next - offset));
101
598k
            offset = next + 1;
102
598k
        }
103
898k
    }
104
105
299k
    return Status::OK();
106
299k
}
_ZN5doris12split_stringINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEcEENS_6StatusERKT_T0_PSt6vectorIS6_SaIS6_EE
Line
Count
Source
81
2.66k
Status split_string(const Str& base, const T separator, std::vector<std::string>* result) {
82
2.66k
    if (!result) {
83
0
        return Status::Error<ErrorCode::INVALID_ARGUMENT>("split_string meet nullptr result input");
84
0
    }
85
86
    // 处理base为空的情况
87
    // 在删除功能中,当varchar类型列的过滤条件为空时,会出现这种情况
88
2.66k
    if (base.size() == 0) {
89
0
        result->push_back("");
90
0
        return Status::OK();
91
0
    }
92
93
2.66k
    size_t offset = 0;
94
5.32k
    while (offset < base.length()) {
95
5.32k
        size_t next = base.find(separator, offset);
96
5.32k
        if (next == std::string::npos) {
97
2.66k
            result->emplace_back(base.substr(offset));
98
2.66k
            break;
99
2.66k
        } else {
100
2.66k
            result->emplace_back(base.substr(offset, next - offset));
101
2.66k
            offset = next + 1;
102
2.66k
        }
103
5.32k
    }
104
105
2.66k
    return Status::OK();
106
2.66k
}
107
108
uint32_t olap_adler32_init();
109
uint32_t olap_adler32(uint32_t adler, const char* buf, size_t len);
110
111
// 获取系统当前时间,并将时间转换为字符串
112
Status gen_timestamp_string(std::string* out_string);
113
114
Status check_datapath_rw(const std::string& path);
115
116
Status read_write_test_file(const std::string& test_file_path);
117
118
// 打印Errno
119
class Errno {
120
public:
121
    // 返回Errno对应的错误信息,线程安全
122
    static const char* str();
123
    static const char* str(int no);
124
    static int no();
125
126
private:
127
    static const int BUF_SIZE = 256;
128
    static __thread char _buf[BUF_SIZE];
129
};
130
131
// 检查int8_t, int16_t, int32_t, int64_t的值是否溢出
132
template <typename T>
133
919
bool valid_signed_number(const std::string& value_str) {
134
919
    char* endptr = nullptr;
135
919
    errno = 0;
136
919
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
137
138
919
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
139
919
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
140
2
        return false;
141
2
    }
142
143
917
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
144
6
        return false;
145
6
    }
146
147
911
    return true;
148
917
}
_ZN5doris19valid_signed_numberIlEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
133
65
bool valid_signed_number(const std::string& value_str) {
134
65
    char* endptr = nullptr;
135
65
    errno = 0;
136
65
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
137
138
65
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
139
65
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
140
2
        return false;
141
2
    }
142
143
63
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
144
0
        return false;
145
0
    }
146
147
63
    return true;
148
63
}
_ZN5doris19valid_signed_numberIaEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
133
25
bool valid_signed_number(const std::string& value_str) {
134
25
    char* endptr = nullptr;
135
25
    errno = 0;
136
25
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
137
138
25
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
139
25
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
140
0
        return false;
141
0
    }
142
143
25
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
144
2
        return false;
145
2
    }
146
147
23
    return true;
148
25
}
_ZN5doris19valid_signed_numberIsEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
133
17
bool valid_signed_number(const std::string& value_str) {
134
17
    char* endptr = nullptr;
135
17
    errno = 0;
136
17
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
137
138
17
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
139
17
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
140
0
        return false;
141
0
    }
142
143
17
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
144
2
        return false;
145
2
    }
146
147
15
    return true;
148
17
}
_ZN5doris19valid_signed_numberIiEEbRKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
133
812
bool valid_signed_number(const std::string& value_str) {
134
812
    char* endptr = nullptr;
135
812
    errno = 0;
136
812
    int64_t value = strtol(value_str.c_str(), &endptr, 10);
137
138
812
    if ((errno == ERANGE && (value == LONG_MAX || value == LONG_MIN)) ||
139
814
        (errno != 0 && value == 0) || endptr == value_str || *endptr != '\0') {
140
0
        return false;
141
0
    }
142
143
813
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
144
2
        return false;
145
2
    }
146
147
810
    return true;
148
812
}
149
150
template <>
151
bool valid_signed_number<int128_t>(const std::string& value_str);
152
153
// 检查uint8_t, uint16_t, uint32_t, uint64_t的值是否溢出
154
template <typename T>
155
0
bool valid_unsigned_number(const std::string& value_str) {
156
0
    if (value_str[0] == '-') {
157
0
        return false;
158
0
    }
159
160
0
    char* endptr = nullptr;
161
0
    errno = 0;
162
0
    uint64_t value = strtoul(value_str.c_str(), &endptr, 10);
163
164
0
    if ((errno == ERANGE && (value == ULONG_MAX)) || (errno != 0 && value == 0) ||
165
0
        endptr == value_str || *endptr != '\0') {
166
0
        return false;
167
0
    }
168
169
0
    if (value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max()) {
170
0
        return false;
171
0
    }
172
173
0
    return true;
174
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
175
176
bool valid_decimal(const std::string& value_str, const uint32_t precision, const uint32_t frac);
177
178
// Validate date/datetime format and fractional-second scale roughly.
179
bool valid_datetime(const std::string& value_str, const uint32_t scale);
180
181
bool valid_bool(const std::string& value_str);
182
183
bool valid_ipv4(const std::string& value_str);
184
185
bool valid_ipv6(const std::string& value_str);
186
187
bool valid_uuid(const std::string& value_str);
188
189
30.2M
constexpr bool is_string_type(const FieldType& field_type) {
190
30.2M
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
191
30.2M
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
192
30.2M
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
193
30.2M
}
194
195
// Util used to get string name of thrift enum item
196
#define EnumToString(enum_type, index, out)                   \
197
36.7M
    do {                                                      \
198
36.7M
        auto it = _##enum_type##_VALUES_TO_NAMES.find(index); \
199
36.7M
        if (it == _##enum_type##_VALUES_TO_NAMES.end()) {     \
200
0
            out = "NULL";                                     \
201
36.7M
        } else {                                              \
202
36.7M
            out = it->second;                                 \
203
36.7M
        }                                                     \
204
36.7M
    } while (0)
205
206
struct RowLocation {
207
15.1M
    RowLocation() : segment_id(0), row_id(0) {}
208
0
    RowLocation(uint32_t sid, uint32_t rid) : segment_id(sid), row_id(rid) {}
209
    RowLocation(RowsetId rsid, uint32_t sid, uint32_t rid)
210
12.3M
            : rowset_id(rsid), segment_id(sid), row_id(rid) {}
211
    RowsetId rowset_id;
212
    uint32_t segment_id;
213
    uint32_t row_id;
214
215
0
    bool operator==(const RowLocation& rhs) const {
216
0
        return rowset_id == rhs.rowset_id && segment_id == rhs.segment_id && row_id == rhs.row_id;
217
0
    }
218
219
1.75k
    bool operator<(const RowLocation& rhs) const {
220
1.75k
        if (rowset_id != rhs.rowset_id) {
221
88
            return rowset_id < rhs.rowset_id;
222
1.66k
        } else if (segment_id != rhs.segment_id) {
223
0
            return segment_id < rhs.segment_id;
224
1.66k
        } else {
225
1.66k
            return row_id < rhs.row_id;
226
1.66k
        }
227
1.75k
    }
228
};
229
using RowLocationSet = std::set<RowLocation>;
230
using RowLocationPairList = std::list<std::pair<RowLocation, RowLocation>>;
231
232
// Wire-protocol values: never reorder or reuse an existing value. A new value may use a new
233
// encoded structure and size, provided its decoder keeps supporting all older values.
234
enum class ROW_VERSION : uint8_t {
235
    // The row ID is a uint32 ordinal local to the FileMapping identified by file_id.
236
    FILE_LOCAL_ROW_ID = 0,
237
    // The row ID is an opaque uint64 ID in a fixed Lance dataset snapshot.
238
    LANCE_DATASET_ROW_ID = 1,
239
};
240
241
/*
242
 * A serialized global row location has a fixed size of 24 bytes. The version determines how the
243
 * bytes at offsets 4..7 and 16..23 must be interpreted:
244
 *
245
 * FILE_LOCAL_ROW_ID (version = 0), used by Doris, Parquet, and ORC:
246
 *
247
 *   byte offset   0        1..7             8..15          16..19      20..23
248
 *               +--------+----------------+---------------+-----------+-----------+
249
 *               | ver=0  | reserved       | backend_id    | file_id   | row_id    |
250
 *               +--------+----------------+---------------+-----------+-----------+
251
 *                 uint8      7 bytes          int64          uint32      uint32
252
 *
253
 *   row_id is an ordinal local to the FileMapping selected by file_id.
254
 *
255
 * LANCE_DATASET_ROW_ID (version = 1), used by Lance:
256
 *
257
 *   byte offset   0        1..3       4..7          8..15          16..23
258
 *               +--------+----------+-------------+---------------+----------------+
259
 *               | ver=1  | reserved | file_id     | backend_id    | lance_row_id   |
260
 *               +--------+----------+-------------+---------------+----------------+
261
 *                 uint8     3 bytes     uint32        int64           uint64
262
 *
263
 *   lance_row_id is an opaque row ID in the fixed dataset snapshot recorded by the FileMapping.
264
 *
265
 * The first union reuses four bytes that are padding in version 0 as Lance's file_id in version 1.
266
 * The second union reuses the original {uint32 file_id, uint32 row_id} payload as one uint64 Lance
267
 * row ID. Therefore, always check version before reading either union.
268
 */
269
struct GlobalRowLoacationV2 {
270
    static constexpr uint8_t VERSION = static_cast<uint8_t>(ROW_VERSION::FILE_LOCAL_ROW_ID);
271
272
    struct FileLocalRowId {
273
        uint32_t file_id;
274
        uint32_t row_id;
275
    };
276
277
    GlobalRowLoacationV2(uint8_t ver, uint64_t bid, uint32_t fid, uint32_t rid)
278
26.4M
            : version(ver),
279
26.4M
              reserved_for_file_local(0),
280
26.4M
              backend_id(bid),
281
26.4M
              file_local {.file_id = fid, .row_id = rid} {}
282
    GlobalRowLoacationV2(ROW_VERSION ver, uint64_t bid, uint32_t fid, uint64_t rid)
283
21
            : version(static_cast<uint8_t>(ver)),
284
21
              lance_file_id(fid),
285
21
              backend_id(bid),
286
21
              lance_row_id(rid) {}
287
288
    uint8_t version;
289
    uint8_t reserved_before_file_id[3] {};
290
    union {
291
        // version 0: offsets 4..7 remain reserved, preserving the original V2 layout.
292
        uint32_t reserved_for_file_local;
293
        // version 1: offsets 4..7 identify the FileMapping for lance_row_id.
294
        uint32_t lance_file_id;
295
    };
296
    int64_t backend_id;
297
    union {
298
        // version 0: file_id is at offset 16 and its uint32 row ordinal is at offset 20.
299
        FileLocalRowId file_local;
300
        // version 1: offsets 16..23 are one opaque uint64 Lance row ID.
301
        uint64_t lance_row_id;
302
    };
303
};
304
305
static_assert(sizeof(GlobalRowLoacationV2) == 24);
306
static_assert(sizeof(GlobalRowLoacationV2::FileLocalRowId) == 8);
307
static_assert(offsetof(GlobalRowLoacationV2, version) == 0);
308
static_assert(offsetof(GlobalRowLoacationV2, reserved_before_file_id) == 1);
309
static_assert(offsetof(GlobalRowLoacationV2, reserved_for_file_local) == 4);
310
static_assert(offsetof(GlobalRowLoacationV2, lance_file_id) == 4);
311
static_assert(offsetof(GlobalRowLoacationV2, backend_id) == 8);
312
static_assert(offsetof(GlobalRowLoacationV2, file_local) == 16);
313
static_assert(offsetof(GlobalRowLoacationV2::FileLocalRowId, file_id) == 0);
314
static_assert(offsetof(GlobalRowLoacationV2::FileLocalRowId, row_id) == 4);
315
static_assert(offsetof(GlobalRowLoacationV2, lance_row_id) == 16);
316
317
} // namespace doris