Skip to content

ExecutionPlan: add APIs for filter pushdown & optimizer rule to apply them #15566

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 42 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a653b15
ExecutionPlan: add APIs for filter pushdown & optimizer rule to apply…
adriangb Apr 3, 2025
a5f998c
wip
adriangb Apr 3, 2025
7e2db66
fix tests
adriangb Apr 3, 2025
cb1f830
fix
adriangb Apr 3, 2025
e92d8b5
fix
adriangb Apr 3, 2025
ca391c1
fix doc
adriangb Apr 3, 2025
c78a590
fix doc
adriangb Apr 3, 2025
34c8285
Improve doc comments of `filter-pushdown-apis` (#22)
alamb Apr 5, 2025
e15374f
Apply suggestions from code review
adriangb Apr 5, 2025
2ceec35
simplify according to pr feedback
adriangb Apr 5, 2025
3fbf379
Add missing file
adriangb Apr 5, 2025
e6721d1
Add tests
adriangb Apr 5, 2025
b7b588b
pipe config in
adriangb Apr 5, 2025
d1f01dd
docstrings
adriangb Apr 5, 2025
5929d03
Update datafusion/physical-plan/src/filter_pushdown.rs
adriangb Apr 5, 2025
24483bc
fix
adriangb Apr 5, 2025
d0295ed
fix
adriangb Apr 6, 2025
2d46289
fmt
adriangb Apr 6, 2025
4318267
fix doc
adriangb Apr 6, 2025
7d29056
add example usage of config
adriangb Apr 6, 2025
d382bd3
fix test
adriangb Apr 6, 2025
2dfa8b8
convert exec API and optimizer rule
berkaysynnada Apr 14, 2025
cda6e8d
re-add docs
adriangb Apr 14, 2025
e4d8a8c
dbg
berkaysynnada Apr 16, 2025
3ec1b2a
dbg 2
berkaysynnada Apr 16, 2025
a2df5e0
avoid clones
adriangb Apr 16, 2025
6938d52
part 3
berkaysynnada Apr 16, 2025
6836dd4
fix lint
adriangb Apr 16, 2025
28bb8ea
Merge branch 'filter-pushdown-apis' into filter-pushdown-apis
berkaysynnada Apr 16, 2025
7e95283
tests pass
berkaysynnada Apr 16, 2025
e2f8c12
Update filter.rs
berkaysynnada Apr 16, 2025
bff47be
update projection tests
berkaysynnada Apr 16, 2025
ce49ad4
update slt files
adriangb Apr 16, 2025
d5792bc
Merge branch 'main' into filter-pushdown-apis
adriangb Apr 16, 2025
834f33e
fix
adriangb Apr 16, 2025
9e59246
fix references
adriangb Apr 16, 2025
57a1230
improve impls and update tests
berkaysynnada Apr 17, 2025
367377f
apply stop logic
berkaysynnada Apr 17, 2025
616165d
update slt's
berkaysynnada Apr 17, 2025
b30953f
update other tests
berkaysynnada Apr 17, 2025
ec54cca
minor
berkaysynnada Apr 17, 2025
6345315
rename modules to match logical optimizer, tweak docs
adriangb Apr 17, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
322 changes: 322 additions & 0 deletions datafusion/core/tests/physical_optimizer/filter_pushdown.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,322 @@
// 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 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::internal_err;
use datafusion_common::{config::ConfigOptions, Statistics};
use datafusion_datasource::file_scan_config::FileScanConfigBuilder;
use datafusion_datasource::source::DataSourceExec;
use datafusion_datasource::{
file::{FileSource, FileSourceFilterPushdownResult},
file_scan_config::FileScanConfig,
file_stream::FileOpener,
};
use datafusion_physical_expr::{conjunction, PhysicalExprRef};
use datafusion_physical_expr_common::physical_expr::fmt_sql;
use datafusion_physical_optimizer::filter_pushdown::PushdownFilter;
use datafusion_physical_optimizer::PhysicalOptimizerRule;
use datafusion_physical_plan::filter::FilterExec;
use datafusion_physical_plan::{
displayable, execution_plan::FilterSupport, metrics::ExecutionPlanMetricsSet,
DisplayFormatType, ExecutionPlan,
};
use object_store::ObjectStore;
use std::sync::{Arc, OnceLock};
use std::{
any::Any,
fmt::{Display, Formatter},
};

/// A placeholder data source that accepts filter pushdown
#[derive(Clone)]
struct TestSource {
support: FilterSupport,
predicate: Option<PhysicalExprRef>,
statistics: Option<Statistics>,
}

impl TestSource {
fn new(support: FilterSupport) -> 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) -> datafusion_common::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 predicate_string = self
.predicate
.as_ref()
.map(|p| format!(", predicate={p}"))
.unwrap_or_default();

write!(f, "{}", predicate_string)
}
DisplayFormatType::TreeRender => {
if let Some(predicate) = &self.predicate {
writeln!(f, "predicate={}", fmt_sql(predicate.as_ref()))?;
}
Ok(())
}
}
}

