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