be/src/storage/segment/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/Descriptors_types.h> |
21 | | #include <gen_cpp/segment_v2.pb.h> |
22 | | #include <sys/types.h> |
23 | | |
24 | | #include <cstddef> // for size_t |
25 | | #include <cstdint> // for uint32_t |
26 | | #include <memory> // for unique_ptr |
27 | | #include <string> |
28 | | #include <utility> |
29 | | #include <vector> |
30 | | |
31 | | #include "common/config.h" |
32 | | #include "common/logging.h" |
33 | | #include "common/status.h" // for Status |
34 | | #include "core/column/column_array.h" // ColumnArray |
35 | | #include "core/data_type/data_type.h" |
36 | | #include "io/cache/cached_remote_file_reader.h" |
37 | | #include "io/fs/file_reader_writer_fwd.h" |
38 | | #include "io/io_common.h" |
39 | | #include "storage/index/index_reader.h" |
40 | | #include "storage/index/ordinal_page_index.h" // for OrdinalPageIndexIterator |
41 | | #include "storage/index/zone_map/zone_map_index.h" |
42 | | #include "storage/olap_common.h" |
43 | | #include "storage/predicate/column_predicate.h" |
44 | | #include "storage/segment/common.h" |
45 | | #include "storage/segment/page_handle.h" // for PageHandle |
46 | | #include "storage/segment/page_pointer.h" |
47 | | #include "storage/segment/parsed_page.h" // for ParsedPage |
48 | | #include "storage/segment/segment_prefetcher.h" |
49 | | #include "storage/segment/stream_reader.h" |
50 | | #include "storage/tablet/tablet_schema.h" |
51 | | #include "storage/types.h" |
52 | | #include "storage/utils.h" |
53 | | #include "util/once.h" |
54 | | |
55 | | namespace doris { |
56 | | #include "common/compile_check_begin.h" |
57 | | |
58 | | class BlockCompressionCodec; |
59 | | class AndBlockColumnPredicate; |
60 | | class ColumnPredicate; |
61 | | class TabletIndex; |
62 | | class StorageReadOptions; |
63 | | |
64 | | namespace io { |
65 | | class FileReader; |
66 | | } // namespace io |
67 | | struct Slice; |
68 | | struct StringRef; |
69 | | |
70 | | using TColumnAccessPaths = std::vector<TColumnAccessPath>; |
71 | | |
72 | | namespace segment_v2 { |
73 | | class EncodingInfo; |
74 | | class ColumnIterator; |
75 | | class BloomFilterIndexReader; |
76 | | class InvertedIndexIterator; |
77 | | class InvertedIndexReader; |
78 | | class IndexFileReader; |
79 | | class PageDecoder; |
80 | | class RowRanges; |
81 | | class ZoneMapIndexReader; |
82 | | class IndexIterator; |
83 | | class ColumnMetaAccessor; |
84 | | |
85 | | struct ColumnReaderOptions { |
86 | | // whether verify checksum when read page |
87 | | bool verify_checksum = true; |
88 | | // for in memory olap table, use DURABLE CachePriority in page cache |
89 | | bool kept_in_memory = false; |
90 | | |
91 | | int be_exec_version = -1; |
92 | | |
93 | | TabletSchemaSPtr tablet_schema = nullptr; |
94 | | }; |
95 | | |
96 | | struct ColumnIteratorOptions { |
97 | | bool use_page_cache = false; |
98 | | bool is_predicate_column = false; |
99 | | // for page cache allocation |
100 | | // page types are divided into DATA_PAGE & INDEX_PAGE |
101 | | // INDEX_PAGE including index_page, dict_page and short_key_page |
102 | | PageTypePB type = PageTypePB::UNKNOWN_PAGE_TYPE; |
103 | | io::FileReader* file_reader = nullptr; // Ref |
104 | | // reader statistics |
105 | | OlapReaderStatistics* stats = nullptr; // Ref |
106 | | io::IOContext io_ctx; |
107 | | |
108 | 2.28M | void sanity_check() const { |
109 | 2.28M | CHECK_NOTNULL(file_reader); |
110 | 2.28M | CHECK_NOTNULL(stats); |
111 | 2.28M | } |
112 | | }; |
113 | | |
114 | | class ColumnIterator; |
115 | | class OffsetFileColumnIterator; |
116 | | class FileColumnIterator; |
117 | | |
118 | | using ColumnIteratorUPtr = std::unique_ptr<ColumnIterator>; |
119 | | using OffsetFileColumnIteratorUPtr = std::unique_ptr<OffsetFileColumnIterator>; |
120 | | using FileColumnIteratorUPtr = std::unique_ptr<FileColumnIterator>; |
121 | | using ColumnIteratorSPtr = std::shared_ptr<ColumnIterator>; |
122 | | |
123 | | // There will be concurrent users to read the same column. So |
124 | | // we should do our best to reduce resource usage through share |
125 | | // same information, such as OrdinalPageIndex and Page data. |
126 | | // This will cache data shared by all reader |
127 | | class ColumnReader : public MetadataAdder<ColumnReader>, |
128 | | public std::enable_shared_from_this<ColumnReader> { |
129 | | public: |
130 | | ColumnReader(); |
131 | | // Create an initialized ColumnReader in *reader. |
132 | | // This should be a lightweight operation without I/O. |
133 | | static Status create(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
134 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
135 | | std::shared_ptr<ColumnReader>* reader); |
136 | | |
137 | | static Status create_array(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
138 | | const io::FileReaderSPtr& file_reader, |
139 | | std::shared_ptr<ColumnReader>* reader); |
140 | | static Status create_map(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
141 | | const io::FileReaderSPtr& file_reader, |
142 | | std::shared_ptr<ColumnReader>* reader); |
143 | | static Status create_struct(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
144 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
145 | | std::shared_ptr<ColumnReader>* reader); |
146 | | static Status create_agg_state(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, |
147 | | uint64_t num_rows, const io::FileReaderSPtr& file_reader, |
148 | | std::shared_ptr<ColumnReader>* reader); |
149 | | |
150 | | enum DictEncodingType { UNKNOWN_DICT_ENCODING, PARTIAL_DICT_ENCODING, ALL_DICT_ENCODING }; |
151 | | |
152 | | static bool is_compaction_reader_type(ReaderType type); |
153 | | |
154 | | ~ColumnReader() override; |
155 | | |
156 | | // create a new column iterator. Client should delete returned iterator |
157 | | virtual Status new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* col, |
158 | | const StorageReadOptions*); |
159 | | Status new_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column); |
160 | | Status new_array_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column); |
161 | | Status new_struct_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column); |
162 | | Status new_map_iterator(ColumnIteratorUPtr* iterator, const TabletColumn* tablet_column); |
163 | | Status new_agg_state_iterator(ColumnIteratorUPtr* iterator); |
164 | | |
165 | | Status new_index_iterator(const std::shared_ptr<IndexFileReader>& index_file_reader, |
166 | | const TabletIndex* index_meta, |
167 | | std::unique_ptr<IndexIterator>* iterator); |
168 | | |
169 | | Status seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter, |
170 | | const ColumnIteratorOptions& iter_opts); |
171 | | Status get_ordinal_index_reader(OrdinalIndexReader*& reader, |
172 | | OlapReaderStatistics* index_load_stats); |
173 | | |
174 | | // read a page from file into a page handle |
175 | | Status read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp, |
176 | | PageHandle* handle, Slice* page_body, PageFooterPB* footer, |
177 | | BlockCompressionCodec* codec, bool is_dict_page = false) const; |
178 | | |
179 | 1.33M | bool is_nullable() const { return _meta_is_nullable; } |
180 | | |
181 | 31.9M | const EncodingInfo* encoding_info() const { return _encoding_info; } |
182 | | |
183 | 1.98M | bool has_zone_map() const { return _zone_map_index != nullptr; } |
184 | | bool has_bloom_filter_index(bool ngram) const; |
185 | | // Check if this column could match `cond' using segment zone map. |
186 | | // Since segment zone map is stored in metadata, this function is fast without I/O. |
187 | | // set matched to true if segment zone map is absent or `cond' could be satisfied, false otherwise. |
188 | | Status match_condition(const AndBlockColumnPredicate* col_predicates, bool* matched) const; |
189 | | |
190 | | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const; |
191 | | |
192 | | // get row ranges with zone map |
193 | | // - cond_column is user's query predicate |
194 | | // - delete_condition is a delete predicate of one version |
195 | | Status get_row_ranges_by_zone_map( |
196 | | const AndBlockColumnPredicate* col_predicates, |
197 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
198 | | RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts); |
199 | | |
200 | | // get row ranges with bloom filter index |
201 | | Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
202 | | RowRanges* row_ranges, |
203 | | const ColumnIteratorOptions& iter_opts); |
204 | | |
205 | 409k | PagePointer get_dict_page_pointer() const { return _meta_dict_page; } |
206 | | |
207 | 28.5M | bool is_empty() const { return _num_rows == 0; } |
208 | | |
209 | | Status prune_predicates_by_zone_map(std::vector<std::shared_ptr<ColumnPredicate>>& predicates, |
210 | | const int column_id, bool* pruned) const; |
211 | | |
212 | 28.3M | CompressionTypePB get_compression() const { return _meta_compression; } |
213 | | |
214 | 797k | uint64_t num_rows() const { return _num_rows; } |
215 | | |
216 | 17.7k | void set_dict_encoding_type(DictEncodingType type) { |
217 | 17.7k | static_cast<void>(_set_dict_encoding_type_once.call([&] { |
218 | 17.7k | _dict_encoding_type = type; |
219 | 17.7k | return Status::OK(); |
220 | 17.7k | })); |
221 | 17.7k | } |
222 | | |
223 | 16.9M | DictEncodingType get_dict_encoding_type() { return _dict_encoding_type; } |
224 | | |
225 | 28.3M | void disable_index_meta_cache() { _use_index_page_cache = false; } |
226 | | |
227 | 25.9k | DataTypePtr get_vec_data_type() { return _data_type; } |
228 | | |
229 | 56.0M | virtual FieldType get_meta_type() { return _meta_type; } |
230 | | |
231 | | int64_t get_metadata_size() const override; |
232 | | |
233 | | #ifdef BE_TEST |
234 | | void check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const; |
235 | | #endif |
236 | | |
237 | | private: |
238 | | friend class VariantColumnReader; |
239 | | friend class FileColumnIterator; |
240 | | friend class SegmentPrefetcher; |
241 | | |
242 | | ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, uint64_t num_rows, |
243 | | io::FileReaderSPtr file_reader); |
244 | | Status init(const ColumnMetaPB* meta); |
245 | | |
246 | | [[nodiscard]] Status _load_zone_map_index(bool use_page_cache, bool kept_in_memory, |
247 | | const ColumnIteratorOptions& iter_opts); |
248 | | [[nodiscard]] Status _load_ordinal_index(bool use_page_cache, bool kept_in_memory, |
249 | | const ColumnIteratorOptions& iter_opts); |
250 | | |
251 | | [[nodiscard]] Status _load_index(const std::shared_ptr<IndexFileReader>& index_file_reader, |
252 | | const TabletIndex* index_meta); |
253 | | [[nodiscard]] Status _load_bloom_filter_index(bool use_page_cache, bool kept_in_memory, |
254 | | const ColumnIteratorOptions& iter_opts); |
255 | | |
256 | | bool _zone_map_match_condition(const segment_v2::ZoneMap& zone_map, |
257 | | const AndBlockColumnPredicate* col_predicates) const; |
258 | | |
259 | | Status _get_filtered_pages( |
260 | | const AndBlockColumnPredicate* col_predicates, |
261 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
262 | | std::vector<uint32_t>* page_indexes, const ColumnIteratorOptions& iter_opts); |
263 | | |
264 | | Status _calculate_row_ranges(const std::vector<uint32_t>& page_indexes, RowRanges* row_ranges, |
265 | | const ColumnIteratorOptions& iter_opts); |
266 | | |
267 | | int64_t _meta_length; |
268 | | FieldType _meta_type; |
269 | | FieldType _meta_children_column_type; |
270 | | bool _meta_is_nullable; |
271 | | bool _use_index_page_cache; |
272 | | int _be_exec_version = -1; |
273 | | |
274 | | PagePointer _meta_dict_page; |
275 | | CompressionTypePB _meta_compression; |
276 | | |
277 | | ColumnReaderOptions _opts; |
278 | | uint64_t _num_rows; |
279 | | |
280 | | io::FileReaderSPtr _file_reader; |
281 | | |
282 | | DictEncodingType _dict_encoding_type; |
283 | | |
284 | | DataTypePtr _data_type; |
285 | | |
286 | | TypeInfoPtr _type_info = |
287 | | TypeInfoPtr(nullptr, |
288 | | nullptr); // initialized in init(), may changed by subclasses. |
289 | | const EncodingInfo* _encoding_info = |
290 | | nullptr; // initialized in init(), used for create PageDecoder |
291 | | |
292 | | // meta for various column indexes (null if the index is absent) |
293 | | std::unique_ptr<ZoneMapPB> _segment_zone_map; |
294 | | |
295 | | mutable std::shared_mutex _load_index_lock; |
296 | | std::unique_ptr<ZoneMapIndexReader> _zone_map_index; |
297 | | std::unique_ptr<OrdinalIndexReader> _ordinal_index; |
298 | | std::shared_ptr<BloomFilterIndexReader> _bloom_filter_index; |
299 | | |
300 | | std::unordered_map<int64_t, IndexReaderPtr> _index_readers; |
301 | | |
302 | | std::vector<std::shared_ptr<ColumnReader>> _sub_readers; |
303 | | |
304 | | DorisCallOnce<Status> _set_dict_encoding_type_once; |
305 | | }; |
306 | | |
307 | | // Base iterator to read one column data |
308 | | class ColumnIterator { |
309 | | public: |
310 | 28.7M | ColumnIterator() = default; |
311 | 28.7M | virtual ~ColumnIterator() = default; |
312 | | |
313 | 51.2k | virtual Status init(const ColumnIteratorOptions& opts) { |
314 | 51.2k | _opts = opts; |
315 | 51.2k | return Status::OK(); |
316 | 51.2k | } |
317 | | |
318 | | // Seek to the given ordinal entry in the column. |
319 | | // Entry 0 is the first entry written to the column. |
320 | | // If provided seek point is past the end of the file, |
321 | | // then returns false. |
322 | | virtual Status seek_to_ordinal(ordinal_t ord) = 0; |
323 | | |
324 | 2.20M | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
325 | 2.20M | bool has_null; |
326 | 2.20M | return next_batch(n, dst, &has_null); |
327 | 2.20M | } |
328 | | |
329 | 0 | virtual Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
330 | 0 | return Status::NotSupported("next_batch not implement"); |
331 | 0 | } |
332 | | |
333 | 0 | virtual Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
334 | 0 | return Status::NotSupported("next_batch_of_zone_map not implement"); |
335 | 0 | } |
336 | | |
337 | | virtual Status read_by_rowids(const rowid_t* rowids, const size_t count, |
338 | 0 | MutableColumnPtr& dst) { |
339 | 0 | return Status::NotSupported("read_by_rowids not implement"); |
340 | 0 | } |
341 | | |
342 | | virtual ordinal_t get_current_ordinal() const = 0; |
343 | | |
344 | | virtual Status get_row_ranges_by_zone_map( |
345 | | const AndBlockColumnPredicate* col_predicates, |
346 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
347 | 37 | RowRanges* row_ranges) { |
348 | 37 | return Status::OK(); |
349 | 37 | } |
350 | | |
351 | | virtual Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
352 | 39 | RowRanges* row_ranges) { |
353 | 39 | return Status::OK(); |
354 | 39 | } |
355 | | |
356 | | virtual Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
357 | 41 | RowRanges* row_ranges) { |
358 | 41 | return Status::OK(); |
359 | 41 | } |
360 | | |
361 | 2 | virtual bool is_all_dict_encoding() const { return false; } |
362 | | |
363 | | virtual Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
364 | 18.6k | const TColumnAccessPaths& predicate_access_paths) { |
365 | 18.6k | if (!predicate_access_paths.empty()) { |
366 | 240 | _reading_flag = ReadingFlag::READING_FOR_PREDICATE; |
367 | 240 | } |
368 | 18.6k | return Status::OK(); |
369 | 18.6k | } |
370 | | |
371 | 28.6M | void set_column_name(const std::string& column_name) { _column_name = column_name; } |
372 | | |
373 | 24.1k | const std::string& column_name() const { return _column_name; } |
374 | | |
375 | | // Since there may be multiple paths with conflicts or overlaps, |
376 | | // we need to define several reading flags: |
377 | | // |
378 | | // NORMAL_READING — Default value, indicating that the column should be read. |
379 | | // SKIP_READING — The column should not be read. |
380 | | // NEED_TO_READ — The column must be read. |
381 | | // READING_FOR_PREDICATE — The column is required for predicate evaluation. |
382 | | // |
383 | | // For example, suppose there are two paths: |
384 | | // - Path 1 specifies that column A needs to be read, so it is marked as NEED_TO_READ. |
385 | | // - Path 2 specifies that the column should not be read, but since it is already marked as NEED_TO_READ, |
386 | | // it should not be changed to SKIP_READING. |
387 | | enum class ReadingFlag : int { |
388 | | NORMAL_READING, |
389 | | SKIP_READING, |
390 | | NEED_TO_READ, |
391 | | READING_FOR_PREDICATE |
392 | | }; |
393 | 193k | void set_reading_flag(ReadingFlag flag) { |
394 | 193k | if (static_cast<int>(flag) > static_cast<int>(_reading_flag)) { |
395 | 145k | _reading_flag = flag; |
396 | 145k | } |
397 | 193k | } |
398 | | |
399 | 71.7k | ReadingFlag reading_flag() const { return _reading_flag; } |
400 | | |
401 | 82.3k | virtual void set_need_to_read() { set_reading_flag(ReadingFlag::NEED_TO_READ); } |
402 | | |
403 | 83.4k | virtual void remove_pruned_sub_iterators() {}; |
404 | | |
405 | 50.5k | virtual Status init_prefetcher(const SegmentPrefetchParams& params) { return Status::OK(); } |
406 | | |
407 | | virtual void collect_prefetchers( |
408 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
409 | 50.5k | PrefetcherInitMethod init_method) {} |
410 | | |
411 | | protected: |
412 | | Result<TColumnAccessPaths> _get_sub_access_paths(const TColumnAccessPaths& access_paths); |
413 | | ColumnIteratorOptions _opts; |
414 | | |
415 | | ReadingFlag _reading_flag {ReadingFlag::NORMAL_READING}; |
416 | | std::string _column_name; |
417 | | }; |
418 | | |
419 | | // This iterator is used to read column data from file |
420 | | // for scalar type |
421 | | class FileColumnIterator final : public ColumnIterator { |
422 | | public: |
423 | | explicit FileColumnIterator(std::shared_ptr<ColumnReader> reader); |
424 | | ~FileColumnIterator() override; |
425 | | |
426 | | Status init(const ColumnIteratorOptions& opts) override; |
427 | | |
428 | | Status seek_to_ordinal(ordinal_t ord) override; |
429 | | |
430 | | Status seek_to_page_start(); |
431 | | |
432 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
433 | | |
434 | | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override; |
435 | | |
436 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
437 | | MutableColumnPtr& dst) override; |
438 | | |
439 | 2 | ordinal_t get_current_ordinal() const override { return _current_ordinal; } |
440 | | |
441 | | // get row ranges by zone map |
442 | | // - cond_column is user's query predicate |
443 | | // - delete_condition is delete predicate of one version |
444 | | Status get_row_ranges_by_zone_map( |
445 | | const AndBlockColumnPredicate* col_predicates, |
446 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
447 | | RowRanges* row_ranges) override; |
448 | | |
449 | | Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
450 | | RowRanges* row_ranges) override; |
451 | | |
452 | | Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
453 | | RowRanges* row_ranges) override; |
454 | | |
455 | 1.79M | ParsedPage* get_current_page() { return &_page; } |
456 | | |
457 | 0 | bool is_nullable() { return _reader->is_nullable(); } |
458 | | |
459 | 13.3k | bool is_all_dict_encoding() const override { return _is_all_dict_encoding; } |
460 | | |
461 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
462 | | void collect_prefetchers( |
463 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
464 | | PrefetcherInitMethod init_method) override; |
465 | | |
466 | | private: |
467 | | Status _seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const; |
468 | | Status _load_next_page(bool* eos); |
469 | | Status _read_data_page(const OrdinalPageIndexIterator& iter); |
470 | | Status _read_dict_data(); |
471 | | void _trigger_prefetch_if_eligible(ordinal_t ord); |
472 | | |
473 | | std::shared_ptr<ColumnReader> _reader = nullptr; |
474 | | |
475 | | // iterator owned compress codec, should NOT be shared by threads, initialized in init() |
476 | | BlockCompressionCodec* _compress_codec = nullptr; |
477 | | |
478 | | // 1. The _page represents current page. |
479 | | // 2. We define an operation is one seek and following read, |
480 | | // If new seek is issued, the _page will be reset. |
481 | | ParsedPage _page; |
482 | | |
483 | | // keep dict page decoder |
484 | | std::unique_ptr<PageDecoder> _dict_decoder; |
485 | | |
486 | | // keep dict page handle to avoid released |
487 | | PageHandle _dict_page_handle; |
488 | | |
489 | | // page iterator used to get next page when current page is finished. |
490 | | // This value will be reset when a new seek is issued |
491 | | OrdinalPageIndexIterator _page_iter; |
492 | | |
493 | | // current value ordinal |
494 | | ordinal_t _current_ordinal = 0; |
495 | | |
496 | | bool _is_all_dict_encoding = false; |
497 | | |
498 | | std::unique_ptr<StringRef[]> _dict_word_info; |
499 | | |
500 | | bool _enable_prefetch {false}; |
501 | | std::unique_ptr<SegmentPrefetcher> _prefetcher; |
502 | | std::shared_ptr<io::CachedRemoteFileReader> _cached_remote_file_reader {nullptr}; |
503 | | }; |
504 | | |
505 | | class EmptyFileColumnIterator final : public ColumnIterator { |
506 | | public: |
507 | 23.5k | Status seek_to_ordinal(ordinal_t ord) override { return Status::OK(); } |
508 | 0 | ordinal_t get_current_ordinal() const override { return 0; } |
509 | | }; |
510 | | |
511 | | // This iterator make offset operation write once for |
512 | | class OffsetFileColumnIterator final : public ColumnIterator { |
513 | | public: |
514 | 106k | explicit OffsetFileColumnIterator(FileColumnIteratorUPtr offset_reader) { |
515 | 106k | _offset_iterator = std::move(offset_reader); |
516 | 106k | } |
517 | | |
518 | 106k | ~OffsetFileColumnIterator() override = default; |
519 | | |
520 | | Status init(const ColumnIteratorOptions& opts) override; |
521 | | |
522 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
523 | 0 | ordinal_t get_current_ordinal() const override { |
524 | 0 | return _offset_iterator->get_current_ordinal(); |
525 | 0 | } |
526 | 457k | Status seek_to_ordinal(ordinal_t ord) override { |
527 | 457k | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
528 | 457k | return Status::OK(); |
529 | 457k | } |
530 | | |
531 | | Status _peek_one_offset(ordinal_t* offset); |
532 | | |
533 | | Status _calculate_offsets(ssize_t start, ColumnArray::ColumnOffsets& column_offsets); |
534 | | |
535 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
536 | 36.7k | MutableColumnPtr& dst) override { |
537 | 36.7k | return _offset_iterator->read_by_rowids(rowids, count, dst); |
538 | 36.7k | } |
539 | | |
540 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
541 | | void collect_prefetchers( |
542 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
543 | | PrefetcherInitMethod init_method) override; |
544 | | |
545 | | private: |
546 | | std::unique_ptr<FileColumnIterator> _offset_iterator; |
547 | | // reuse a tiny column for peek to avoid frequent allocations |
548 | | MutableColumnPtr _peek_tmp_col; |
549 | | }; |
550 | | |
551 | | // This iterator is used to read map value column |
552 | | class MapFileColumnIterator final : public ColumnIterator { |
553 | | public: |
554 | | explicit MapFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
555 | | ColumnIteratorUPtr null_iterator, |
556 | | OffsetFileColumnIteratorUPtr offsets_iterator, |
557 | | ColumnIteratorUPtr key_iterator, |
558 | | ColumnIteratorUPtr val_iterator); |
559 | | |
560 | 37.7k | ~MapFileColumnIterator() override = default; |
561 | | |
562 | | Status init(const ColumnIteratorOptions& opts) override; |
563 | | |
564 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
565 | | |
566 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
567 | | MutableColumnPtr& dst) override; |
568 | | |
569 | | Status seek_to_ordinal(ordinal_t ord) override; |
570 | | |
571 | 0 | ordinal_t get_current_ordinal() const override { |
572 | 0 | return _offsets_iterator->get_current_ordinal(); |
573 | 0 | } |
574 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
575 | | void collect_prefetchers( |
576 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
577 | | PrefetcherInitMethod init_method) override; |
578 | | |
579 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
580 | | const TColumnAccessPaths& predicate_access_paths) override; |
581 | | |
582 | | void set_need_to_read() override; |
583 | | |
584 | | void remove_pruned_sub_iterators() override; |
585 | | |
586 | | private: |
587 | | std::shared_ptr<ColumnReader> _map_reader = nullptr; |
588 | | ColumnIteratorUPtr _null_iterator; |
589 | | OffsetFileColumnIteratorUPtr _offsets_iterator; //OffsetFileIterator |
590 | | ColumnIteratorUPtr _key_iterator; |
591 | | ColumnIteratorUPtr _val_iterator; |
592 | | }; |
593 | | |
594 | | class StructFileColumnIterator final : public ColumnIterator { |
595 | | public: |
596 | | explicit StructFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
597 | | ColumnIteratorUPtr null_iterator, |
598 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators); |
599 | | |
600 | 8.03k | ~StructFileColumnIterator() override = default; |
601 | | |
602 | | Status init(const ColumnIteratorOptions& opts) override; |
603 | | |
604 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
605 | | |
606 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
607 | | MutableColumnPtr& dst) override; |
608 | | |
609 | | Status seek_to_ordinal(ordinal_t ord) override; |
610 | | |
611 | 0 | ordinal_t get_current_ordinal() const override { |
612 | 0 | return _sub_column_iterators[0]->get_current_ordinal(); |
613 | 0 | } |
614 | | |
615 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
616 | | const TColumnAccessPaths& predicate_access_paths) override; |
617 | | |
618 | | void set_need_to_read() override; |
619 | | |
620 | | void remove_pruned_sub_iterators() override; |
621 | | |
622 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
623 | | void collect_prefetchers( |
624 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
625 | | PrefetcherInitMethod init_method) override; |
626 | | |
627 | | private: |
628 | | std::shared_ptr<ColumnReader> _struct_reader = nullptr; |
629 | | ColumnIteratorUPtr _null_iterator; |
630 | | std::vector<ColumnIteratorUPtr> _sub_column_iterators; |
631 | | }; |
632 | | |
633 | | class ArrayFileColumnIterator final : public ColumnIterator { |
634 | | public: |
635 | | explicit ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
636 | | OffsetFileColumnIteratorUPtr offset_reader, |
637 | | ColumnIteratorUPtr item_iterator, |
638 | | ColumnIteratorUPtr null_iterator); |
639 | | |
640 | 69.0k | ~ArrayFileColumnIterator() override = default; |
641 | | |
642 | | Status init(const ColumnIteratorOptions& opts) override; |
643 | | |
644 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
645 | | |
646 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
647 | | MutableColumnPtr& dst) override; |
648 | | |
649 | | Status seek_to_ordinal(ordinal_t ord) override; |
650 | | |
651 | 0 | ordinal_t get_current_ordinal() const override { |
652 | 0 | return _offset_iterator->get_current_ordinal(); |
653 | 0 | } |
654 | | |
655 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
656 | | const TColumnAccessPaths& predicate_access_paths) override; |
657 | | void set_need_to_read() override; |
658 | | |
659 | | void remove_pruned_sub_iterators() override; |
660 | | |
661 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
662 | | void collect_prefetchers( |
663 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
664 | | PrefetcherInitMethod init_method) override; |
665 | | |
666 | | private: |
667 | | std::shared_ptr<ColumnReader> _array_reader = nullptr; |
668 | | std::unique_ptr<OffsetFileColumnIterator> _offset_iterator; |
669 | | std::unique_ptr<ColumnIterator> _null_iterator; |
670 | | std::unique_ptr<ColumnIterator> _item_iterator; |
671 | | |
672 | | Status _seek_by_offsets(ordinal_t ord); |
673 | | }; |
674 | | |
675 | | class RowIdColumnIterator : public ColumnIterator { |
676 | | public: |
677 | | RowIdColumnIterator() = delete; |
678 | | RowIdColumnIterator(int64_t tid, RowsetId rid, int32_t segid) |
679 | 11 | : _tablet_id(tid), _rowset_id(rid), _segment_id(segid) {} |
680 | | |
681 | 7 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
682 | 7 | _current_rowid = cast_set<uint32_t>(ord_idx); |
683 | 7 | return Status::OK(); |
684 | 7 | } |
685 | | |
686 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
687 | 0 | bool has_null; |
688 | 0 | return next_batch(n, dst, &has_null); |
689 | 0 | } |
690 | | |
691 | 7 | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override { |
692 | 28 | for (size_t i = 0; i < *n; ++i) { |
693 | 21 | const auto row_id = cast_set<uint32_t>(_current_rowid + i); |
694 | 21 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
695 | 21 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
696 | 21 | } |
697 | 7 | _current_rowid += *n; |
698 | 7 | return Status::OK(); |
699 | 7 | } |
700 | | |
701 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
702 | 4 | MutableColumnPtr& dst) override { |
703 | 8 | for (size_t i = 0; i < count; ++i) { |
704 | 4 | rowid_t row_id = rowids[i]; |
705 | 4 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
706 | 4 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
707 | 4 | } |
708 | 4 | return Status::OK(); |
709 | 4 | } |
710 | | |
711 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
712 | | |
713 | | private: |
714 | | rowid_t _current_rowid = 0; |
715 | | int64_t _tablet_id = 0; |
716 | | RowsetId _rowset_id; |
717 | | int32_t _segment_id = 0; |
718 | | }; |
719 | | |
720 | | // Add new RowIdColumnIteratorV2 |
721 | | class RowIdColumnIteratorV2 : public ColumnIterator { |
722 | | public: |
723 | | RowIdColumnIteratorV2(uint8_t version, int64_t backend_id, uint32_t file_id) |
724 | 20.2k | : _version(version), _backend_id(backend_id), _file_id(file_id) {} |
725 | | |
726 | 12.0k | Status seek_to_ordinal(ordinal_t ord_idx) override { |
727 | 12.0k | _current_rowid = cast_set<uint32_t>(ord_idx); |
728 | 12.0k | return Status::OK(); |
729 | 12.0k | } |
730 | | |
731 | 10.0k | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
732 | 10.0k | bool has_null; |
733 | 10.0k | return next_batch(n, dst, &has_null); |
734 | 10.0k | } |
735 | | |
736 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
737 | | |
738 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
739 | | MutableColumnPtr& dst) override; |
740 | | |
741 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
742 | | |
743 | | private: |
744 | | uint32_t _current_rowid = 0; |
745 | | uint8_t _version; |
746 | | int64_t _backend_id; |
747 | | uint32_t _file_id; |
748 | | }; |
749 | | |
750 | | // This iterator is used to read default value column |
751 | | class DefaultValueColumnIterator : public ColumnIterator { |
752 | | public: |
753 | | DefaultValueColumnIterator(bool has_default_value, std::string default_value, bool is_nullable, |
754 | | TypeInfoPtr type_info, int precision, int scale, int len) |
755 | 12.2k | : _has_default_value(has_default_value), |
756 | 12.2k | _default_value(std::move(default_value)), |
757 | 12.2k | _is_nullable(is_nullable), |
758 | 12.2k | _type_info(std::move(type_info)), |
759 | 12.2k | _precision(precision), |
760 | 12.2k | _scale(scale), |
761 | 12.2k | _len(len) {} |
762 | | |
763 | | Status init(const ColumnIteratorOptions& opts) override; |
764 | | |
765 | 2.15k | Status seek_to_ordinal(ordinal_t ord_idx) override { |
766 | 2.15k | _current_rowid = ord_idx; |
767 | 2.15k | return Status::OK(); |
768 | 2.15k | } |
769 | | |
770 | 4 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
771 | 4 | bool has_null; |
772 | 4 | return next_batch(n, dst, &has_null); |
773 | 4 | } |
774 | | |
775 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
776 | | |
777 | 4 | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override { |
778 | 4 | return next_batch(n, dst); |
779 | 4 | } |
780 | | |
781 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
782 | | MutableColumnPtr& dst) override; |
783 | | |
784 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
785 | | |
786 | | private: |
787 | | void _insert_many_default(MutableColumnPtr& dst, size_t n); |
788 | | |
789 | | bool _has_default_value; |
790 | | std::string _default_value; |
791 | | bool _is_nullable; |
792 | | TypeInfoPtr _type_info; |
793 | | int _precision; |
794 | | int _scale; |
795 | | const int _len; |
796 | | Field _default_value_field; |
797 | | |
798 | | // current rowid |
799 | | ordinal_t _current_rowid = 0; |
800 | | }; |
801 | | |
802 | | } // namespace segment_v2 |
803 | | #include "common/compile_check_end.h" |
804 | | } // namespace doris |