Coverage Report

Created: 2026-08-07 00:28

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