Coverage Report

Created: 2026-07-09 10:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/index/indexed_column_reader.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/segment_v2.pb.h>
21
#include <glog/logging.h>
22
#include <stddef.h>
23
#include <stdint.h>
24
25
#include <string>
26
#include <utility>
27
28
#include "common/status.h"
29
#include "core/data_type/data_type.h"
30
#include "io/fs/file_reader_writer_fwd.h"
31
#include "storage/index/index_page.h"
32
#include "storage/olap_common.h"
33
#include "storage/segment/common.h"
34
#include "storage/segment/page_handle.h"
35
#include "storage/segment/page_pointer.h"
36
#include "storage/segment/parsed_page.h"
37
#include "util/slice.h"
38
39
namespace doris {
40
41
class KeyCoder;
42
class BlockCompressionCodec;
43
namespace io {
44
struct IOContext;
45
}
46
47
namespace segment_v2 {
48
49
class EncodingInfo;
50
51
// thread-safe reader for IndexedColumn (see comments of `IndexedColumnWriter` to understand what IndexedColumn is)
52
class IndexedColumnReader : public MetadataAdder<IndexedColumnReader> {
53
public:
54
    explicit IndexedColumnReader(io::FileReaderSPtr file_reader, const IndexedColumnMetaPB& meta)
55
552
            : _file_reader(std::move(file_reader)), _meta(meta) {
56
552
        _ordinal_index_reader = std::make_unique<IndexPageReader>();
57
552
        _value_index_reader = std::make_unique<IndexPageReader>();
58
552
    }
59
60
    ~IndexedColumnReader() override;
61
62
    Status load(bool use_page_cache, bool kept_in_memory,
63
                OlapReaderStatistics* index_load_stats = nullptr,
64
                const io::IOContext* io_ctx = nullptr);
65
66
    // read a page specified by `pp' from `file' into `handle'
67
    Status read_page(const PagePointer& pp, PageHandle* handle, Slice* body, PageFooterPB* footer,
68
                     PageTypePB type, BlockCompressionCodec* codec, bool pre_decode,
69
                     OlapReaderStatistics* stats = nullptr,
70
                     const io::IOContext* io_ctx = nullptr) const;
71
72
9.28k
    int64_t num_values() const { return _num_values; }
73
1.53k
    const EncodingInfo* encoding_info() const { return _encoding_info; }
74
60
    FieldType type() const { return _type; }
75
661
    bool support_ordinal_seek() const { return _meta.has_ordinal_index_meta(); }
76
6.21k
    bool support_value_seek() const { return _meta.has_value_index_meta(); }
77
78
1.46k
    CompressionTypePB get_compression() const { return _meta.compression(); }
79
0
    uint64_t get_memory_size() const { return _mem_size; }
80
60
    void set_is_pk_index(bool is_pk) { _is_pk_index = is_pk; }
81
82
private:
83
    Status load_index_page(const PagePointerPB& pp, PageHandle* handle, IndexPageReader* reader,
84
                           OlapReaderStatistics* index_load_stats, const io::IOContext* io_ctx);
85
86
    int64_t get_metadata_size() const override;
87
88
    friend class IndexedColumnIterator;
89
90
    io::FileReaderSPtr _file_reader;
91
    IndexedColumnMetaPB _meta;
92
93
    bool _use_page_cache;
94
    bool _kept_in_memory;
95
    int64_t _num_values = 0;
96
    // whether this column contains any index page.
97
    // could be false when the column contains only one data page.
98
    bool _has_index_page = false;
99
    // valid only when the column contains only one data page
100
    PagePointer _sole_data_page;
101
    std::unique_ptr<IndexPageReader> _ordinal_index_reader;
102
    std::unique_ptr<IndexPageReader> _value_index_reader;
103
    PageHandle _ordinal_index_page_handle;
104
    PageHandle _value_index_page_handle;
105
106
    FieldType _type = FieldType::OLAP_FIELD_TYPE_NONE;
107
    const EncodingInfo* _encoding_info = nullptr;
108
    const KeyCoder* _value_key_coder = nullptr;
109
    uint64_t _mem_size = 0;
110
    bool _is_pk_index = false;
111
};
112
113
class IndexedColumnIterator {
114
public:
115
    explicit IndexedColumnIterator(const IndexedColumnReader* reader,
116
                                   OlapReaderStatistics* stats = nullptr,
117
                                   const io::IOContext* io_ctx = nullptr)
118
1.46k
            : _reader(reader),
119
1.46k
              _ordinal_iter(reader->_ordinal_index_reader.get()),
120
1.46k
              _value_iter(reader->_value_index_reader.get()),
121
1.46k
              _stats(stats),
122
1.46k
              _io_ctx(io_ctx) {}
123
124
    // Seek to the given ordinal entry. Entry 0 is the first entry.
125
    // Return Status::Error<ENTRY_NOT_FOUND> if provided seek point is past the end.
126
    // Return NotSupported for column without ordinal index.
127
    Status seek_to_ordinal(ordinal_t idx);
128
129
    // Seek the index to the given key, or to the index entry immediately
130
    // before it. Then seek the data block to the value matching value or to
131
    // the value immediately after it.
132
    //
133
    // Sets *exact_match to indicate whether the seek found the exact
134
    // key requested.
135
    //
136
    // Return Status::Error<ENTRY_NOT_FOUND> if the given key is greater than all keys in this column.
137
    // Return NotSupported for column without value index.
138
    Status seek_at_or_after(const void* key, bool* exact_match);
139
5.50k
    Status seek_at_or_after(const std::string* key, bool* exact_match) {
140
5.50k
        Slice slice(key->data(), key->size());
141
5.50k
        return seek_at_or_after(static_cast<const void*>(&slice), exact_match);
142
5.50k
    }
143
144
    // Get the ordinal index that the iterator is currently pointed to.
145
5.49k
    ordinal_t get_current_ordinal() const {
146
        DCHECK(_seeked);
147
5.49k
        return _current_ordinal;
148
5.49k
    }
149
150
    // After one seek, we can only call this function once to read data
151
    Status next_batch(size_t* n, MutableColumnPtr& dst);
152
153
private:
154
    Status _read_data_page(const PagePointer& pp);
155
156
    const IndexedColumnReader* _reader = nullptr;
157
    // iterator for ordinal index page
158
    IndexPageIterator _ordinal_iter;
159
    // iterator for value index page
160
    IndexPageIterator _value_iter;
161
162
    bool _seeked = false;
163
    // current in-use index iterator, could be `&_ordinal_iter` or `&_value_iter` or null
164
    IndexPageIterator* _current_iter = nullptr;
165
    // seeked data page, containing value at `_current_ordinal`
166
    ParsedPage _data_page;
167
    // next_batch() will read from this position
168
    ordinal_t _current_ordinal = 0;
169
    // iterator owned compress codec, should NOT be shared by threads, initialized before used
170
    BlockCompressionCodec* _compress_codec = nullptr;
171
    OlapReaderStatistics* _stats = nullptr;
172
    const io::IOContext* _io_ctx = nullptr;
173
};
174
175
} // namespace segment_v2
176
} // namespace doris