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/PlanNodes_types.h> |
21 | | |
22 | | #include <functional> |
23 | | #include <memory> |
24 | | #include <set> |
25 | | #include <string> |
26 | | #include <tuple> |
27 | | #include <unordered_map> |
28 | | #include <vector> |
29 | | |
30 | | #include "common/status.h" |
31 | | #include "core/column/column.h" |
32 | | #include "core/column/column_nullable.h" |
33 | | #include "core/data_type/data_type.h" |
34 | | #include "exprs/vexpr.h" |
35 | | #include "exprs/vexpr_context.h" |
36 | | #include "exprs/vexpr_fwd.h" |
37 | | #include "format/column_descriptor.h" |
38 | | #include "format/table/table_schema_change_helper.h" |
39 | | #include "runtime/descriptors.h" |
40 | | #include "runtime/runtime_state.h" |
41 | | #include "storage/predicate/block_column_predicate.h" |
42 | | #include "storage/segment/common.h" |
43 | | #include "util/profile_collector.h" |
44 | | |
45 | | namespace doris { |
46 | | class ColumnPredicate; |
47 | | } // namespace doris |
48 | | |
49 | | namespace doris { |
50 | | #include "common/compile_check_begin.h" |
51 | | |
52 | | class Block; |
53 | | class VSlotRef; |
54 | | |
55 | | // Context passed from FileScanner to readers for condition cache integration. |
56 | | // On MISS: readers populate filter_result per-granule during predicate evaluation. |
57 | | // On HIT: readers skip granules where filter_result[granule] == false. |
58 | | struct ConditionCacheContext { |
59 | | bool is_hit = false; |
60 | | std::shared_ptr<std::vector<bool>> filter_result; // per-granule: true = has surviving rows |
61 | | int64_t base_granule = 0; // global granule index of the first granule in filter_result |
62 | | static constexpr int GRANULE_SIZE = 2048; |
63 | | }; |
64 | | |
65 | | /// Base context for the unified init_reader(ReaderInitContext*) template method. |
66 | | /// Contains fields shared by ALL reader types. Format-specific readers define |
67 | | /// subclasses (ParquetInitContext, OrcInitContext, etc.) with extra fields. |
68 | | /// FileScanner allocates the appropriate subclass and populates the shared fields |
69 | | /// before calling init_reader(). |
70 | | struct ReaderInitContext { |
71 | 103 | virtual ~ReaderInitContext() = default; |
72 | | |
73 | | // ---- Owned by FileScanner, shared by all readers ---- |
74 | | std::vector<ColumnDescriptor>* column_descs = nullptr; |
75 | | std::unordered_map<std::string, uint32_t>* col_name_to_block_idx = nullptr; |
76 | | RuntimeState* state = nullptr; |
77 | | const TupleDescriptor* tuple_descriptor = nullptr; |
78 | | const RowDescriptor* row_descriptor = nullptr; |
79 | | const TFileScanRangeParams* params = nullptr; |
80 | | const TFileRangeDesc* range = nullptr; |
81 | | TPushAggOp::type push_down_agg_type = TPushAggOp::type::NONE; |
82 | | |
83 | | // ---- Output slots (filled by on_before_init_reader) ---- |
84 | | std::vector<std::string> column_names; |
85 | | std::shared_ptr<TableSchemaChangeHelper::Node> table_info_node = |
86 | | TableSchemaChangeHelper::ConstNode::get_instance(); |
87 | | std::set<uint64_t> column_ids; |
88 | | std::set<uint64_t> filter_column_ids; |
89 | | }; |
90 | | |
91 | | /// Safe downcast for ReaderInitContext subclasses. |
92 | | /// Uses dynamic_cast + DORIS_CHECK: crashes on type mismatch (per Doris coding standards). |
93 | | template <typename To, typename From> |
94 | 102 | To* checked_context_cast(From* ptr) { |
95 | 102 | auto* result = dynamic_cast<To*>(ptr); |
96 | 102 | DORIS_CHECK(result != nullptr); |
97 | 102 | return result; |
98 | 102 | } _ZN5doris20checked_context_castINS_14OrcInitContextENS_17ReaderInitContextEEEPT_PT0_ Line | Count | Source | 94 | 36 | To* checked_context_cast(From* ptr) { | 95 | 36 | auto* result = dynamic_cast<To*>(ptr); | 96 | 36 | DORIS_CHECK(result != nullptr); | 97 | 36 | return result; | 98 | 36 | } |
Unexecuted instantiation: _ZN5doris20checked_context_castINS_14CsvInitContextENS_17ReaderInitContextEEEPT_PT0_ Unexecuted instantiation: _ZN5doris20checked_context_castINS_15JsonInitContextENS_17ReaderInitContextEEEPT_PT0_ _ZN5doris20checked_context_castINS_18ParquetInitContextENS_17ReaderInitContextEEEPT_PT0_ Line | Count | Source | 94 | 66 | To* checked_context_cast(From* ptr) { | 95 | 66 | auto* result = dynamic_cast<To*>(ptr); | 96 | 66 | DORIS_CHECK(result != nullptr); | 97 | 66 | return result; | 98 | 66 | } |
Unexecuted instantiation: _ZN5doris20checked_context_castINS_14WalInitContextENS_17ReaderInitContextEEEPT_PT0_ |
99 | | |
100 | | /// Base reader interface for all file readers. |
101 | | /// A GenericReader is responsible for reading a file and returning |
102 | | /// a set of blocks with specified schema. |
103 | | /// |
104 | | /// Provides hook virtual methods that implement the Template Method pattern: |
105 | | /// init_reader: _open_file_reader → on_before_init_reader → _do_init_reader → on_after_init_reader |
106 | | /// get_next_block: on_before_read_block → _do_get_next_block → on_after_read_block |
107 | | /// |
108 | | /// Column-filling logic (partition/missing/synthesized) lives in TableFormatReader. |
109 | | class GenericReader : public ProfileCollector { |
110 | | public: |
111 | 198 | GenericReader() : _push_down_agg_type(TPushAggOp::type::NONE) {} |
112 | 130 | void set_push_down_agg_type(TPushAggOp::type push_down_agg_type) { |
113 | 130 | if (!_push_down_agg_type_locked) { |
114 | 130 | _push_down_agg_type = push_down_agg_type; |
115 | 130 | } |
116 | 130 | } |
117 | | // Lock the current push_down_agg_type so FileScanner cannot override it. |
118 | | // Used by readers that must disable COUNT pushdown (e.g., ACID deletes, Paimon DV). |
119 | 0 | void lock_push_down_agg_type() { _push_down_agg_type_locked = true; } |
120 | 0 | TPushAggOp::type get_push_down_agg_type() const { return _push_down_agg_type; } |
121 | | |
122 | | /// Template method for reading blocks. |
123 | | /// Calls: on_before_read_block → _do_get_next_block → on_after_read_block |
124 | 179 | Status get_next_block(Block* block, size_t* read_rows, bool* eof) { |
125 | 179 | RETURN_IF_ERROR(on_before_read_block(block)); |
126 | 179 | RETURN_IF_ERROR(_do_get_next_block(block, read_rows, eof)); |
127 | 179 | RETURN_IF_ERROR(on_after_read_block(block, read_rows)); |
128 | 179 | return Status::OK(); |
129 | 179 | } |
130 | | |
131 | | // Type is always nullable to process illegal values. |
132 | | // Results are cached after the first successful call. |
133 | 26 | Status get_columns(std::unordered_map<std::string, DataTypePtr>* name_to_type) { |
134 | 26 | if (_get_columns_cached) { |
135 | 0 | *name_to_type = _cached_name_to_type; |
136 | 0 | return Status::OK(); |
137 | 0 | } |
138 | 26 | RETURN_IF_ERROR(_get_columns_impl(name_to_type)); |
139 | 26 | _cached_name_to_type = *name_to_type; |
140 | 26 | _get_columns_cached = true; |
141 | | |
142 | 26 | return Status::OK(); |
143 | 26 | } |
144 | | |
145 | 0 | virtual Status _get_columns_impl(std::unordered_map<std::string, DataTypePtr>* name_to_type) { |
146 | 0 | return Status::NotSupported("get_columns is not implemented"); |
147 | 0 | } |
148 | | |
149 | | // This method is responsible for initializing the resource for parsing schema. |
150 | | // It will be called before `get_parsed_schema`. |
151 | 0 | virtual Status init_schema_reader() { |
152 | 0 | return Status::NotSupported("init_schema_reader is not implemented for this reader."); |
153 | 0 | } |
154 | | // `col_types` is always nullable to process illegal values. |
155 | | virtual Status get_parsed_schema(std::vector<std::string>* col_names, |
156 | 0 | std::vector<DataTypePtr>* col_types) { |
157 | 0 | return Status::NotSupported("get_parsed_schema is not implemented for this reader."); |
158 | 0 | } |
159 | 198 | ~GenericReader() override = default; |
160 | | |
161 | 11 | virtual Status close() { return Status::OK(); } |
162 | | |
163 | 41 | Status read_by_rows(const std::list<int64_t>& row_ids) { |
164 | 41 | _read_by_rows = true; |
165 | 41 | _row_ids = row_ids; |
166 | 41 | return _set_read_one_line_impl(); |
167 | 41 | } |
168 | | |
169 | | /// The reader is responsible for counting the number of rows read, |
170 | | /// because some readers, such as parquet/orc, |
171 | | /// can skip some pages/rowgroups through indexes. |
172 | 0 | virtual bool count_read_rows() { return false; } |
173 | | |
174 | | /// Returns true if on_before_init_reader has already set _column_descs. |
175 | 204 | bool has_column_descs() const { return _column_descs != nullptr; } |
176 | | |
177 | | /// Unified initialization entry point (NVI pattern). |
178 | | /// Enforces the template method sequence for ALL readers: |
179 | | /// _open_file_reader → on_before_init_reader → _do_init_reader → on_after_init_reader |
180 | | /// Subclasses implement _open_file_reader and _do_init_reader(ReaderInitContext*). |
181 | | /// FileScanner constructs the appropriate ReaderInitContext subclass and calls this. |
182 | | /// |
183 | | /// NOTE: During migration, readers not yet ported to this API still use their |
184 | | /// format-specific init_reader(...) methods. This method is non-virtual so it |
185 | | /// cannot be accidentally overridden. |
186 | 102 | Status init_reader(ReaderInitContext* ctx) { |
187 | | // Apply push_down_agg_type early so _open_file_reader and _do_init_reader |
188 | | // can use it (e.g., PaimonCppReader skips full init on COUNT pushdown). |
189 | | // on_after_init_reader may reset this (e.g., Iceberg with equality deletes). |
190 | 102 | set_push_down_agg_type(ctx->push_down_agg_type); |
191 | | |
192 | 102 | RETURN_IF_ERROR(_open_file_reader(ctx)); |
193 | | |
194 | | // Standalone readers (delete file readers, push handler) set column_descs=nullptr |
195 | | // and pre-populate column_names directly. Skip hooks for them. |
196 | 102 | if (ctx->column_descs != nullptr) { |
197 | 36 | RETURN_IF_ERROR(on_before_init_reader(ctx)); |
198 | 36 | } |
199 | | |
200 | 102 | RETURN_IF_ERROR(_do_init_reader(ctx)); |
201 | | |
202 | 102 | if (ctx->column_descs != nullptr) { |
203 | 36 | RETURN_IF_ERROR(on_after_init_reader(ctx)); |
204 | 36 | } |
205 | | |
206 | 102 | return Status::OK(); |
207 | 102 | } |
208 | | |
209 | | /// Hook called before core init. Default just sets _column_descs. |
210 | | /// TableFormatReader overrides with partition/missing column computation. |
211 | | /// ORC/Parquet/Hive/Iceberg further override with format-specific schema matching. |
212 | 0 | virtual Status on_before_init_reader(ReaderInitContext* ctx) { |
213 | 0 | _column_descs = ctx->column_descs; |
214 | 0 | return Status::OK(); |
215 | 0 | } |
216 | | |
217 | | protected: |
218 | | // ---- Init-time hooks (Template Method for init_reader) ---- |
219 | | |
220 | | /// Opens the file and prepares I/O resources before hooks run. Override in |
221 | | /// subclasses to open files, read metadata, set up decompressors, etc. |
222 | | /// For Parquet/ORC, opens the file and reads footer metadata. |
223 | | /// For CSV/JSON, opens the file, creates decompressors, and sets up line readers. |
224 | | /// Default is no-op (for JNI, Native, Arrow readers). |
225 | 0 | virtual Status _open_file_reader(ReaderInitContext* /*ctx*/) { return Status::OK(); } |
226 | | |
227 | | /// Core initialization (format-specific). Subclasses override to perform |
228 | | /// their actual parsing engine setup. The context should be downcast to |
229 | | /// the appropriate subclass using checked_context_cast<T>. |
230 | | /// Default returns NotSupported — readers not yet migrated to the unified |
231 | | /// init_reader(ReaderInitContext*) API still use their old init methods. |
232 | 0 | virtual Status _do_init_reader(ReaderInitContext* /*ctx*/) { |
233 | 0 | return Status::NotSupported( |
234 | 0 | "_do_init_reader(ReaderInitContext*) not yet implemented for this reader"); |
235 | 0 | } |
236 | | |
237 | | // ---- Existing init-time hooks ---- |
238 | | |
239 | | /// Called after core init completes. Subclasses override to process |
240 | | /// delete files, deletion vectors, etc. |
241 | 36 | virtual Status on_after_init_reader(ReaderInitContext* /*ctx*/) { return Status::OK(); } |
242 | | |
243 | | // ---- Read-time hooks ---- |
244 | | |
245 | | /// Called before reading a block. Subclasses override to modify block |
246 | | /// structure (e.g. add ACID columns, expand for equality delete). |
247 | 177 | virtual Status on_before_read_block(Block* block) { return Status::OK(); } |
248 | | |
249 | | /// Core block reading. Subclasses must override with actual read logic. |
250 | | virtual Status _do_get_next_block(Block* block, size_t* read_rows, bool* eof) = 0; |
251 | | |
252 | | /// Called after reading a block. Subclasses override to post-process |
253 | | /// (e.g. remove ACID columns, apply equality delete filter). |
254 | 3 | virtual Status on_after_read_block(Block* block, size_t* read_rows) { return Status::OK(); } |
255 | | |
256 | 0 | virtual Status _set_read_one_line_impl() { |
257 | 0 | return Status::NotSupported("read_by_rows is not implemented for this reader."); |
258 | 0 | } |
259 | | |
260 | | const size_t _MIN_BATCH_SIZE = 4064; // 4094 - 32(padding) |
261 | | |
262 | | TPushAggOp::type _push_down_agg_type {}; |
263 | | bool _push_down_agg_type_locked = false; |
264 | | |
265 | | public: |
266 | | // Pass condition cache context to the reader for HIT/MISS tracking. |
267 | 0 | virtual void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) {} |
268 | | |
269 | | // Returns true if this reader can produce an accurate total row count from metadata |
270 | | // without reading actual data. Used to determine if CountReader decorator can be applied. |
271 | | // Only ORC and Parquet readers support this (via file footer metadata). |
272 | 0 | virtual bool supports_count_pushdown() const { return false; } |
273 | | |
274 | | // Returns the total number of rows the reader will produce. |
275 | | // Used to pre-allocate condition cache with the correct number of granules. |
276 | 0 | virtual int64_t get_total_rows() const { return 0; } |
277 | | |
278 | | // Returns true if this reader has delete operations (e.g. Iceberg position/equality deletes, |
279 | | // Hive ACID deletes). Used to disable condition cache when deletes are present, since cached |
280 | | // granule results may become stale if delete files change between queries. |
281 | 0 | virtual bool has_delete_operations() const { return false; } |
282 | | |
283 | | protected: |
284 | | bool _read_by_rows = false; |
285 | | std::list<int64_t> _row_ids; |
286 | | |
287 | | // Cache to save some common part such as file footer. |
288 | | // Maybe null if not used |
289 | | FileMetaCache* _meta_cache = nullptr; |
290 | | |
291 | | // ---- Column descriptors (set by init_reader, owned by FileScanner) ---- |
292 | | const std::vector<ColumnDescriptor>* _column_descs = nullptr; |
293 | | |
294 | | // ---- get_columns cache ---- |
295 | | bool _get_columns_cached = false; |
296 | | std::unordered_map<std::string, DataTypePtr> _cached_name_to_type; |
297 | | }; |
298 | | |
299 | | /// Provides an accessor for the current batch's row positions within the file. |
300 | | /// Implemented by RowGroupReader (Parquet) and OrcReader. |
301 | | class RowPositionProvider { |
302 | | public: |
303 | 120 | virtual ~RowPositionProvider() = default; |
304 | | virtual const std::vector<segment_v2::rowid_t>& current_batch_row_positions() const = 0; |
305 | | }; |
306 | | |
307 | | #include "common/compile_check_end.h" |
308 | | } // namespace doris |