|
| 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 | +//! Extension point for refining a [`ParquetAccessPlan`] during file open. |
| 19 | +//! |
| 20 | +//! The Parquet opener narrows down which row groups (and which rows within |
| 21 | +//! them) it will read through a fixed sequence of built-in passes: |
| 22 | +//! |
| 23 | +//! - file-range pruning, |
| 24 | +//! - row-group statistics pruning, |
| 25 | +//! - bloom-filter pruning, |
| 26 | +//! - limit-based pruning, |
| 27 | +//! - page-index pruning. |
| 28 | +//! |
| 29 | +//! Each pass operates on a [`ParquetAccessPlan`]. The |
| 30 | +//! [`ParquetAccessPlanOptimizer`] trait exposes that pipeline as an extension |
| 31 | +//! point so external crates can append additional passes — sampling, custom |
| 32 | +//! statistics, user-defined Parquet indexes, etc. — without having to fork |
| 33 | +//! the opener. |
| 34 | +//! |
| 35 | +//! User-supplied optimizers are invoked **after** the built-in passes for the |
| 36 | +//! corresponding [`OptimizerStage`]. They can read the access plan and the |
| 37 | +//! pruning context, then return a (possibly narrowed) plan; they cannot |
| 38 | +//! widen access beyond what the built-ins produced. |
| 39 | +
|
| 40 | +use std::fmt::Debug; |
| 41 | +use std::sync::Arc; |
| 42 | + |
| 43 | +use datafusion_common::Result; |
| 44 | +use datafusion_datasource::{FileRange, PartitionedFile}; |
| 45 | +use datafusion_physical_expr_common::physical_expr::PhysicalExpr; |
| 46 | +use parquet::file::metadata::ParquetMetaData; |
| 47 | + |
| 48 | +use crate::ParquetAccessPlan; |
| 49 | +use crate::ParquetFileMetrics; |
| 50 | +use crate::page_filter::PagePruningAccessPlanFilter; |
| 51 | +use arrow::datatypes::SchemaRef; |
| 52 | +use datafusion_pruning::PruningPredicate; |
| 53 | + |
| 54 | +/// Stage at which a [`ParquetAccessPlanOptimizer`] runs during file open. |
| 55 | +/// |
| 56 | +/// Each stage has access to a different subset of file metadata, reflecting |
| 57 | +/// the order in which the opener loads it. See [`AccessPlanContext`] for |
| 58 | +/// the fields available at each stage. |
| 59 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 60 | +pub enum OptimizerStage { |
| 61 | + /// Runs after the Parquet footer and page index (when enabled) are |
| 62 | + /// loaded, and after the built-in file-range and row-group-statistics |
| 63 | + /// passes have refined the plan. Bloom filters have **not** been |
| 64 | + /// loaded at this point. |
| 65 | + AfterMetadata, |
| 66 | + /// Runs after the bloom filters for surviving row groups have been |
| 67 | + /// loaded, and after the built-in bloom-filter pruning pass. |
| 68 | + AfterBloomFilters, |
| 69 | + /// Runs at the end of the pruning pipeline, just before the stream |
| 70 | + /// is built, after the built-in limit and page-index passes. |
| 71 | + BeforeBuildStream, |
| 72 | +} |
| 73 | + |
| 74 | +/// Read-only context passed to a [`ParquetAccessPlanOptimizer`]. |
| 75 | +/// |
| 76 | +/// Some fields are only populated at certain [`OptimizerStage`]s; see each |
| 77 | +/// field's docs. |
| 78 | +#[derive(Debug)] |
| 79 | +pub struct AccessPlanContext<'a> { |
| 80 | + /// Execution partition index for the scan. |
| 81 | + pub partition_index: usize, |
| 82 | + /// The file being opened. |
| 83 | + pub partitioned_file: &'a PartitionedFile, |
| 84 | + /// Optional byte range restricting which part of the file to read. |
| 85 | + pub file_range: Option<&'a FileRange>, |
| 86 | + /// Schema of the file after type coercions (the schema the parquet |
| 87 | + /// reader will produce, before projection). |
| 88 | + pub physical_file_schema: &'a SchemaRef, |
| 89 | + /// Loaded Parquet metadata, including page index when enabled. |
| 90 | + pub file_metadata: &'a ParquetMetaData, |
| 91 | + /// Raw predicate applied to this scan, if any. |
| 92 | + pub predicate: Option<&'a Arc<dyn PhysicalExpr>>, |
| 93 | + /// Row-group-level pruning predicate derived from `predicate`. |
| 94 | + pub pruning_predicate: Option<&'a Arc<PruningPredicate>>, |
| 95 | + /// Page-index pruning predicate derived from `predicate`. |
| 96 | + pub page_pruning_predicate: Option<&'a Arc<PagePruningAccessPlanFilter>>, |
| 97 | + /// Outer query limit, if any. |
| 98 | + pub limit: Option<usize>, |
| 99 | + /// Whether the query requires the original row order to be preserved. |
| 100 | + pub preserve_order: bool, |
| 101 | + /// Per-file metrics. Optimizers may emit to these counters. |
| 102 | + pub file_metrics: &'a ParquetFileMetrics, |
| 103 | + /// Current optimizer stage. |
| 104 | + pub stage: OptimizerStage, |
| 105 | +} |
| 106 | + |
| 107 | +/// Trait for narrowing a [`ParquetAccessPlan`] during Parquet file open. |
| 108 | +/// |
| 109 | +/// The opener invokes registered optimizers after each built-in pruning |
| 110 | +/// stage (see [`OptimizerStage`]). An optimizer that does not apply at the |
| 111 | +/// current stage should return the plan unchanged. |
| 112 | +/// |
| 113 | +/// # Example |
| 114 | +/// |
| 115 | +/// A sampling optimizer that keeps a fraction of the surviving row groups: |
| 116 | +/// |
| 117 | +/// ```ignore |
| 118 | +/// use std::sync::Arc; |
| 119 | +/// use datafusion_common::Result; |
| 120 | +/// use datafusion_datasource_parquet::{ |
| 121 | +/// access_plan_optimizer::{ |
| 122 | +/// AccessPlanContext, OptimizerStage, ParquetAccessPlanOptimizer, |
| 123 | +/// }, |
| 124 | +/// ParquetAccessPlan, RowGroupAccess, |
| 125 | +/// }; |
| 126 | +/// |
| 127 | +/// #[derive(Debug)] |
| 128 | +/// struct SampleHalf; |
| 129 | +/// |
| 130 | +/// impl ParquetAccessPlanOptimizer for SampleHalf { |
| 131 | +/// fn stage(&self) -> OptimizerStage { OptimizerStage::BeforeBuildStream } |
| 132 | +/// |
| 133 | +/// fn optimize( |
| 134 | +/// &self, |
| 135 | +/// _ctx: &AccessPlanContext<'_>, |
| 136 | +/// mut plan: ParquetAccessPlan, |
| 137 | +/// ) -> Result<ParquetAccessPlan> { |
| 138 | +/// for (idx, count) in (0..plan.len()).step_by(2).zip(std::iter::repeat(())) { |
| 139 | +/// let _ = count; |
| 140 | +/// plan.skip(idx); |
| 141 | +/// } |
| 142 | +/// Ok(plan) |
| 143 | +/// } |
| 144 | +/// } |
| 145 | +/// ``` |
| 146 | +pub trait ParquetAccessPlanOptimizer: Debug + Send + Sync { |
| 147 | + /// At which stage of the opener this optimizer runs. |
| 148 | + fn stage(&self) -> OptimizerStage; |
| 149 | + |
| 150 | + /// Refine `plan` for `ctx`. Returning `plan` unchanged is the no-op. |
| 151 | + fn optimize( |
| 152 | + &self, |
| 153 | + ctx: &AccessPlanContext<'_>, |
| 154 | + plan: ParquetAccessPlan, |
| 155 | + ) -> Result<ParquetAccessPlan>; |
| 156 | +} |
| 157 | + |
| 158 | +/// Run all `optimizers` whose [`OptimizerStage`] matches `ctx.stage`. |
| 159 | +pub(crate) fn run_stage( |
| 160 | + optimizers: &[Arc<dyn ParquetAccessPlanOptimizer>], |
| 161 | + ctx: &AccessPlanContext<'_>, |
| 162 | + mut plan: ParquetAccessPlan, |
| 163 | +) -> Result<ParquetAccessPlan> { |
| 164 | + for opt in optimizers { |
| 165 | + if opt.stage() == ctx.stage { |
| 166 | + plan = opt.optimize(ctx, plan)?; |
| 167 | + } |
| 168 | + } |
| 169 | + Ok(plan) |
| 170 | +} |
0 commit comments