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 | | |
57 | | class BlockCompressionCodec; |
58 | | class AndBlockColumnPredicate; |
59 | | class ColumnPredicate; |
60 | | class TabletIndex; |
61 | | class StorageReadOptions; |
62 | | |
63 | | namespace io { |
64 | | class FileReader; |
65 | | } // namespace io |
66 | | struct Slice; |
67 | | struct StringRef; |
68 | | |
69 | | using TColumnAccessPaths = std::vector<TColumnAccessPath>; |
70 | | |
71 | | namespace segment_v2 { |
72 | | class EncodingInfo; |
73 | | class ColumnIterator; |
74 | | class BloomFilterIndexReader; |
75 | | class InvertedIndexIterator; |
76 | | class InvertedIndexReader; |
77 | | class IndexFileReader; |
78 | | class PageDecoder; |
79 | | class RowRanges; |
80 | | class ZoneMapIndexReader; |
81 | | class IndexIterator; |
82 | | class ColumnMetaAccessor; |
83 | | |
84 | | struct ColumnReaderOptions { |
85 | | // whether verify checksum when read page |
86 | | bool verify_checksum = true; |
87 | | // for in memory olap table, use DURABLE CachePriority in page cache |
88 | | bool kept_in_memory = false; |
89 | | |
90 | | int be_exec_version = -1; |
91 | | |
92 | | TabletSchemaSPtr tablet_schema = nullptr; |
93 | | }; |
94 | | |
95 | | struct ColumnIteratorOptions { |
96 | | bool use_page_cache = false; |
97 | | bool is_predicate_column = false; |
98 | | // for page cache allocation |
99 | | // page types are divided into DATA_PAGE & INDEX_PAGE |
100 | | // INDEX_PAGE including index_page, dict_page and short_key_page |
101 | | PageTypePB type = PageTypePB::UNKNOWN_PAGE_TYPE; |
102 | | io::FileReader* file_reader = nullptr; // Ref |
103 | | // reader statistics |
104 | | OlapReaderStatistics* stats = nullptr; // Ref |
105 | | io::IOContext io_ctx; |
106 | | bool only_read_offsets = false; |
107 | | |
108 | 12.8k | void sanity_check() const { |
109 | 12.8k | CHECK_NOTNULL(file_reader); |
110 | 12.8k | CHECK_NOTNULL(stats); |
111 | 12.8k | } |
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 | 3.01k | bool is_nullable() const { return _meta_is_nullable; } |
180 | | |
181 | 25.7k | const EncodingInfo* encoding_info() const { return _encoding_info; } |
182 | | |
183 | 0 | 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 | 2.75k | PagePointer get_dict_page_pointer() const { return _meta_dict_page; } |
206 | | |
207 | 8.04k | 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 | 7.88k | CompressionTypePB get_compression() const { return _meta_compression; } |
213 | | |
214 | 4.57k | uint64_t num_rows() const { return _num_rows; } |
215 | | |
216 | 0 | void set_dict_encoding_type(DictEncodingType type) { |
217 | 0 | static_cast<void>(_set_dict_encoding_type_once.call([&] { |
218 | 0 | _dict_encoding_type = type; |
219 | 0 | return Status::OK(); |
220 | 0 | })); |
221 | 0 | } |
222 | | |
223 | 0 | DictEncodingType get_dict_encoding_type() { return _dict_encoding_type; } |
224 | | |
225 | 7.88k | void disable_index_meta_cache() { _use_index_page_cache = false; } |
226 | | |
227 | 16 | DataTypePtr get_vec_data_type() { return _data_type; } |
228 | | |
229 | 13.8k | 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 | | FieldType _type = |
287 | | FieldType::OLAP_FIELD_TYPE_NONE; // initialized in init(), may changed by subclasses. |
288 | | const EncodingInfo* _encoding_info = |
289 | | nullptr; // initialized in init(), used for create PageDecoder |
290 | | |
291 | | // meta for various column indexes (null if the index is absent) |
292 | | std::unique_ptr<ZoneMapPB> _segment_zone_map; |
293 | | |
294 | | mutable std::shared_mutex _load_index_lock; |
295 | | std::unique_ptr<ZoneMapIndexReader> _zone_map_index; |
296 | | std::unique_ptr<OrdinalIndexReader> _ordinal_index; |
297 | | std::shared_ptr<BloomFilterIndexReader> _bloom_filter_index; |
298 | | |
299 | | std::unordered_map<int64_t, IndexReaderPtr> _index_readers; |
300 | | |
301 | | std::vector<std::shared_ptr<ColumnReader>> _sub_readers; |
302 | | |
303 | | DorisCallOnce<Status> _set_dict_encoding_type_once; |
304 | | }; |
305 | | |
306 | | // Base iterator to read one column data |
307 | | class ColumnIterator { |
308 | | public: |
309 | 8.54k | ColumnIterator() = default; |
310 | 8.54k | virtual ~ColumnIterator() = default; |
311 | | |
312 | 46 | virtual Status init(const ColumnIteratorOptions& opts) { |
313 | 46 | _opts = opts; |
314 | 46 | return Status::OK(); |
315 | 46 | } |
316 | | |
317 | | // Seek to the given ordinal entry in the column. |
318 | | // Entry 0 is the first entry written to the column. |
319 | | // If provided seek point is past the end of the file, |
320 | | // then returns false. |
321 | | virtual Status seek_to_ordinal(ordinal_t ord) = 0; |
322 | | |
323 | 18.6k | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
324 | 18.6k | bool has_null; |
325 | 18.6k | return next_batch(n, dst, &has_null); |
326 | 18.6k | } |
327 | | |
328 | 0 | virtual Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
329 | 0 | return Status::NotSupported("next_batch not implement"); |
330 | 0 | } |
331 | | |
332 | 0 | virtual Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
333 | 0 | return Status::NotSupported("next_batch_of_zone_map not implement"); |
334 | 0 | } |
335 | | |
336 | | virtual Status read_by_rowids(const rowid_t* rowids, const size_t count, |
337 | 0 | MutableColumnPtr& dst) { |
338 | 0 | return Status::NotSupported("read_by_rowids not implement"); |
339 | 0 | } |
340 | | |
341 | | virtual ordinal_t get_current_ordinal() const = 0; |
342 | | |
343 | | virtual Status get_row_ranges_by_zone_map( |
344 | | const AndBlockColumnPredicate* col_predicates, |
345 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
346 | 0 | RowRanges* row_ranges) { |
347 | 0 | return Status::OK(); |
348 | 0 | } |
349 | | |
350 | | virtual Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
351 | 0 | RowRanges* row_ranges) { |
352 | 0 | return Status::OK(); |
353 | 0 | } |
354 | | |
355 | | virtual Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
356 | 0 | RowRanges* row_ranges) { |
357 | 0 | return Status::OK(); |
358 | 0 | } |
359 | | |
360 | 0 | virtual bool is_all_dict_encoding() const { return false; } |
361 | | |
362 | | virtual Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
363 | 4 | const TColumnAccessPaths& predicate_access_paths) { |
364 | 4 | if (!predicate_access_paths.empty()) { |
365 | 2 | _reading_flag = ReadingFlag::READING_FOR_PREDICATE; |
366 | 2 | } |
367 | 4 | return Status::OK(); |
368 | 4 | } |
369 | | |
370 | 8.15k | void set_column_name(const std::string& column_name) { _column_name = column_name; } |
371 | | |
372 | 10 | const std::string& column_name() const { return _column_name; } |
373 | | |
374 | | // Since there may be multiple paths with conflicts or overlaps, |
375 | | // we need to define several reading flags: |
376 | | // |
377 | | // NORMAL_READING — Default value, indicating that the column should be read. |
378 | | // SKIP_READING — The column should not be read. |
379 | | // NEED_TO_READ — The column must be read. |
380 | | // READING_FOR_PREDICATE — The column is required for predicate evaluation. |
381 | | // |
382 | | // For example, suppose there are two paths: |
383 | | // - Path 1 specifies that column A needs to be read, so it is marked as NEED_TO_READ. |
384 | | // - Path 2 specifies that the column should not be read, but since it is already marked as NEED_TO_READ, |
385 | | // it should not be changed to SKIP_READING. |
386 | | enum class ReadingFlag : int { |
387 | | NORMAL_READING, |
388 | | SKIP_READING, |
389 | | NEED_TO_READ, |
390 | | READING_FOR_PREDICATE |
391 | | }; |
392 | 26 | void set_reading_flag(ReadingFlag flag) { |
393 | 26 | if (static_cast<int>(flag) > static_cast<int>(_reading_flag)) { |
394 | 16 | _reading_flag = flag; |
395 | 16 | } |
396 | 26 | } |
397 | | |
398 | 336 | ReadingFlag reading_flag() const { return _reading_flag; } |
399 | | |
400 | 3 | virtual void set_need_to_read() { set_reading_flag(ReadingFlag::NEED_TO_READ); } |
401 | | |
402 | 0 | virtual void remove_pruned_sub_iterators() {}; |
403 | | |
404 | 0 | virtual Status init_prefetcher(const SegmentPrefetchParams& params) { return Status::OK(); } |
405 | | |
406 | | virtual void collect_prefetchers( |
407 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
408 | 0 | PrefetcherInitMethod init_method) {} |
409 | | |
410 | | static constexpr const char* ACCESS_OFFSET = "OFFSET"; |
411 | | static constexpr const char* ACCESS_ALL = "*"; |
412 | | static constexpr const char* ACCESS_MAP_KEYS = "KEYS"; |
413 | | static constexpr const char* ACCESS_MAP_VALUES = "VALUES"; |
414 | | static constexpr const char* ACCESS_NULL = "NULL"; |
415 | | |
416 | | // Meta-only read modes: |
417 | | // - OFFSET_ONLY: only read offset information (e.g., for array_size/map_size/string_length) |
418 | | // - NULL_MAP_ONLY: only read null map (e.g., for IS NULL / IS NOT NULL predicates) |
419 | | // When these modes are enabled, actual content data is skipped. |
420 | | enum class ReadMode : int { DEFAULT, OFFSET_ONLY, NULL_MAP_ONLY }; |
421 | | |
422 | 2.98k | bool read_offset_only() const { return _read_mode == ReadMode::OFFSET_ONLY; } |
423 | 23.3k | bool read_null_map_only() const { return _read_mode == ReadMode::NULL_MAP_ONLY; } |
424 | | |
425 | | protected: |
426 | | // Checks sub access paths for OFFSET or NULL meta-only modes and |
427 | | // updates _read_mode accordingly. Use the accessor helpers |
428 | | // read_offset_only() / read_null_map_only() to query the current mode. |
429 | | void _check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths); |
430 | | |
431 | | Result<TColumnAccessPaths> _get_sub_access_paths(const TColumnAccessPaths& access_paths); |
432 | | ColumnIteratorOptions _opts; |
433 | | |
434 | | ReadingFlag _reading_flag {ReadingFlag::NORMAL_READING}; |
435 | | ReadMode _read_mode = ReadMode::DEFAULT; |
436 | | std::string _column_name; |
437 | | }; |
438 | | |
439 | | // This iterator is used to read column data from file |
440 | | // for scalar type |
441 | | class FileColumnIterator : public ColumnIterator { |
442 | | public: |
443 | | explicit FileColumnIterator(std::shared_ptr<ColumnReader> reader); |
444 | | ~FileColumnIterator() override; |
445 | | |
446 | | Status init(const ColumnIteratorOptions& opts) override; |
447 | | |
448 | | Status seek_to_ordinal(ordinal_t ord) override; |
449 | | |
450 | | Status seek_to_page_start(); |
451 | | |
452 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
453 | | |
454 | | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override; |
455 | | |
456 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
457 | | MutableColumnPtr& dst) override; |
458 | | |
459 | 2 | ordinal_t get_current_ordinal() const override { return _current_ordinal; } |
460 | | |
461 | | // get row ranges by zone map |
462 | | // - cond_column is user's query predicate |
463 | | // - delete_condition is delete predicate of one version |
464 | | Status get_row_ranges_by_zone_map( |
465 | | const AndBlockColumnPredicate* col_predicates, |
466 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
467 | | RowRanges* row_ranges) override; |
468 | | |
469 | | Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
470 | | RowRanges* row_ranges) override; |
471 | | |
472 | | Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
473 | | RowRanges* row_ranges) override; |
474 | | |
475 | 716 | ParsedPage* get_current_page() { return &_page; } |
476 | | |
477 | 0 | bool is_nullable() { return _reader->is_nullable(); } |
478 | | |
479 | 0 | bool is_all_dict_encoding() const override { return _is_all_dict_encoding; } |
480 | | |
481 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
482 | | void collect_prefetchers( |
483 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
484 | | PrefetcherInitMethod init_method) override; |
485 | | |
486 | | protected: |
487 | | // Exposed to derived iterators (e.g. StringFileColumnIterator) so they can |
488 | | // query column metadata such as the storage field type. |
489 | 2 | const std::shared_ptr<ColumnReader>& get_reader() const { return _reader; } |
490 | | |
491 | | private: |
492 | | Status _seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const; |
493 | | Status _load_next_page(bool* eos); |
494 | | Status _read_data_page(const OrdinalPageIndexIterator& iter); |
495 | | Status _read_dict_data(); |
496 | | void _trigger_prefetch_if_eligible(ordinal_t ord); |
497 | | |
498 | | std::shared_ptr<ColumnReader> _reader = nullptr; |
499 | | |
500 | | BlockCompressionCodec* _compress_codec = nullptr; |
501 | | |
502 | | // 1. The _page represents current page. |
503 | | // 2. We define an operation is one seek and following read, |
504 | | // If new seek is issued, the _page will be reset. |
505 | | ParsedPage _page; |
506 | | |
507 | | // keep dict page decoder |
508 | | std::unique_ptr<PageDecoder> _dict_decoder; |
509 | | |
510 | | // keep dict page handle to avoid released |
511 | | PageHandle _dict_page_handle; |
512 | | |
513 | | // page iterator used to get next page when current page is finished. |
514 | | // This value will be reset when a new seek is issued |
515 | | OrdinalPageIndexIterator _page_iter; |
516 | | |
517 | | // current value ordinal |
518 | | ordinal_t _current_ordinal = 0; |
519 | | |
520 | | bool _is_all_dict_encoding = false; |
521 | | |
522 | | std::unique_ptr<StringRef[]> _dict_word_info; |
523 | | |
524 | | bool _enable_prefetch {false}; |
525 | | std::unique_ptr<SegmentPrefetcher> _prefetcher; |
526 | | std::shared_ptr<io::CachedRemoteFileReader> _cached_remote_file_reader {nullptr}; |
527 | | }; |
528 | | |
529 | | class EmptyFileColumnIterator final : public ColumnIterator { |
530 | | public: |
531 | 46 | Status seek_to_ordinal(ordinal_t ord) override { return Status::OK(); } |
532 | 0 | ordinal_t get_current_ordinal() const override { return 0; } |
533 | | }; |
534 | | |
535 | | // StringFileColumnIterator extends FileColumnIterator with meta-only reading |
536 | | // support for string/binary column types. When the OFFSET path is detected in |
537 | | // set_access_paths, it sets only_read_offsets on the ColumnIteratorOptions so |
538 | | // that the BinaryPlainPageDecoder skips chars memcpy and only fills offsets. |
539 | | class StringFileColumnIterator final : public FileColumnIterator { |
540 | | public: |
541 | | explicit StringFileColumnIterator(std::shared_ptr<ColumnReader> reader); |
542 | 2.65k | ~StringFileColumnIterator() override = default; |
543 | | |
544 | | Status init(const ColumnIteratorOptions& opts) override; |
545 | | |
546 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
547 | | const TColumnAccessPaths& predicate_access_paths) override; |
548 | | }; |
549 | | |
550 | | // This iterator make offset operation write once for |
551 | | class OffsetFileColumnIterator final : public ColumnIterator { |
552 | | public: |
553 | 186 | explicit OffsetFileColumnIterator(FileColumnIteratorUPtr offset_reader) { |
554 | 186 | _offset_iterator = std::move(offset_reader); |
555 | 186 | } |
556 | | |
557 | 186 | ~OffsetFileColumnIterator() override = default; |
558 | | |
559 | | Status init(const ColumnIteratorOptions& opts) override; |
560 | | |
561 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
562 | | |
563 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
564 | 0 | bool has_null; |
565 | 0 | return next_batch(n, dst, &has_null); |
566 | 0 | } |
567 | | |
568 | 0 | ordinal_t get_current_ordinal() const override { |
569 | 0 | return _offset_iterator->get_current_ordinal(); |
570 | 0 | } |
571 | 180 | Status seek_to_ordinal(ordinal_t ord) override { |
572 | 180 | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
573 | 180 | return Status::OK(); |
574 | 180 | } |
575 | | |
576 | | Status _peek_one_offset(ordinal_t* offset); |
577 | | |
578 | | Status _calculate_offsets(ssize_t start, ColumnArray::ColumnOffsets& column_offsets); |
579 | | |
580 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
581 | 8 | MutableColumnPtr& dst) override { |
582 | 8 | return _offset_iterator->read_by_rowids(rowids, count, dst); |
583 | 8 | } |
584 | | |
585 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
586 | | void collect_prefetchers( |
587 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
588 | | PrefetcherInitMethod init_method) override; |
589 | | |
590 | | private: |
591 | | std::unique_ptr<FileColumnIterator> _offset_iterator; |
592 | | // reuse a tiny column for peek to avoid frequent allocations |
593 | | MutableColumnPtr _peek_tmp_col; |
594 | | }; |
595 | | |
596 | | // This iterator is used to read map value column |
597 | | class MapFileColumnIterator final : public ColumnIterator { |
598 | | public: |
599 | | explicit MapFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
600 | | ColumnIteratorUPtr null_iterator, |
601 | | OffsetFileColumnIteratorUPtr offsets_iterator, |
602 | | ColumnIteratorUPtr key_iterator, |
603 | | ColumnIteratorUPtr val_iterator); |
604 | | |
605 | 172 | ~MapFileColumnIterator() override = default; |
606 | | |
607 | | Status init(const ColumnIteratorOptions& opts) override; |
608 | | |
609 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
610 | | |
611 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
612 | | MutableColumnPtr& dst) override; |
613 | | |
614 | | Status seek_to_ordinal(ordinal_t ord) override; |
615 | | |
616 | 0 | ordinal_t get_current_ordinal() const override { |
617 | 0 | if (read_null_map_only() && _null_iterator) { |
618 | 0 | return _null_iterator->get_current_ordinal(); |
619 | 0 | } |
620 | 0 | return _offsets_iterator->get_current_ordinal(); |
621 | 0 | } |
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 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
628 | | const TColumnAccessPaths& predicate_access_paths) override; |
629 | | |
630 | | void set_need_to_read() override; |
631 | | |
632 | | void remove_pruned_sub_iterators() override; |
633 | | |
634 | | private: |
635 | | std::shared_ptr<ColumnReader> _map_reader = nullptr; |
636 | | ColumnIteratorUPtr _null_iterator; |
637 | | OffsetFileColumnIteratorUPtr _offsets_iterator; //OffsetFileIterator |
638 | | ColumnIteratorUPtr _key_iterator; |
639 | | ColumnIteratorUPtr _val_iterator; |
640 | | }; |
641 | | |
642 | | class StructFileColumnIterator final : public ColumnIterator { |
643 | | public: |
644 | | explicit StructFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
645 | | ColumnIteratorUPtr null_iterator, |
646 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators); |
647 | | |
648 | 4 | ~StructFileColumnIterator() override = default; |
649 | | |
650 | | Status init(const ColumnIteratorOptions& opts) override; |
651 | | |
652 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
653 | | |
654 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
655 | 0 | bool has_null; |
656 | 0 | return next_batch(n, dst, &has_null); |
657 | 0 | } |
658 | | |
659 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
660 | | MutableColumnPtr& dst) override; |
661 | | |
662 | | Status seek_to_ordinal(ordinal_t ord) override; |
663 | | |
664 | 0 | ordinal_t get_current_ordinal() const override { |
665 | 0 | if (read_null_map_only() && _null_iterator) { |
666 | 0 | return _null_iterator->get_current_ordinal(); |
667 | 0 | } |
668 | 0 | return _sub_column_iterators[0]->get_current_ordinal(); |
669 | 0 | } |
670 | | |
671 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
672 | | const TColumnAccessPaths& predicate_access_paths) override; |
673 | | |
674 | | void set_need_to_read() override; |
675 | | |
676 | | void remove_pruned_sub_iterators() override; |
677 | | |
678 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
679 | | void collect_prefetchers( |
680 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
681 | | PrefetcherInitMethod init_method) override; |
682 | | |
683 | | private: |
684 | | std::shared_ptr<ColumnReader> _struct_reader = nullptr; |
685 | | ColumnIteratorUPtr _null_iterator; |
686 | | std::vector<ColumnIteratorUPtr> _sub_column_iterators; |
687 | | }; |
688 | | |
689 | | class ArrayFileColumnIterator final : public ColumnIterator { |
690 | | public: |
691 | | explicit ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
692 | | OffsetFileColumnIteratorUPtr offset_reader, |
693 | | ColumnIteratorUPtr item_iterator, |
694 | | ColumnIteratorUPtr null_iterator); |
695 | | |
696 | 12 | ~ArrayFileColumnIterator() override = default; |
697 | | |
698 | | Status init(const ColumnIteratorOptions& opts) override; |
699 | | |
700 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
701 | | |
702 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
703 | 0 | bool has_null; |
704 | 0 | return next_batch(n, dst, &has_null); |
705 | 0 | } |
706 | | |
707 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
708 | | MutableColumnPtr& dst) override; |
709 | | |
710 | | Status seek_to_ordinal(ordinal_t ord) override; |
711 | | |
712 | 0 | ordinal_t get_current_ordinal() const override { |
713 | 0 | if (read_null_map_only() && _null_iterator) { |
714 | 0 | return _null_iterator->get_current_ordinal(); |
715 | 0 | } |
716 | 0 | return _offset_iterator->get_current_ordinal(); |
717 | 0 | } |
718 | | |
719 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
720 | | const TColumnAccessPaths& predicate_access_paths) override; |
721 | | void set_need_to_read() override; |
722 | | |
723 | | void remove_pruned_sub_iterators() override; |
724 | | |
725 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
726 | | void collect_prefetchers( |
727 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
728 | | PrefetcherInitMethod init_method) override; |
729 | | |
730 | | private: |
731 | | std::shared_ptr<ColumnReader> _array_reader = nullptr; |
732 | | std::unique_ptr<OffsetFileColumnIterator> _offset_iterator; |
733 | | std::unique_ptr<ColumnIterator> _null_iterator; |
734 | | std::unique_ptr<ColumnIterator> _item_iterator; |
735 | | |
736 | | Status _seek_by_offsets(ordinal_t ord); |
737 | | }; |
738 | | |
739 | | class RowIdColumnIterator : public ColumnIterator { |
740 | | public: |
741 | | RowIdColumnIterator() = delete; |
742 | | RowIdColumnIterator(int64_t tid, RowsetId rid, int32_t segid) |
743 | 0 | : _tablet_id(tid), _rowset_id(rid), _segment_id(segid) {} |
744 | | |
745 | 0 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
746 | 0 | _current_rowid = cast_set<uint32_t>(ord_idx); |
747 | 0 | return Status::OK(); |
748 | 0 | } |
749 | | |
750 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
751 | 0 | bool has_null; |
752 | 0 | return next_batch(n, dst, &has_null); |
753 | 0 | } |
754 | | |
755 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override { |
756 | 0 | for (size_t i = 0; i < *n; ++i) { |
757 | 0 | const auto row_id = cast_set<uint32_t>(_current_rowid + i); |
758 | 0 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
759 | 0 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
760 | 0 | } |
761 | 0 | _current_rowid += *n; |
762 | 0 | return Status::OK(); |
763 | 0 | } |
764 | | |
765 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
766 | 0 | MutableColumnPtr& dst) override { |
767 | 0 | for (size_t i = 0; i < count; ++i) { |
768 | 0 | rowid_t row_id = rowids[i]; |
769 | 0 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
770 | 0 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
771 | 0 | } |
772 | 0 | return Status::OK(); |
773 | 0 | } |
774 | | |
775 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
776 | | |
777 | | private: |
778 | | rowid_t _current_rowid = 0; |
779 | | int64_t _tablet_id = 0; |
780 | | RowsetId _rowset_id; |
781 | | int32_t _segment_id = 0; |
782 | | }; |
783 | | |
784 | | // Add new RowIdColumnIteratorV2 |
785 | | class RowIdColumnIteratorV2 : public ColumnIterator { |
786 | | public: |
787 | | RowIdColumnIteratorV2(uint8_t version, int64_t backend_id, uint32_t file_id) |
788 | 16 | : _version(version), _backend_id(backend_id), _file_id(file_id) {} |
789 | | |
790 | 0 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
791 | 0 | _current_rowid = cast_set<uint32_t>(ord_idx); |
792 | 0 | return Status::OK(); |
793 | 0 | } |
794 | | |
795 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
796 | 0 | bool has_null; |
797 | 0 | return next_batch(n, dst, &has_null); |
798 | 0 | } |
799 | | |
800 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
801 | | |
802 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
803 | | MutableColumnPtr& dst) override; |
804 | | |
805 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
806 | | |
807 | | private: |
808 | | uint32_t _current_rowid = 0; |
809 | | uint8_t _version; |
810 | | int64_t _backend_id; |
811 | | uint32_t _file_id; |
812 | | }; |
813 | | |
814 | | // This iterator is used to read default value column |
815 | | class DefaultValueColumnIterator : public ColumnIterator { |
816 | | public: |
817 | | DefaultValueColumnIterator(bool has_default_value, std::string default_value, bool is_nullable, |
818 | | FieldType type, int precision, int scale, int len) |
819 | 4 | : _has_default_value(has_default_value), |
820 | 4 | _default_value(std::move(default_value)), |
821 | 4 | _is_nullable(is_nullable), |
822 | 4 | _type(type), |
823 | 4 | _precision(precision), |
824 | 4 | _scale(scale), |
825 | 4 | _len(len) {} |
826 | | |
827 | | Status init(const ColumnIteratorOptions& opts) override; |
828 | | |
829 | 1 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
830 | 1 | _current_rowid = ord_idx; |
831 | 1 | return Status::OK(); |
832 | 1 | } |
833 | | |
834 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
835 | 0 | bool has_null; |
836 | 0 | return next_batch(n, dst, &has_null); |
837 | 0 | } |
838 | | |
839 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
840 | | |
841 | 0 | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override { |
842 | 0 | return next_batch(n, dst); |
843 | 0 | } |
844 | | |
845 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
846 | | MutableColumnPtr& dst) override; |
847 | | |
848 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
849 | | |
850 | | private: |
851 | | void _insert_many_default(MutableColumnPtr& dst, size_t n); |
852 | | |
853 | | bool _has_default_value; |
854 | | std::string _default_value; |
855 | | bool _is_nullable; |
856 | | FieldType _type; |
857 | | int _precision; |
858 | | int _scale; |
859 | | const int _len; |
860 | | Field _default_value_field; |
861 | | |
862 | | // current rowid |
863 | | ordinal_t _current_rowid = 0; |
864 | | }; |
865 | | |
866 | | } // namespace segment_v2 |
867 | | } // namespace doris |