Skip to content

Apache DataFusion SQL Query Engine

refactor: centralize join-input table-scan filter extraction before u… (#23166)

Phoenix8511e18431a3
diff --git a/datafusion/sql/src/unparser/plan.rs b/datafusion/sql/src/unparser/plan.rs
index 37ff8145d3..5e4a353f8f 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -584,6 +584,19 @@ impl Unparser<'_> {
self.project_window_output(&window_expr, select, None)
}
+ fn extract_join_input_table_scan_filters(
+ plan: &Arc<LogicalPlan>,
+ table_scan_filters: &mut Vec<Expr>,
+ ) -> Result<Arc<LogicalPlan>> {
+ match try_transform_to_simple_table_scan_with_filters(plan)? {
+ Some((plan, filters)) => {
+ table_scan_filters.extend(filters);
+ Ok(Arc::new(plan))
+ }
+ None => Ok(Arc::clone(plan)),
+ }
+ }
+
#[cfg_attr(feature = "recursive_protection", recursive::recursive)]
fn select_to_sql_recursively(
&self,
@@ -1153,14 +1166,10 @@ impl Unparser<'_> {
// The outer projection plan will handle projecting the correct columns.
let already_projected = select.already_projected();
- let left_plan =
- match try_transform_to_simple_table_scan_with_filters(left_plan)? {
- Some((plan, filters)) => {
- table_scan_filters.extend(filters);
- Arc::new(plan)
- }
- None => Arc::clone(left_plan),
- };
+ let left_plan = Self::extract_join_input_table_scan_filters(
+ left_plan,
+ &mut table_scan_filters,
+ )?;
let left_plan = if already_projected {
Self::unwrap_qualified_passthrough_join_projection(left_plan)
} else {
@@ -1181,14 +1190,10 @@ impl Unparser<'_> {
None
};
- let right_plan =
- match try_transform_to_simple_table_scan_with_filters(right_plan)? {
- Some((plan, filters)) => {
- table_scan_filters.extend(filters);
- Arc::new(plan)
- }
- None => Arc::clone(right_plan),
- };
+ let right_plan = Self::extract_join_input_table_scan_filters(
+ right_plan,
+ &mut table_scan_filters,
+ )?;
let mut right_relation = RelationBuilder::default();
if already_projected
diff --git a/datafusion/sql/tests/cases/plan_to_sql.rs b/datafusion/sql/tests/cases/plan_to_sql.rs
index 937ed9894f..2194085e65 100644
--- a/datafusion/sql/tests/cases/plan_to_sql.rs
+++ b/datafusion/sql/tests/cases/plan_to_sql.rs
@@ -2736,9 +2736,9 @@ fn test_unparse_inner_join_with_table_scan_projection() -> Result<()> {
Ok(())
}
-#[test]
-fn test_unparse_projected_join_unwraps_right_nested_passthrough_projection() -> Result<()>
-{
+/// Build the three base table scans (`left_table`, `mid_table`, `right_table`)
+/// shared by the nested passthrough-projection join unparsing tests.
+fn nested_passthrough_join_tables() -> Result<(LogicalPlan, LogicalPlan, LogicalPlan)> {
let left_schema = Schema::new(vec![
Field::new("left_id", DataType::Int32, false),
Field::new("mid_id", DataType::Int32, false),
@@ -2755,6 +2755,13 @@ fn test_unparse_projected_join_unwraps_right_nested_passthrough_projection() ->
let left = table_scan(Some("left_table"), &left_schema, None)?.build()?;
let mid = table_scan(Some("mid_table"), &mid_schema, None)?.build()?;
let right = table_scan(Some("right_table"), &right_schema, None)?.build()?;
+ Ok((left, mid, right))
+}
+
+#[test]
+fn test_unparse_projected_join_unwraps_right_nested_passthrough_projection() -> Result<()>
+{
+ let (left, mid, right) = nested_passthrough_join_tables()?;
let nested_right = LogicalPlanBuilder::from(mid)
.join(
@@ -2793,6 +2800,52 @@ fn test_unparse_projected_join_unwraps_right_nested_passthrough_projection() ->
Ok(())
}
+#[test]
+fn test_unparse_projected_join_unwraps_left_nested_passthrough_projection() -> Result<()>
+{
+ let (left, mid, right) = nested_passthrough_join_tables()?;
+
+ // Left join input is a qualified passthrough `Projection(Join)`, and the
+ // outer join condition (`mid_table.right_id`) references an alias from
+ // inside it. The unparser must not wrap this in a derived table that would
+ // hide `mid_table.right_id` from the outer condition.
+ let nested_left = LogicalPlanBuilder::from(left)
+ .join(
+ mid,
+ datafusion_expr::JoinType::Inner,
+ (vec!["left_table.mid_id"], vec!["mid_table.mid_id"]),
+ None,
+ )?
+ .project(vec![
+ col("left_table.left_id"),
+ col("mid_table.mid_id"),
+ col("mid_table.right_id"),
+ ])?
+ .build()?;
+
+ let plan = LogicalPlanBuilder::from(nested_left)
+ .join(
+ right,
+ datafusion_expr::JoinType::Inner,
+ (vec!["mid_table.right_id"], vec!["right_table.right_id"]),
+ None,
+ )?
+ .project(vec![
+ col("left_table.left_id"),
+ col("mid_table.mid_id"),
+ col("right_table.value"),
+ ])?
+ .build()?;
+
+ let sql = plan_to_sql(&plan)?;
+ assert_snapshot!(
+ sql,
+ @r#"SELECT left_table.left_id, mid_table.mid_id, right_table."value" FROM left_table INNER JOIN mid_table ON left_table.mid_id = mid_table.mid_id INNER JOIN right_table ON mid_table.right_id = right_table.right_id"#
+ );
+
+ Ok(())
+}
+
#[test]
fn test_unparse_left_semi_join_with_table_scan_projection() -> Result<()> {
let schema = Schema::new(vec![