Coverage Report

Created: 2026-10-15 07:33

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 "core/extended_types.h"
42
#include "io/io_common.h"
43
#include "storage/field_type.h"
44
#include "storage/index/inverted/inverted_index_stats.h"
45
#include "storage/index/snii/snii_query_stats.h"
46
#include "storage/olap_define.h"
47
#include "storage/rowset/rowset_fwd.h"
48
#include "storage/rowset_id.h"
49
#include "util/hash_util.hpp"
50
#include "util/time.h"
51
#include "util/uid_util.h"
52
53
namespace doris {
54
namespace io {
55
struct ReadAheadStatistics;
56
}
57
58
using SchemaHash = int32_t;
59
60
using TabletUid = UniqueId;
61
62
enum CompactionType {
63
    BASE_COMPACTION = 1,
64
    CUMULATIVE_COMPACTION = 2,
65
    FULL_COMPACTION = 3,
66
    // Only used by scheduler to route row-binlog tablets to the binlog thread pool.
67
    CUMU_BINLOG_COMPACTION = 4
68
};
69
70
struct CompactionScoreStats {
71
    int64_t max_score = 0;
72
    int64_t size_based_max_score = 0;
73
    int64_t time_series_max_score = 0;
74
    bool scanned = false;
75
};
76
77
enum DataDirType {
78
    SPILL_DISK_DIR,
79
    OLAP_DATA_DIR,
80
    DATA_CACHE_DIR,
81
};
82
83
struct DataDirInfo {
84
    std::string path;
85
    size_t path_hash = 0;
86
    int64_t disk_capacity = 1; // actual disk capacity
87
    int64_t available = 0;     // available space, in bytes unit
88
    int64_t local_used_capacity = 0;
89
    int64_t remote_used_capacity = 0;
90
    int64_t trash_used_capacity = 0;
91
    bool is_used = false;                                      // whether available mark
92
    TStorageMedium::type storage_medium = TStorageMedium::HDD; // Storage medium type: SSD|HDD
93
    DataDirType data_dir_type = DataDirType::OLAP_DATA_DIR;
94
    std::string metric_name;
95
};
96
97
// Sort DataDirInfo by available space.
98
struct DataDirInfoLessAvailability {
99
17
    bool operator()(const DataDirInfo& left, const DataDirInfo& right) const {
100
17
        return left.available < right.available;
101
17
    }
102
};
103
104
struct TabletInfo {
105
    TabletInfo(TTabletId in_tablet_id, UniqueId in_uid)
106
1.26M
            : tablet_id(in_tablet_id), tablet_uid(in_uid) {}
107
108
9.14M
    bool operator<(const TabletInfo& right) const {
109
9.14M
        if (tablet_id != right.tablet_id) {
110
8.98M
            return tablet_id < right.tablet_id;
111
8.98M
        } else {
112
168k
            return tablet_uid < right.tablet_uid;
113
168k
        }
114
9.14M
    }
115
116
69
    std::string to_string() const {
117
69
        std::stringstream ss;
118
69
        ss << tablet_id << "." << tablet_uid.to_string();
119
69
        return ss.str();
120
69
    }
121
122
    TTabletId tablet_id;
123
    UniqueId tablet_uid;
124
};
125
126
struct TabletSize {
127
    TabletSize(TTabletId in_tablet_id, size_t in_tablet_size)
128
0
            : tablet_id(in_tablet_id), tablet_size(in_tablet_size) {}
129
130
    TTabletId tablet_id;
131
    size_t tablet_size;
132
};
133
134
// FieldType moved to storage/field_type.h (included above) so that
135
// data-type headers can name storage cell types without pulling in the
136
// whole of olap_common.h.
137
138
// Define all aggregation methods supported by TabletColumn
139
// Note that in practice, not all types can use all the following aggregation methods
140
// For example, it is meaningless to use SUM for the string type (but it will not cause the program to crash)
141
// The implementation of the TabletColumn class does not perform such checks, and should be constrained when creating the table
142
enum class FieldAggregationMethod {
143
    OLAP_FIELD_AGGREGATION_NONE = 0,
144
    OLAP_FIELD_AGGREGATION_SUM = 1,
145
    OLAP_FIELD_AGGREGATION_MIN = 2,
146
    OLAP_FIELD_AGGREGATION_MAX = 3,
147
    OLAP_FIELD_AGGREGATION_REPLACE = 4,
148
    OLAP_FIELD_AGGREGATION_HLL_UNION = 5,
149
    OLAP_FIELD_AGGREGATION_UNKNOWN = 6,
150
    OLAP_FIELD_AGGREGATION_BITMAP_UNION = 7,
151
    // Replace if and only if added value is not null
152
    OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL = 8,
153
    OLAP_FIELD_AGGREGATION_QUANTILE_UNION = 9,
154
    OLAP_FIELD_AGGREGATION_GENERIC = 10
155
};
156
157
enum class PushType {
158
    PUSH_NORMAL = 1,          // for broker/hadoop load, not used any more
159
    PUSH_FOR_DELETE = 2,      // for delete
160
    PUSH_FOR_LOAD_DELETE = 3, // not used any more
161
    PUSH_NORMAL_V2 = 4,       // for spark load
162
};
163
164
// <start_version_id, end_version_id>, such as <100, 110>
165
//using Version = std::pair<TupleVersion, TupleVersion>;
166
167
struct Version {
168
    int64_t first;
169
    int64_t second;
170
171
11.0M
    Version(int64_t first_, int64_t second_) : first(first_), second(second_) {}
172
1.58M
    Version() : first(0), second(0) {}
173
174
0
    static Version mock() {
175
        // 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
176
        // The version number takes a BIG NUMBER plus the version number of the current SchemaChange
177
0
        return Version(1 << 28, 1 << 29);
178
0
    }
179
180
    friend std::ostream& operator<<(std::ostream& os, const Version& version);
181
182
9.47k
    bool operator!=(const Version& rhs) const { return first != rhs.first || second != rhs.second; }
183
184
2.13M
    bool operator==(const Version& rhs) const { return first == rhs.first && second == rhs.second; }
185
186
736k
    bool contains(const Version& other) const {
187
736k
        return first <= other.first && second >= other.second;
188
736k
    }
189
190
5.29k
    std::string to_string() const { return fmt::format("[{}-{}]", first, second); }
191
};
192
193
struct TsoRange : public Version {
194
8.59k
    TsoRange() : Version(-1, -1) {}
195
229k
    TsoRange(int64_t start_tso, int64_t end_tso) : Version(start_tso, end_tso) {}
196
197
111k
    int64_t start_tso() const { return first; }
198
111k
    int64_t end_tso() const { return second; }
199
200
0
    bool contains(const TsoRange& other) const { return Version::contains(other); }
201
};
202
203
using Versions = std::vector<Version>;
204
205
910
inline std::ostream& operator<<(std::ostream& os, const Version& version) {
206
910
    return os << version.to_string();
207
910
}
208
209
0
inline std::ostream& operator<<(std::ostream& os, const Versions& versions) {
210
0
    for (auto& version : versions) {
211
0
        os << version;
212
0
    }
213
0
    return os;
214
0
}
215
216
// used for hash-struct of hash_map<Version, Rowset*>.
217
struct HashOfVersion {
218
1.75M
    size_t operator()(const Version& version) const {
219
1.75M
        size_t seed = 0;
220
1.75M
        seed = HashUtil::hash64(&version.first, sizeof(version.first), seed);
221
1.75M
        seed = HashUtil::hash64(&version.second, sizeof(version.second), seed);
222
1.75M
        return seed;
223
1.75M
    }
224
};
225
226
// It is used to represent Graph vertex.
227
struct Vertex {
228
    int64_t value = 0;
229
    std::list<int64_t> edges;
230
231
590k
    Vertex(int64_t v) : value(v) {}
232
};
233
234
// ReaderStatistics used to collect statistics when scan data from storage
235
struct OlapReaderStatistics {
236
    int64_t io_ns = 0;
237
    int64_t compressed_bytes_read = 0;
238
239
    int64_t decompress_ns = 0;
240
    int64_t uncompressed_bytes_read = 0;
241
242
    // total read bytes in memory
243
    int64_t bytes_read = 0;
244
245
    int64_t block_fetch_ns = 0; // time of rowset reader's `next_batch()` call
246
    int64_t block_load_ns = 0;
247
    int64_t blocks_load = 0;
248
    // Not used any more, will be removed after non-vectorized code is removed
249
    int64_t block_seek_num = 0;
250
    // Not used any more, will be removed after non-vectorized code is removed
251
    int64_t block_seek_ns = 0;
252
253
    // block_load_ns
254
    //      block_init_ns
255
    //          block_init_seek_ns
256
    //          generate_row_ranges_ns
257
    //      predicate_column_read_ns
258
    //          predicate_column_read_seek_ns
259
    //      lazy_read_ns
260
    //          block_lazy_read_seek_ns
261
    int64_t block_init_ns = 0;
262
    int64_t block_init_seek_num = 0;
263
    int64_t block_init_seek_ns = 0;
264
    int64_t predicate_column_read_ns = 0;
265
    int64_t non_predicate_read_ns = 0;
266
    int64_t predicate_column_read_seek_num = 0;
267
    int64_t predicate_column_read_seek_ns = 0;
268
    int64_t lazy_read_ns = 0;
269
    int64_t block_lazy_read_seek_num = 0;
270
    int64_t block_lazy_read_seek_ns = 0;
271
    int64_t lazy_read_pruned_ns = 0;
272
273
    int64_t raw_rows_read = 0;
274
275
    int64_t rows_vec_cond_filtered = 0;
276
    int64_t rows_short_circuit_cond_filtered = 0;
277
    int64_t rows_expr_cond_filtered = 0;
278
    int64_t vec_cond_input_rows = 0;
279
    int64_t short_circuit_cond_input_rows = 0;
280
    int64_t expr_cond_input_rows = 0;
281
    int64_t rows_vec_del_cond_filtered = 0;
282
    int64_t vec_cond_ns = 0;
283
    int64_t short_cond_ns = 0;
284
    int64_t expr_filter_ns = 0;
285
    int64_t output_col_ns = 0;
286
    int64_t rows_key_range_filtered = 0;
287
    int64_t rows_stats_filtered = 0;
288
    int64_t rows_stats_rp_filtered = 0;
289
    int64_t expr_zonemap_filtered_segments = 0;
290
    int64_t expr_zonemap_filtered_pages = 0;
291
    int64_t expr_zonemap_unusable_evals = 0;
292
    int64_t in_zonemap_point_check_count = 0;
293
    int64_t in_zonemap_range_only_count = 0;
294
    int64_t rows_bf_filtered = 0;
295
    int64_t segment_dict_filtered = 0;
296
    // Including the number of rows filtered out according to the Delete information in the Tablet,
297
    // and the number of rows filtered for marked deleted rows under the unique key model.
298
    // This metric is mainly used to record the number of rows filtered by the delete condition in Segment V1,
299
    // and it is also used to record the replaced rows in the Unique key model in the "Reader" class.
300
    // In segmentv2, if you want to get all filtered rows, you need the sum of "rows_del_filtered" and "rows_conditions_filtered".
301
    int64_t rows_del_filtered = 0;
302
    int64_t rows_del_by_bitmap = 0;
303
    // the number of rows filtered by various column indexes.
304
    int64_t rows_conditions_filtered = 0;
305
    int64_t generate_row_ranges_by_keys_ns = 0;
306
    int64_t generate_row_ranges_by_column_conditions_ns = 0;
307
    int64_t generate_row_ranges_by_bf_ns = 0;
308
    int64_t generate_row_ranges_by_zonemap_ns = 0;
309
    int64_t generate_row_ranges_by_dict_ns = 0;
310
311
    int64_t index_load_ns = 0;
312
313
    int64_t total_pages_num = 0;
314
    int64_t cached_pages_num = 0;
315
316
    int64_t rows_inverted_index_filtered = 0;
317
    int64_t inverted_index_filter_timer = 0;
318
    int64_t inverted_index_query_timer = 0;
319
    int64_t inverted_index_query_cache_hit = 0;
320
    int64_t inverted_index_query_cache_miss = 0;
321
    int64_t inverted_index_query_cache_lookup = 0;
322
    int64_t inverted_index_query_cache_insert = 0;
323
    int64_t inverted_index_query_null_bitmap_timer = 0;
324
    int64_t inverted_index_query_bitmap_copy_timer = 0;
325
    int64_t inverted_index_searcher_open_timer = 0;
326
    int64_t inverted_index_searcher_search_timer = 0;
327
    int64_t inverted_index_searcher_search_init_timer = 0;
328
    int64_t inverted_index_searcher_search_exec_timer = 0;
329
    int64_t inverted_index_searcher_cache_hit = 0;
330
    int64_t inverted_index_searcher_cache_miss = 0;
331
    int64_t inverted_index_downgrade_count = 0;
332
    // Pushed-down conjuncts skipped (never index-evaluated) because the row
333
    // bitmap was already empty when their turn came.
334
    int64_t inverted_index_conjuncts_short_circuited = 0;
335
    // Rows pruned by the approximate (gram) index: rows removed when the candidate bitmap is
336
    // intersected with _row_bitmap.
337
    int64_t rows_gram_index_filtered = 0;
338
    // Candidate rows produced by the approximate (gram) index: rows that still need expression
339
    // re-verification after the intersection.
340
    int64_t gram_index_candidate_rows = 0;
341
    // Query nodes the gram index's cost gate widened to the whole segment rather than read. A
342
    // non-zero count is why the two numbers above can show an index that eliminated nothing: it
343
    // was not consulted for those nodes, by a cost decision rather than because no gram matched.
344
    int64_t gram_index_gate_gave_up = 0;
345
    int64_t inverted_index_analyzer_timer = 0;
346
    int64_t inverted_index_lookup_timer = 0;
347
    // See snii_query_stats.h: one field here instead of one per SNII counter.
348
    snii::SniiQueryStats snii_stats;
349
    InvertedIndexStatistics inverted_index_stats;
350
351
    int64_t ann_index_load_ns = 0;
352
    int64_t ann_topn_search_ns = 0;
353
    int64_t ann_index_topn_search_cnt = 0;
354
    int64_t ann_ivf_on_disk_load_ns = 0;
355
    int64_t ann_ivf_on_disk_cache_hit_cnt = 0;
356
    int64_t ann_ivf_on_disk_cache_miss_cnt = 0;
357
    int64_t ann_index_cache_hits = 0;
358
359
    // Detailed timing for ANN operations
360
    int64_t ann_index_topn_engine_search_ns = 0;  // time spent in engine for range search
361
    int64_t ann_index_topn_result_process_ns = 0; // time spent processing TopN results
362
    int64_t ann_index_topn_engine_convert_ns = 0; // time spent on FAISS-side conversions (TopN)
363
    int64_t ann_index_topn_engine_prepare_ns =
364
            0; // time spent preparing before engine search (TopN)
365
    int64_t rows_ann_index_topn_filtered = 0;
366
367
    int64_t ann_index_range_search_ns = 0;
368
    int64_t ann_index_range_search_cnt = 0;
369
    // Detailed timing for ANN Range search
370
    int64_t ann_range_engine_search_ns = 0; // time spent in engine for range search
371
    int64_t ann_range_pre_process_ns = 0;   // time spent preparing before engine search
372
373
    int64_t ann_range_result_convert_ns = 0; // time spent processing range results
374
    int64_t ann_range_engine_convert_ns = 0; // time spent on FAISS-side conversions (Range)
375
    int64_t rows_ann_index_range_filtered = 0;
376
    int64_t ann_index_range_cache_hits = 0;
377
    int64_t ann_fall_back_brute_force_cnt = 0;
378
    int64_t ann_topn_fallback_by_small_candidate_cnt = 0;
379
    int64_t ann_topn_fallback_small_candidate_rows = 0;
380
    int64_t ann_range_fallback_by_small_candidate_cnt = 0;
381
    int64_t ann_range_fallback_small_candidate_rows = 0;
382
383
    int64_t output_index_result_column_timer = 0;
384
    // number of segment filtered by column stat when creating seg iterator
385
    int64_t filtered_segment_number = 0;
386
    // number of condition cache lookups, including misses
387
    int64_t condition_cache_lookup_count = 0;
388
    // number of segment with condition cache hit
389
    int64_t condition_cache_hit_seg_nums = 0;
390
    // number of rows filtered by condition cache hit
391
    int64_t condition_cache_filtered_rows = 0;
392
    // total number of segment
393
    int64_t total_segment_number = 0;
394
395
    io::FileCacheStatistics file_cache_stats;
396
    // Shared with range tasks so asynchronous completion never writes into this reader's lifetime.
397
    std::shared_ptr<io::ReadAheadStatistics> read_ahead_stats;
398
    int64_t load_segments_timer = 0;
399
400
    int64_t collect_iterator_merge_next_timer = 0;
401
    int64_t collect_iterator_normal_next_timer = 0;
402
    int64_t delete_bitmap_get_agg_ns = 0;
403
404
    int64_t tablet_reader_init_timer_ns = 0;
405
    int64_t tablet_reader_capture_rs_readers_timer_ns = 0;
406
    int64_t tablet_reader_init_keys_param_timer_ns = 0;
407
    int64_t tablet_reader_init_orderby_keys_param_timer_ns = 0;
408
    int64_t tablet_reader_init_conditions_param_timer_ns = 0;
409
    int64_t tablet_reader_init_delete_condition_param_timer_ns = 0;
410
    int64_t block_reader_vcollect_iter_init_timer_ns = 0;
411
    int64_t block_reader_rs_readers_init_timer_ns = 0;
412
    int64_t block_reader_build_heap_init_timer_ns = 0;
413
414
    int64_t rowset_reader_get_segment_iterators_timer_ns = 0;
415
    int64_t rowset_reader_create_iterators_timer_ns = 0;
416
    int64_t rowset_reader_init_iterators_timer_ns = 0;
417
    int64_t rowset_reader_load_segments_timer_ns = 0;
418
419
    int64_t segment_iterator_init_timer_ns = 0;
420
    int64_t segment_iterator_init_column_iterators_timer_ns = 0;
421
    int64_t segment_iterator_init_index_iterators_timer_ns = 0;
422
    int64_t segment_iterator_init_segment_prefetchers_timer_ns = 0;
423
424
    int64_t segment_create_column_readers_timer_ns = 0;
425
    int64_t segment_load_index_timer_ns = 0;
426
427
    int64_t adaptive_batch_size_predict_min_rows = INT64_MAX;
428
    int64_t adaptive_batch_size_predict_max_rows = 0;
429
430
    int64_t variant_scan_sparse_column_timer_ns = 0;
431
    int64_t variant_scan_sparse_column_bytes = 0;
432
    int64_t variant_fill_path_from_sparse_column_timer_ns = 0;
433
    int64_t variant_subtree_default_iter_count = 0;
434
    int64_t variant_subtree_leaf_iter_count = 0;
435
    int64_t variant_subtree_hierarchical_iter_count = 0;
436
    int64_t variant_subtree_sparse_iter_count = 0;
437
    int64_t variant_doc_value_column_iter_count = 0;
438
};
439
440
using ColumnId = uint32_t;
441
// Column unique id set
442
using UniqueIdSet = std::set<uint32_t>;
443
// Column unique Id -> column id map
444
using UniqueIdToColumnIdMap = std::map<ColumnId, ColumnId>;
445
446
// RowsetId moved to storage/rowset_id.h (included above): core/column/column.h
447
// needs the complete type, and this way it gets it without the rest of
448
// olap_common.h.
449
450
using RowsetIdUnorderedSet = std::unordered_set<RowsetId>;
451
452
// Extract rowset id from filename, return uninitialized rowset id if filename is invalid
453
62.0k
inline RowsetId extract_rowset_id(std::string_view filename) {
454
62.0k
    RowsetId rowset_id;
455
62.0k
    if (filename.ends_with(".dat")) {
456
        // filename format: {rowset_id}_{segment_num}.dat
457
56.0k
        auto end = filename.find('_');
458
56.0k
        if (end == std::string::npos) {
459
0
            return rowset_id;
460
0
        }
461
56.0k
        rowset_id.init(filename.substr(0, end));
462
56.0k
        return rowset_id;
463
56.0k
    }
464
6.01k
    if (filename.ends_with(".idx")) {
465
        // filename format: {rowset_id}_{segment_num}_{index_id}.idx
466
5.83k
        auto end = filename.find('_');
467
5.83k
        if (end == std::string::npos) {
468
0
            return rowset_id;
469
0
        }
470
5.83k
        rowset_id.init(filename.substr(0, end));
471
5.83k
        return rowset_id;
472
5.83k
    }
473
184
    return rowset_id;
474
6.01k
}
475
476
class DeleteBitmap;
477
// merge on write context
478
struct MowContext {
479
    MowContext(int64_t version, int64_t txnid, std::shared_ptr<RowsetIdUnorderedSet> ids,
480
               std::vector<RowsetSharedPtr> rowset_ptrs, std::shared_ptr<DeleteBitmap> db)
481
210
            : max_version(version),
482
210
              txn_id(txnid),
483
210
              rowset_ids(std::move(ids)),
484
210
              rowset_ptrs(std::move(rowset_ptrs)),
485
210
              delete_bitmap(std::move(db)) {}
486
    int64_t max_version;
487
    int64_t txn_id;
488
    std::shared_ptr<RowsetIdUnorderedSet> rowset_ids;
489
    std::vector<RowsetSharedPtr> rowset_ptrs;
490
    std::shared_ptr<DeleteBitmap> delete_bitmap;
491
};
492
493
// used for controll compaction
494
struct VersionWithTime {
495
    std::atomic<int64_t> version;
496
    int64_t update_ts;
497
498
28.8k
    VersionWithTime() : version(0), update_ts(MonotonicMillis()) {}
499
500
23.8k
    void update_version_monoto(int64_t new_version) {
501
23.8k
        int64_t cur_version = version.load(std::memory_order_relaxed);
502
23.8k
        while (cur_version < new_version) {
503
23.8k
            if (version.compare_exchange_strong(cur_version, new_version, std::memory_order_relaxed,
504
23.8k
                                                std::memory_order_relaxed)) {
505
23.8k
                update_ts = MonotonicMillis();
506
23.8k
                break;
507
23.8k
            }
508
23.8k
        }
509
23.8k
    }
510
};
511
} // namespace doris