Skip to content

disallow pushdown of volatile functions #16861

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 5 commits into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 39 additions & 1 deletion datafusion/core/tests/physical_optimizer/filter_pushdown/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,12 @@ use datafusion::{
};
use datafusion_common::config::ConfigOptions;
use datafusion_execution::object_store::ObjectStoreUrl;
use datafusion_expr::ScalarUDF;
use datafusion_functions::math::random::RandomFunc;
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_physical_expr::{aggregate::AggregateExprBuilder, Partitioning};
use datafusion_physical_expr::{
aggregate::AggregateExprBuilder, Partitioning, ScalarFunctionExpr,
};
use datafusion_physical_expr::{expressions::col, LexOrdering, PhysicalSortExpr};
use datafusion_physical_optimizer::{
filter_pushdown::FilterPushdown, PhysicalOptimizerRule,
Expand Down Expand Up @@ -76,6 +80,40 @@ fn test_pushdown_into_scan() {
);
}

#[test]
fn test_pushdown_volatile_functions_not_allowed() {
// Test that we do not push down filters with volatile functions
// Use random() as an example of a volatile function
let scan = TestScanBuilder::new(schema()).with_support(true).build();
let predicate = Arc::new(BinaryExpr::new(
Arc::new(Column::new_with_schema("a", &schema()).unwrap()),
Operator::Eq,
Arc::new(
ScalarFunctionExpr::try_new(
Arc::new(ScalarUDF::from(RandomFunc::new())),
vec![],
&schema(),
)
.unwrap(),
),
)) as Arc<dyn PhysicalExpr>;
let plan = Arc::new(FilterExec::try_new(predicate, scan).unwrap());
// expect the filter to not be pushed down
insta::assert_snapshot!(
OptimizationTest::new(plan, FilterPushdown::new(), true),
@r"
OptimizationTest:
input:
- FilterExec: a@0 = random()
- DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
output:
Ok:
- FilterExec: a@0 = random()
- DataSourceExec: file_groups={1 group: [[test.parquet]]}, projection=[a, b, c], file_type=test, pushdown_supported=true
",
);
}

/// Show that we can use config options to determine how to do pushdown.
#[test]
fn test_pushdown_into_scan_with_config_options() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use datafusion_datasource::{
file_stream::FileOpener, schema_adapter::DefaultSchemaAdapterFactory,
schema_adapter::SchemaAdapterFactory, source::DataSourceExec, PartitionedFile,
};
use datafusion_physical_expr::conjunction;
use datafusion_physical_expr_common::physical_expr::fmt_sql;
use datafusion_physical_optimizer::PhysicalOptimizerRule;
use datafusion_physical_plan::filter_pushdown::{FilterPushdownPhase, PushedDown};
Expand Down Expand Up @@ -224,7 +223,9 @@ impl FileSource for TestSource {
filters.push(Arc::clone(internal));
}
let new_node = Arc::new(TestSource {
predicate: Some(conjunction(filters.clone())),
predicate: datafusion_physical_expr::utils::conjunction_opt(
filters.clone(),
),
..self.clone()
});
Ok(FilterPushdownPropagation::with_parent_pushdown_result(
Expand Down
Loading