fn push_down_filters(
&self,
filters: &[PhysicalExprRef],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for convenience and consistency, can we rename this as parent_filters as well?

) -> datafusion_common::Result<Option<FileSourceFilterPushdownResult>> {
let new = Arc::new(TestSource {
support: self.support,
predicate: Some(conjunction(filters.iter().map(Arc::clone))),
statistics: self.statistics.clone(),
});
Ok(Some(FileSourceFilterPushdownResult::new(
new,
vec![self.support; filters.len()],
)))
}
}

fn test_scan(support: FilterSupport) -> 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(FilterSupport::HandledExact);
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, PushdownFilter{}),
@r"
OptimizationTest:
input:
- FilterExec: a@0 = foo
- DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test
output:
Ok:
- DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, predicate=a@0 = foo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe display "exactness" as well to be prettier

"
);
}

#[test]
fn test_parquet_pushdown() {
// filter should be pushed down into the parquet scan with two filters
let scan = test_scan(FilterSupport::HandledExact);
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, PushdownFilter{}),
@r"
OptimizationTest:
input:
- FilterExec: b@1 = bar
- FilterExec: a@0 = foo
- DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test
output:
Ok:
- DataSourceExec: file_groups={0 groups: []}, projection=[a, b, c], file_type=test, predicate=a@0 = foo AND b@1 = bar
"
);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need some more tests here (I can help write the but I about out of time this morning)

  1. Tests with actual ParquetSource (to ensure everything works when hooked up correctly)
  2. Tests for CoealesceBatches and ProjectionExec (basically using the great examples on the comments of the rule)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a great question.

The reason I specifically didn't implement ParquetSource is because that has a bunch of knock-on effects related to the interaction with the existing filter pushdown mechanisms in ParquetSource + ListingTable that I think should be dealt with in their own PR.

My plan was to avoid bloating this PR and limit the blast radius by only implementing FilterExec as an example.
So the testing plan for this PR becomes (1) it doesn't break any other tests / the rest of the system despite being on by default and (2) these minimal tests show that the POC works.
Then as we add more implementations we can enrich these tests.

One idea is that we could add mock implementations for joins, projections, repartitions, etc. like I did for a DataSource and use those in tests.

Another proposal could be merging this but feature flagging the whole thing until we have a rich enough implementation for it to (1) be useful and (2) have extensive real world e2e tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added tests for the filter + projection case, aggregations (not yet supported) and CoalesceBatchesExec + RepartitionExec.

I still didn't implement any other cases, including ParquetSource, for the reasons above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this makes sense

/// 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) -> Self
where
O: PhysicalOptimizerRule,
{
Self::new_with_config(input_plan, opt, &ConfigOptions::default())
}

pub fn new_with_config<O>(
input_plan: Arc<dyn ExecutionPlan>,
opt: O,
config: &ConfigOptions,
) -> Self
where
O: PhysicalOptimizerRule,
{
let input = format_execution_plan(&input_plan);

let input_schema = input_plan.schema();

let output_result = opt.optimize(input_plan, 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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This output format is super clear and legible!

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()
}
1 change: 1 addition & 0 deletions datafusion/core/tests/physical_optimizer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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;
Expand Down
12 changes: 11 additions & 1 deletion datafusion/datasource/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use crate::file_scan_config::FileScanConfig;
use crate::file_stream::FileOpener;
use arrow::datatypes::SchemaRef;
use datafusion_common::Statistics;
use datafusion_physical_expr::LexOrdering;
use datafusion_physical_expr::{LexOrdering, PhysicalExprRef};
use datafusion_physical_plan::execution_plan::FilterPushdownResult;
use datafusion_physical_plan::metrics::ExecutionPlanMetricsSet;
use datafusion_physical_plan::DisplayFormatType;

Expand Down Expand Up @@ -93,4 +94,13 @@ pub trait FileSource: Send + Sync {
}
Ok(None)
}

fn push_down_filters(
&self,
_filters: &[PhysicalExprRef],
) -> datafusion_common::Result<Option<FileSourceFilterPushdownResult>> {
Ok(None)
}
}

pub type FileSourceFilterPushdownResult = FilterPushdownResult<Arc<dyn FileSource>>;
Loading