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.9k | void sanity_check() const { |
109 | 12.9k | CHECK_NOTNULL(file_reader); |
110 | 12.9k | CHECK_NOTNULL(stats); |
111 | 12.9k | } |
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, const std::string& rowset_id, |
167 | | uint32_t segment_id, size_t rows_of_segment, |
168 | | std::unique_ptr<IndexIterator>* iterator); |
169 | | |
170 | | Status seek_at_or_before(ordinal_t ordinal, OrdinalPageIndexIterator* iter, |
171 | | const ColumnIteratorOptions& iter_opts); |
172 | | Status get_ordinal_index_reader(OrdinalIndexReader*& reader, |
173 | | OlapReaderStatistics* index_load_stats); |
174 | | |
175 | | // read a page from file into a page handle |
176 | | Status read_page(const ColumnIteratorOptions& iter_opts, const PagePointer& pp, |
177 | | PageHandle* handle, Slice* page_body, PageFooterPB* footer, |
178 | | BlockCompressionCodec* codec) const; |
179 | | |
180 | 2.88k | bool is_nullable() const { return _meta_is_nullable; } |
181 | | |
182 | 25.8k | const EncodingInfo* encoding_info() const { return _encoding_info; } |
183 | | |
184 | 0 | bool has_zone_map() const { return _zone_map_index != nullptr; } |
185 | | bool has_bloom_filter_index(bool ngram) const; |
186 | | // Check if this column could match `cond' using segment zone map. |
187 | | // Since segment zone map is stored in metadata, this function is fast without I/O. |
188 | | // set matched to true if segment zone map is absent or `cond' could be satisfied, false otherwise. |
189 | | Status match_condition(const AndBlockColumnPredicate* col_predicates, bool* matched) const; |
190 | | |
191 | | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) const; |
192 | | |
193 | | // get row ranges with zone map |
194 | | // - cond_column is user's query predicate |
195 | | // - delete_condition is a delete predicate of one version |
196 | | Status get_row_ranges_by_zone_map( |
197 | | const AndBlockColumnPredicate* col_predicates, |
198 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
199 | | RowRanges* row_ranges, const ColumnIteratorOptions& iter_opts); |
200 | | |
201 | | // get row ranges with bloom filter index |
202 | | Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
203 | | RowRanges* row_ranges, |
204 | | const ColumnIteratorOptions& iter_opts); |
205 | | |
206 | 2.76k | PagePointer get_dict_page_pointer() const { return _meta_dict_page; } |
207 | | |
208 | 8.07k | bool is_empty() const { return _num_rows == 0; } |
209 | | |
210 | | Status prune_predicates_by_zone_map(std::vector<std::shared_ptr<ColumnPredicate>>& predicates, |
211 | | const int column_id, bool* pruned) const; |
212 | | |
213 | 7.91k | CompressionTypePB get_compression() const { return _meta_compression; } |
214 | | |
215 | 4.28k | uint64_t num_rows() const { return _num_rows; } |
216 | | |
217 | 0 | void set_dict_encoding_type(DictEncodingType type) { |
218 | 0 | static_cast<void>(_set_dict_encoding_type_once.call([&] { |
219 | 0 | _dict_encoding_type = type; |
220 | 0 | return Status::OK(); |
221 | 0 | })); |
222 | 0 | } |
223 | | |
224 | 0 | DictEncodingType get_dict_encoding_type() { return _dict_encoding_type; } |
225 | | |
226 | 7.91k | void disable_index_meta_cache() { _use_index_page_cache = false; } |
227 | | |
228 | 18 | DataTypePtr get_vec_data_type() { return _data_type; } |
229 | | |
230 | 16.6k | virtual FieldType get_meta_type() { return _meta_type; } |
231 | | |
232 | | int64_t get_metadata_size() const override; |
233 | | |
234 | | #ifdef BE_TEST |
235 | | void check_data_by_zone_map_for_test(const MutableColumnPtr& dst) const; |
236 | | #endif |
237 | | |
238 | | private: |
239 | | friend class VariantColumnReader; |
240 | | friend class FileColumnIterator; |
241 | | friend class SegmentPrefetcher; |
242 | | |
243 | | ColumnReader(const ColumnReaderOptions& opts, const ColumnMetaPB& meta, uint64_t num_rows, |
244 | | io::FileReaderSPtr file_reader); |
245 | | Status init(const ColumnMetaPB* meta); |
246 | | |
247 | | [[nodiscard]] Status _load_zone_map_index(bool use_page_cache, bool kept_in_memory, |
248 | | const ColumnIteratorOptions& iter_opts); |
249 | | [[nodiscard]] Status _load_ordinal_index(bool use_page_cache, bool kept_in_memory, |
250 | | const ColumnIteratorOptions& iter_opts); |
251 | | |
252 | | [[nodiscard]] Status _load_index(const std::shared_ptr<IndexFileReader>& index_file_reader, |
253 | | const TabletIndex* index_meta, const std::string& rowset_id, |
254 | | uint32_t segment_id, size_t rows_of_segment); |
255 | | [[nodiscard]] Status _load_bloom_filter_index(bool use_page_cache, bool kept_in_memory, |
256 | | const ColumnIteratorOptions& iter_opts); |
257 | | |
258 | | bool _zone_map_match_condition(const segment_v2::ZoneMap& zone_map, |
259 | | const AndBlockColumnPredicate* col_predicates) const; |
260 | | |
261 | | Status _get_filtered_pages( |
262 | | const AndBlockColumnPredicate* col_predicates, |
263 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
264 | | const RowRanges& input_row_ranges, std::vector<uint32_t>* page_indexes, |
265 | | const ColumnIteratorOptions& iter_opts); |
266 | | |
267 | | Status _calculate_row_ranges(const std::vector<uint32_t>& page_indexes, RowRanges* row_ranges, |
268 | | const ColumnIteratorOptions& iter_opts); |
269 | | |
270 | | int64_t _meta_length; |
271 | | FieldType _meta_type; |
272 | | FieldType _meta_children_column_type; |
273 | | bool _meta_is_nullable; |
274 | | bool _use_index_page_cache; |
275 | | int _be_exec_version = -1; |
276 | | |
277 | | PagePointer _meta_dict_page; |
278 | | CompressionTypePB _meta_compression; |
279 | | |
280 | | ColumnReaderOptions _opts; |
281 | | uint64_t _num_rows; |
282 | | |
283 | | io::FileReaderSPtr _file_reader; |
284 | | |
285 | | DictEncodingType _dict_encoding_type; |
286 | | |
287 | | DataTypePtr _data_type; |
288 | | |
289 | | FieldType _type = |
290 | | FieldType::OLAP_FIELD_TYPE_NONE; // initialized in init(), may changed by subclasses. |
291 | | const EncodingInfo* _encoding_info = |
292 | | nullptr; // initialized in init(), used for create PageDecoder |
293 | | |
294 | | // meta for various column indexes (null if the index is absent) |
295 | | std::unique_ptr<ZoneMapPB> _segment_zone_map; |
296 | | |
297 | | mutable std::shared_mutex _load_index_lock; |
298 | | std::unique_ptr<ZoneMapIndexReader> _zone_map_index; |
299 | | std::unique_ptr<OrdinalIndexReader> _ordinal_index; |
300 | | std::shared_ptr<BloomFilterIndexReader> _bloom_filter_index; |
301 | | |
302 | | std::unordered_map<int64_t, IndexReaderPtr> _index_readers; |
303 | | |
304 | | std::vector<std::shared_ptr<ColumnReader>> _sub_readers; |
305 | | |
306 | | DorisCallOnce<Status> _set_dict_encoding_type_once; |
307 | | }; |
308 | | |
309 | | // Base iterator to read one column data |
310 | | class ColumnIterator { |
311 | | public: |
312 | 8.58k | ColumnIterator() = default; |
313 | 8.58k | virtual ~ColumnIterator() = default; |
314 | | |
315 | 46 | virtual Status init(const ColumnIteratorOptions& opts) { |
316 | 46 | _opts = opts; |
317 | 46 | return Status::OK(); |
318 | 46 | } |
319 | | |
320 | | // Seek to the given ordinal entry in the column. |
321 | | // Entry 0 is the first entry written to the column. |
322 | | // If provided seek point is past the end of the file, |
323 | | // then returns false. |
324 | | virtual Status seek_to_ordinal(ordinal_t ord) = 0; |
325 | | |
326 | 18.6k | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
327 | 18.6k | bool has_null; |
328 | 18.6k | return next_batch(n, dst, &has_null); |
329 | 18.6k | } |
330 | | |
331 | 0 | virtual Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) { |
332 | 0 | return Status::NotSupported("next_batch not implement"); |
333 | 0 | } |
334 | | |
335 | 0 | virtual Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) { |
336 | 0 | return Status::NotSupported("next_batch_of_zone_map not implement"); |
337 | 0 | } |
338 | | |
339 | | virtual Status read_by_rowids(const rowid_t* rowids, const size_t count, |
340 | 0 | MutableColumnPtr& dst) { |
341 | 0 | return Status::NotSupported("read_by_rowids not implement"); |
342 | 0 | } |
343 | | |
344 | | virtual ordinal_t get_current_ordinal() const = 0; |
345 | | |
346 | | virtual Status get_row_ranges_by_zone_map( |
347 | | const AndBlockColumnPredicate* col_predicates, |
348 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
349 | 0 | RowRanges* row_ranges) { |
350 | 0 | return Status::OK(); |
351 | 0 | } |
352 | | |
353 | | virtual Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
354 | 0 | RowRanges* row_ranges) { |
355 | 0 | return Status::OK(); |
356 | 0 | } |
357 | | |
358 | | virtual Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
359 | 0 | RowRanges* row_ranges) { |
360 | 0 | return Status::OK(); |
361 | 0 | } |
362 | | |
363 | 0 | virtual bool is_all_dict_encoding() const { return false; } |
364 | | |
365 | | virtual Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
366 | 4 | const TColumnAccessPaths& predicate_access_paths) { |
367 | 4 | if (!predicate_access_paths.empty()) { |
368 | 2 | _reading_flag = ReadingFlag::READING_FOR_PREDICATE; |
369 | 2 | } |
370 | 4 | return Status::OK(); |
371 | 4 | } |
372 | | |
373 | 8.18k | void set_column_name(const std::string& column_name) { _column_name = column_name; } |
374 | | |
375 | 10 | const std::string& column_name() const { return _column_name; } |
376 | | |
377 | | // Since there may be multiple paths with conflicts or overlaps, |
378 | | // we need to define several reading flags: |
379 | | // |
380 | | // NORMAL_READING — Default value, indicating that the column should be read. |
381 | | // SKIP_READING — The column should not be read. |
382 | | // NEED_TO_READ — The column must be read. |
383 | | // READING_FOR_PREDICATE — The column is required for predicate evaluation. |
384 | | // |
385 | | // For example, suppose there are two paths: |
386 | | // - Path 1 specifies that column A needs to be read, so it is marked as NEED_TO_READ. |
387 | | // - Path 2 specifies that the column should not be read, but since it is already marked as NEED_TO_READ, |
388 | | // it should not be changed to SKIP_READING. |
389 | | enum class ReadingFlag : int { |
390 | | NORMAL_READING, |
391 | | SKIP_READING, |
392 | | NEED_TO_READ, |
393 | | READING_FOR_PREDICATE |
394 | | }; |
395 | 26 | void set_reading_flag(ReadingFlag flag) { |
396 | 26 | if (static_cast<int>(flag) > static_cast<int>(_reading_flag)) { |
397 | 16 | _reading_flag = flag; |
398 | 16 | } |
399 | 26 | } |
400 | | |
401 | 206 | ReadingFlag reading_flag() const { return _reading_flag; } |
402 | | |
403 | 3 | virtual void set_need_to_read() { set_reading_flag(ReadingFlag::NEED_TO_READ); } |
404 | | |
405 | 0 | virtual void remove_pruned_sub_iterators() {}; |
406 | | |
407 | 0 | virtual Status init_prefetcher(const SegmentPrefetchParams& params) { return Status::OK(); } |
408 | | |
409 | | virtual void collect_prefetchers( |
410 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
411 | 0 | PrefetcherInitMethod init_method) {} |
412 | | |
413 | | static constexpr const char* ACCESS_OFFSET = "OFFSET"; |
414 | | static constexpr const char* ACCESS_ALL = "*"; |
415 | | static constexpr const char* ACCESS_MAP_KEYS = "KEYS"; |
416 | | static constexpr const char* ACCESS_MAP_VALUES = "VALUES"; |
417 | | static constexpr const char* ACCESS_NULL = "NULL"; |
418 | | |
419 | | // Meta-only read modes: |
420 | | // - OFFSET_ONLY: only read offset information (e.g., for array_size/map_size/string_length) |
421 | | // - NULL_MAP_ONLY: only read null map (e.g., for IS NULL / IS NOT NULL predicates) |
422 | | // When these modes are enabled, actual content data is skipped. |
423 | | enum class ReadMode : int { DEFAULT, OFFSET_ONLY, NULL_MAP_ONLY }; |
424 | | |
425 | 3.00k | bool read_offset_only() const { return _read_mode == ReadMode::OFFSET_ONLY; } |
426 | 23.2k | bool read_null_map_only() const { return _read_mode == ReadMode::NULL_MAP_ONLY; } |
427 | | |
428 | | protected: |
429 | | // Checks sub access paths for OFFSET or NULL meta-only modes and |
430 | | // updates _read_mode accordingly. Use the accessor helpers |
431 | | // read_offset_only() / read_null_map_only() to query the current mode. |
432 | | void _check_and_set_meta_read_mode(const TColumnAccessPaths& sub_all_access_paths); |
433 | | |
434 | | Result<TColumnAccessPaths> _get_sub_access_paths(const TColumnAccessPaths& access_paths); |
435 | | ColumnIteratorOptions _opts; |
436 | | |
437 | | ReadingFlag _reading_flag {ReadingFlag::NORMAL_READING}; |
438 | | ReadMode _read_mode = ReadMode::DEFAULT; |
439 | | std::string _column_name; |
440 | | }; |
441 | | |
442 | | // This iterator is used to read column data from file |
443 | | // for scalar type |
444 | | class FileColumnIterator : public ColumnIterator { |
445 | | public: |
446 | | explicit FileColumnIterator(std::shared_ptr<ColumnReader> reader); |
447 | | ~FileColumnIterator() override; |
448 | | |
449 | | Status init(const ColumnIteratorOptions& opts) override; |
450 | | |
451 | | Status seek_to_ordinal(ordinal_t ord) override; |
452 | | |
453 | | Status seek_to_page_start(); |
454 | | |
455 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
456 | | |
457 | | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override; |
458 | | |
459 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
460 | | MutableColumnPtr& dst) override; |
461 | | |
462 | 2 | ordinal_t get_current_ordinal() const override { return _current_ordinal; } |
463 | | |
464 | | // get row ranges by zone map |
465 | | // - cond_column is user's query predicate |
466 | | // - delete_condition is delete predicate of one version |
467 | | Status get_row_ranges_by_zone_map( |
468 | | const AndBlockColumnPredicate* col_predicates, |
469 | | const std::vector<std::shared_ptr<const ColumnPredicate>>* delete_predicates, |
470 | | RowRanges* row_ranges) override; |
471 | | |
472 | | Status get_row_ranges_by_bloom_filter(const AndBlockColumnPredicate* col_predicates, |
473 | | RowRanges* row_ranges) override; |
474 | | |
475 | | Status get_row_ranges_by_dict(const AndBlockColumnPredicate* col_predicates, |
476 | | RowRanges* row_ranges) override; |
477 | | |
478 | 728 | ParsedPage* get_current_page() { return &_page; } |
479 | | |
480 | 0 | bool is_nullable() { return _reader->is_nullable(); } |
481 | | |
482 | 0 | bool is_all_dict_encoding() const override { return _is_all_dict_encoding; } |
483 | | |
484 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
485 | | void collect_prefetchers( |
486 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
487 | | PrefetcherInitMethod init_method) override; |
488 | | |
489 | | protected: |
490 | | // Exposed to derived iterators (e.g. StringFileColumnIterator) so they can |
491 | | // query column metadata such as the storage field type. |
492 | 2 | const std::shared_ptr<ColumnReader>& get_reader() const { return _reader; } |
493 | | |
494 | | private: |
495 | | Status _seek_to_pos_in_page(ParsedPage* page, ordinal_t offset_in_page) const; |
496 | | Status _load_next_page(bool* eos); |
497 | | Status _read_data_page(const OrdinalPageIndexIterator& iter); |
498 | | Status _read_dict_data(); |
499 | | void _trigger_prefetch_if_eligible(ordinal_t ord); |
500 | | |
501 | | std::shared_ptr<ColumnReader> _reader = nullptr; |
502 | | |
503 | | BlockCompressionCodec* _compress_codec = nullptr; |
504 | | |
505 | | // 1. The _page represents current page. |
506 | | // 2. We define an operation is one seek and following read, |
507 | | // If new seek is issued, the _page will be reset. |
508 | | ParsedPage _page; |
509 | | |
510 | | // keep dict page decoder |
511 | | std::unique_ptr<PageDecoder> _dict_decoder; |
512 | | |
513 | | // keep dict page handle to avoid released |
514 | | PageHandle _dict_page_handle; |
515 | | |
516 | | // page iterator used to get next page when current page is finished. |
517 | | // This value will be reset when a new seek is issued |
518 | | OrdinalPageIndexIterator _page_iter; |
519 | | |
520 | | // current value ordinal |
521 | | ordinal_t _current_ordinal = 0; |
522 | | |
523 | | bool _is_all_dict_encoding = false; |
524 | | |
525 | | std::unique_ptr<StringRef[]> _dict_word_info; |
526 | | |
527 | | bool _enable_prefetch {false}; |
528 | | std::unique_ptr<SegmentPrefetcher> _prefetcher; |
529 | | std::shared_ptr<io::CachedRemoteFileReader> _cached_remote_file_reader {nullptr}; |
530 | | }; |
531 | | |
532 | | class EmptyFileColumnIterator final : public ColumnIterator { |
533 | | public: |
534 | 46 | Status seek_to_ordinal(ordinal_t ord) override { return Status::OK(); } |
535 | 0 | ordinal_t get_current_ordinal() const override { return 0; } |
536 | | }; |
537 | | |
538 | | // StringFileColumnIterator extends FileColumnIterator with meta-only reading |
539 | | // support for string/binary column types. When the OFFSET path is detected in |
540 | | // set_access_paths, it sets only_read_offsets on the ColumnIteratorOptions so |
541 | | // that the BinaryPlainPageDecoder skips chars memcpy and only fills offsets. |
542 | | class StringFileColumnIterator final : public FileColumnIterator { |
543 | | public: |
544 | | explicit StringFileColumnIterator(std::shared_ptr<ColumnReader> reader); |
545 | 2.66k | ~StringFileColumnIterator() override = default; |
546 | | |
547 | | Status init(const ColumnIteratorOptions& opts) override; |
548 | | |
549 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
550 | | const TColumnAccessPaths& predicate_access_paths) override; |
551 | | }; |
552 | | |
553 | | // This iterator make offset operation write once for |
554 | | class OffsetFileColumnIterator final : public ColumnIterator { |
555 | | public: |
556 | 189 | explicit OffsetFileColumnIterator(FileColumnIteratorUPtr offset_reader) { |
557 | 189 | _offset_iterator = std::move(offset_reader); |
558 | 189 | } |
559 | | |
560 | 189 | ~OffsetFileColumnIterator() 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 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
567 | 0 | bool has_null; |
568 | 0 | return next_batch(n, dst, &has_null); |
569 | 0 | } |
570 | | |
571 | 0 | ordinal_t get_current_ordinal() const override { |
572 | 0 | return _offset_iterator->get_current_ordinal(); |
573 | 0 | } |
574 | 183 | Status seek_to_ordinal(ordinal_t ord) override { |
575 | 183 | RETURN_IF_ERROR(_offset_iterator->seek_to_ordinal(ord)); |
576 | 183 | return Status::OK(); |
577 | 183 | } |
578 | | |
579 | | Status _peek_one_offset(ordinal_t* offset); |
580 | | |
581 | | Status _calculate_offsets(ssize_t start, ColumnArray::ColumnOffsets& column_offsets); |
582 | | |
583 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
584 | 6 | MutableColumnPtr& dst) override { |
585 | 6 | return _offset_iterator->read_by_rowids(rowids, count, dst); |
586 | 6 | } |
587 | | |
588 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
589 | | void collect_prefetchers( |
590 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
591 | | PrefetcherInitMethod init_method) override; |
592 | | |
593 | | private: |
594 | | std::unique_ptr<FileColumnIterator> _offset_iterator; |
595 | | // reuse a tiny column for peek to avoid frequent allocations |
596 | | MutableColumnPtr _peek_tmp_col; |
597 | | }; |
598 | | |
599 | | // This iterator is used to read map value column |
600 | | class MapFileColumnIterator final : public ColumnIterator { |
601 | | public: |
602 | | explicit MapFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
603 | | ColumnIteratorUPtr null_iterator, |
604 | | OffsetFileColumnIteratorUPtr offsets_iterator, |
605 | | ColumnIteratorUPtr key_iterator, |
606 | | ColumnIteratorUPtr val_iterator); |
607 | | |
608 | 175 | ~MapFileColumnIterator() override = default; |
609 | | |
610 | | Status init(const ColumnIteratorOptions& opts) override; |
611 | | |
612 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
613 | | |
614 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
615 | | MutableColumnPtr& dst) override; |
616 | | |
617 | | Status seek_to_ordinal(ordinal_t ord) override; |
618 | | |
619 | 0 | ordinal_t get_current_ordinal() const override { |
620 | 0 | if (read_null_map_only() && _null_iterator) { |
621 | 0 | return _null_iterator->get_current_ordinal(); |
622 | 0 | } |
623 | 0 | return _offsets_iterator->get_current_ordinal(); |
624 | 0 | } |
625 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
626 | | void collect_prefetchers( |
627 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
628 | | PrefetcherInitMethod init_method) override; |
629 | | |
630 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
631 | | const TColumnAccessPaths& predicate_access_paths) override; |
632 | | |
633 | | void set_need_to_read() override; |
634 | | |
635 | | void remove_pruned_sub_iterators() override; |
636 | | |
637 | | private: |
638 | | std::shared_ptr<ColumnReader> _map_reader = nullptr; |
639 | | ColumnIteratorUPtr _null_iterator; |
640 | | OffsetFileColumnIteratorUPtr _offsets_iterator; //OffsetFileIterator |
641 | | ColumnIteratorUPtr _key_iterator; |
642 | | ColumnIteratorUPtr _val_iterator; |
643 | | }; |
644 | | |
645 | | class StructFileColumnIterator final : public ColumnIterator { |
646 | | public: |
647 | | explicit StructFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
648 | | ColumnIteratorUPtr null_iterator, |
649 | | std::vector<ColumnIteratorUPtr>&& sub_column_iterators); |
650 | | |
651 | 4 | ~StructFileColumnIterator() override = default; |
652 | | |
653 | | Status init(const ColumnIteratorOptions& opts) override; |
654 | | |
655 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
656 | | |
657 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
658 | 0 | bool has_null; |
659 | 0 | return next_batch(n, dst, &has_null); |
660 | 0 | } |
661 | | |
662 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
663 | | MutableColumnPtr& dst) override; |
664 | | |
665 | | Status seek_to_ordinal(ordinal_t ord) override; |
666 | | |
667 | 0 | ordinal_t get_current_ordinal() const override { |
668 | 0 | if (read_null_map_only() && _null_iterator) { |
669 | 0 | return _null_iterator->get_current_ordinal(); |
670 | 0 | } |
671 | 0 | return _sub_column_iterators[0]->get_current_ordinal(); |
672 | 0 | } |
673 | | |
674 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
675 | | const TColumnAccessPaths& predicate_access_paths) override; |
676 | | |
677 | | void set_need_to_read() override; |
678 | | |
679 | | void remove_pruned_sub_iterators() override; |
680 | | |
681 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
682 | | void collect_prefetchers( |
683 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
684 | | PrefetcherInitMethod init_method) override; |
685 | | |
686 | | private: |
687 | | std::shared_ptr<ColumnReader> _struct_reader = nullptr; |
688 | | ColumnIteratorUPtr _null_iterator; |
689 | | std::vector<ColumnIteratorUPtr> _sub_column_iterators; |
690 | | }; |
691 | | |
692 | | class ArrayFileColumnIterator final : public ColumnIterator { |
693 | | public: |
694 | | explicit ArrayFileColumnIterator(std::shared_ptr<ColumnReader> reader, |
695 | | OffsetFileColumnIteratorUPtr offset_reader, |
696 | | ColumnIteratorUPtr item_iterator, |
697 | | ColumnIteratorUPtr null_iterator); |
698 | | |
699 | 12 | ~ArrayFileColumnIterator() override = default; |
700 | | |
701 | | Status init(const ColumnIteratorOptions& opts) override; |
702 | | |
703 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
704 | | |
705 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
706 | 0 | bool has_null; |
707 | 0 | return next_batch(n, dst, &has_null); |
708 | 0 | } |
709 | | |
710 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
711 | | MutableColumnPtr& dst) override; |
712 | | |
713 | | Status seek_to_ordinal(ordinal_t ord) override; |
714 | | |
715 | 0 | ordinal_t get_current_ordinal() const override { |
716 | 0 | if (read_null_map_only() && _null_iterator) { |
717 | 0 | return _null_iterator->get_current_ordinal(); |
718 | 0 | } |
719 | 0 | return _offset_iterator->get_current_ordinal(); |
720 | 0 | } |
721 | | |
722 | | Status set_access_paths(const TColumnAccessPaths& all_access_paths, |
723 | | const TColumnAccessPaths& predicate_access_paths) override; |
724 | | void set_need_to_read() override; |
725 | | |
726 | | void remove_pruned_sub_iterators() override; |
727 | | |
728 | | Status init_prefetcher(const SegmentPrefetchParams& params) override; |
729 | | void collect_prefetchers( |
730 | | std::map<PrefetcherInitMethod, std::vector<SegmentPrefetcher*>>& prefetchers, |
731 | | PrefetcherInitMethod init_method) override; |
732 | | |
733 | | private: |
734 | | std::shared_ptr<ColumnReader> _array_reader = nullptr; |
735 | | std::unique_ptr<OffsetFileColumnIterator> _offset_iterator; |
736 | | std::unique_ptr<ColumnIterator> _null_iterator; |
737 | | std::unique_ptr<ColumnIterator> _item_iterator; |
738 | | |
739 | | Status _seek_by_offsets(ordinal_t ord); |
740 | | }; |
741 | | |
742 | | class RowIdColumnIterator : public ColumnIterator { |
743 | | public: |
744 | | RowIdColumnIterator() = delete; |
745 | | RowIdColumnIterator(int64_t tid, RowsetId rid, int32_t segid) |
746 | 0 | : _tablet_id(tid), _rowset_id(rid), _segment_id(segid) {} |
747 | | |
748 | 0 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
749 | 0 | _current_rowid = cast_set<uint32_t>(ord_idx); |
750 | 0 | return Status::OK(); |
751 | 0 | } |
752 | | |
753 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
754 | 0 | bool has_null; |
755 | 0 | return next_batch(n, dst, &has_null); |
756 | 0 | } |
757 | | |
758 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override { |
759 | 0 | for (size_t i = 0; i < *n; ++i) { |
760 | 0 | const auto row_id = cast_set<uint32_t>(_current_rowid + i); |
761 | 0 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
762 | 0 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
763 | 0 | } |
764 | 0 | _current_rowid += *n; |
765 | 0 | return Status::OK(); |
766 | 0 | } |
767 | | |
768 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
769 | 0 | MutableColumnPtr& dst) override { |
770 | 0 | for (size_t i = 0; i < count; ++i) { |
771 | 0 | rowid_t row_id = rowids[i]; |
772 | 0 | GlobalRowLoacation location(_tablet_id, _rowset_id, _segment_id, row_id); |
773 | 0 | dst->insert_data(reinterpret_cast<const char*>(&location), sizeof(GlobalRowLoacation)); |
774 | 0 | } |
775 | 0 | return Status::OK(); |
776 | 0 | } |
777 | | |
778 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
779 | | |
780 | | private: |
781 | | rowid_t _current_rowid = 0; |
782 | | int64_t _tablet_id = 0; |
783 | | RowsetId _rowset_id; |
784 | | int32_t _segment_id = 0; |
785 | | }; |
786 | | |
787 | | // Add new RowIdColumnIteratorV2 |
788 | | class RowIdColumnIteratorV2 : public ColumnIterator { |
789 | | public: |
790 | | RowIdColumnIteratorV2(uint8_t version, int64_t backend_id, uint32_t file_id) |
791 | 16 | : _version(version), _backend_id(backend_id), _file_id(file_id) {} |
792 | | |
793 | 0 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
794 | 0 | _current_rowid = cast_set<uint32_t>(ord_idx); |
795 | 0 | return Status::OK(); |
796 | 0 | } |
797 | | |
798 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
799 | 0 | bool has_null; |
800 | 0 | return next_batch(n, dst, &has_null); |
801 | 0 | } |
802 | | |
803 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
804 | | |
805 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
806 | | MutableColumnPtr& dst) override; |
807 | | |
808 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
809 | | |
810 | | private: |
811 | | uint32_t _current_rowid = 0; |
812 | | uint8_t _version; |
813 | | int64_t _backend_id; |
814 | | uint32_t _file_id; |
815 | | }; |
816 | | |
817 | | // This iterator is used to read default value column |
818 | | class DefaultValueColumnIterator : public ColumnIterator { |
819 | | public: |
820 | | DefaultValueColumnIterator(bool has_default_value, std::string default_value, bool is_nullable, |
821 | | FieldType type, int precision, int scale, int len) |
822 | 4 | : _has_default_value(has_default_value), |
823 | 4 | _default_value(std::move(default_value)), |
824 | 4 | _is_nullable(is_nullable), |
825 | 4 | _type(type), |
826 | 4 | _precision(precision), |
827 | 4 | _scale(scale), |
828 | 4 | _len(len) {} |
829 | | |
830 | | Status init(const ColumnIteratorOptions& opts) override; |
831 | | |
832 | 1 | Status seek_to_ordinal(ordinal_t ord_idx) override { |
833 | 1 | _current_rowid = ord_idx; |
834 | 1 | return Status::OK(); |
835 | 1 | } |
836 | | |
837 | 0 | Status next_batch(size_t* n, MutableColumnPtr& dst) { |
838 | 0 | bool has_null; |
839 | 0 | return next_batch(n, dst, &has_null); |
840 | 0 | } |
841 | | |
842 | | Status next_batch(size_t* n, MutableColumnPtr& dst, bool* has_null) override; |
843 | | |
844 | 0 | Status next_batch_of_zone_map(size_t* n, MutableColumnPtr& dst) override { |
845 | 0 | return next_batch(n, dst); |
846 | 0 | } |
847 | | |
848 | | Status read_by_rowids(const rowid_t* rowids, const size_t count, |
849 | | MutableColumnPtr& dst) override; |
850 | | |
851 | 0 | ordinal_t get_current_ordinal() const override { return _current_rowid; } |
852 | | |
853 | | private: |
854 | | void _insert_many_default(MutableColumnPtr& dst, size_t n); |
855 | | |
856 | | bool _has_default_value; |
857 | | std::string _default_value; |
858 | | bool _is_nullable; |
859 | | FieldType _type; |
860 | | int _precision; |
861 | | int _scale; |
862 | | const int _len; |
863 | | Field _default_value_field; |
864 | | |
865 | | // current rowid |
866 | | ordinal_t _current_rowid = 0; |
867 | | }; |
868 | | |
869 | | } // namespace segment_v2 |
870 | | } // namespace doris |