Coverage Report

Created: 2026-08-02 13:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/olap_common.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
#include <gen_cpp/Types_types.h>
21
#include <netinet/in.h>
22
23
#include <atomic>
24
#include <charconv>
25
#include <cstdint>
26
#include <functional>
27
#include <list>
28
#include <map>
29
#include <memory>
30
#include <ostream>
31
#include <sstream>
32
#include <string>
33
#include <typeinfo>
34
#include <unordered_map>
35
#include <unordered_set>
36
#include <utility>
37
38
#include "common/cast_set.h"
39
#include "common/config.h"
40
#include "common/exception.h"
41
#include "io/io_common.h"
42
#include "storage/index/inverted/inverted_index_stats.h"
43
#include "storage/olap_define.h"
44
#include "storage/rowset/rowset_fwd.h"
45
#include "util/hash_util.hpp"
46
#include "util/time.h"
47
#include "util/uid_util.h"
48
49
namespace doris {
50
static constexpr int64_t MAX_ROWSET_ID = 1L << 56;
51
static constexpr int64_t LOW_56_BITS = 0x00ffffffffffffff;
52
53
using SchemaHash = int32_t;
54
using int128_t = __int128;
55
using uint128_t = unsigned __int128;
56
57
using TabletUid = UniqueId;
58
59
enum CompactionType {
60
    BASE_COMPACTION = 1,
61
    CUMULATIVE_COMPACTION = 2,
62
    FULL_COMPACTION = 3,
63
    BINLOG_COMPACTION = 4
64
};
65
66
enum DataDirType {
67
    SPILL_DISK_DIR,
68
    OLAP_DATA_DIR,
69
    DATA_CACHE_DIR,
70
};
71
72
struct DataDirInfo {
73
    std::string path;
74
    size_t path_hash = 0;
75
    int64_t disk_capacity = 1; // actual disk capacity
76
    int64_t available = 0;     // available space, in bytes unit
77
    int64_t local_used_capacity = 0;
78
    int64_t remote_used_capacity = 0;
79
    int64_t trash_used_capacity = 0;
80
    bool is_used = false;                                      // whether available mark
81
    TStorageMedium::type storage_medium = TStorageMedium::HDD; // Storage medium type: SSD|HDD
82
    DataDirType data_dir_type = DataDirType::OLAP_DATA_DIR;
83
    std::string metric_name;
84
};
85
86
// Sort DataDirInfo by available space.
87
struct DataDirInfoLessAvailability {
88
7
    bool operator()(const DataDirInfo& left, const DataDirInfo& right) const {
89
7
        return left.available < right.available;
90
7
    }
91
};
92
93
struct TabletInfo {
94
    TabletInfo(TTabletId in_tablet_id, UniqueId in_uid)
95
843
            : tablet_id(in_tablet_id), tablet_uid(in_uid) {}
96
97
4.77k
    bool operator<(const TabletInfo& right) const {
98
4.77k
        if (tablet_id != right.tablet_id) {
99
3.94k
            return tablet_id < right.tablet_id;
100
3.94k
        } else {
101
824
            return tablet_uid < right.tablet_uid;
102
824
        }
103
4.77k
    }
104
105
44
    std::string to_string() const {
106
44
        std::stringstream ss;
107
44
        ss << tablet_id << "." << tablet_uid.to_string();
108
44
        return ss.str();
109
44
    }
110
111
    TTabletId tablet_id;
112
    UniqueId tablet_uid;
113
};
114
115
struct TabletSize {
116
    TabletSize(TTabletId in_tablet_id, size_t in_tablet_size)
117
0
            : tablet_id(in_tablet_id), tablet_size(in_tablet_size) {}
118
119
    TTabletId tablet_id;
120
    size_t tablet_size;
121
};
122
123
// Storage-engine cell types, used by TabletColumn / KeyCoder and the
124
// data_type traits chain. When adding a new value, also extend CppTypeTraits,
125
// FieldTypeTraits and the field_type_size() switch in storage/types.h. Decide how it maps to
126
// PrimitiveType and explicitly define its behavior in primitive_type_to_storage_field_type() and
127
// storage_field_type_to_primitive_type(), either by providing a mapping or by throwing.
128
enum class FieldType {
129
    OLAP_FIELD_TYPE_TINYINT = 1, // MYSQL_TYPE_TINY
130
    OLAP_FIELD_TYPE_UNSIGNED_TINYINT = 2,
131
    OLAP_FIELD_TYPE_SMALLINT = 3, // MYSQL_TYPE_SHORT
132
    OLAP_FIELD_TYPE_UNSIGNED_SMALLINT = 4,
133
    OLAP_FIELD_TYPE_INT = 5, // MYSQL_TYPE_LONG
134
    OLAP_FIELD_TYPE_UNSIGNED_INT = 6,
135
    OLAP_FIELD_TYPE_BIGINT = 7, // MYSQL_TYPE_LONGLONG
136
    OLAP_FIELD_TYPE_UNSIGNED_BIGINT = 8,
137
    OLAP_FIELD_TYPE_LARGEINT = 9,
138
    OLAP_FIELD_TYPE_FLOAT = 10,  // MYSQL_TYPE_FLOAT
139
    OLAP_FIELD_TYPE_DOUBLE = 11, // MYSQL_TYPE_DOUBLE
140
    OLAP_FIELD_TYPE_DISCRETE_DOUBLE = 12,
141
    OLAP_FIELD_TYPE_CHAR = 13,     // MYSQL_TYPE_STRING
142
    OLAP_FIELD_TYPE_DATE = 14,     // MySQL_TYPE_NEWDATE
143
    OLAP_FIELD_TYPE_DATETIME = 15, // MySQL_TYPE_DATETIME
144
    OLAP_FIELD_TYPE_DECIMAL = 16,  // DECIMAL, using different store format against MySQL
145
    OLAP_FIELD_TYPE_VARCHAR = 17,
146
147
    OLAP_FIELD_TYPE_STRUCT = 18,  // Struct
148
    OLAP_FIELD_TYPE_ARRAY = 19,   // ARRAY
149
    OLAP_FIELD_TYPE_MAP = 20,     // Map
150
    OLAP_FIELD_TYPE_UNKNOWN = 21, // UNKNOW OLAP_FIELD_TYPE_STRING
151
    OLAP_FIELD_TYPE_NONE = 22,
152
    OLAP_FIELD_TYPE_HLL = 23,
153
    OLAP_FIELD_TYPE_BOOL = 24,
154
    OLAP_FIELD_TYPE_BITMAP = 25,
155
    OLAP_FIELD_TYPE_STRING = 26,
156
    OLAP_FIELD_TYPE_QUANTILE_STATE = 27,
157
    OLAP_FIELD_TYPE_DATEV2 = 28,
158
    OLAP_FIELD_TYPE_DATETIMEV2 = 29,
159
    OLAP_FIELD_TYPE_TIMEV2 = 30,
160
    OLAP_FIELD_TYPE_DECIMAL32 = 31,
161
    OLAP_FIELD_TYPE_DECIMAL64 = 32,
162
    OLAP_FIELD_TYPE_DECIMAL128I = 33,
163
    OLAP_FIELD_TYPE_JSONB = 34,
164
    OLAP_FIELD_TYPE_VARIANT = 35,
165
    OLAP_FIELD_TYPE_AGG_STATE = 36,
166
    OLAP_FIELD_TYPE_DECIMAL256 = 37,
167
    OLAP_FIELD_TYPE_IPV4 = 38,
168
    OLAP_FIELD_TYPE_IPV6 = 39,
169
    OLAP_FIELD_TYPE_TIMESTAMPTZ = 40,
170
    OLAP_FIELD_TYPE_DATETIMEV2_NANO = 41,
171
};
172
173
// Define all aggregation methods supported by TabletColumn
174
// Note that in practice, not all types can use all the following aggregation methods
175
// For example, it is meaningless to use SUM for the string type (but it will not cause the program to crash)
176
// The implementation of the TabletColumn class does not perform such checks, and should be constrained when creating the table
177
enum class FieldAggregationMethod {
178
    OLAP_FIELD_AGGREGATION_NONE = 0,
179
    OLAP_FIELD_AGGREGATION_SUM = 1,
180
    OLAP_FIELD_AGGREGATION_MIN = 2,
181
    OLAP_FIELD_AGGREGATION_MAX = 3,
182
    OLAP_FIELD_AGGREGATION_REPLACE = 4,
183
    OLAP_FIELD_AGGREGATION_HLL_UNION = 5,
184
    OLAP_FIELD_AGGREGATION_UNKNOWN = 6,
185
    OLAP_FIELD_AGGREGATION_BITMAP_UNION = 7,
186
    // Replace if and only if added value is not null
187
    OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL = 8,
188
    OLAP_FIELD_AGGREGATION_QUANTILE_UNION = 9,
189
    OLAP_FIELD_AGGREGATION_GENERIC = 10
190
};
191
192
enum class PushType {
193
    PUSH_NORMAL = 1,          // for broker/hadoop load, not used any more
194
    PUSH_FOR_DELETE = 2,      // for delete
195
    PUSH_FOR_LOAD_DELETE = 3, // not used any more
196
    PUSH_NORMAL_V2 = 4,       // for spark load
197
};
198
199
372k
constexpr bool field_is_slice_type(const FieldType& field_type) {
200
372k
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
201
372k
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
202
372k
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
203
372k
}
204
205
6.42k
constexpr bool field_is_decimal_type(const FieldType& field_type) {
206
6.42k
    return field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL ||
207
6.42k
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL32 ||
208
6.42k
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL64 ||
209
6.42k
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL128I ||
210
6.42k
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL256;
211
6.42k
}
212
213
470
constexpr bool field_is_numeric_type(const FieldType& field_type) {
214
470
    return field_type == FieldType::OLAP_FIELD_TYPE_INT ||
215
470
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT ||
216
470
           field_type == FieldType::OLAP_FIELD_TYPE_BIGINT ||
217
470
           field_type == FieldType::OLAP_FIELD_TYPE_SMALLINT ||
218
470
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT ||
219
470
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT ||
220
470
           field_type == FieldType::OLAP_FIELD_TYPE_TINYINT ||
221
470
           field_type == FieldType::OLAP_FIELD_TYPE_DOUBLE ||
222
470
           field_type == FieldType::OLAP_FIELD_TYPE_FLOAT ||
223
470
           field_type == FieldType::OLAP_FIELD_TYPE_DATE ||
224
470
           field_type == FieldType::OLAP_FIELD_TYPE_DATEV2 ||
225
470
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIME ||
226
470
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2 ||
227
470
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2_NANO ||
228
470
           field_type == FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ ||
229
470
           field_type == FieldType::OLAP_FIELD_TYPE_LARGEINT ||
230
470
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL ||
231
470
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL32 ||
232
470
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL64 ||
233
470
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL128I ||
234
470
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL256 ||
235
470
           field_type == FieldType::OLAP_FIELD_TYPE_BOOL ||
236
470
           field_type == FieldType::OLAP_FIELD_TYPE_IPV4 ||
237
470
           field_type == FieldType::OLAP_FIELD_TYPE_IPV6;
238
470
}
239
240
// <start_version_id, end_version_id>, such as <100, 110>
241
//using Version = std::pair<TupleVersion, TupleVersion>;
242
243
struct Version {
244
    int64_t first;
245
    int64_t second;
246
247
2.23M
    Version(int64_t first_, int64_t second_) : first(first_), second(second_) {}
248
5.65k
    Version() : first(0), second(0) {}
249
250
0
    static Version mock() {
251
        // Every time SchemaChange is used for external rowing, some temporary versions (such as 999, 1000, 1001) will be written, in order to avoid Cache conflicts, temporary
252
        // The version number takes a BIG NUMBER plus the version number of the current SchemaChange
253
0
        return Version(1 << 28, 1 << 29);
254
0
    }
255
256
    friend std::ostream& operator<<(std::ostream& os, const Version& version);
257
258
500
    bool operator!=(const Version& rhs) const { return first != rhs.first || second != rhs.second; }
259
260
504k
    bool operator==(const Version& rhs) const { return first == rhs.first && second == rhs.second; }
261
262
168k
    bool contains(const Version& other) const {
263
168k
        return first <= other.first && second >= other.second;
264
168k
    }
265
266
1.86k
    std::string to_string() const { return fmt::format("[{}-{}]", first, second); }
267
};
268
269
struct TsoRange : public Version {
270
5.51k
    TsoRange() : Version(-1, -1) {}
271
1.56k
    TsoRange(int64_t start_tso, int64_t end_tso) : Version(start_tso, end_tso) {}
272
273
156
    int64_t start_tso() const { return first; }
274
4.94k
    int64_t end_tso() const { return second; }
275
276
0
    bool contains(const TsoRange& other) const { return Version::contains(other); }
277
};
278
279
using Versions = std::vector<Version>;
280
281
680
inline std::ostream& operator<<(std::ostream& os, const Version& version) {
282
680
    return os << version.to_string();
283
680
}
284
285
0
inline std::ostream& operator<<(std::ostream& os, const Versions& versions) {
286
0
    for (auto& version : versions) {
287
0
        os << version;
288
0
    }
289
0
    return os;
290
0
}
291
292
// used for hash-struct of hash_map<Version, Rowset*>.
293
struct HashOfVersion {
294
29.3k
    size_t operator()(const Version& version) const {
295
29.3k
        size_t seed = 0;
296
29.3k
        seed = HashUtil::hash64(&version.first, sizeof(version.first), seed);
297
29.3k
        seed = HashUtil::hash64(&version.second, sizeof(version.second), seed);
298
29.3k
        return seed;
299
29.3k
    }
300
};
301
302
// It is used to represent Graph vertex.
303
struct Vertex {
304
    int64_t value = 0;
305
    std::list<int64_t> edges;
306
307
10.2k
    Vertex(int64_t v) : value(v) {}
308
};
309
310
// ReaderStatistics used to collect statistics when scan data from storage
311
struct OlapReaderStatistics {
312
    int64_t io_ns = 0;
313
    int64_t compressed_bytes_read = 0;
314
315
    int64_t decompress_ns = 0;
316
    int64_t uncompressed_bytes_read = 0;
317
318
    // total read bytes in memory
319
    int64_t bytes_read = 0;
320
321
    int64_t block_fetch_ns = 0; // time of rowset reader's `next_batch()` call
322
    int64_t block_load_ns = 0;
323
    int64_t blocks_load = 0;
324
    // Not used any more, will be removed after non-vectorized code is removed
325
    int64_t block_seek_num = 0;
326
    // Not used any more, will be removed after non-vectorized code is removed
327
    int64_t block_seek_ns = 0;
328
329
    // block_load_ns
330
    //      block_init_ns
331
    //          block_init_seek_ns
332
    //          generate_row_ranges_ns
333
    //      predicate_column_read_ns
334
    //          predicate_column_read_seek_ns
335
    //      lazy_read_ns
336
    //          block_lazy_read_seek_ns
337
    int64_t block_init_ns = 0;
338
    int64_t block_init_seek_num = 0;
339
    int64_t block_init_seek_ns = 0;
340
    int64_t predicate_column_read_ns = 0;
341
    int64_t non_predicate_read_ns = 0;
342
    int64_t predicate_column_read_seek_num = 0;
343
    int64_t predicate_column_read_seek_ns = 0;
344
    int64_t lazy_read_ns = 0;
345
    int64_t block_lazy_read_seek_num = 0;
346
    int64_t block_lazy_read_seek_ns = 0;
347
    int64_t lazy_read_pruned_ns = 0;
348
349
    int64_t raw_rows_read = 0;
350
351
    int64_t rows_vec_cond_filtered = 0;
352
    int64_t rows_short_circuit_cond_filtered = 0;
353
    int64_t rows_expr_cond_filtered = 0;
354
    int64_t vec_cond_input_rows = 0;
355
    int64_t short_circuit_cond_input_rows = 0;
356
    int64_t expr_cond_input_rows = 0;
357
    int64_t rows_vec_del_cond_filtered = 0;
358
    int64_t vec_cond_ns = 0;
359
    int64_t short_cond_ns = 0;
360
    int64_t expr_filter_ns = 0;
361
    int64_t output_col_ns = 0;
362
    int64_t rows_key_range_filtered = 0;
363
    int64_t rows_stats_filtered = 0;
364
    int64_t rows_stats_rp_filtered = 0;
365
    int64_t expr_zonemap_filtered_segments = 0;
366
    int64_t expr_zonemap_filtered_pages = 0;
367
    int64_t expr_zonemap_unusable_evals = 0;
368
    int64_t in_zonemap_point_check_count = 0;
369
    int64_t in_zonemap_range_only_count = 0;
370
    int64_t rows_bf_filtered = 0;
371
    int64_t segment_dict_filtered = 0;
372
    // Including the number of rows filtered out according to the Delete information in the Tablet,
373
    // and the number of rows filtered for marked deleted rows under the unique key model.
374
    // This metric is mainly used to record the number of rows filtered by the delete condition in Segment V1,
375
    // and it is also used to record the replaced rows in the Unique key model in the "Reader" class.
376
    // In segmentv2, if you want to get all filtered rows, you need the sum of "rows_del_filtered" and "rows_conditions_filtered".
377
    int64_t rows_del_filtered = 0;
378
    int64_t rows_del_by_bitmap = 0;
379
    // the number of rows filtered by various column indexes.
380
    int64_t rows_conditions_filtered = 0;
381
    int64_t generate_row_ranges_by_keys_ns = 0;
382
    int64_t generate_row_ranges_by_column_conditions_ns = 0;
383
    int64_t generate_row_ranges_by_bf_ns = 0;
384
    int64_t generate_row_ranges_by_zonemap_ns = 0;
385
    int64_t generate_row_ranges_by_dict_ns = 0;
386
387
    int64_t index_load_ns = 0;
388
389
    int64_t total_pages_num = 0;
390
    int64_t cached_pages_num = 0;
391
392
    int64_t rows_inverted_index_filtered = 0;
393
    int64_t inverted_index_filter_timer = 0;
394
    int64_t inverted_index_query_timer = 0;
395
    int64_t inverted_index_query_cache_hit = 0;
396
    int64_t inverted_index_query_cache_miss = 0;
397
    int64_t inverted_index_query_null_bitmap_timer = 0;
398
    int64_t inverted_index_query_bitmap_copy_timer = 0;
399
    int64_t inverted_index_searcher_open_timer = 0;
400
    int64_t inverted_index_searcher_search_timer = 0;
401
    int64_t inverted_index_searcher_search_init_timer = 0;
402
    int64_t inverted_index_searcher_search_exec_timer = 0;
403
    int64_t inverted_index_searcher_cache_hit = 0;
404
    int64_t inverted_index_searcher_cache_miss = 0;
405
    int64_t inverted_index_downgrade_count = 0;
406
    int64_t inverted_index_analyzer_timer = 0;
407
    int64_t inverted_index_lookup_timer = 0;
408
    InvertedIndexStatistics inverted_index_stats;
409
410
    int64_t ann_index_load_ns = 0;
411
    int64_t ann_topn_search_ns = 0;
412
    int64_t ann_index_topn_search_cnt = 0;
413
    int64_t ann_ivf_on_disk_load_ns = 0;
414
    int64_t ann_ivf_on_disk_cache_hit_cnt = 0;
415
    int64_t ann_ivf_on_disk_cache_miss_cnt = 0;
416
    int64_t ann_index_cache_hits = 0;
417
418
    // Detailed timing for ANN operations
419
    int64_t ann_index_topn_engine_search_ns = 0;  // time spent in engine for range search
420
    int64_t ann_index_topn_result_process_ns = 0; // time spent processing TopN results
421
    int64_t ann_index_topn_engine_convert_ns = 0; // time spent on FAISS-side conversions (TopN)
422
    int64_t ann_index_topn_engine_prepare_ns =
423
            0; // time spent preparing before engine search (TopN)
424
    int64_t rows_ann_index_topn_filtered = 0;
425
426
    int64_t ann_index_range_search_ns = 0;
427
    int64_t ann_index_range_search_cnt = 0;
428
    // Detailed timing for ANN Range search
429
    int64_t ann_range_engine_search_ns = 0; // time spent in engine for range search
430
    int64_t ann_range_pre_process_ns = 0;   // time spent preparing before engine search
431
432
    int64_t ann_range_result_convert_ns = 0; // time spent processing range results
433
    int64_t ann_range_engine_convert_ns = 0; // time spent on FAISS-side conversions (Range)
434
    int64_t rows_ann_index_range_filtered = 0;
435
    int64_t ann_index_range_cache_hits = 0;
436
    int64_t ann_fall_back_brute_force_cnt = 0;
437
    int64_t ann_topn_fallback_by_small_candidate_cnt = 0;
438
    int64_t ann_topn_fallback_small_candidate_rows = 0;
439
    int64_t ann_range_fallback_by_small_candidate_cnt = 0;
440
    int64_t ann_range_fallback_small_candidate_rows = 0;
441
442
    int64_t output_index_result_column_timer = 0;
443
    // number of segment filtered by column stat when creating seg iterator
444
    int64_t filtered_segment_number = 0;
445
    // number of segment with condition cache hit
446
    int64_t condition_cache_hit_seg_nums = 0;
447
    // number of rows filtered by condition cache hit
448
    int64_t condition_cache_filtered_rows = 0;
449
    // total number of segment
450
    int64_t total_segment_number = 0;
451
452
    io::FileCacheStatistics file_cache_stats;
453
    int64_t load_segments_timer = 0;
454
455
    int64_t collect_iterator_merge_next_timer = 0;
456
    int64_t collect_iterator_normal_next_timer = 0;
457
    int64_t delete_bitmap_get_agg_ns = 0;
458
459
    int64_t tablet_reader_init_timer_ns = 0;
460
    int64_t tablet_reader_capture_rs_readers_timer_ns = 0;
461
    int64_t tablet_reader_init_return_columns_timer_ns = 0;
462
    int64_t tablet_reader_init_keys_param_timer_ns = 0;
463
    int64_t tablet_reader_init_orderby_keys_param_timer_ns = 0;
464
    int64_t tablet_reader_init_conditions_param_timer_ns = 0;
465
    int64_t tablet_reader_init_delete_condition_param_timer_ns = 0;
466
    int64_t block_reader_vcollect_iter_init_timer_ns = 0;
467
    int64_t block_reader_rs_readers_init_timer_ns = 0;
468
    int64_t block_reader_build_heap_init_timer_ns = 0;
469
470
    int64_t rowset_reader_get_segment_iterators_timer_ns = 0;
471
    int64_t rowset_reader_create_iterators_timer_ns = 0;
472
    int64_t rowset_reader_init_iterators_timer_ns = 0;
473
    int64_t rowset_reader_load_segments_timer_ns = 0;
474
475
    int64_t segment_iterator_init_timer_ns = 0;
476
    int64_t segment_iterator_init_return_column_iterators_timer_ns = 0;
477
    int64_t segment_iterator_init_index_iterators_timer_ns = 0;
478
    int64_t segment_iterator_init_segment_prefetchers_timer_ns = 0;
479
480
    int64_t segment_create_column_readers_timer_ns = 0;
481
    int64_t segment_load_index_timer_ns = 0;
482
483
    int64_t adaptive_batch_size_predict_min_rows = INT64_MAX;
484
    int64_t adaptive_batch_size_predict_max_rows = 0;
485
486
    int64_t variant_scan_sparse_column_timer_ns = 0;
487
    int64_t variant_scan_sparse_column_bytes = 0;
488
    int64_t variant_fill_path_from_sparse_column_timer_ns = 0;
489
    int64_t variant_subtree_default_iter_count = 0;
490
    int64_t variant_subtree_leaf_iter_count = 0;
491
    int64_t variant_subtree_hierarchical_iter_count = 0;
492
    int64_t variant_subtree_sparse_iter_count = 0;
493
    int64_t variant_doc_value_column_iter_count = 0;
494
};
495
496
using ColumnId = uint32_t;
497
// Column unique id set
498
using UniqueIdSet = std::set<uint32_t>;
499
// Column unique Id -> column id map
500
using UniqueIdToColumnIdMap = std::map<ColumnId, ColumnId>;
501
502
// 8 bit rowset id version
503
// 56 bit, inc number from 1
504
// 128 bit backend uid, it is a uuid bit, id version
505
struct RowsetId {
506
    int8_t version = 0;
507
    int64_t hi = 0;
508
    int64_t mi = 0;
509
    int64_t lo = 0;
510
511
5.96k
    void init(std::string_view rowset_id_str) {
512
        // for new rowsetid its a 48 hex string
513
        // if the len < 48, then it is an old format rowset id
514
5.96k
        if (rowset_id_str.length() < 48) [[unlikely]] {
515
149
            int64_t high;
516
149
            auto [_, ec] = std::from_chars(rowset_id_str.data(),
517
149
                                           rowset_id_str.data() + rowset_id_str.length(), high);
518
149
            if (ec != std::errc {}) [[unlikely]] {
519
1
                if (config::force_regenerate_rowsetid_on_start_error) {
520
1
                    LOG(WARNING) << "failed to init rowset id: " << rowset_id_str;
521
1
                    high = MAX_ROWSET_ID - 1;
522
1
                } else {
523
0
                    throw Exception(
524
0
                            Status::FatalError("failed to init rowset id: {}", rowset_id_str));
525
0
                }
526
1
            }
527
149
            init(1, high, 0, 0);
528
5.81k
        } else {
529
5.81k
            int64_t high = 0;
530
5.81k
            int64_t middle = 0;
531
5.81k
            int64_t low = 0;
532
5.81k
            from_hex(&high, rowset_id_str.substr(0, 16));
533
5.81k
            from_hex(&middle, rowset_id_str.substr(16, 16));
534
5.81k
            from_hex(&low, rowset_id_str.substr(32, 16));
535
5.81k
            init(high >> 56, high & LOW_56_BITS, middle, low);
536
5.81k
        }
537
5.96k
    }
538
539
    // to compatible with old version
540
4.44k
    void init(int64_t rowset_id) { init(1, rowset_id, 0, 0); }
541
542
22.8k
    void init(int64_t id_version, int64_t high, int64_t middle, int64_t low) {
543
22.8k
        version = cast_set<int8_t>(id_version);
544
22.8k
        if (UNLIKELY(high >= MAX_ROWSET_ID)) {
545
0
            throw Exception(Status::FatalError("inc rowsetid is too large:{}", high));
546
0
        }
547
22.8k
        hi = (id_version << 56) + (high & LOW_56_BITS);
548
22.8k
        mi = middle;
549
22.8k
        lo = low;
550
22.8k
    }
551
552
36.0k
    std::string to_string() const {
553
36.0k
        if (version < 2) {
554
18.2k
            return std::to_string(hi & LOW_56_BITS);
555
18.2k
        } else {
556
17.8k
            char buf[48];
557
17.8k
            to_hex(hi, buf);
558
17.8k
            to_hex(mi, buf + 16);
559
17.8k
            to_hex(lo, buf + 32);
560
17.8k
            return {buf, 48};
561
17.8k
        }
562
36.0k
    }
563
564
    // std::unordered_map need this api
565
169k
    bool operator==(const RowsetId& rhs) const {
566
169k
        return hi == rhs.hi && mi == rhs.mi && lo == rhs.lo;
567
169k
    }
568
569
261
    bool operator!=(const RowsetId& rhs) const {
570
261
        return hi != rhs.hi || mi != rhs.mi || lo != rhs.lo;
571
261
    }
572
573
45.0M
    bool operator<(const RowsetId& rhs) const {
574
45.0M
        if (hi != rhs.hi) {
575
4.58M
            return hi < rhs.hi;
576
40.4M
        } else if (mi != rhs.mi) {
577
0
            return mi < rhs.mi;
578
40.4M
        } else {
579
40.4M
            return lo < rhs.lo;
580
40.4M
        }
581
45.0M
    }
582
583
2.07k
    friend std::ostream& operator<<(std::ostream& out, const RowsetId& rowset_id) {
584
2.07k
        out << rowset_id.to_string();
585
2.07k
        return out;
586
2.07k
    }
587
};
588
589
using RowsetIdUnorderedSet = std::unordered_set<RowsetId>;
590
591
// Extract rowset id from filename, return uninitialized rowset id if filename is invalid
592
483
inline RowsetId extract_rowset_id(std::string_view filename) {
593
483
    RowsetId rowset_id;
594
483
    if (filename.ends_with(".dat")) {
595
        // filename format: {rowset_id}_{segment_num}.dat
596
243
        auto end = filename.find('_');
597
243
        if (end == std::string::npos) {
598
0
            return rowset_id;
599
0
        }
600
243
        rowset_id.init(filename.substr(0, end));
601
243
        return rowset_id;
602
243
    }
603
240
    if (filename.ends_with(".idx")) {
604
        // filename format: {rowset_id}_{segment_num}_{index_id}.idx
605
240
        auto end = filename.find('_');
606
240
        if (end == std::string::npos) {
607
0
            return rowset_id;
608
0
        }
609
240
        rowset_id.init(filename.substr(0, end));
610
240
        return rowset_id;
611
240
    }
612
0
    return rowset_id;
613
240
}
614
615
class DeleteBitmap;
616
// merge on write context
617
struct MowContext {
618
    MowContext(int64_t version, int64_t txnid, std::shared_ptr<RowsetIdUnorderedSet> ids,
619
               std::vector<RowsetSharedPtr> rowset_ptrs, std::shared_ptr<DeleteBitmap> db)
620
10
            : max_version(version),
621
10
              txn_id(txnid),
622
10
              rowset_ids(std::move(ids)),
623
10
              rowset_ptrs(std::move(rowset_ptrs)),
624
10
              delete_bitmap(std::move(db)) {}
625
    int64_t max_version;
626
    int64_t txn_id;
627
    std::shared_ptr<RowsetIdUnorderedSet> rowset_ids;
628
    std::vector<RowsetSharedPtr> rowset_ptrs;
629
    std::shared_ptr<DeleteBitmap> delete_bitmap;
630
};
631
632
// used for controll compaction
633
struct VersionWithTime {
634
    std::atomic<int64_t> version;
635
    int64_t update_ts;
636
637
62
    VersionWithTime() : version(0), update_ts(MonotonicMillis()) {}
638
639
0
    void update_version_monoto(int64_t new_version) {
640
0
        int64_t cur_version = version.load(std::memory_order_relaxed);
641
0
        while (cur_version < new_version) {
642
0
            if (version.compare_exchange_strong(cur_version, new_version, std::memory_order_relaxed,
643
0
                                                std::memory_order_relaxed)) {
644
0
                update_ts = MonotonicMillis();
645
0
                break;
646
0
            }
647
0
        }
648
0
    }
649
};
650
} // namespace doris
651
652
// This intended to be a "good" hash function.  It may change from time to time.
653
template <>
654
struct std::hash<doris::RowsetId> {
655
3.84k
    size_t operator()(const doris::RowsetId& rowset_id) const {
656
3.84k
        size_t seed = 0;
657
3.84k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.hi, sizeof(rowset_id.hi),
658
3.84k
                                                 seed);
659
3.84k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.mi, sizeof(rowset_id.mi),
660
3.84k
                                                 seed);
661
3.84k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.lo, sizeof(rowset_id.lo),
662
3.84k
                                                 seed);
663
3.84k
        return seed;
664
3.84k
    }
665
};