Coverage Report

Created: 2026-08-13 12:11

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