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 | | // http://www.apache.org/licenses/LICENSE-2.0 |
9 | | // Unless required by applicable law or agreed to in writing, |
10 | | // software distributed under the License is distributed on an |
11 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
12 | | // KIND, either express or implied. See the License for the |
13 | | // specific language governing permissions and limitations |
14 | | // under the License. |
15 | | |
16 | | #pragma once |
17 | | |
18 | | #include <algorithm> |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | #include <limits> |
22 | | #include <map> |
23 | | #include <memory> |
24 | | #include <string> |
25 | | #include <utility> |
26 | | #include <vector> |
27 | | |
28 | | #include "common/cast_set.h" |
29 | | #include "common/status.h" |
30 | | #include "core/data_type/data_type.h" |
31 | | #include "core/field.h" |
32 | | #include "exprs/vexpr_fwd.h" |
33 | | #include "format_v2/column_data.h" |
34 | | #include "gen_cpp/PlanNodes_types.h" |
35 | | #include "io/file_factory.h" |
36 | | #include "io/fs/file_reader_writer_fwd.h" |
37 | | |
38 | | namespace doris { |
39 | | class Block; |
40 | | struct ConditionCacheContext; |
41 | | |
42 | | namespace io { |
43 | | struct IOContext; |
44 | | } // namespace io |
45 | | } // namespace doris |
46 | | |
47 | | namespace doris::format { |
48 | | |
49 | | class TableColumnMapper; |
50 | | struct TableColumnMapperOptions; |
51 | | |
52 | | enum class FileFormat { |
53 | | PARQUET, |
54 | | ORC, |
55 | | CSV, |
56 | | JSON, |
57 | | TEXT, |
58 | | JNI, |
59 | | NATIVE, |
60 | | ARROW, |
61 | | WAL, |
62 | | }; |
63 | | |
64 | | struct FileScanRequest { |
65 | 743 | virtual ~FileScanRequest() = default; |
66 | | |
67 | | std::string debug_string() const; |
68 | | |
69 | | // Columns that must be read before row-level filtering. They are materialized eagerly because |
70 | | // conjuncts/delete_conjuncts need them to decide the selected rows. |
71 | | std::vector<LocalColumnIndex> predicate_columns; |
72 | | // Columns read after row-level filtering. A complex root may intentionally also appear in |
73 | | // predicate_columns when its eager predicate subtree is smaller than its final output subtree. |
74 | | std::vector<LocalColumnIndex> non_predicate_columns; |
75 | | // Predicate columns introduced only to evaluate hidden filter slots. Their values are dead |
76 | | // after all file-local predicates run, although the shared file block still needs row-shaped |
77 | | // placeholders until TableReader finalizes projected columns. |
78 | | std::vector<LocalColumnId> predicate_only_columns; |
79 | | // file-local column id -> file-local output block position. |
80 | | std::map<LocalColumnId, LocalIndex> local_positions; |
81 | | // Optional output position for a root that has independent eager-predicate and deferred-output |
82 | | // projections. local_positions continues to identify the position referenced by localized |
83 | | // predicate expressions. |
84 | | std::map<LocalColumnId, LocalIndex> non_predicate_positions; |
85 | | // Row-level filters converted to file-local expressions from table-level predicates. |
86 | | VExprContextSPtrs conjuncts; |
87 | | // Only this leading subset may participate in footer/page metadata pruning. The boundary is |
88 | | // inherited from table-conjunct order so an omitted slotless unsafe expression remains a fence. |
89 | | size_t metadata_pruning_safe_conjunct_count = std::numeric_limits<size_t>::max(); |
90 | | // Delete predicates converted to file-local expressions. A TRUE result means that the row is |
91 | | // deleted, so readers must invert each result when building their keep filter. |
92 | | VExprContextSPtrs delete_conjuncts; |
93 | | // File-local ids retained only because Nereids keeps a minimum-width output tuple for an |
94 | | // explicit COUNT(*). These columns have no semantic value: for example, after pruning a scan |
95 | | // may retain an unsupported TIME_MILLIS leaf even though COUNT(*) only needs one row per |
96 | | // surviving input row. A reader may synthesize defaults instead of reading a marked column |
97 | | // while it remains non-predicate. If filters or equality deletes promote the same id to |
98 | | // predicate_columns, the value is semantically required and must still be validated and read. |
99 | | std::vector<LocalColumnId> count_star_placeholder_columns; |
100 | | |
101 | | // Table formats may assign semantics that legacy physical files do not encode. Each path here |
102 | | // identifies an unannotated Parquet group that the physical reader must validate and decode as |
103 | | // Variant. Keeping this explicit prevents generic Parquet scans from guessing based on names. |
104 | | std::vector<LocalColumnIndex> variant_schema_overrides; |
105 | | |
106 | 2.28k | bool is_count_star_placeholder(LocalColumnId column_id) const { |
107 | 2.28k | return std::ranges::find(count_star_placeholder_columns, column_id) != |
108 | 2.28k | count_star_placeholder_columns.end(); |
109 | 2.28k | } |
110 | | |
111 | 189 | bool is_predicate_only(LocalColumnId column_id) const { |
112 | 189 | return std::ranges::find(predicate_only_columns, column_id) != predicate_only_columns.end(); |
113 | 189 | } |
114 | | |
115 | 914 | LocalIndex non_predicate_position(LocalColumnId column_id) const { |
116 | 914 | const auto it = non_predicate_positions.find(column_id); |
117 | 914 | return it == non_predicate_positions.end() ? local_positions.at(column_id) : it->second; |
118 | 914 | } |
119 | | |
120 | 702 | bool has_deferred_non_predicate_column(LocalColumnId column_id) const { |
121 | 702 | return non_predicate_positions.contains(column_id); |
122 | 702 | } |
123 | | |
124 | 151 | size_t block_column_count() const { |
125 | 151 | size_t count = 0; |
126 | 226 | for (const auto& [_, position] : local_positions) { |
127 | 226 | count = std::max(count, position.value() + 1); |
128 | 226 | } |
129 | 151 | for (const auto& [_, position] : non_predicate_positions) { |
130 | 0 | count = std::max(count, position.value() + 1); |
131 | 0 | } |
132 | 151 | return count; |
133 | 151 | } |
134 | | }; |
135 | | |
136 | | // Helper for constructing the scan-column layout in FileScanRequest. |
137 | | // FileScanRequest keeps predicate and non-predicate columns separate because columnar readers such |
138 | | // as Parquet can read predicate columns first, filter rows, and then lazily read the remaining |
139 | | // projected columns. The two lists still share one file-local output block, whose positions are |
140 | | // stored in local_positions. This builder centralizes the mechanical rules for that shared layout: |
141 | | // - each root file column gets one stable predicate block position; |
142 | | // - predicate columns dominate non-predicate columns because they are already returned in the file |
143 | | // block and can be reused for final materialization; |
144 | | // - a smaller complex predicate subtree may get a second deferred output position; |
145 | | // - repeated nested projections for the same root are merged instead of duplicated. |
146 | | // TableColumnMapper should still own table-to-file semantic resolution. This helper only owns the |
147 | | // FileScanRequest layout contract after a file-local projection has been produced. |
148 | | class FileScanRequestBuilder { |
149 | | public: |
150 | 486 | explicit FileScanRequestBuilder(FileScanRequest* request) : _request(request) { |
151 | 486 | DORIS_CHECK(_request != nullptr); |
152 | 486 | } |
153 | | |
154 | 233 | Status add_predicate_column(LocalColumnIndex projection) { |
155 | 233 | return _add_column(std::move(projection), &_request->predicate_columns, |
156 | 233 | /*is_predicate_column=*/true); |
157 | 233 | } |
158 | | |
159 | 459 | Status add_non_predicate_column(LocalColumnIndex projection) { |
160 | 459 | return _add_column(std::move(projection), &_request->non_predicate_columns, |
161 | 459 | /*is_predicate_column=*/false); |
162 | 459 | } |
163 | | |
164 | 8 | Status add_deferred_non_predicate_column(LocalColumnIndex projection) { |
165 | 8 | const auto file_column_id = projection.column_id(); |
166 | 8 | DORIS_CHECK(file_column_id != LocalColumnId::invalid()); |
167 | 8 | DORIS_CHECK(_request->local_positions.contains(file_column_id)); |
168 | 8 | DORIS_CHECK(std::ranges::any_of(_request->predicate_columns, |
169 | 8 | [&](const LocalColumnIndex& predicate) { |
170 | 8 | return predicate.column_id() == file_column_id; |
171 | 8 | })); |
172 | | |
173 | 8 | if (!_request->non_predicate_positions.contains(file_column_id)) { |
174 | 6 | _request->non_predicate_positions.emplace(file_column_id, |
175 | 6 | _next_block_position(*_request)); |
176 | 6 | } |
177 | 8 | _sort_projection_children_by_file_id(&projection); |
178 | 8 | auto existing = std::ranges::find_if(_request->non_predicate_columns, |
179 | 8 | [&](const LocalColumnIndex& output) { |
180 | 2 | return output.column_id() == file_column_id; |
181 | 2 | }); |
182 | 8 | if (existing == _request->non_predicate_columns.end()) { |
183 | 8 | _request->non_predicate_columns.push_back(std::move(projection)); |
184 | 8 | } else { |
185 | 0 | RETURN_IF_ERROR(merge_local_column_index(&*existing, projection)); |
186 | 0 | _sort_projection_children_by_file_id(&*existing); |
187 | 0 | } |
188 | 8 | if (!_request->is_predicate_only(file_column_id)) { |
189 | | // The eager complex value has a different physical shape from the final value and |
190 | | // must never leak into table materialization after its predicates have run. |
191 | 8 | _request->predicate_only_columns.push_back(file_column_id); |
192 | 8 | } |
193 | 8 | return Status::OK(); |
194 | 8 | } |
195 | | |
196 | 143 | Status add_predicate_column(LocalColumnId column_id) { |
197 | 143 | return add_predicate_column(LocalColumnIndex::top_level(column_id)); |
198 | 143 | } |
199 | | |
200 | 269 | Status add_non_predicate_column(LocalColumnId column_id) { |
201 | 269 | return add_non_predicate_column(LocalColumnIndex::top_level(column_id)); |
202 | 269 | } |
203 | | |
204 | | private: |
205 | 674 | static LocalIndex _next_block_position(const FileScanRequest& request) { |
206 | 674 | size_t next_position = 0; |
207 | 8.35k | for (const auto& [_, block_position] : request.local_positions) { |
208 | 8.35k | next_position = std::max(next_position, block_position.value() + 1); |
209 | 8.35k | } |
210 | 674 | for (const auto& [_, block_position] : request.non_predicate_positions) { |
211 | 2 | next_position = std::max(next_position, block_position.value() + 1); |
212 | 2 | } |
213 | 674 | return LocalIndex(next_position); |
214 | 674 | } |
215 | | |
216 | 782 | static void _sort_projection_children_by_file_id(LocalColumnIndex* projection) { |
217 | 782 | DORIS_CHECK(projection != nullptr); |
218 | 782 | if (projection->project_all_children) { |
219 | 713 | return; |
220 | 713 | } |
221 | 79 | for (auto& child : projection->children) { |
222 | 79 | _sort_projection_children_by_file_id(&child); |
223 | 79 | } |
224 | 69 | std::ranges::sort(projection->children, |
225 | 69 | [](const LocalColumnIndex& lhs, const LocalColumnIndex& rhs) { |
226 | 19 | return lhs.local_id() < rhs.local_id(); |
227 | 19 | }); |
228 | 69 | } |
229 | | |
230 | | Status _add_column(LocalColumnIndex projection, std::vector<LocalColumnIndex>* scan_columns, |
231 | 692 | bool is_predicate_column) { |
232 | 692 | DORIS_CHECK(scan_columns != nullptr); |
233 | 692 | const auto file_column_id = projection.column_id(); |
234 | 692 | DORIS_CHECK(file_column_id != LocalColumnId::invalid()); |
235 | 692 | if (!is_predicate_column && |
236 | 692 | std::ranges::find_if(_request->predicate_columns, |
237 | 459 | [&](const LocalColumnIndex& p) { |
238 | 62 | return p.column_id() == file_column_id; |
239 | 62 | }) != _request->predicate_columns.end() && |
240 | 692 | !_request->has_deferred_non_predicate_column(file_column_id)) { |
241 | 2 | return Status::OK(); |
242 | 2 | } |
243 | 690 | if (!_request->local_positions.contains(file_column_id)) { |
244 | 668 | _request->local_positions.emplace(file_column_id, _next_block_position(*_request)); |
245 | 668 | } |
246 | | |
247 | 690 | _sort_projection_children_by_file_id(&projection); |
248 | 690 | auto existing_projection_it = std::ranges::find_if( |
249 | 690 | *scan_columns, |
250 | 8.09k | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
251 | 690 | if (existing_projection_it == scan_columns->end()) { |
252 | 685 | scan_columns->push_back(std::move(projection)); |
253 | 685 | } else { |
254 | 5 | RETURN_IF_ERROR(merge_local_column_index(&*existing_projection_it, projection)); |
255 | 5 | _sort_projection_children_by_file_id(&*existing_projection_it); |
256 | 5 | } |
257 | | |
258 | 690 | if (is_predicate_column && !_request->has_deferred_non_predicate_column(file_column_id)) { |
259 | 230 | auto it = std::ranges::find_if( |
260 | 230 | _request->non_predicate_columns, |
261 | 230 | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
262 | 230 | if (it != _request->non_predicate_columns.end()) { |
263 | 10 | _request->non_predicate_columns.erase(it); |
264 | 10 | } |
265 | 230 | } |
266 | 690 | return Status::OK(); |
267 | 690 | } |
268 | | |
269 | | FileScanRequest* _request = nullptr; |
270 | | }; |
271 | | |
272 | | struct FileAggregateRequest { |
273 | | struct Column { |
274 | | // File-local projection for the aggregate column. For nested MIN/MAX, this points to the |
275 | | // single primitive leaf that can be represented by file statistics. For COUNT(col), this |
276 | | // points to the top-level column whose NULL-ness should be counted. |
277 | | LocalColumnIndex projection; |
278 | | }; |
279 | | |
280 | | TPushAggOp::type agg_type = TPushAggOp::type::NONE; |
281 | | // Empty for COUNT(*)/row-count pushdown. Non-empty for COUNT(col), where the file reader must |
282 | | // return the number of non-NULL rows for the requested column instead of total rows. |
283 | | std::vector<Column> columns; |
284 | | }; |
285 | | |
286 | | struct FileAggregateResult { |
287 | | struct Column { |
288 | | // Mirrors FileAggregateRequest::Column::projection so TableReader can put the returned |
289 | | // aggregate value back into the matching projected nested shape. |
290 | | LocalColumnIndex projection; |
291 | | bool has_min = false; |
292 | | bool has_max = false; |
293 | | Field min_value; |
294 | | Field max_value; |
295 | | }; |
296 | | |
297 | | int64_t count = 0; |
298 | | std::vector<Column> columns; |
299 | | }; |
300 | | |
301 | | /** |
302 | | * +-----> get_schema() -----------------+ |
303 | | * FileReader() -----> init() ----| -----> close() |
304 | | * +-----> open() -----> get_block() ----+ |
305 | | */ |
306 | | class FileReader { |
307 | | public: |
308 | | struct ReaderStatistics { |
309 | | int32_t filtered_row_groups = 0; |
310 | | int32_t filtered_row_groups_by_min_max = 0; |
311 | | int32_t filtered_row_groups_by_bloom_filter = 0; |
312 | | int32_t read_row_groups = 0; |
313 | | int64_t filtered_group_rows = 0; |
314 | | int64_t filtered_page_rows = 0; |
315 | | int64_t lazy_read_filtered_rows = 0; |
316 | | int64_t read_rows = 0; |
317 | | int64_t filtered_bytes = 0; |
318 | | int64_t column_read_time = 0; |
319 | | int64_t parse_meta_time = 0; |
320 | | int64_t parse_footer_time = 0; |
321 | | int64_t file_footer_read_calls = 0; |
322 | | int64_t file_footer_hit_cache = 0; |
323 | | int64_t file_reader_create_time = 0; |
324 | | int64_t open_file_num = 0; |
325 | | int64_t row_group_filter_time = 0; |
326 | | int64_t page_index_filter_time = 0; |
327 | | int64_t read_page_index_time = 0; |
328 | | int64_t parse_page_index_time = 0; |
329 | | int64_t predicate_filter_time = 0; |
330 | | int64_t dict_filter_rewrite_time = 0; |
331 | | int64_t bloom_filter_read_time = 0; |
332 | | }; |
333 | | |
334 | | FileReader(std::shared_ptr<io::FileSystemProperties>& system_properties, |
335 | | std::unique_ptr<io::FileDescription>& file_description, |
336 | | std::shared_ptr<io::IOContext> io_ctx, RuntimeProfile* profile) |
337 | 681 | : _system_properties(system_properties), |
338 | 681 | _file_description(std::move(file_description)), |
339 | 681 | _io_ctx(io_ctx), |
340 | 681 | _profile(profile) {} |
341 | 681 | virtual ~FileReader() = default; |
342 | | |
343 | | // Initialize file reader and parse file metadata. |
344 | | virtual Status init(RuntimeState* state); |
345 | | |
346 | | // Set the maximum row count for the next physical read batch. Readers that do not batch by |
347 | | // rows may ignore it. |
348 | 0 | virtual void set_batch_size(size_t batch_size) { (void)batch_size; } |
349 | | |
350 | | // Get semantic file-local schema from file metadata. The file schema is determined by file |
351 | | // format and file content, and does not contain table/global schema semantics. A file reader may |
352 | | // expose raw file identifiers, such as Parquet field_id, through ColumnDefinition::identifier, |
353 | | // but it must not interpret table-format semantics such as Iceberg name mapping, |
354 | | // default/generated columns, or partition columns. File-format physical wrappers should be |
355 | | // normalized away before exposing this schema; for example, Parquet MAP is exposed as key/value |
356 | | // children rather than key_value/entry. |
357 | | // Doris plans external-table scan types as nullable, including all nested children of complex |
358 | | // types. This protects Doris from illegal or inconsistent values produced by external systems. |
359 | | // Therefore every ColumnDefinition::type returned here must be nullable. Complex types must |
360 | | // also expose nullable child types recursively, even if the physical file marks those fields as |
361 | | // required. |
362 | | // This method can only be called after init() successfully, but does not require open() to be |
363 | | // called. |
364 | | virtual Status get_schema(std::vector<ColumnDefinition>* file_schema) const = 0; |
365 | | |
366 | | // Create the mapper that matches this reader's scan-request capabilities. TableReader still |
367 | | // owns table-format semantics such as BY_NAME/BY_FIELD_ID/BY_INDEX, partition values and |
368 | | // default expressions; the FileReader only chooses whether file-local requests support columnar |
369 | | // lazy materialization/pruning or must materialize one flat list of required columns. |
370 | | virtual std::unique_ptr<TableColumnMapper> create_column_mapper( |
371 | | TableColumnMapperOptions options) const; |
372 | | |
373 | | // Open the file reader with file-local scan request. The file reader should initialize its internal state according to the request, but does not need to interpret table/global schema semantics. For example, all schema change, filter localization, default/generated/partition columns should be handled in table reader layer. This method can only be called after init() successfully. |
374 | 626 | virtual Status open(std::shared_ptr<FileScanRequest> request) { |
375 | 626 | _request = std::move(request); |
376 | 626 | return Status::OK(); |
377 | 626 | } |
378 | | |
379 | | // Readers opt in only when they can keep an immutable request for the active physical |
380 | | // granule and switch a newer snapshot at a well-defined boundary. |
381 | 0 | virtual bool supports_scan_request_refresh() const { return false; } |
382 | | |
383 | 0 | virtual Status queue_scan_request(std::shared_ptr<FileScanRequest> request) { |
384 | 0 | (void)request; |
385 | 0 | return Status::NotSupported("FileReader does not support scan request refresh"); |
386 | 0 | } |
387 | | |
388 | 0 | virtual Status get_block(Block* file_block, size_t* rows, bool* eof) { |
389 | 0 | if (rows != nullptr) { |
390 | 0 | *rows = 0; |
391 | 0 | } |
392 | 0 | if (eof != nullptr) { |
393 | 0 | *eof = true; |
394 | 0 | } |
395 | 0 | _eof = true; |
396 | 0 | return Status::OK(); |
397 | 0 | } |
398 | | |
399 | | virtual Status get_aggregate_result(const FileAggregateRequest& request, |
400 | 0 | FileAggregateResult* result) { |
401 | 0 | return Status::NotSupported("FileReader does not support aggregate pushdown"); |
402 | 0 | } |
403 | | |
404 | | // Condition cache is managed by TableReader and consumed by physical file readers. |
405 | | // On cache HIT, readers may skip granules whose cached bit is false before doing column IO. |
406 | | // On cache MISS, readers mark a granule true when row-level predicates keep at least one row |
407 | | // in that granule. Readers that cannot map batch rows to stable file-global row ids should |
408 | | // keep the default no-op implementation. |
409 | 0 | virtual void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) {} |
410 | | |
411 | | // Total rows covered by this physical reader. TableReader uses it to pre-size the miss bitmap. |
412 | | // Readers should return 0 if the metadata is unavailable or the row coordinate is unstable. |
413 | 0 | virtual int64_t get_total_rows() const { return 0; } |
414 | | |
415 | 199 | virtual Status close() { |
416 | 199 | _file_reader.reset(); |
417 | 199 | _tracing_file_reader.reset(); |
418 | 199 | _io_ctx.reset(); |
419 | 199 | _eof = true; |
420 | 199 | return Status::OK(); |
421 | 199 | } |
422 | | |
423 | | protected: |
424 | 0 | virtual void _init_profile() {} |
425 | 561 | void _record_scan_rows(int64_t rows) { |
426 | 561 | DORIS_CHECK(rows >= 0); |
427 | 561 | _reader_statistics.read_rows += rows; |
428 | 561 | if (_io_ctx != nullptr && _io_ctx->file_reader_stats != nullptr) { |
429 | 176 | _io_ctx->file_reader_stats->read_rows += cast_set<size_t>(rows); |
430 | 176 | } |
431 | 561 | } |
432 | | |
433 | | io::FileReaderSPtr _file_reader; |
434 | | // _tracing_file_reader wraps _file_reader. |
435 | | // _file_reader is original file reader. |
436 | | // _tracing_file_reader is tracing file reader with io context. |
437 | | // If io_ctx is null, _tracing_file_reader will be the same as file_reader. |
438 | | io::FileReaderSPtr _tracing_file_reader = nullptr; |
439 | | std::shared_ptr<FileScanRequest> _request; |
440 | | bool _eof = true; |
441 | | ReaderStatistics _reader_statistics; |
442 | | std::shared_ptr<io::FileSystemProperties> _system_properties; |
443 | | std::unique_ptr<io::FileDescription> _file_description; |
444 | | std::shared_ptr<io::IOContext> _io_ctx; |
445 | | RuntimeProfile* _profile = nullptr; |
446 | | }; |
447 | | |
448 | | } // namespace doris::format |