Skip to content

Commit e125954

Browse files
committed
Add test for exact one-row subquery promotion
Introduce a focused safe-case test to ensure that a scalar subquery producing exactly one row is correctly promoted into an Inner Join. This change provides balanced coverage alongside existing unsafe-case tests, confirming that the post-join filter behaves as intended in both safe and unsafe scenarios.
1 parent 1d4ed52 commit e125954

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

datafusion/optimizer/src/push_down_filter.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,6 +3727,35 @@ mod tests {
37273727
)
37283728
}
37293729

3730+
#[test]
3731+
fn cross_join_with_exact_one_row_subquery_promotes_post_join_filter() -> Result<()> {
3732+
let left = LogicalPlanBuilder::from(test_table_scan()?)
3733+
.project(vec![col("a"), col("b")])?
3734+
.build()?;
3735+
let right = LogicalPlanBuilder::from(test_table_scan_with_name("test1")?)
3736+
.project(vec![col("a")])?
3737+
.aggregate(Vec::<Expr>::new(), vec![avg(col("a")).alias("avg_a")])?
3738+
.alias("sq")?
3739+
.build()?;
3740+
let plan = LogicalPlanBuilder::from(left)
3741+
.cross_join(right)?
3742+
.filter(col("test.b").gt(col("sq.avg_a")))?
3743+
.build()?;
3744+
3745+
assert_optimized_plan_equal!(
3746+
plan,
3747+
@r"
3748+
Inner Join: Filter: test.b > sq.avg_a
3749+
Projection: test.a, test.b
3750+
TableScan: test
3751+
SubqueryAlias: sq
3752+
Aggregate: groupBy=[[]], aggr=[[avg(test1.a) AS avg_a]]
3753+
Projection: test1.a
3754+
TableScan: test1
3755+
"
3756+
)
3757+
}
3758+
37303759
#[test]
37313760
fn cross_join_with_at_most_one_row_side_keeps_post_join_filter() -> Result<()> {
37323761
let left = LogicalPlanBuilder::from(test_table_scan()?)

0 commit comments

Comments
 (0)