diff --git a/datafusion/datasource-parquet/src/page_filter.rs b/datafusion/datasource-parquet/src/page_filter.rs index f93f84fb0d230..ef998b0e0beed 100644 --- a/datafusion/datasource-parquet/src/page_filter.rs +++ b/datafusion/datasource-parquet/src/page_filter.rs @@ -18,6 +18,7 @@ use arrow::{ }; use datafusion_common::ScalarValue; use datafusion_common::pruning::PruningStatistics; +use datafusion_physical_expr::expressions::Column; use datafusion_physical_expr::{PhysicalExpr, split_conjunction}; use datafusion_pruning::{MAX_IN_LIST_SIZE, PruningPredicate, PruningPredicateBuilder}; @@ -169,7 +170,7 @@ impl PagePruningAccessPlanFilter { continue; } - if pp.required_columns().single_column().is_none() { + if single_column(&pp).is_none() { debug!("Ignoring multi-column page pruning predicate: {predicate}"); all_predicates_supported = false; continue; @@ -258,11 +259,7 @@ impl PagePruningAccessPlanFilter { .try_build_inverse(Arc::clone(predicate.orig_expr())) .ok() .flatten()?; - inverted - .required_columns() - .single_column() - .is_some() - .then_some(inverted) + single_column(&inverted).is_some().then_some(inverted) }) .collect() } else { @@ -481,11 +478,22 @@ impl PagePruningAccessPlanFilter { pub(crate) fn predicate_column_names(&self) -> impl Iterator { self.predicates .iter() - .filter_map(|p| p.required_columns().single_column()) + .filter_map(single_column) .map(|c| c.name()) } } +/// Returns the column if every statistic that `predicate` requires is of that +/// column. Returns `None` if the predicate reads several columns or none. +/// +/// The pages of each column have their own row boundaries. So page pruning can +/// evaluate a predicate only on the pages of one column. +fn single_column(predicate: &PruningPredicate) -> Option<&Column> { + let mut columns = predicate.required_columns().columns(); + let first = columns.next()?; + columns.all(|c| c == first).then_some(first) +} + fn update_selection( current_selection: Option, row_selection: RowSelection, @@ -609,7 +617,7 @@ fn prune_pages_for_predicate( #[cfg(test)] tests::PAGE_PREDICATE_CALLS.set(tests::PAGE_PREDICATE_CALLS.get() + 1); - let column = predicate.required_columns().single_column()?; + let column = single_column(predicate)?; let converter = match StatisticsConverter::try_new(column.name(), arrow_schema, parquet_schema) { Ok(converter) => converter, @@ -866,7 +874,7 @@ mod tests { use arrow::record_batch::RecordBatch; use bytes::Bytes; use datafusion_expr::Operator; - use datafusion_physical_expr::expressions::{BinaryExpr, Column, lit}; + use datafusion_physical_expr::expressions::{BinaryExpr, lit}; use datafusion_physical_plan::metrics::ExecutionPlanMetricsSet; use parquet::arrow::ArrowWriter; use parquet::arrow::arrow_reader::{ @@ -881,6 +889,42 @@ mod tests { pub(super) static PAGE_PREDICATE_CALLS: std::cell::Cell = const { std::cell::Cell::new(0) }; } + #[test] + fn single_column_requires_one_column() { + let schema = Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Int32, true), + ])); + let cmp = |name: &str, idx, op| -> Arc { + Arc::new(BinaryExpr::new( + Arc::new(Column::new(name, idx)), + op, + lit(1), + )) + }; + let one: Arc = Arc::new(BinaryExpr::new( + cmp("a", 0, Operator::Gt), + Operator::And, + cmp("a", 0, Operator::Lt), + )); + let pp = PruningPredicateBuilder::new() + .with_file_schema(Arc::clone(&schema)) + .try_build(one) + .unwrap(); + assert_eq!(single_column(&pp), Some(&Column::new("a", 0))); + + let two: Arc = Arc::new(BinaryExpr::new( + cmp("a", 0, Operator::Gt), + Operator::And, + cmp("b", 1, Operator::Lt), + )); + let pp = PruningPredicateBuilder::new() + .with_file_schema(schema) + .try_build(two) + .unwrap(); + assert_eq!(single_column(&pp), None); + } + #[test] fn inverse_pruning_short_circuits() { // Each row group has three two-row pages. Only the middle page can diff --git a/datafusion/pruning/src/pruning_predicate.rs b/datafusion/pruning/src/pruning_predicate.rs index c064193872516..4d1bcea1c396e 100644 --- a/datafusion/pruning/src/pruning_predicate.rs +++ b/datafusion/pruning/src/pruning_predicate.rs @@ -950,6 +950,14 @@ impl RequiredColumns { Self::default() } + /// Returns the column of each statistic that the predicate requires, in order. + /// + /// A column appears once for each of its statistics. For example, if the + /// predicate requires the min and the max of `a`, `a` appears twice. + pub fn columns(&self) -> impl Iterator { + self.columns.iter().map(|(column, _, _)| column) + } + /// Returns Some(column) if this is a single column predicate. /// /// Returns None if this is a multi-column predicate. @@ -958,6 +966,10 @@ impl RequiredColumns { /// * `a > 5 OR a < 10` returns `Some(a)` /// * `a > 5 OR b < 10` returns `None` /// * `true` returns None + #[deprecated( + since = "57.0.0", + note = "use `RequiredColumns::columns` and compare the columns instead" + )] pub fn single_column(&self) -> Option<&phys_expr::Column> { if self.columns.windows(2).all(|w| { // check if all columns are the same (ignoring statistics and field) @@ -4413,6 +4425,26 @@ mod tests { Ok(()) } + #[test] + fn required_columns_lists_each_statistic_column() { + let schema = Arc::new(Schema::new(vec![ + Field::new("a", DataType::Int32, true), + Field::new("b", DataType::Int32, true), + ])); + let expr = col("a").gt(lit(1)).and(col("b").lt(lit(2))); + let expr = logical2physical(&expr, &schema); + let predicate = PruningPredicateBuilder::new() + .with_file_schema(schema) + .try_build(expr) + .unwrap(); + let names: Vec<_> = predicate + .required_columns() + .columns() + .map(|c| c.name()) + .collect(); + assert_eq!(names, ["a", "a", "a", "b", "b"]); + } + #[test] fn large_string_in_list_prunes_exact_intervals() -> Result<()> { let types = [ @@ -4492,10 +4524,12 @@ mod tests { assert!(Arc::ptr_eq(predicate.orig_expr(), &expr)); assert_eq!(predicate.literal_guarantees().len(), 1); assert_eq!(predicate.literal_guarantees()[0].literals.len(), count); - assert_eq!( - predicate.required_columns().single_column().unwrap().name(), - "c1" - ); + let names: HashSet<_> = predicate + .required_columns() + .columns() + .map(|c| c.name()) + .collect(); + assert_eq!(names, HashSet::from(["c1"])); if count > MAX_IN_LIST_SIZE { // The expression and statistics schema do not grow with the domain. assert_eq!(predicate.required_columns.columns.len(), 4);