Skip to content

Apache DataFusion SQL Query Engine

Add additional tests for filter pushdown apis (#15955)

Adrian Garcia Badaracco95df9ee69e35
diff --git a/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs b/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
new file mode 100644
index 0000000000..a28933d97b
--- /dev/null
+++ b/datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
@@ -0,0 +1,378 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::sync::{Arc, LazyLock};
+
+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
+use datafusion::{
+ logical_expr::Operator,
+ physical_plan::{
+ expressions::{BinaryExpr, Column, Literal},
+ PhysicalExpr,
+ },
+ scalar::ScalarValue,
+};
+use datafusion_common::config::ConfigOptions;
+use datafusion_functions_aggregate::count::count_udaf;
+use datafusion_physical_expr::expressions::col;
+use datafusion_physical_expr::{aggregate::AggregateExprBuilder, Partitioning};
+use datafusion_physical_optimizer::filter_pushdown::FilterPushdown;
+use datafusion_physical_plan::{
+ aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy},
+ coalesce_batches::CoalesceBatchesExec,
+ filter::FilterExec,
+ repartition::RepartitionExec,
+};
+
+use util::{OptimizationTest, TestNode, TestScanBuilder};
+
+mod util;
+
+#[test]
+fn test_pushdown_into_scan() {
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(FilterExec::try_new(predicate, scan).unwrap());
+
+ // expect the predicate to be pushed down into the DataSource
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ "
+ );
+}
+
+/// Show that we can use config options to determine how to do pushdown.
+#[test]
+fn test_pushdown_into_scan_with_config_options() {
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(FilterExec::try_new(predicate, scan).unwrap()) as _;
+
+ let mut cfg = ConfigOptions::default();
+ insta::assert_snapshot!(
+ OptimizationTest::new(
+ Arc::clone(&plan),
+ FilterPushdown {},
+ false
+ ),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ "
+ );
+
+ cfg.execution.parquet.pushdown_filters = true;
+ insta::assert_snapshot!(
+ OptimizationTest::new(
+ plan,
+ FilterPushdown {},
+ true
+ ),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ "
+ );
+}
+
+#[test]
+fn test_filter_collapse() {
+ // filter should be pushed down into the parquet scan with two filters
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let predicate1 = col_lit_predicate("a", "foo", &schema());
+ let filter1 = Arc::new(FilterExec::try_new(predicate1, scan).unwrap());
+ let predicate2 = col_lit_predicate("b", "bar", &schema());
+ let plan = Arc::new(FilterExec::try_new(predicate2, filter1).unwrap());
+
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: b@1 = bar
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo AND b@1 = bar
+ "
+ );
+}
+
+#[test]
+fn test_filter_with_projection() {
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let projection = vec![1, 0];
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(
+ FilterExec::try_new(predicate, Arc::clone(&scan))
+ .unwrap()
+ .with_projection(Some(projection))
+ .unwrap(),
+ );
+
+ // expect the predicate to be pushed down into the DataSource but the FilterExec to be converted to ProjectionExec
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = foo, projection=[b@1, a@0]
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - ProjectionExec: expr=[b@1 as b, a@0 as a]
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ ",
+ );
+
+ // add a test where the filter is on a column that isn't included in the output
+ let projection = vec![1];
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(
+ FilterExec::try_new(predicate, scan)
+ .unwrap()
+ .with_projection(Some(projection))
+ .unwrap(),
+ );
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{},true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: a@0 = foo, projection=[b@1]
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - ProjectionExec: expr=[b@1 as b]
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ "
+ );
+}
+
+#[test]
+fn test_push_down_through_transparent_nodes() {
+ // expect the predicate to be pushed down into the DataSource
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let coalesce = Arc::new(CoalesceBatchesExec::new(scan, 1));
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let filter = Arc::new(FilterExec::try_new(predicate, coalesce).unwrap());
+ let repartition = Arc::new(
+ RepartitionExec::try_new(filter, Partitioning::RoundRobinBatch(1)).unwrap(),
+ );
+ let predicate = col_lit_predicate("b", "bar", &schema());
+ let plan = Arc::new(FilterExec::try_new(predicate, repartition).unwrap());
+
+ // expect the predicate to be pushed down into the DataSource
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{},true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: b@1 = bar
+ - RepartitionExec: partitioning=RoundRobinBatch(1), input_partitions=1
+ - FilterExec: a@0 = foo
+ - CoalesceBatchesExec: target_batch_size=1
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - RepartitionExec: partitioning=RoundRobinBatch(1), input_partitions=1
+ - CoalesceBatchesExec: target_batch_size=1
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo AND b@1 = bar
+ "
+ );
+}
+
+#[test]
+fn test_no_pushdown_through_aggregates() {
+ // There are 2 important points here:
+ // 1. The outer filter **is not** pushed down at all because we haven't implemented pushdown support
+ // yet for AggregateExec.
+ // 2. The inner filter **is** pushed down into the DataSource.
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+
+ let coalesce = Arc::new(CoalesceBatchesExec::new(scan, 10));
+
+ let filter = Arc::new(
+ FilterExec::try_new(col_lit_predicate("a", "foo", &schema()), coalesce).unwrap(),
+ );
+
+ let aggregate_expr =
+ vec![
+ AggregateExprBuilder::new(count_udaf(), vec![col("a", &schema()).unwrap()])
+ .schema(schema())
+ .alias("cnt")
+ .build()
+ .map(Arc::new)
+ .unwrap(),
+ ];
+ let group_by = PhysicalGroupBy::new_single(vec![
+ (col("a", &schema()).unwrap(), "a".to_string()),
+ (col("b", &schema()).unwrap(), "b".to_string()),
+ ]);
+ let aggregate = Arc::new(
+ AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by,
+ aggregate_expr.clone(),
+ vec![None],
+ filter,
+ schema(),
+ )
+ .unwrap(),
+ );
+
+ let coalesce = Arc::new(CoalesceBatchesExec::new(aggregate, 100));
+
+ let predicate = col_lit_predicate("b", "bar", &schema());
+ let plan = Arc::new(FilterExec::try_new(predicate, coalesce).unwrap());
+
+ // expect the predicate to be pushed down into the DataSource
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - FilterExec: b@1 = bar
+ - CoalesceBatchesExec: target_batch_size=100
+ - AggregateExec: mode=Final, gby=[a@0 as a, b@1 as b], aggr=[cnt], ordering_mode=PartiallySorted([0])
+ - FilterExec: a@0 = foo
+ - CoalesceBatchesExec: target_batch_size=10
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - FilterExec: b@1 = bar
+ - CoalesceBatchesExec: target_batch_size=100
+ - AggregateExec: mode=Final, gby=[a@0 as a, b@1 as b], aggr=[cnt]
+ - CoalesceBatchesExec: target_batch_size=10
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ "
+ );
+}
+
+/// Test various combinations of handling of child pushdown results
+/// in an ExectionPlan in combination with support/not support in a DataSource.
+#[test]
+fn test_node_handles_child_pushdown_result() {
+ // If we set `with_support(true)` + `inject_filter = true` then the filter is pushed down to the DataSource
+ // and no FilterExec is created.
+ let scan = TestScanBuilder::new(schema()).with_support(true).build();
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(TestNode::new(true, Arc::clone(&scan), predicate));
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - TestInsertExec { inject_filter: true }
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
+ output:
+ Ok:
+ - TestInsertExec { inject_filter: true }
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
+ ",
+ );
+
+ // If we set `with_support(false)` + `inject_filter = true` then the filter is not pushed down to the DataSource
+ // and a FilterExec is created.
+ let scan = TestScanBuilder::new(schema()).with_support(false).build();
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(TestNode::new(true, Arc::clone(&scan), predicate));
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - TestInsertExec { inject_filter: true }
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=false
+ output:
+ Ok:
+ - TestInsertExec { inject_filter: false }
+ - FilterExec: a@0 = foo
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=false
+ ",
+ );
+
+ // If we set `with_support(false)` + `inject_filter = false` then the filter is not pushed down to the DataSource
+ // and no FilterExec is created.
+ let scan = TestScanBuilder::new(schema()).with_support(false).build();
+ let predicate = col_lit_predicate("a", "foo", &schema());
+ let plan = Arc::new(TestNode::new(false, Arc::clone(&scan), predicate));
+ insta::assert_snapshot!(
+ OptimizationTest::new(plan, FilterPushdown{}, true),
+ @r"
+ OptimizationTest:
+ input:
+ - TestInsertExec { inject_filter: false }
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=false
+ output:
+ Ok:
+ - TestInsertExec { inject_filter: false }
+ - DataSourceExec: file_groups={1 group: [[test.paqruet]]}, projection=[a, b, c], file_type=test, pushdown_supported=false
+ ",
+ );
+}
+
+/// Schema:
+/// a: String
+/// b: String
+/// c: f64
+static TEST_SCHEMA: LazyLock<SchemaRef> = LazyLock::new(|| {
+ let fields = vec![
+ Field::new("a", DataType::Utf8, false),
+ Field::new("b", DataType::Utf8, false),
+ Field::new("c", DataType::Float64, false),
+ ];
+ Arc::new(Schema::new(fields))
+});
+
+fn schema() -> SchemaRef {
+ Arc::clone(&TEST_SCHEMA)
+}
+
+/// Returns a predicate that is a binary expression col = lit
+fn col_lit_predicate(
+ column_name: &str,
+ scalar_value: impl Into<ScalarValue>,
+ schema: &Schema,
+) -> Arc<dyn PhysicalExpr> {
+ let scalar_value = scalar_value.into();
+ Arc::new(BinaryExpr::new(
+ Arc::new(Column::new_with_schema(column_name, schema).unwrap()),
+ Operator::Eq,
+ Arc::new(Literal::new(scalar_value)),
+ ))
+}
diff --git a/datafusion/core/tests/physical_optimizer/filter_pushdown/util.rs b/datafusion/core/tests/physical_optimizer/filter_pushdown/util.rs
new file mode 100644
index 0000000000..811aba495a
--- /dev/null
+++ b/datafusion/core/tests/physical_optimizer/filter_pushdown/util.rs
@@ -0,0 +1,540 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::{
+ any::Any,
+ fmt::{Display, Formatter},
+};
+use std::{
+ pin::Pin,
+ sync::Arc,
+ task::{Context, Poll},
+};
+
+use arrow::datatypes::SchemaRef;
+use arrow::error::ArrowError;
+use arrow::{array::RecordBatch, compute::concat_batches};
+use datafusion::{datasource::object_store::ObjectStoreUrl, physical_plan::PhysicalExpr};
+use datafusion_common::{config::ConfigOptions, Statistics};
+use datafusion_common::{internal_err, Result};
+use datafusion_datasource::file_scan_config::FileScanConfigBuilder;
+use datafusion_datasource::file_stream::FileOpenFuture;
+use datafusion_datasource::source::DataSourceExec;
+use datafusion_datasource::{
+ file::FileSource, file_scan_config::FileScanConfig, file_stream::FileOpener,
+};
+use datafusion_datasource::{
+ file_meta::FileMeta, schema_adapter::DefaultSchemaAdapterFactory, PartitionedFile,
+};
+use datafusion_physical_expr::conjunction;
+use datafusion_physical_expr_common::physical_expr::fmt_sql;
+use datafusion_physical_optimizer::PhysicalOptimizerRule;
+use datafusion_physical_plan::{
+ displayable, metrics::ExecutionPlanMetricsSet, DisplayFormatType, ExecutionPlan,
+};
+use datafusion_physical_plan::{
+ filter::FilterExec,
+ filter_pushdown::{
+ ChildPushdownResult, FilterDescription, FilterPushdownPropagation,
+ PredicateSupport, PredicateSupports,
+ },
+ DisplayAs, PlanProperties,
+};
+
+use futures::stream::BoxStream;
+use futures::{FutureExt, Stream};
+use object_store::ObjectStore;
+
+pub struct TestOpener {
+ batches: Vec<RecordBatch>,
+ batch_size: Option<usize>,
+ schema: Option<SchemaRef>,
+ projection: Option<Vec<usize>>,
+}
+
+impl FileOpener for TestOpener {
+ fn open(&self, _file_meta: FileMeta) -> Result<FileOpenFuture> {
+ let mut batches = self.batches.clone();
+ if let Some(batch_size) = self.batch_size {
+ let batch = concat_batches(&batches[0].schema(), &batches)?;
+ let mut new_batches = Vec::new();
+ for i in (0..batch.num_rows()).step_by(batch_size) {
+ let end = std::cmp::min(i + batch_size, batch.num_rows());
+ let batch = batch.slice(i, end - i);
+ new_batches.push(batch);
+ }
+ batches = new_batches.into_iter().collect();
+ }
+ if let Some(schema) = &self.schema {
+ let factory = DefaultSchemaAdapterFactory::from_schema(Arc::clone(schema));
+ let (mapper, projection) = factory.map_schema(&batches[0].schema()).unwrap();
+ let mut new_batches = Vec::new();
+ for batch in batches {
+ let batch = batch.project(&projection).unwrap();
+ let batch = mapper.map_batch(batch).unwrap();
+ new_batches.push(batch);
+ }
+ batches = new_batches;
+ }
+ if let Some(projection) = &self.projection {
+ batches = batches
+ .into_iter()
+ .map(|batch| batch.project(projection).unwrap())
+ .collect();
+ }
+
+ let stream = TestStream::new(batches);
+
+ Ok((async {
+ let stream: BoxStream<'static, Result<RecordBatch, ArrowError>> =
+ Box::pin(stream);
+ Ok(stream)
+ })
+ .boxed())
+ }
+}
+
+/// A placeholder data source that accepts filter pushdown
+#[derive(Clone, Default)]
+pub struct TestSource {
+ support: bool,
+ predicate: Option<Arc<dyn PhysicalExpr>>,
+ statistics: Option<Statistics>,
+ batch_size: Option<usize>,
+ batches: Vec<RecordBatch>,
+ schema: Option<SchemaRef>,
+ metrics: ExecutionPlanMetricsSet,
+ projection: Option<Vec<usize>>,
+}
+
+impl TestSource {
+ fn new(support: bool, batches: Vec<RecordBatch>) -> Self {
+ Self {
+ support,
+ predicate: None,
+ statistics: None,
+ batch_size: None,
+ schema: None,
+ projection: None,
+ metrics: ExecutionPlanMetricsSet::new(),
+ batches,
+ }
+ }
+}
+
+impl FileSource for TestSource {
+ fn create_file_opener(
+ &self,
+ _object_store: Arc<dyn ObjectStore>,
+ _base_config: &FileScanConfig,
+ _partition: usize,
+ ) -> Arc<dyn FileOpener> {
+ Arc::new(TestOpener {
+ batches: self.batches.clone(),
+ batch_size: self.batch_size,
+ schema: self.schema.clone(),
+ projection: self.projection.clone(),
+ })
+ }
+
+ fn as_any(&self) -> &dyn Any {
+ todo!("should not be called")
+ }
+
+ fn with_batch_size(&self, batch_size: usize) -> Arc<dyn FileSource> {
+ Arc::new(TestSource {
+ batch_size: Some(batch_size),
+ ..self.clone()
+ })
+ }
+
+ fn with_schema(&self, schema: SchemaRef) -> Arc<dyn FileSource> {
+ Arc::new(TestSource {
+ schema: Some(schema),
+ ..self.clone()
+ })
+ }
+
+ fn with_projection(&self, config: &FileScanConfig) -> Arc<dyn FileSource> {
+ Arc::new(TestSource {
+ projection: config.projection.clone(),
+ ..self.clone()
+ })
+ }
+
+ fn with_statistics(&self, statistics: Statistics) -> Arc<dyn FileSource> {
+ Arc::new(TestSource {
+ statistics: Some(statistics),
+ ..self.clone()
+ })
+ }
+
+ fn metrics(&self) -> &ExecutionPlanMetricsSet {
+ &self.metrics
+ }
+
+ fn statistics(&self) -> Result<Statistics> {
+ Ok(self
+ .statistics
+ .as_ref()
+ .expect("statistics not set")
+ .clone())
+ }
+
+ fn file_type(&self) -> &str {
+ "test"
+ }
+
+ fn fmt_extra(&self, t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result {
+ match t {
+ DisplayFormatType::Default | DisplayFormatType::Verbose => {
+ let support = format!(", pushdown_supported={}", self.support);
+
+ let predicate_string = self
+ .predicate
+ .as_ref()
+ .map(|p| format!(", predicate={p}"))
+ .unwrap_or_default();
+
+ write!(f, "{}{}", support, predicate_string)
+ }
+ DisplayFormatType::TreeRender => {
+ if let Some(predicate) = &self.predicate {
+ writeln!(f, "pushdown_supported={}", fmt_sql(predicate.as_ref()))?;
+ writeln!(f, "predicate={}", fmt_sql(predicate.as_ref()))?;
+ }
+ Ok(())
+ }
+ }
+ }
+
+ fn try_pushdown_filters(
+ &self,
+ mut filters: Vec<Arc<dyn PhysicalExpr>>,
+ config: &ConfigOptions,
+ ) -> Result<FilterPushdownPropagation<Arc<dyn FileSource>>> {
+ if self.support && config.execution.parquet.pushdown_filters {
+ if let Some(internal) = self.predicate.as_ref() {
+ filters.push(Arc::clone(internal));
+ }
+ let new_node = Arc::new(TestSource {
+ predicate: Some(conjunction(filters.clone())),
+ ..self.clone()
+ });
+ Ok(FilterPushdownPropagation {
+ filters: PredicateSupports::all_supported(filters),
+ updated_node: Some(new_node),
+ })
+ } else {
+ Ok(FilterPushdownPropagation::unsupported(filters))
+ }
+ }
+}
+
+#[derive(Debug, Clone)]
+pub struct TestScanBuilder {
+ support: bool,
+ batches: Vec<RecordBatch>,
+ schema: SchemaRef,
+}
+
+impl TestScanBuilder {
+ pub fn new(schema: SchemaRef) -> Self {
+ Self {
+ support: false,
+ batches: vec![],
+ schema,
+ }
+ }
+
+ pub fn with_support(mut self, support: bool) -> Self {
+ self.support = support;
+ self
+ }
+
+ pub fn build(self) -> Arc<dyn ExecutionPlan> {
+ let source = Arc::new(TestSource::new(self.support, self.batches));
+ let base_config = FileScanConfigBuilder::new(
+ ObjectStoreUrl::parse("test://").unwrap(),
+ Arc::clone(&self.schema),
+ source,
+ )
+ .with_file(PartitionedFile::new("test.paqruet", 123))
+ .build();
+ DataSourceExec::from_data_source(base_config)
+ }
+}
+
+/// Index into the data that has been returned so far
+#[derive(Debug, Default, Clone)]
+pub struct BatchIndex {
+ inner: Arc<std::sync::Mutex<usize>>,
+}
+
+impl BatchIndex {
+ /// Return the current index
+ pub fn value(&self) -> usize {
+ let inner = self.inner.lock().unwrap();
+ *inner
+ }
+
+ // increment the current index by one
+ pub fn incr(&self) {
+ let mut inner = self.inner.lock().unwrap();
+ *inner += 1;
+ }
+}
+
+/// Iterator over batches
+#[derive(Debug, Default)]
+pub struct TestStream {
+ /// Vector of record batches
+ data: Vec<RecordBatch>,
+ /// Index into the data that has been returned so far
+ index: BatchIndex,
+}
+
+impl TestStream {
+ /// Create an iterator for a vector of record batches. Assumes at
+ /// least one entry in data (for the schema)
+ pub fn new(data: Vec<RecordBatch>) -> Self {
+ // check that there is at least one entry in data and that all batches have the same schema
+ assert!(!data.is_empty(), "data must not be empty");
+ assert!(
+ data.iter().all(|batch| batch.schema() == data[0].schema()),
+ "all batches must have the same schema"
+ );
+ Self {
+ data,
+ ..Default::default()
+ }
+ }
+}
+
+impl Stream for TestStream {
+ type Item = Result<RecordBatch, ArrowError>;
+
+ fn poll_next(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Option<Self::Item>> {
+ let next_batch = self.index.value();
+
+ Poll::Ready(if next_batch < self.data.len() {
+ let next_batch = self.index.value();
+ self.index.incr();
+ Some(Ok(self.data[next_batch].clone()))
+ } else {
+ None
+ })
+ }
+
+ fn size_hint(&self) -> (usize, Option<usize>) {
+ (self.data.len(), Some(self.data.len()))
+ }
+}
+
+/// A harness for testing physical optimizers.
+///
+/// You can use this to test the output of a physical optimizer rule using insta snapshots
+#[derive(Debug)]
+pub struct OptimizationTest {
+ input: Vec<String>,
+ output: Result<Vec<String>, String>,
+}
+
+impl OptimizationTest {
+ pub fn new<O>(
+ input_plan: Arc<dyn ExecutionPlan>,
+ opt: O,
+ allow_pushdown_filters: bool,
+ ) -> Self
+ where
+ O: PhysicalOptimizerRule,
+ {
+ let mut parquet_pushdown_config = ConfigOptions::default();
+ parquet_pushdown_config.execution.parquet.pushdown_filters =
+ allow_pushdown_filters;
+
+ let input = format_execution_plan(&input_plan);
+ let input_schema = input_plan.schema();
+
+ let output_result = opt.optimize(input_plan, &parquet_pushdown_config);
+ let output = output_result
+ .and_then(|plan| {
+ if opt.schema_check() && (plan.schema() != input_schema) {
+ internal_err!(
+ "Schema mismatch:\n\nBefore:\n{:?}\n\nAfter:\n{:?}",
+ input_schema,
+ plan.schema()
+ )
+ } else {
+ Ok(plan)
+ }
+ })
+ .map(|plan| format_execution_plan(&plan))
+ .map_err(|e| e.to_string());
+
+ Self { input, output }
+ }
+}
+
+impl Display for OptimizationTest {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ writeln!(f, "OptimizationTest:")?;
+ writeln!(f, " input:")?;
+ for line in &self.input {
+ writeln!(f, " - {line}")?;
+ }
+ writeln!(f, " output:")?;
+ match &self.output {
+ Ok(output) => {
+ writeln!(f, " Ok:")?;
+ for line in output {
+ writeln!(f, " - {line}")?;
+ }
+ }
+ Err(err) => {
+ writeln!(f, " Err: {err}")?;
+ }
+ }
+ Ok(())
+ }
+}
+
+pub fn format_execution_plan(plan: &Arc<dyn ExecutionPlan>) -> Vec<String> {
+ format_lines(&displayable(plan.as_ref()).indent(false).to_string())
+}
+
+fn format_lines(s: &str) -> Vec<String> {
+ s.trim().split('\n').map(|s| s.to_string()).collect()
+}
+
+#[derive(Debug)]
+pub(crate) struct TestNode {
+ inject_filter: bool,
+ input: Arc<dyn ExecutionPlan>,
+ predicate: Arc<dyn PhysicalExpr>,
+}
+
+impl TestNode {
+ pub fn new(
+ inject_filter: bool,
+ input: Arc<dyn ExecutionPlan>,
+ predicate: Arc<dyn PhysicalExpr>,
+ ) -> Self {
+ Self {
+ inject_filter,
+ input,
+ predicate,
+ }
+ }
+}
+
+impl DisplayAs for TestNode {
+ fn fmt_as(&self, _t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result {
+ write!(
+ f,
+ "TestInsertExec {{ inject_filter: {} }}",
+ self.inject_filter
+ )
+ }
+}
+
+impl ExecutionPlan for TestNode {
+ fn name(&self) -> &str {
+ "TestInsertExec"
+ }
+
+ fn as_any(&self) -> &dyn Any {
+ self
+ }
+
+ fn properties(&self) -> &PlanProperties {
+ self.input.properties()
+ }
+
+ fn children(&self) -> Vec<&Arc<dyn ExecutionPlan>> {
+ vec![&self.input]
+ }
+
+ fn with_new_children(
+ self: Arc<Self>,
+ children: Vec<Arc<dyn ExecutionPlan>>,
+ ) -> Result<Arc<dyn ExecutionPlan>> {
+ assert!(children.len() == 1);
+ Ok(Arc::new(TestNode::new(
+ self.inject_filter,
+ children[0].clone(),
+ self.predicate.clone(),
+ )))
+ }
+
+ fn execute(
+ &self,
+ _partition: usize,
+ _context: Arc<datafusion_execution::TaskContext>,
+ ) -> Result<datafusion_execution::SendableRecordBatchStream> {
+ unimplemented!("TestInsertExec is a stub for testing.")
+ }
+
+ fn gather_filters_for_pushdown(
+ &self,
+ parent_filters: Vec<Arc<dyn PhysicalExpr>>,
+ _config: &ConfigOptions,
+ ) -> Result<FilterDescription> {
+ Ok(FilterDescription::new_with_child_count(1)
+ .all_parent_filters_supported(parent_filters)
+ .with_self_filter(Arc::clone(&self.predicate)))
+ }
+
+ fn handle_child_pushdown_result(
+ &self,
+ child_pushdown_result: ChildPushdownResult,
+ _config: &ConfigOptions,
+ ) -> Result<FilterPushdownPropagation<Arc<dyn ExecutionPlan>>> {
+ if self.inject_filter {
+ // Add a FilterExec if our own filter was not handled by the child
+
+ // We have 1 child
+ assert_eq!(child_pushdown_result.self_filters.len(), 1);
+ let self_pushdown_result = child_pushdown_result.self_filters[0].clone();
+ // And pushed down 1 filter
+ assert_eq!(self_pushdown_result.len(), 1);
+ let self_pushdown_result = self_pushdown_result.into_inner();
+
+ match &self_pushdown_result[0] {
+ PredicateSupport::Unsupported(filter) => {
+ // We have a filter to push down
+ let new_child =
+ FilterExec::try_new(Arc::clone(filter), Arc::clone(&self.input))?;
+ let new_self =
+ TestNode::new(false, Arc::new(new_child), self.predicate.clone());
+ let mut res =
+ FilterPushdownPropagation::transparent(child_pushdown_result);
+ res.updated_node = Some(Arc::new(new_self) as Arc<dyn ExecutionPlan>);
+ Ok(res)
+ }
+ PredicateSupport::Supported(_) => {
+ let res =
+ FilterPushdownPropagation::transparent(child_pushdown_result);
+ Ok(res)
+ }
+ }
+ } else {
+ let res = FilterPushdownPropagation::transparent(child_pushdown_result);
+ Ok(res)
+ }
+ }
+}
diff --git a/datafusion/core/tests/physical_optimizer/mod.rs b/datafusion/core/tests/physical_optimizer/mod.rs
index c115a5253a..98e7b87ad2 100644
--- a/datafusion/core/tests/physical_optimizer/mod.rs
+++ b/datafusion/core/tests/physical_optimizer/mod.rs
@@ -21,12 +21,12 @@ mod aggregate_statistics;
mod combine_partial_final_agg;
mod enforce_distribution;
mod enforce_sorting;
+mod filter_pushdown;
mod join_selection;
mod limit_pushdown;
mod limited_distinct_aggregation;
mod partition_statistics;
mod projection_pushdown;
-mod push_down_filter;
mod replace_with_order_preserving_variants;
mod sanity_checker;
mod test_utils;
diff --git a/datafusion/core/tests/physical_optimizer/push_down_filter.rs b/datafusion/core/tests/physical_optimizer/push_down_filter.rs
deleted file mode 100644
index 326a7b837e..0000000000
--- a/datafusion/core/tests/physical_optimizer/push_down_filter.rs
+++ /dev/null
@@ -1,539 +0,0 @@
-// Licensed to the Apache Software Foundation (ASF) under one
-// or more contributor license agreements. See the NOTICE file
-// distributed with this work for additional information
-// regarding copyright ownership. The ASF licenses this file
-// to you under the Apache License, Version 2.0 (the
-// "License"); you may not use this file except in compliance
-// with the License. You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing,
-// software distributed under the License is distributed on an
-// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
-// KIND, either express or implied. See the License for the
-// specific language governing permissions and limitations
-// under the License.
-
-use std::sync::{Arc, OnceLock};
-use std::{
- any::Any,
- fmt::{Display, Formatter},
-};
-
-use arrow::datatypes::{DataType, Field, Schema, SchemaRef};
-use datafusion::{
- datasource::object_store::ObjectStoreUrl,
- logical_expr::Operator,
- physical_plan::{
- expressions::{BinaryExpr, Column, Literal},
- PhysicalExpr,
- },
- scalar::ScalarValue,
-};
-use datafusion_common::{config::ConfigOptions, Statistics};
-use datafusion_common::{internal_err, Result};
-use datafusion_datasource::file_scan_config::FileScanConfigBuilder;
-use datafusion_datasource::source::DataSourceExec;
-use datafusion_datasource::{
- file::FileSource, file_scan_config::FileScanConfig, file_stream::FileOpener,
-};
-use datafusion_expr::test::function_stub::count_udaf;
-use datafusion_physical_expr::expressions::col;
-use datafusion_physical_expr::{
- aggregate::AggregateExprBuilder, conjunction, Partitioning,
-};
-use datafusion_physical_expr_common::physical_expr::fmt_sql;
-use datafusion_physical_optimizer::filter_pushdown::FilterPushdown;
-use datafusion_physical_optimizer::PhysicalOptimizerRule;
-use datafusion_physical_plan::filter_pushdown::{
- FilterPushdownPropagation, PredicateSupports,
-};
-use datafusion_physical_plan::{
- aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy},
- coalesce_batches::CoalesceBatchesExec,
- filter::FilterExec,
- repartition::RepartitionExec,
-};
-use datafusion_physical_plan::{
- displayable, metrics::ExecutionPlanMetricsSet, DisplayFormatType, ExecutionPlan,
-};
-
-use object_store::ObjectStore;
-
-/// A placeholder data source that accepts filter pushdown
-#[derive(Clone, Default)]
-struct TestSource {
- support: bool,
- predicate: Option<Arc<dyn PhysicalExpr>>,
- statistics: Option<Statistics>,
-}
-
-impl TestSource {
- fn new(support: bool) -> Self {
- Self {
- support,
- predicate: None,
- statistics: None,
- }
- }
-}
-
-impl FileSource for TestSource {
- fn create_file_opener(
- &self,
- _object_store: Arc<dyn ObjectStore>,
- _base_config: &FileScanConfig,
- _partition: usize,
- ) -> Arc<dyn FileOpener> {
- todo!("should not be called")
- }
-
- fn as_any(&self) -> &dyn Any {
- todo!("should not be called")
- }
-
- fn with_batch_size(&self, _batch_size: usize) -> Arc<dyn FileSource> {
- todo!("should not be called")
- }
-
- fn with_schema(&self, _schema: SchemaRef) -> Arc<dyn FileSource> {
- todo!("should not be called")
- }
-
- fn with_projection(&self, _config: &FileScanConfig) -> Arc<dyn FileSource> {
- todo!("should not be called")
- }
-
- fn with_statistics(&self, statistics: Statistics) -> Arc<dyn FileSource> {
- Arc::new(TestSource {
- statistics: Some(statistics),
- ..self.clone()
- })
- }
-
- fn metrics(&self) -> &ExecutionPlanMetricsSet {
- todo!("should not be called")
- }
-
- fn statistics(&self) -> Result<Statistics> {
- Ok(self
- .statistics
- .as_ref()
- .expect("statistics not set")
- .clone())
- }
-
- fn file_type(&self) -> &str {
- "test"
- }
-
- fn fmt_extra(&self, t: DisplayFormatType, f: &mut Formatter) -> std::fmt::Result {
- match t {
- DisplayFormatType::Default | DisplayFormatType::Verbose => {
- let support = format!(", pushdown_supported={}", self.support);
-
- let predicate_string = self
- .predicate
- .as_ref()
- .map(|p| format!(", predicate={p}"))
- .unwrap_or_default();
-
- write!(f, "{}{}", support, predicate_string)
- }
- DisplayFormatType::TreeRender => {
- if let Some(predicate) = &self.predicate {
- writeln!(f, "pushdown_supported={}", fmt_sql(predicate.as_ref()))?;
- writeln!(f, "predicate={}", fmt_sql(predicate.as_ref()))?;
- }
- Ok(())
- }
- }
- }
-
- fn try_pushdown_filters(
- &self,
- mut filters: Vec<Arc<dyn PhysicalExpr>>,
- config: &ConfigOptions,
- ) -> Result<FilterPushdownPropagation<Arc<dyn FileSource>>> {
- if self.support && config.execution.parquet.pushdown_filters {
- if let Some(internal) = self.predicate.as_ref() {
- filters.push(Arc::clone(internal));
- }
- let new_node = Arc::new(TestSource {
- support: true,
- predicate: Some(conjunction(filters.clone())),
- statistics: self.statistics.clone(), // should be updated in reality
- });
- Ok(FilterPushdownPropagation {
- filters: PredicateSupports::all_supported(filters),
- updated_node: Some(new_node),
- })
- } else {
- Ok(FilterPushdownPropagation::unsupported(filters))
- }
- }
-}
-
-fn test_scan(support: bool) -> Arc<dyn ExecutionPlan> {
- let schema = schema();
- let source = Arc::new(TestSource::new(support));
- let base_config = FileScanConfigBuilder::new(
- ObjectStoreUrl::parse("test://").unwrap(),
- Arc::clone(schema),
- source,
- )
- .build();
- DataSourceExec::from_data_source(base_config)
-}
-
-#[test]
-fn test_pushdown_into_scan() {
- let scan = test_scan(true);
- let predicate = col_lit_predicate("a", "foo", schema());
- let plan = Arc::new(FilterExec::try_new(predicate, scan).unwrap());
-
- // expect the predicate to be pushed down into the DataSource
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{}, true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: a@0 = foo
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
- "
- );
-}
-
-/// Show that we can use config options to determine how to do pushdown.
-#[test]
-fn test_pushdown_into_scan_with_config_options() {
- let scan = test_scan(true);
- let predicate = col_lit_predicate("a", "foo", schema());
- let plan = Arc::new(FilterExec::try_new(predicate, scan).unwrap()) as _;
-
- let mut cfg = ConfigOptions::default();
- insta::assert_snapshot!(
- OptimizationTest::new(
- Arc::clone(&plan),
- FilterPushdown {},
- false
- ),
- @r"
- OptimizationTest:
- input:
- - FilterExec: a@0 = foo
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - FilterExec: a@0 = foo
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- "
- );
-
- cfg.execution.parquet.pushdown_filters = true;
- insta::assert_snapshot!(
- OptimizationTest::new(
- plan,
- FilterPushdown {},
- true
- ),
- @r"
- OptimizationTest:
- input:
- - FilterExec: a@0 = foo
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
- "
- );
-}
-
-#[test]
-fn test_filter_collapse() {
- // filter should be pushed down into the parquet scan with two filters
- let scan = test_scan(true);
- let predicate1 = col_lit_predicate("a", "foo", schema());
- let filter1 = Arc::new(FilterExec::try_new(predicate1, scan).unwrap());
- let predicate2 = col_lit_predicate("b", "bar", schema());
- let plan = Arc::new(FilterExec::try_new(predicate2, filter1).unwrap());
-
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{}, true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: b@1 = bar
- - FilterExec: a@0 = foo
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo AND b@1 = bar
- "
- );
-}
-
-#[test]
-fn test_filter_with_projection() {
- let scan = test_scan(true);
- let projection = vec![1, 0];
- let predicate = col_lit_predicate("a", "foo", schema());
- let filter = Arc::new(
- FilterExec::try_new(Arc::clone(&predicate), Arc::clone(&scan))
- .unwrap()
- .with_projection(Some(projection))
- .unwrap(),
- );
- let predicate = col_lit_predicate("b", "bar", &filter.schema());
- let plan = Arc::new(FilterExec::try_new(predicate, filter).unwrap());
-
- // expect the predicate to be pushed down into the DataSource but the FilterExec to be converted to ProjectionExec
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{}, true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: b@0 = bar
- - FilterExec: a@0 = foo, projection=[b@1, a@0]
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - ProjectionExec: expr=[b@1 as b, a@0 as a]
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo AND b@1 = bar
- ",
- );
-
- // add a test where the filter is on a column that isn't included in the output
- let projection = vec![1];
- let predicate = col_lit_predicate("a", "foo", schema());
- let plan = Arc::new(
- FilterExec::try_new(predicate, scan)
- .unwrap()
- .with_projection(Some(projection))
- .unwrap(),
- );
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{},true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: a@0 = foo, projection=[b@1]
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - ProjectionExec: expr=[b@1 as b]
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
- "
- );
-}
-
-#[test]
-fn test_push_down_through_transparent_nodes() {
- // expect the predicate to be pushed down into the DataSource
- let scan = test_scan(true);
- let coalesce = Arc::new(CoalesceBatchesExec::new(scan, 1));
- let predicate = col_lit_predicate("a", "foo", schema());
- let filter = Arc::new(FilterExec::try_new(predicate, coalesce).unwrap());
- let repartition = Arc::new(
- RepartitionExec::try_new(filter, Partitioning::RoundRobinBatch(1)).unwrap(),
- );
- let predicate = col_lit_predicate("b", "bar", schema());
- let plan = Arc::new(FilterExec::try_new(predicate, repartition).unwrap());
-
- // expect the predicate to be pushed down into the DataSource
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{},true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: b@1 = bar
- - RepartitionExec: partitioning=RoundRobinBatch(1), input_partitions=0
- - FilterExec: a@0 = foo
- - CoalesceBatchesExec: target_batch_size=1
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - RepartitionExec: partitioning=RoundRobinBatch(1), input_partitions=0
- - CoalesceBatchesExec: target_batch_size=1
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo AND b@1 = bar
- "
- );
-}
-
-#[test]
-fn test_no_pushdown_through_aggregates() {
- // There are 2 important points here:
- // 1. The outer filter **is not** pushed down at all because we haven't implemented pushdown support
- // yet for AggregateExec.
- // 2. The inner filter **is** pushed down into the DataSource.
- let scan = test_scan(true);
-
- let coalesce = Arc::new(CoalesceBatchesExec::new(scan, 10));
-
- let filter = Arc::new(
- FilterExec::try_new(col_lit_predicate("a", "foo", schema()), coalesce).unwrap(),
- );
-
- let aggregate_expr =
- vec![
- AggregateExprBuilder::new(count_udaf(), vec![col("a", schema()).unwrap()])
- .schema(Arc::clone(schema()))
- .alias("cnt")
- .build()
- .map(Arc::new)
- .unwrap(),
- ];
- let group_by = PhysicalGroupBy::new_single(vec![
- (col("a", schema()).unwrap(), "a".to_string()),
- (col("b", schema()).unwrap(), "b".to_string()),
- ]);
- let aggregate = Arc::new(
- AggregateExec::try_new(
- AggregateMode::Final,
- group_by,
- aggregate_expr.clone(),
- vec![None],
- filter,
- Arc::clone(schema()),
- )
- .unwrap(),
- );
-
- let coalesce = Arc::new(CoalesceBatchesExec::new(aggregate, 100));
-
- let predicate = col_lit_predicate("b", "bar", schema());
- let plan = Arc::new(FilterExec::try_new(predicate, coalesce).unwrap());
-
- // expect the predicate to be pushed down into the DataSource
- insta::assert_snapshot!(
- OptimizationTest::new(plan, FilterPushdown{}, true),
- @r"
- OptimizationTest:
- input:
- - FilterExec: b@1 = bar
- - CoalesceBatchesExec: target_batch_size=100
- - AggregateExec: mode=Final, gby=[a@0 as a, b@1 as b], aggr=[cnt], ordering_mode=PartiallySorted([0])
- - FilterExec: a@0 = foo
- - CoalesceBatchesExec: target_batch_size=10
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true
- output:
- Ok:
- - FilterExec: b@1 = bar
- - CoalesceBatchesExec: target_batch_size=100
- - AggregateExec: mode=Final, gby=[a@0 as a, b@1 as b], aggr=[cnt]
- - CoalesceBatchesExec: target_batch_size=10
- - DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, pushdown_supported=true, predicate=a@0 = foo
- "
- );
-}
-
-/// Schema:
-/// a: String
-/// b: String
-/// c: f64
-static TEST_SCHEMA: OnceLock<SchemaRef> = OnceLock::new();
-
-fn schema() -> &'static SchemaRef {
- TEST_SCHEMA.get_or_init(|| {
- let fields = vec![
- Field::new("a", DataType::Utf8, false),
- Field::new("b", DataType::Utf8, false),
- Field::new("c", DataType::Float64, false),
- ];
- Arc::new(Schema::new(fields))
- })
-}
-
-/// Returns a predicate that is a binary expression col = lit
-fn col_lit_predicate(
- column_name: &str,
- scalar_value: impl Into<ScalarValue>,
- schema: &Schema,
-) -> Arc<dyn PhysicalExpr> {
- let scalar_value = scalar_value.into();
- Arc::new(BinaryExpr::new(
- Arc::new(Column::new_with_schema(column_name, schema).unwrap()),
- Operator::Eq,
- Arc::new(Literal::new(scalar_value)),
- ))
-}
-
-/// A harness for testing physical optimizers.
-///
-/// You can use this to test the output of a physical optimizer rule using insta snapshots
-#[derive(Debug)]
-pub struct OptimizationTest {
- input: Vec<String>,
- output: Result<Vec<String>, String>,
-}
-
-impl OptimizationTest {
- pub fn new<O>(
- input_plan: Arc<dyn ExecutionPlan>,
- opt: O,
- allow_pushdown_filters: bool,
- ) -> Self
- where
- O: PhysicalOptimizerRule,
- {
- let mut parquet_pushdown_config = ConfigOptions::default();
- parquet_pushdown_config.execution.parquet.pushdown_filters =
- allow_pushdown_filters;
-
- let input = format_execution_plan(&input_plan);
- let input_schema = input_plan.schema();
-
- let output_result = opt.optimize(input_plan, &parquet_pushdown_config);
- let output = output_result
- .and_then(|plan| {
- if opt.schema_check() && (plan.schema() != input_schema) {
- internal_err!(
- "Schema mismatch:\n\nBefore:\n{:?}\n\nAfter:\n{:?}",
- input_schema,
- plan.schema()
- )
- } else {
- Ok(plan)
- }
- })
- .map(|plan| format_execution_plan(&plan))
- .map_err(|e| e.to_string());
-
- Self { input, output }
- }
-}
-
-impl Display for OptimizationTest {
- fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- writeln!(f, "OptimizationTest:")?;
- writeln!(f, " input:")?;
- for line in &self.input {
- writeln!(f, " - {line}")?;
- }
- writeln!(f, " output:")?;
- match &self.output {
- Ok(output) => {
- writeln!(f, " Ok:")?;
- for line in output {
- writeln!(f, " - {line}")?;
- }
- }
- Err(err) => {
- writeln!(f, " Err: {err}")?;
- }
- }
- Ok(())
- }
-}
-
-pub fn format_execution_plan(plan: &Arc<dyn ExecutionPlan>) -> Vec<String> {
- format_lines(&displayable(plan.as_ref()).indent(false).to_string())
-}
-
-fn format_lines(s: &str) -> Vec<String> {
- s.trim().split('\n').map(|s| s.to_string()).collect()
-}
diff --git a/datafusion/datasource/src/file_scan_config.rs b/datafusion/datasource/src/file_scan_config.rs
index ae94af5a7b..147fcc59d5 100644
--- a/datafusion/datasource/src/file_scan_config.rs
+++ b/datafusion/datasource/src/file_scan_config.rs
@@ -600,12 +600,9 @@ impl DataSource for FileScanConfig {
let result = self.file_source.try_pushdown_filters(filters, config)?;
match result.updated_node {
Some(new_file_source) => {
- let file_scan_config = FileScanConfigBuilder::new(
- self.object_store_url.clone(),
- Arc::clone(&self.file_schema),
- new_file_source,
- )
- .build();
+ let file_scan_config = FileScanConfigBuilder::from(self.clone())
+ .with_source(new_file_source)
+ .build();
Ok(FilterPushdownPropagation {
filters: result.filters,
updated_node: Some(Arc::new(file_scan_config) as _),