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