be/src/format_v2/file_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 | | // 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 <map> |
22 | | #include <memory> |
23 | | #include <string> |
24 | | #include <utility> |
25 | | #include <vector> |
26 | | |
27 | | #include "common/status.h" |
28 | | #include "core/data_type/data_type.h" |
29 | | #include "core/field.h" |
30 | | #include "exprs/vexpr_fwd.h" |
31 | | #include "format_v2/column_data.h" |
32 | | #include "gen_cpp/PlanNodes_types.h" |
33 | | #include "io/file_factory.h" |
34 | | #include "io/fs/file_reader_writer_fwd.h" |
35 | | |
36 | | namespace doris { |
37 | | class Block; |
38 | | struct ConditionCacheContext; |
39 | | |
40 | | namespace io { |
41 | | struct IOContext; |
42 | | } // namespace io |
43 | | } // namespace doris |
44 | | |
45 | | namespace doris::format { |
46 | | |
47 | | class TableColumnMapper; |
48 | | struct TableColumnMapperOptions; |
49 | | |
50 | | enum class FileFormat { |
51 | | PARQUET, |
52 | | ORC, |
53 | | CSV, |
54 | | JSON, |
55 | | TEXT, |
56 | | JNI, |
57 | | NATIVE, |
58 | | ARROW, |
59 | | }; |
60 | | |
61 | | struct FileScanRequest { |
62 | 277 | virtual ~FileScanRequest() = default; |
63 | | |
64 | | std::string debug_string() const; |
65 | | |
66 | | // Columns that must be read before row-level filtering. They are materialized eagerly because |
67 | | // conjuncts/delete_conjuncts need them to decide the selected rows. |
68 | | std::vector<LocalColumnIndex> predicate_columns; |
69 | | // Columns read after row-level filtering. Predicate columns are also available for output and |
70 | | // should not be duplicated here. |
71 | | std::vector<LocalColumnIndex> non_predicate_columns; |
72 | | // file-local column id -> file-local output block position. |
73 | | std::map<LocalColumnId, LocalIndex> local_positions; |
74 | | // Row-level filters converted to file-local expressions from table-level predicates. |
75 | | VExprContextSPtrs conjuncts; |
76 | | // Delete predicates converted to file-local expressions. |
77 | | VExprContextSPtrs delete_conjuncts; |
78 | | }; |
79 | | |
80 | | // Helper for constructing the scan-column layout in FileScanRequest. |
81 | | // FileScanRequest keeps predicate and non-predicate columns separate because columnar readers such |
82 | | // as Parquet can read predicate columns first, filter rows, and then lazily read the remaining |
83 | | // projected columns. The two lists still share one file-local output block, whose positions are |
84 | | // stored in local_positions. This builder centralizes the mechanical rules for that shared layout: |
85 | | // - each root file column gets one stable block position; |
86 | | // - predicate columns dominate non-predicate columns because they are already returned in the file |
87 | | // block and can be reused for final materialization; |
88 | | // - repeated nested projections for the same root are merged instead of duplicated. |
89 | | // TableColumnMapper should still own table-to-file semantic resolution. This helper only owns the |
90 | | // FileScanRequest layout contract after a file-local projection has been produced. |
91 | | class FileScanRequestBuilder { |
92 | | public: |
93 | 186 | explicit FileScanRequestBuilder(FileScanRequest* request) : _request(request) { |
94 | 186 | DORIS_CHECK(_request != nullptr); |
95 | 186 | } |
96 | | |
97 | 77 | Status add_predicate_column(LocalColumnIndex projection) { |
98 | 77 | return _add_column(std::move(projection), &_request->predicate_columns, |
99 | 77 | /*is_predicate_column=*/true); |
100 | 77 | } |
101 | | |
102 | 116 | Status add_non_predicate_column(LocalColumnIndex projection) { |
103 | 116 | return _add_column(std::move(projection), &_request->non_predicate_columns, |
104 | 116 | /*is_predicate_column=*/false); |
105 | 116 | } |
106 | | |
107 | 15 | Status add_predicate_column(LocalColumnId column_id) { |
108 | 15 | return add_predicate_column(LocalColumnIndex::top_level(column_id)); |
109 | 15 | } |
110 | | |
111 | 24 | Status add_non_predicate_column(LocalColumnId column_id) { |
112 | 24 | return add_non_predicate_column(LocalColumnIndex::top_level(column_id)); |
113 | 24 | } |
114 | | |
115 | | private: |
116 | 182 | static LocalIndex _next_block_position(const FileScanRequest& request) { |
117 | 182 | size_t next_position = 0; |
118 | 182 | for (const auto& [_, block_position] : request.local_positions) { |
119 | 70 | next_position = std::max(next_position, block_position.value() + 1); |
120 | 70 | } |
121 | 182 | return LocalIndex(next_position); |
122 | 182 | } |
123 | | |
124 | 245 | static void _sort_projection_children_by_file_id(LocalColumnIndex* projection) { |
125 | 245 | DORIS_CHECK(projection != nullptr); |
126 | 245 | if (projection->project_all_children) { |
127 | 211 | return; |
128 | 211 | } |
129 | 50 | for (auto& child : projection->children) { |
130 | 50 | _sort_projection_children_by_file_id(&child); |
131 | 50 | } |
132 | 34 | std::ranges::sort(projection->children, |
133 | 34 | [](const LocalColumnIndex& lhs, const LocalColumnIndex& rhs) { |
134 | 23 | return lhs.local_id() < rhs.local_id(); |
135 | 23 | }); |
136 | 34 | } |
137 | | |
138 | | Status _add_column(LocalColumnIndex projection, std::vector<LocalColumnIndex>* scan_columns, |
139 | 193 | bool is_predicate_column) { |
140 | 193 | DORIS_CHECK(scan_columns != nullptr); |
141 | 193 | const auto file_column_id = projection.column_id(); |
142 | 193 | DORIS_CHECK(file_column_id != LocalColumnId::invalid()); |
143 | 193 | if (!is_predicate_column && |
144 | 193 | std::ranges::find_if(_request->predicate_columns, [&](const LocalColumnIndex& p) { |
145 | 6 | return p.column_id() == file_column_id; |
146 | 6 | }) != _request->predicate_columns.end()) { |
147 | 2 | return Status::OK(); |
148 | 2 | } |
149 | 191 | if (!_request->local_positions.contains(file_column_id)) { |
150 | 182 | _request->local_positions.emplace(file_column_id, _next_block_position(*_request)); |
151 | 182 | } |
152 | | |
153 | 191 | _sort_projection_children_by_file_id(&projection); |
154 | 191 | auto existing_projection_it = std::ranges::find_if( |
155 | 191 | *scan_columns, |
156 | 191 | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
157 | 191 | if (existing_projection_it == scan_columns->end()) { |
158 | 187 | scan_columns->push_back(std::move(projection)); |
159 | 187 | } else { |
160 | 4 | RETURN_IF_ERROR(merge_local_column_index(&*existing_projection_it, projection)); |
161 | 4 | _sort_projection_children_by_file_id(&*existing_projection_it); |
162 | 4 | } |
163 | | |
164 | 191 | if (is_predicate_column) { |
165 | 77 | auto it = std::ranges::find_if( |
166 | 77 | _request->non_predicate_columns, |
167 | 77 | [&](const LocalColumnIndex& p) { return p.column_id() == file_column_id; }); |
168 | 77 | if (it != _request->non_predicate_columns.end()) { |
169 | 4 | _request->non_predicate_columns.erase(it); |
170 | 4 | } |
171 | 77 | } |
172 | 191 | return Status::OK(); |
173 | 191 | } |
174 | | |
175 | | FileScanRequest* _request = nullptr; |
176 | | }; |
177 | | |
178 | | struct FileAggregateRequest { |
179 | | struct Column { |
180 | | // File-local projection for the aggregate column. For nested MIN/MAX, this points to the |
181 | | // single primitive leaf that can be represented by file statistics. For COUNT(col), this |
182 | | // points to the top-level column whose NULL-ness should be counted. |
183 | | LocalColumnIndex projection; |
184 | | }; |
185 | | |
186 | | TPushAggOp::type agg_type = TPushAggOp::type::NONE; |
187 | | // Empty for COUNT(*)/row-count pushdown. Non-empty for COUNT(col), where the file reader must |
188 | | // return the number of non-NULL rows for the requested column instead of total rows. |
189 | | std::vector<Column> columns; |
190 | | }; |
191 | | |
192 | | struct FileAggregateResult { |
193 | | struct Column { |
194 | | // Mirrors FileAggregateRequest::Column::projection so TableReader can put the returned |
195 | | // aggregate value back into the matching projected nested shape. |
196 | | LocalColumnIndex projection; |
197 | | bool has_min = false; |
198 | | bool has_max = false; |
199 | | Field min_value; |
200 | | Field max_value; |
201 | | }; |
202 | | |
203 | | int64_t count = 0; |
204 | | std::vector<Column> columns; |
205 | | }; |
206 | | |
207 | | /** |
208 | | * +-----> get_schema() -----------------+ |
209 | | * FileReader() -----> init() ----| -----> close() |
210 | | * +-----> open() -----> get_block() ----+ |
211 | | */ |
212 | | class FileReader { |
213 | | public: |
214 | | struct ReaderStatistics { |
215 | | int32_t filtered_row_groups = 0; |
216 | | int32_t filtered_row_groups_by_min_max = 0; |
217 | | int32_t filtered_row_groups_by_bloom_filter = 0; |
218 | | int32_t read_row_groups = 0; |
219 | | int64_t filtered_group_rows = 0; |
220 | | int64_t filtered_page_rows = 0; |
221 | | int64_t lazy_read_filtered_rows = 0; |
222 | | int64_t read_rows = 0; |
223 | | int64_t filtered_bytes = 0; |
224 | | int64_t column_read_time = 0; |
225 | | int64_t parse_meta_time = 0; |
226 | | int64_t parse_footer_time = 0; |
227 | | int64_t file_footer_read_calls = 0; |
228 | | int64_t file_footer_hit_cache = 0; |
229 | | int64_t file_reader_create_time = 0; |
230 | | int64_t open_file_num = 0; |
231 | | int64_t row_group_filter_time = 0; |
232 | | int64_t page_index_filter_time = 0; |
233 | | int64_t read_page_index_time = 0; |
234 | | int64_t parse_page_index_time = 0; |
235 | | int64_t predicate_filter_time = 0; |
236 | | int64_t dict_filter_rewrite_time = 0; |
237 | | int64_t bloom_filter_read_time = 0; |
238 | | }; |
239 | | |
240 | | FileReader(std::shared_ptr<io::FileSystemProperties>& system_properties, |
241 | | std::unique_ptr<io::FileDescription>& file_description, |
242 | | std::shared_ptr<io::IOContext> io_ctx, RuntimeProfile* profile) |
243 | 206 | : _system_properties(system_properties), |
244 | 206 | _file_description(std::move(file_description)), |
245 | 206 | _io_ctx(io_ctx), |
246 | 206 | _profile(profile) {} |
247 | 206 | virtual ~FileReader() = default; |
248 | | |
249 | | // Initialize file reader and parse file metadata. |
250 | | virtual Status init(RuntimeState* state); |
251 | | |
252 | | // Set the maximum row count for the next physical read batch. Readers that do not batch by |
253 | | // rows may ignore it. |
254 | 0 | virtual void set_batch_size(size_t batch_size) { (void)batch_size; } |
255 | | |
256 | | // Get semantic file-local schema from file metadata. The file schema is determined by file |
257 | | // format and file content, and does not contain table/global schema semantics. A file reader may |
258 | | // expose raw file identifiers, such as Parquet field_id, through ColumnDefinition::identifier, |
259 | | // but it must not interpret table-format semantics such as Iceberg name mapping, |
260 | | // default/generated columns, or partition columns. File-format physical wrappers should be |
261 | | // normalized away before exposing this schema; for example, Parquet MAP is exposed as key/value |
262 | | // children rather than key_value/entry. |
263 | | // Doris plans external-table scan types as nullable, including all nested children of complex |
264 | | // types. This protects Doris from illegal or inconsistent values produced by external systems. |
265 | | // Therefore every ColumnDefinition::type returned here must be nullable. Complex types must |
266 | | // also expose nullable child types recursively, even if the physical file marks those fields as |
267 | | // required. |
268 | | // This method can only be called after init() successfully, but does not require open() to be |
269 | | // called. |
270 | | virtual Status get_schema(std::vector<ColumnDefinition>* file_schema) const = 0; |
271 | | |
272 | | // Create the mapper that matches this reader's scan-request capabilities. TableReader still |
273 | | // owns table-format semantics such as BY_NAME/BY_FIELD_ID/BY_INDEX, partition values and |
274 | | // default expressions; the FileReader only chooses whether file-local requests support columnar |
275 | | // lazy materialization/pruning or must materialize one flat list of required columns. |
276 | | virtual std::unique_ptr<TableColumnMapper> create_column_mapper( |
277 | | TableColumnMapperOptions options) const; |
278 | | |
279 | | // 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. |
280 | 184 | virtual Status open(std::shared_ptr<FileScanRequest> request) { |
281 | 184 | _request = std::move(request); |
282 | 184 | return Status::OK(); |
283 | 184 | } |
284 | | |
285 | 0 | virtual Status get_block(Block* file_block, size_t* rows, bool* eof) { |
286 | 0 | if (rows != nullptr) { |
287 | 0 | *rows = 0; |
288 | 0 | } |
289 | 0 | if (eof != nullptr) { |
290 | 0 | *eof = true; |
291 | 0 | } |
292 | 0 | _eof = true; |
293 | 0 | return Status::OK(); |
294 | 0 | } |
295 | | |
296 | | virtual Status get_aggregate_result(const FileAggregateRequest& request, |
297 | 0 | FileAggregateResult* result) { |
298 | 0 | return Status::NotSupported("FileReader does not support aggregate pushdown"); |
299 | 0 | } |
300 | | |
301 | | // Condition cache is managed by TableReader and consumed by physical file readers. |
302 | | // On cache HIT, readers may skip granules whose cached bit is false before doing column IO. |
303 | | // On cache MISS, readers mark a granule true when row-level predicates keep at least one row |
304 | | // in that granule. Readers that cannot map batch rows to stable file-global row ids should |
305 | | // keep the default no-op implementation. |
306 | 0 | virtual void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx) {} |
307 | | |
308 | | // Total rows covered by this physical reader. TableReader uses it to pre-size the miss bitmap. |
309 | | // Readers should return 0 if the metadata is unavailable or the row coordinate is unstable. |
310 | 0 | virtual int64_t get_total_rows() const { return 0; } |
311 | | |
312 | 67 | virtual Status close() { |
313 | 67 | _file_reader.reset(); |
314 | 67 | _tracing_file_reader.reset(); |
315 | 67 | _io_ctx.reset(); |
316 | 67 | _eof = true; |
317 | 67 | return Status::OK(); |
318 | 67 | } |
319 | | |
320 | | protected: |
321 | 10 | virtual void _init_profile() {} |
322 | | |
323 | | io::FileReaderSPtr _file_reader; |
324 | | // _tracing_file_reader wraps _file_reader. |
325 | | // _file_reader is original file reader. |
326 | | // _tracing_file_reader is tracing file reader with io context. |
327 | | // If io_ctx is null, _tracing_file_reader will be the same as file_reader. |
328 | | io::FileReaderSPtr _tracing_file_reader = nullptr; |
329 | | std::shared_ptr<FileScanRequest> _request; |
330 | | bool _eof = true; |
331 | | ReaderStatistics _reader_statistics; |
332 | | std::shared_ptr<io::FileSystemProperties> _system_properties; |
333 | | std::unique_ptr<io::FileDescription> _file_description; |
334 | | std::shared_ptr<io::IOContext> _io_ctx; |
335 | | RuntimeProfile* _profile = nullptr; |
336 | | }; |
337 | | |
338 | | } // namespace doris::format |