Apache DataFusion SQL Query Engine
perf: build each column's unbounded range once per projection (#26879)
Alex Huang0f5c9ebee84f
diff --git a/datafusion/physical-expr/benches/equivalence_properties.rs b/datafusion/physical-expr/benches/equivalence_properties.rsindex b9318871d2..0b14efcd3e 100644--- a/datafusion/physical-expr/benches/equivalence_properties.rs+++ b/datafusion/physical-expr/benches/equivalence_properties.rs@@ -20,17 +20,25 @@//! The benchmarks are parameterized by the number of equivalence classes, since//! that -- not the schema width, which sits behind an `Arc` -- is what these//! checks carry around.+//!+//! `project/wide_struct_fields` measures [`EquivalenceProperties::project`] for a+//! projection that reads many fields of one wide struct column, as flattening a+//! parsed JSON document does, with the input ordered by another column or by the+//! struct itself.use std::sync::Arc;use arrow::compute::SortOptions;-use arrow::datatypes::{DataType, Field, Schema, SchemaRef};+use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};+use datafusion_common::config::ConfigOptions;use datafusion_expr::Operator;+use datafusion_functions::core::get_field;use datafusion_physical_expr::expressions::{BinaryExpr, Column, lit};+use datafusion_physical_expr::projection::ProjectionMapping;use datafusion_physical_expr::{EquivalenceProperties, LexOrdering, PhysicalExpr, PhysicalSortExpr,- PhysicalSortRequirement,+ PhysicalSortRequirement, ScalarFunctionExpr,};fn schema(n_cols: usize) -> SchemaRef {@@ -258,5 +266,63 @@ fn bench_shapes(c: &mut Criterion) {group.finish();}-criterion_group!(benches, bench_ordering_satisfaction, bench_shapes);+fn bench_project_wide_struct_fields(c: &mut Criterion) {+ const FIELDS: usize = 300;+ const READ: usize = 230;+ let wide: Fields = (0..FIELDS)+ .map(|i| Field::new(format!("f{i}"), DataType::Utf8, true))+ .collect();+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int64, true),+ Field::new("s", DataType::Struct(wide), true),+ ]));+ let col_a = Arc::new(Column::new("a", 0)) as Arc<dyn PhysicalExpr>;+ let col_s = Arc::new(Column::new("s", 1)) as Arc<dyn PhysicalExpr>;+ let config = Arc::new(ConfigOptions::default());+ let mut exprs = (0..READ)+ .map(|i| {+ let field = ScalarFunctionExpr::try_new(+ get_field(),+ vec![Arc::clone(&col_s), lit(format!("f{i}"))],+ &schema,+ Arc::clone(&config),+ )+ .unwrap();+ (Arc::new(field) as Arc<dyn PhysicalExpr>, format!("f{i}"))+ })+ .collect::<Vec<_>>();+ exprs.push((Arc::clone(&col_a), "a".to_string()));+ let mapping = ProjectionMapping::try_new(exprs, &schema).unwrap();+ let output = Arc::new(Schema::new(+ mapping+ .iter()+ .flat_map(|(source, targets)| {+ let data_type = source.data_type(&schema).unwrap();+ targets.iter().map(move |(target, _)| {+ Field::new(target.to_string(), data_type.clone(), true)+ })+ })+ .collect::<Vec<_>>(),+ ));+ let mut group = c.benchmark_group("equivalence_properties");+ for (suffix, sort_col) in [("", col_a), ("/ordered_by_s", col_s)] {+ let mut props = EquivalenceProperties::new(Arc::clone(&schema));+ props.add_orderings([vec![PhysicalSortExpr::new(+ sort_col,+ SortOptions::default(),+ )]]);+ group.bench_function(+ format!("project/wide_struct_fields/{READ}_of_{FIELDS}{suffix}"),+ |b| b.iter(|| props.project(&mapping, Arc::clone(&output))),+ );+ }+ group.finish();+}++criterion_group!(+ benches,+ bench_ordering_satisfaction,+ bench_shapes,+ bench_project_wide_struct_fields+);criterion_main!(benches);diff --git a/datafusion/physical-expr/src/equivalence/properties/dependency.rs b/datafusion/physical-expr/src/equivalence/properties/dependency.rsindex 5a7b757896..b626e644a2 100644--- a/datafusion/physical-expr/src/equivalence/properties/dependency.rs+++ b/datafusion/physical-expr/src/equivalence/properties/dependency.rs@@ -381,11 +381,14 @@ mod tests {use crate::{ConstExpr, EquivalenceProperties, ScalarFunctionExpr};use arrow::compute::SortOptions;- use arrow::datatypes::{DataType, Field, Schema, TimeUnit};+ use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef, TimeUnit};+ use arrow::record_batch::RecordBatch;use datafusion_common::config::ConfigOptions;use datafusion_common::{Constraint, Constraints, Result};+ use datafusion_expr::ColumnarValue;use datafusion_expr::Operator;- use datafusion_expr::sort_properties::SortProperties;+ use datafusion_expr::interval_arithmetic::Interval;+ use datafusion_expr::sort_properties::{ExprProperties, SortProperties};use datafusion_functions::string::concat;use datafusion_physical_expr_common::sort_expr::{LexRequirement, PhysicalSortRequirement,@@ -1598,4 +1601,255 @@ mod tests {Ok(())}++ /// An expression with its own identity that evaluates to its child and+ /// records the range it is given for its child. Of its+ /// properties it reports only an ordering: its child's, or ascending when+ /// `ordered` is set.+ #[derive(Debug)]+ struct RangeProbe {+ child: Arc<dyn PhysicalExpr>,+ id: usize,+ ordered: bool,+ seen: Seen,+ }++ impl PartialEq for RangeProbe {+ fn eq(&self, other: &Self) -> bool {+ self.id == other.id && self.child.eq(&other.child)+ }+ }+ impl Eq for RangeProbe {}+ impl std::hash::Hash for RangeProbe {+ fn hash<H: std::hash::Hasher>(&self, state: &mut H) {+ self.id.hash(state);+ self.child.hash(state);+ }+ }+ impl Display for RangeProbe {+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ write!(f, "probe{}({})", self.id, self.child)+ }+ }++ impl PhysicalExpr for RangeProbe {+ fn data_type(&self, input_schema: &Schema) -> Result<DataType> {+ self.child.data_type(input_schema)+ }+ fn nullable(&self, input_schema: &Schema) -> Result<bool> {+ self.child.nullable(input_schema)+ }+ fn evaluate(&self, batch: &RecordBatch) -> Result<ColumnarValue> {+ self.child.evaluate(batch)+ }+ fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {+ vec![&self.child]+ }+ fn with_new_children(+ self: Arc<Self>,+ children: Vec<Arc<dyn PhysicalExpr>>,+ ) -> Result<Arc<dyn PhysicalExpr>> {+ Ok(Arc::new(RangeProbe {+ child: Arc::clone(&children[0]),+ id: self.id,+ ordered: self.ordered,+ seen: Arc::clone(&self.seen),+ }))+ }+ fn get_properties(&self, children: &[ExprProperties]) -> Result<ExprProperties> {+ self.seen.lock().unwrap().push(children[0].range.clone());+ let order = if self.ordered {+ SortProperties::Ordered(SortOptions::default())+ } else {+ children[0].sort_properties+ };+ Ok(ExprProperties::new_unknown().with_order(order))+ }+ fn fmt_sql(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {+ write!(f, "{self}")+ }+ }++ type Seen = Arc<std::sync::Mutex<Vec<Interval>>>;++ /// Projects each `(child, id, ordered)` through a probe named `p{id}`.+ /// Returns the mapping and, per probe, the range types it will be given.+ fn probe_mapping(+ sources: Vec<(Arc<dyn PhysicalExpr>, usize, bool)>,+ schema: &SchemaRef,+ ) -> Result<(ProjectionMapping, Vec<Seen>)> {+ let mut exprs = vec![];+ let mut seen = vec![];+ for (child, id, ordered) in sources {+ let probe_seen = Seen::default();+ exprs.push((+ Arc::new(RangeProbe {+ child,+ id,+ ordered,+ seen: Arc::clone(&probe_seen),+ }) as Arc<dyn PhysicalExpr>,+ format!("p{id}"),+ ));+ seen.push(probe_seen);+ }+ Ok((ProjectionMapping::try_new(exprs, schema)?, seen))+ }++ fn assert_only_saw(seen: &Seen, expected: &DataType) {+ let seen = seen.lock().unwrap();+ assert!(!seen.is_empty());+ assert!(seen.iter().all(|r| &r.data_type() == expected), "{seen:?}");+ }++ fn sorted_orderings(properties: &EquivalenceProperties) -> Vec<String> {+ let mut orderings = properties+ .oeq_class()+ .iter()+ .map(|o| {+ o.iter()+ .map(|e| e.to_string())+ .collect::<Vec<_>>()+ .join(", ")+ })+ .collect::<Vec<_>>();+ orderings.sort();+ orderings+ }++ #[test]+ fn project_gives_each_source_the_range_of_its_own_column() -> Result<()> {+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int64, true),+ Field::new("b", DataType::Utf8, true),+ ]));+ let (a, b) = (col("a", &schema)?, col("b", &schema)?);+ let mut input = EquivalenceProperties::new(Arc::clone(&schema));+ input.add_orderings([vec![PhysicalSortExpr::new(+ Arc::clone(&a),+ SortOptions::default(),+ )]]);++ // Several sources over each column, so all but the first of each are+ // served from the cache: `a` through the sort key, `b` as a plain+ // column. `p4` declares itself ordered over the unordered `b`.+ let (mapping, seen) = probe_mapping(+ vec![+ (Arc::clone(&a), 0, false),+ (a, 1, false),+ (Arc::clone(&b), 2, false),+ (Arc::clone(&b), 3, false),+ (b, 4, true),+ ],+ &schema,+ )?;+ let projected = input.project(&mapping, output_schema(&mapping, &schema)?);++ for (seen, expected) in seen.iter().zip([+ DataType::Int64,+ DataType::Int64,+ DataType::Utf8,+ DataType::Utf8,+ DataType::Utf8,+ ]) {+ assert_only_saw(seen, &expected);+ }+ assert_eq!(+ sorted_orderings(&projected),+ vec!["p0@0 ASC", "p1@1 ASC", "p4@4 ASC"]+ );+ Ok(())+ }++ #[test]+ fn project_builds_the_range_of_an_ordered_column_once() -> Result<()> {+ let fields = Fields::from(vec![+ Field::new("x", DataType::Int64, true),+ Field::new("y", DataType::Utf8, true),+ ]);+ let schema = Arc::new(Schema::new(vec![Field::new(+ "s",+ DataType::Struct(fields),+ true,+ )]));+ let s = col("s", &schema)?;+ let mut input = EquivalenceProperties::new(Arc::clone(&schema));+ input.add_orderings([vec![PhysicalSortExpr::new(+ Arc::clone(&s),+ SortOptions::default(),+ )]]);++ // Every probe refers to the sort key, so both substituting the+ // ordering and deriving the projected one examine each of them.+ let (mapping, seen) = probe_mapping(+ (0..3).map(|id| (Arc::clone(&s), id, false)).collect(),+ &schema,+ )?;+ let projected = input.project(&mapping, output_schema(&mapping, &schema)?);++ assert_eq!(+ sorted_orderings(&projected),+ vec!["p0@0 ASC", "p1@1 ASC", "p2@2 ASC"]+ );+ // A rebuilt range of a struct holds a new null array.+ let mut arrays = vec![];+ for seen in &seen {+ for range in seen.lock().unwrap().iter() {+ let datafusion_common::ScalarValue::Struct(array) = range.lower() else {+ panic!("{range:?}");+ };+ arrays.push(Arc::clone(array));+ }+ }+ // Each probe is examined by both steps.+ assert!(arrays.len() >= 6, "{}", arrays.len());+ assert!(arrays.iter().all(|a| Arc::ptr_eq(a, &arrays[0])));+ Ok(())+ }++ #[test]+ fn project_does_not_reuse_a_column_range_across_calls() -> Result<()> {+ for data_type in [DataType::Utf8, DataType::Int64] {+ let schema =+ Arc::new(Schema::new(vec![Field::new("a", data_type.clone(), true)]));+ let (mapping, seen) =+ probe_mapping(vec![(col("a", &schema)?, 0, false)], &schema)?;+ EquivalenceProperties::new(Arc::clone(&schema))+ .project(&mapping, output_schema(&mapping, &schema)?);+ assert_only_saw(&seen[0], &data_type);+ }+ Ok(())+ }++ #[test]+ fn project_drops_a_derivation_whose_sort_key_type_fails() -> Result<()> {+ let schema = Arc::new(Schema::new(vec![+ Field::new("a", DataType::Int64, true),+ Field::new("b", DataType::Utf8, true),+ ]));+ // `a + b` has no type, so its range cannot be built.+ let untyped = Arc::new(BinaryExpr::new(+ col("a", &schema)?,+ Operator::Plus,+ col("b", &schema)?,+ )) as Arc<dyn PhysicalExpr>;+ assert!(untyped.data_type(&schema).is_err());+ let mut input = EquivalenceProperties::new(Arc::clone(&schema));+ input.add_orderings([vec![PhysicalSortExpr::new(+ Arc::clone(&untyped),+ SortOptions::default(),+ )]]);++ // The probe is ordered only through a derivation from `a + b`, which+ // fails before the probe is asked for its properties. The output type+ // cannot be computed either, so it is given as `Null`.+ let (mapping, seen) = probe_mapping(vec![(untyped, 0, false)], &schema)?;+ let out_schema =+ Arc::new(Schema::new(vec![Field::new("p0", DataType::Null, true)]));+ let projected = input.project(&mapping, out_schema);++ assert!(sorted_orderings(&projected).is_empty());+ assert!(seen[0].lock().unwrap().is_empty());+ Ok(())+ }}diff --git a/datafusion/physical-expr/src/equivalence/properties/mod.rs b/datafusion/physical-expr/src/equivalence/properties/mod.rsindex 467471366f..9e47fa70c6 100644--- a/datafusion/physical-expr/src/equivalence/properties/mod.rs+++ b/datafusion/physical-expr/src/equivalence/properties/mod.rs@@ -226,13 +226,15 @@ impl EquivalenceProperties {r_expr: Arc<dyn PhysicalExpr>,sort_expr: &PhysicalSortExpr,schema: &SchemaRef,+ column_ranges: &ColumnRanges,) -> Option<PhysicalSortExpr> {if r_expr.eq(&sort_expr.expr) {// No point in substituting an expression with itself.return None;}let dependencies = Dependencies::new(std::iter::once(sort_expr.clone()));- let properties = get_expr_properties(&r_expr, &dependencies, schema).ok()?;+ let properties =+ get_expr_properties(&r_expr, &dependencies, schema, column_ranges).ok()?;(properties.strictly_order_preserving&& properties.sort_properties == SortProperties::Ordered(sort_expr.options)).then(|| PhysicalSortExpr::new(r_expr, sort_expr.options))@@ -821,6 +823,7 @@ impl EquivalenceProperties {schema: &SchemaRef,mapping: &ProjectionMapping,oeq_class: OrderingEquivalenceClass,+ column_ranges: &ColumnRanges,) -> OrderingEquivalenceClass {let new_orderings = oeq_class.into_iter().flat_map(|order| {// Modify/expand existing orderings by substituting sort@@ -839,6 +842,7 @@ impl EquivalenceProperties {r_expr,&original_sort_expr,schema,+ column_ranges,)}).chain(std::iter::once(sort_expr))@@ -1017,14 +1021,21 @@ impl EquivalenceProperties {) -> Vec<LexOrdering> {// Normalize source expressions in the mapping:let mapping = self.normalize_mapping(mapping);+ // Substitution examines every source that refers to an ordered column, and building the+ // orderings examines every source, even one that refers to no ordered column (it then+ // gets an empty set of dependencies). Sharing one cache across both steps builds the+ // unbounded range of a wide column referenced by many sources only once.+ let column_ranges = ColumnRanges::default();+ oeq_class =+ Self::substitute_oeq_class(&self.schema, &mapping, oeq_class, &column_ranges);// Get dependency map for existing orderings:- oeq_class = Self::substitute_oeq_class(&self.schema, &mapping, oeq_class);let dependency_map = self.construct_dependency_map(oeq_class, &mapping);let orderings = mapping.iter().flat_map(|(source, targets)| {referred_dependencies(&dependency_map, source).into_iter().filter_map(|deps| {- let ep = get_expr_properties(source, &deps, &self.schema);+ let ep =+ get_expr_properties(source, &deps, &self.schema, &column_ranges);let sort_properties = ep.map(|prop| prop.sort_properties);if let Ok(SortProperties::Ordered(options)) = sort_properties {Some((options, deps))@@ -1321,7 +1332,12 @@ impl EquivalenceProperties {options,)));matches!(- get_expr_properties(transformed_expr, &dependencies, &self.schema)+ get_expr_properties(+ transformed_expr,+ &dependencies,+ &self.schema,+ &ColumnRanges::default(),+ ).map(|properties| properties.sort_properties),Ok(SortProperties::Ordered(actual)) if actual == options)@@ -1449,6 +1465,34 @@ fn expr_refers(.any(|child| expr_refers(child, referred_expr))}+/// Unbounded ranges of the columns of one schema, built on first use.+///+/// Building the unbounded range of a nested type materializes a null array of that type, so+/// a wide struct referenced by many projection sources is costly to rebuild per reference.+/// Only `Column`s are cached, by index: the schema is fixed for the cache's lifetime. Other+/// expressions and failures are computed as before and never cached. Cloning a cached range+/// clones its two endpoints, which for nested types share their arrays.+#[derive(Default)]+struct ColumnRanges(std::cell::RefCell<HashMap<usize, Interval>>);++impl ColumnRanges {+ fn unbounded(+ &self,+ expr: &Arc<dyn PhysicalExpr>,+ schema: &SchemaRef,+ ) -> Result<Interval> {+ let Some(column) = expr.downcast_ref::<Column>() else {+ return Interval::make_unbounded(&expr.data_type(schema)?);+ };+ if let Some(range) = self.0.borrow().get(&column.index()) {+ return Ok(range.clone());+ }+ let range = Interval::make_unbounded(&expr.data_type(schema)?)?;+ self.0.borrow_mut().insert(column.index(), range.clone());+ Ok(range)+ }+}+/// This function examines the given expression and its properties to determine/// the ordering properties of the expression. The range knowledge is not utilized/// yet in the scope of this function.@@ -1468,6 +1512,7 @@ fn get_expr_properties(expr: &Arc<dyn PhysicalExpr>,dependencies: &Dependencies,schema: &SchemaRef,+ column_ranges: &ColumnRanges,) -> Result<ExprProperties> {if let Some(column_order) = dependencies.iter().find(|&order| expr.eq(&order.expr)) {// If exact match is found, return its ordering. This is a base case@@ -1480,14 +1525,14 @@ fn get_expr_properties(// sort key, so their strictness only has to be relative to it.Ok(ExprProperties {sort_properties: SortProperties::Ordered(column_order.options),- range: Interval::make_unbounded(&expr.data_type(schema)?)?,+ range: column_ranges.unbounded(expr, schema)?,preserves_lex_ordering: false,strictly_order_preserving: true,})} else if expr.downcast_ref::<Column>().is_some() {Ok(ExprProperties {sort_properties: SortProperties::Unordered,- range: Interval::make_unbounded(&expr.data_type(schema)?)?,+ range: column_ranges.unbounded(expr, schema)?,preserves_lex_ordering: false,// A base case of the recursion: a column is the identity mapping// of itself, which is trivially one-to-one.@@ -1506,7 +1551,7 @@ fn get_expr_properties(let child_states = expr.children().iter()- .map(|child| get_expr_properties(child, dependencies, schema))+ .map(|child| get_expr_properties(child, dependencies, schema, column_ranges)).collect::<Result<Vec<_>>>()?;// Calculate expression ordering using ordering of its children.expr.get_properties(&child_states)