be/src/format_v2/parquet/parquet_scan.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 <gen_cpp/parquet_types.h> |
19 | | |
20 | | #include <cstddef> |
21 | | #include <cstdint> |
22 | | #include <map> |
23 | | #include <memory> |
24 | | #include <optional> |
25 | | #include <unordered_map> |
26 | | #include <unordered_set> |
27 | | #include <utility> |
28 | | #include <vector> |
29 | | |
30 | | #include "common/status.h" |
31 | | #include "core/column/column.h" |
32 | | #include "format_v2/file_reader.h" |
33 | | #include "format_v2/parquet/parquet_profile.h" |
34 | | #include "format_v2/parquet/parquet_statistics.h" |
35 | | #include "format_v2/parquet/reader/column_reader.h" |
36 | | #include "format_v2/parquet/selection_vector.h" |
37 | | #include "runtime/runtime_profile.h" |
38 | | #include "storage/segment/condition_cache.h" |
39 | | |
40 | | namespace cctz { |
41 | | class time_zone; |
42 | | } // namespace cctz |
43 | | |
44 | | namespace doris { |
45 | | class Block; |
46 | | class RuntimeState; |
47 | | |
48 | | namespace format { |
49 | | struct FileScanRequest; |
50 | | } // namespace format |
51 | | } // namespace doris |
52 | | |
53 | | namespace doris::format::parquet { |
54 | | |
55 | | struct ParquetFileContext; |
56 | | struct ParquetColumnSchema; |
57 | | struct ParquetPageCacheRange; |
58 | | struct ParquetScanRange; |
59 | | class NativeParquetMetadata; |
60 | | |
61 | | namespace detail { |
62 | | struct PredicateConjunctStage { |
63 | | VExprContextSPtr owner_context; |
64 | | VExprSPtr expression; |
65 | | std::vector<size_t> required_positions; |
66 | | }; |
67 | | |
68 | | struct PredicateConjunctSchedule { |
69 | | std::map<size_t, VExprContextSPtrs> single_column_conjuncts; |
70 | | VExprContextSPtrs remaining_conjuncts; |
71 | | std::vector<PredicateConjunctStage> remaining_stages; |
72 | | bool supports_lazy_materialization = true; |
73 | | }; |
74 | | |
75 | | struct AdaptivePredicateStats { |
76 | | double cost_per_input_row_ns = 0; |
77 | | double survival_ratio = 1; |
78 | | size_t samples = 0; |
79 | | }; |
80 | | |
81 | | size_t finalize_variant_leaf_projection_for_row_group(const tparquet::RowGroup& row_group, |
82 | | const ParquetColumnSchema& schema, |
83 | | format::LocalColumnIndex* projection, |
84 | | size_t* full_projections = nullptr); |
85 | | bool variant_leaf_projection_is_safe_for_row_group(const tparquet::RowGroup& row_group, |
86 | | const ParquetColumnSchema& schema, |
87 | | const format::LocalColumnIndex& projection); |
88 | | |
89 | | std::vector<size_t> order_adaptive_predicates( |
90 | | const std::vector<size_t>& positions, |
91 | | const std::unordered_map<size_t, AdaptivePredicateStats>& stats); |
92 | | std::vector<size_t> adaptive_prefetch_prefix( |
93 | | const std::vector<size_t>& ordered_positions, |
94 | | const std::unordered_map<size_t, AdaptivePredicateStats>& stats, |
95 | | double minimum_reach_probability); |
96 | | bool should_sample_adaptive_predicate(size_t samples, size_t batch_sequence); |
97 | | Status validate_ephemeral_expr_result_column(size_t original_columns, int result_column_id, |
98 | | size_t current_columns); |
99 | | Status build_native_prefetch_ranges( |
100 | | const tparquet::FileMetaData& metadata, |
101 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
102 | | const std::vector<format::LocalColumnIndex>& scan_columns, int row_group_idx, |
103 | | size_t file_size, bool parquet_816_padding, std::vector<ParquetPageCacheRange>* ranges); |
104 | | Status select_native_row_groups_by_scan_range(const tparquet::FileMetaData& metadata, |
105 | | const ParquetScanRange& scan_range, |
106 | | std::vector<int64_t>* row_group_first_rows, |
107 | | std::vector<int>* selected_row_groups); |
108 | | #ifdef BE_TEST |
109 | | void reset_physical_leaf_set_build_count(); |
110 | | size_t physical_leaf_set_build_count(); |
111 | | #endif |
112 | | } // namespace detail |
113 | | |
114 | | // ============================================================================ |
115 | | // ============================================================================ |
116 | | |
117 | | struct ParquetScanRange { |
118 | | int64_t start_offset = 0; |
119 | | int64_t size = -1; // -1 means read the whole file |
120 | | int64_t file_size = -1; // -1 means unknown |
121 | | }; |
122 | | |
123 | | struct RowGroupReadPlan { |
124 | | int row_group_id = -1; // row group id |
125 | | int64_t first_file_row = 0; // first file row for this row group (0-based) |
126 | | int64_t row_group_rows = 0; // row count of this row group |
127 | | std::vector<RowRange> selected_ranges; // row ranges to read after page-index pruning |
128 | | std::map<int, ParquetPageSkipPlan> |
129 | | page_skip_plans; // leaf_column_id -> data pages that can be skipped completely |
130 | | // Deferred planning transfers parsed indexes to execution so narrowed scans never issue the |
131 | | // same remote index reads a second time while opening the row group. |
132 | | std::unordered_map<int, tparquet::OffsetIndex> offset_indexes; |
133 | | // Candidate ordinals refer to a deterministic traversal of the immutable request projection. |
134 | | // Only full fallbacks are retained, keeping the row-group delta independent of projection size. |
135 | | std::vector<size_t> full_variant_projection_ordinals; |
136 | | size_t variant_leaf_projection_columns = 0; |
137 | | size_t variant_full_projection_columns = 0; |
138 | | // Footer statistics are cheap and eager. Remote dictionary/Bloom/page-index probes fill the |
139 | | // remaining fields only when this row group reaches the scheduler. |
140 | | bool expensive_pruning_pending = false; |
141 | | |
142 | 562 | bool has_row_group_physical_projection() const { |
143 | 562 | return !full_variant_projection_ordinals.empty(); |
144 | 562 | } |
145 | | }; |
146 | | |
147 | | struct RowGroupScanPlan { |
148 | | std::vector<RowGroupReadPlan> row_groups; // row groups selected after pruning |
149 | | ParquetPruningStats pruning_stats; // pruning statistics |
150 | | // Row-group plans only add full-fallback leaves to this immutable request-level baseline. |
151 | | std::unordered_set<int> requested_leaf_column_ids; |
152 | | bool enable_bloom_filter = false; |
153 | | }; |
154 | | |
155 | | // ============================================================================ |
156 | | // ============================================================================ |
157 | | |
158 | | Status plan_parquet_row_groups(const NativeParquetMetadata& metadata, |
159 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
160 | | const format::FileScanRequest& request, |
161 | | const ParquetScanRange& scan_range, bool enable_bloom_filter, |
162 | | RowGroupScanPlan* plan, const cctz::time_zone* timezone = nullptr, |
163 | | const RuntimeState* runtime_state = nullptr, |
164 | | ParquetFileContext* file_context = nullptr, |
165 | | const ParquetColumnReaderProfile& column_reader_profile = {}); |
166 | | |
167 | | Status finalize_parquet_row_group_plans( |
168 | | const NativeParquetMetadata& metadata, |
169 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
170 | | const format::FileScanRequest& request, bool enable_bloom_filter, RowGroupScanPlan* plan, |
171 | | const cctz::time_zone* timezone, const RuntimeState* runtime_state, |
172 | | ParquetFileContext* file_context, const ParquetColumnReaderProfile& column_reader_profile, |
173 | | const ParquetProfile* parquet_profile = nullptr); |
174 | | |
175 | | IColumn::Filter selection_to_filter(const SelectionVector& selection, uint16_t selected_rows, |
176 | | int64_t batch_rows); |
177 | | |
178 | | uint16_t apply_compact_filter_to_selection(const IColumn::Filter& filter, |
179 | | SelectionVector* selection, uint16_t selected_rows); |
180 | | |
181 | | Status execute_batch_filters(const format::FileScanRequest& request, int64_t batch_rows, |
182 | | Block* file_block, SelectionVector* selection, uint16_t* selected_rows, |
183 | | int64_t* conjunct_filtered_rows = nullptr); |
184 | | |
185 | | // ============================================================================ |
186 | | // ============================================================================ |
187 | | // while true: |
188 | | // 3. read_current_row_group_batch(batch_rows) |
189 | | // ============================================================================ |
190 | | class ParquetScanScheduler { |
191 | | public: |
192 | | static constexpr int64_t DEFAULT_READ_BATCH_SIZE = 4096; |
193 | | |
194 | | void set_plan(std::shared_ptr<RowGroupScanPlan> plan); |
195 | 484 | void set_page_skip_profile(ParquetPageSkipProfile page_skip_profile) { |
196 | 484 | _page_skip_profile = page_skip_profile; |
197 | 484 | } |
198 | 484 | void set_scan_profile(ParquetScanProfile scan_profile) { _scan_profile = scan_profile; } |
199 | 353 | void set_pruning_profile(const ParquetProfile* parquet_profile) { |
200 | 353 | _parquet_profile = parquet_profile; |
201 | 353 | } |
202 | 498 | void set_merge_read_options(RuntimeProfile* profile, int64_t merge_read_slice_size) { |
203 | 498 | _profile = profile; |
204 | 498 | _merge_read_slice_size = merge_read_slice_size; |
205 | 498 | } |
206 | 484 | void set_global_rowid_context(std::optional<format::GlobalRowIdContext> context) { |
207 | 484 | _global_rowid_context = context; |
208 | 484 | } |
209 | | void set_condition_cache_context(std::shared_ptr<ConditionCacheContext> ctx); |
210 | 499 | void set_timezone(const cctz::time_zone* timezone) { _timezone = timezone; } |
211 | 83 | void set_int96_timezone(std::optional<const cctz::time_zone*> timezone) { |
212 | 83 | _int96_timezone = timezone; |
213 | 83 | } |
214 | 498 | void set_enable_strict_mode(bool enable_strict_mode) { |
215 | 498 | _enable_strict_mode = enable_strict_mode; |
216 | 498 | } |
217 | 499 | void set_runtime_state(RuntimeState* runtime_state) { _runtime_state = runtime_state; } |
218 | | void set_scan_request(std::shared_ptr<format::FileScanRequest> request); |
219 | | void queue_scan_request(std::shared_ptr<format::FileScanRequest> request); |
220 | | // Release row-group readers before the owning RuntimeProfile is reported. Native readers |
221 | | // publish their accumulated page/decode statistics from their destructor. |
222 | 260 | void close() { reset_current_row_group(); } |
223 | | // Upper scanner owns adaptive memory feedback; scheduler only applies the current row cap when |
224 | | // splitting selected row ranges into physical read batches. |
225 | 504 | void set_batch_size(size_t batch_size) { |
226 | 504 | _batch_size = batch_size == 0 ? 1 : static_cast<int64_t>(batch_size); |
227 | 504 | } |
228 | | void reset(); |
229 | 493 | bool empty() const { return _scan_plan == nullptr || _scan_plan->row_groups.empty(); } |
230 | 2 | int64_t condition_cache_filtered_rows() const { return _condition_cache_filtered_rows; } |
231 | 1.33k | int64_t predicate_filtered_rows() const { return _predicate_filtered_rows; } |
232 | 2.02k | int64_t raw_rows_read() const { return _raw_rows_read; } |
233 | | |
234 | | Status read_next_batch(ParquetFileContext& file_context, |
235 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
236 | | Block* file_block, size_t* rows, bool* eof); |
237 | | |
238 | | private: |
239 | | static constexpr size_t PROFILE_FLUSH_BATCH_INTERVAL = 16; |
240 | | |
241 | | void reset_current_row_group(); |
242 | | void activate_pending_scan_request_at_row_group_boundary(); |
243 | | void flush_current_reader_profiles(); |
244 | | bool finish_current_reader_batch_profiles(); |
245 | | const detail::PredicateConjunctSchedule& predicate_conjunct_schedule( |
246 | | const format::FileScanRequest& request); |
247 | | std::vector<format::LocalColumnIndex> adaptive_predicate_prefetch_columns( |
248 | | const format::FileScanRequest& request, |
249 | | const std::vector<format::LocalColumnIndex>& physical_predicate_columns); |
250 | | |
251 | | Status open_next_row_group(ParquetFileContext& file_context, |
252 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
253 | | const format::FileScanRequest& request, bool* has_row_group); |
254 | | |
255 | | Status skip_current_row_group_rows(int64_t rows); |
256 | | Status flush_pending_non_predicate_skip_rows(); |
257 | | |
258 | | Status read_filter_columns(int64_t batch_rows, const format::FileScanRequest& request, |
259 | | Block* file_block, SelectionVector* selection, |
260 | | uint16_t* selected_rows, int64_t* conjunct_filtered_rows, |
261 | | bool* predicate_columns_filtered); |
262 | | |
263 | | Status prepare_current_dictionary_filters( |
264 | | ParquetFileContext& file_context, |
265 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
266 | | const format::FileScanRequest& request, |
267 | | const std::vector<format::LocalColumnIndex>& physical_predicate_columns, |
268 | | int row_group_idx, const tparquet::RowGroup& row_group_metadata); |
269 | | |
270 | | Status prefetch_current_row_group_columns( |
271 | | ParquetFileContext& file_context, |
272 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
273 | | const format::FileScanRequest& request, |
274 | | const std::vector<format::LocalColumnIndex>& scan_columns, bool* prefetched); |
275 | | |
276 | | Status read_current_row_group_batch( |
277 | | ParquetFileContext& file_context, |
278 | | const std::vector<std::unique_ptr<ParquetColumnSchema>>& file_schema, |
279 | | int64_t batch_rows, const format::FileScanRequest& request, |
280 | | int64_t batch_first_file_row, Block* file_block, size_t* rows); |
281 | | |
282 | | Status materialize_pending_predicate_batch(const format::FileScanRequest& request, |
283 | | Block* file_block, size_t* rows); |
284 | | |
285 | | void mark_condition_cache_granules(const SelectionVector& selection, uint16_t selected_rows, |
286 | | int64_t batch_first_file_row); |
287 | | |
288 | | std::shared_ptr<RowGroupScanPlan> _scan_plan; // shared with the reader's aggregate path |
289 | | size_t _next_row_group_plan_idx = 0; // index of the next row group to process |
290 | | |
291 | | bool _has_current_row_group = false; |
292 | | // Readers retain pointers into this immutable row-group map, so it must outlive both maps below. |
293 | | std::unordered_map<int, tparquet::OffsetIndex> _current_offset_indexes; |
294 | | // The logical request remains immutable across the file. This row-group copy owns only the |
295 | | // physical projections used by readers and deferred prefetch. |
296 | | std::unique_ptr<format::FileScanRequest> _current_row_group_request; |
297 | | // File-local ids are signed because virtual columns use reserved negative values. Keeping the |
298 | | // typed id as the map key prevents GLOBAL_ROWID_COLUMN_ID from wrapping to a storage ColumnId. |
299 | | std::map<format::LocalColumnId, std::unique_ptr<ParquetColumnReader>> |
300 | | _current_predicate_columns; // predicate ColumnReaders |
301 | | std::map<format::LocalColumnId, std::unique_ptr<ParquetColumnReader>> |
302 | | _current_non_predicate_columns; // non-predicate ColumnReaders |
303 | | std::map<format::LocalColumnId, IColumn::Filter> |
304 | | _current_dictionary_filters; // local id -> dict entry bitmap |
305 | | std::map<format::LocalColumnId, std::vector<std::pair<VExprContextSPtr, VExprSPtr>>> |
306 | | _current_dictionary_residual_conjuncts; // local id -> row-level residual conjuncts |
307 | | int64_t _current_row_group_rows = 0; // current row group row count |
308 | | int _current_row_group_id = -1; // current row group id in parquet metadata |
309 | | int64_t _current_row_group_rows_read = 0; // rows read in the current row group (cursor) |
310 | | int64_t _current_row_group_first_row = 0; // first file row of the current row group |
311 | | std::vector<RowRange> |
312 | | _current_selected_ranges; // selected ranges for the current row group after page-index pruning |
313 | | size_t _current_range_idx = 0; // current selected_range index |
314 | | int64_t _current_range_rows_read = 0; // rows read in the current range |
315 | | // Predicate readers move immediately because they decide which rows survive. Non-predicate |
316 | | // readers can lag behind across fully filtered batches and range gaps; the lag is flushed once |
317 | | // before the next surviving batch is materialized, or discarded with the row group. |
318 | | int64_t _pending_non_predicate_skip_rows = 0; |
319 | | // Empty predicate batches may widen their physical probe. If the first non-empty probe finds |
320 | | // more rows than the caller's cap, keep its narrow predicate result here and materialize lazy |
321 | | // columns in capped physical slices on subsequent calls. |
322 | | int64_t _pending_predicate_batch_rows = 0; |
323 | | int64_t _pending_predicate_batch_rows_consumed = 0; |
324 | | size_t _pending_predicate_selected_offset = 0; |
325 | | std::vector<SelectionVector::Index> _pending_predicate_selection; |
326 | | std::map<size_t, ColumnPtr> _pending_predicate_columns; |
327 | | SelectionVector _pending_output_selection; |
328 | | |
329 | | bool _current_predicate_prefetched = false; |
330 | | bool _current_non_predicate_prefetched = false; |
331 | | bool _current_merge_range_active = false; |
332 | | ParquetPageSkipProfile _page_skip_profile; |
333 | | ParquetScanProfile _scan_profile; |
334 | | const ParquetProfile* _parquet_profile = nullptr; |
335 | | RuntimeProfile* _profile = nullptr; |
336 | | int64_t _merge_read_slice_size = -1; |
337 | | std::optional<format::GlobalRowIdContext> _global_rowid_context; |
338 | | const cctz::time_zone* _timezone = nullptr; |
339 | | std::optional<const cctz::time_zone*> _int96_timezone; |
340 | | bool _enable_strict_mode = false; |
341 | | bool _enable_bloom_filter = false; |
342 | | RuntimeState* _runtime_state = nullptr; |
343 | | int64_t _batch_size = DEFAULT_READ_BATCH_SIZE; |
344 | | // Batch control scratch is scheduler-owned so adaptive row caps change logical sizes without |
345 | | // reallocating selection indices, dense filter bytes, or compacted-column positions. |
346 | | SelectionVector _selection; |
347 | | std::vector<uint32_t> _read_column_positions_scratch; |
348 | | const format::FileScanRequest* _predicate_schedule_request = nullptr; |
349 | | std::shared_ptr<format::FileScanRequest> _active_request; |
350 | | std::shared_ptr<format::FileScanRequest> _pending_request; |
351 | | bool _remaining_plans_need_replanning = false; |
352 | | bool _requested_leaf_ids_need_refresh = false; |
353 | | detail::PredicateConjunctSchedule _predicate_schedule; |
354 | | std::vector<size_t> _predicate_positions_scratch; |
355 | | std::unordered_map<size_t, size_t> _predicate_indices_by_position_scratch; |
356 | | std::unordered_set<size_t> _materialized_predicate_positions_scratch; |
357 | | std::vector<size_t> _ordered_predicate_positions_scratch; |
358 | | std::unordered_map<uint32_t, std::vector<SelectionVector::Index>> |
359 | | _predicate_column_selection_scratch; |
360 | | IColumn::Filter _predicate_compaction_filter_scratch; |
361 | | size_t _predicate_batch_sequence = 0; |
362 | | size_t _batches_since_profile_flush = 0; |
363 | | std::unordered_map<size_t, detail::AdaptivePredicateStats> _predicate_runtime_stats; |
364 | | double _predicate_survival_ratio = -1; |
365 | | std::shared_ptr<ConditionCacheContext> _condition_cache_ctx; |
366 | | int64_t _condition_cache_filtered_rows = 0; |
367 | | int64_t _predicate_filtered_rows = 0; |
368 | | int64_t _raw_rows_read = 0; |
369 | | }; |
370 | | |
371 | | } // namespace doris::format::parquet |