Skip to content

Apache DataFusion SQL Query Engine

fix: unparse columns of stacked pushdown projections unqualified (#23176)

Phoenix3496b9e9a1cf
diff --git a/datafusion/core/tests/sql/unparser.rs b/datafusion/core/tests/sql/unparser.rs
index 7762512fac..4597b7e640 100644
--- a/datafusion/core/tests/sql/unparser.rs
+++ b/datafusion/core/tests/sql/unparser.rs
@@ -343,6 +343,62 @@ async fn optimized_duckdb_unparse_preserves_derived_table_scope() -> Result<()>
Ok(())
}
+// https://github.com/apache/datafusion/issues/23138
+//
+// CSE on `coalesce(discount_pct, 0)` factors a shared CAST into an extra inner
+// projection, so `SubqueryAlias: o` ends up over two stacked projections. When
+// the unparser renders that as nested derived tables it must qualify the
+// pass-through `order_id` with a name in scope at each level -- it must not
+// rebase it to the outer subquery alias `o`, which is not visible inside the
+// inner derived table.
+const ISSUE_23138_QUERY: &str = r#"
+SELECT * FROM
+(
+ SELECT order_id FROM "warehouse"."main"."order_items"
+) oi
+JOIN (
+ SELECT order_id, coalesce(discount_pct, 0) AS discount_pct_2
+ FROM "warehouse"."main"."orders"
+) o USING (order_id)
+"#;
+
+#[tokio::test]
+async fn optimized_duckdb_unparse_qualifies_nested_passthrough_column() -> Result<()> {
+ let ctx = issue_22961_context()?;
+ let plan = ctx.sql(ISSUE_23138_QUERY).await?.into_optimized_plan()?;
+ let dialect = DuckDBDialect::new();
+ let unparser = Unparser::new(&dialect);
+ let sql = unparser.plan_to_sql(&plan)?.to_string();
+
+ // The intermediate derived table has no `o` in scope, so the pass-through
+ // `order_id` must be unqualified there, not rebased to the subquery alias
+ // `o` (which is only the base-table alias one level deeper). The bug emitted
+ // `"o"."order_id"` inside that derived table; the fix emits a bare column.
+ let expected = concat!(
+ r#"SELECT "o"."order_id", "o"."discount_pct_2" "#,
+ r#"FROM "warehouse"."main"."order_items" AS "oi" "#,
+ r#"INNER JOIN (SELECT "order_id", "#,
+ r#"CASE WHEN "__common_expr_1" IS NOT NULL "#,
+ r#"THEN "__common_expr_1" ELSE 0.00 END AS "discount_pct_2" "#,
+ r#"FROM (SELECT CAST("o"."discount_pct" AS DECIMAL(22,2)) "#,
+ r#"AS "__common_expr_1", "o"."order_id" "#,
+ r#"FROM "warehouse"."main"."orders" AS "o")) AS "o" "#,
+ r#"ON "oi"."order_id" = "o"."order_id""#,
+ );
+ assert_eq!(sql, expected);
+
+ assert!(
+ sql.contains(r#"(SELECT "order_id", CASE WHEN"#),
+ "pass-through order_id should be unqualified in derived table: {sql}"
+ );
+ assert!(
+ !sql.contains(r#"(SELECT "o"."order_id", CASE WHEN"#),
+ "derived table must not reference out-of-scope alias o: {sql}"
+ );
+
+ Ok(())
+}
+
/// The outcome of running a single roundtrip test.
///
/// A successful test produces [`TestCaseResult::Success`].
diff --git a/datafusion/sql/src/unparser/plan.rs b/datafusion/sql/src/unparser/plan.rs
index 5e4a353f8f..b538b31a76 100644
--- a/datafusion/sql/src/unparser/plan.rs
+++ b/datafusion/sql/src/unparser/plan.rs
@@ -1992,6 +1992,21 @@ impl Unparser<'_> {
Ok(Some(relation))
}
+ /// Strip the table qualifier from every column in a pushdown pass-through
+ /// projection expression, so it resolves against the unnamed derived table
+ /// rendered for the inner pushdown projection rather than a deeper table
+ /// alias that is out of scope at this nesting level.
+ fn strip_pushdown_column_qualifiers(expr: Expr) -> Result<Expr> {
+ expr.transform(|e| match e {
+ Expr::Column(mut column) => {
+ column.relation = None;
+ Ok(Transformed::yes(Expr::Column(column)))
+ }
+ other => Ok(Transformed::no(other)),
+ })
+ .data()
+ }
+
/// Try to unparse a table scan with pushdown operations into a new subquery plan.
/// If the table scan is without any pushdown operations, return None.
fn unparse_table_scan_pushdown(
@@ -2121,6 +2136,33 @@ impl Unparser<'_> {
alias.clone(),
already_projected,
)? {
+ // The pushed-down scan alias is only in scope for the
+ // projection directly above the aliased table scan. `plan`
+ // is the result of pushing the alias further down: if it is
+ // itself a `Projection`, the input was another projection
+ // (e.g. common subexpression elimination stacked one), so
+ // this projection sits over a derived table rather than
+ // directly over the aliased scan, and the alias is out of
+ // scope here. Its qualified pass-through columns must then
+ // reference the derived table's output unqualified instead
+ // of being rebased to the alias. Build it directly so the
+ // unqualified columns are not re-normalized back to the
+ // alias. (Otherwise `plan` is the scan-derived plan and we
+ // fall through to rebase to the alias, correct one level
+ // above the scan.)
+ if alias.is_some() && matches!(plan, LogicalPlan::Projection(_)) {
+ let exprs = projection
+ .expr
+ .iter()
+ .cloned()
+ .map(Self::strip_pushdown_column_qualifiers)
+ .collect::<Result<Vec<_>>>()?;
+ return Ok(Some(LogicalPlan::Projection(Projection::try_new(
+ exprs,
+ Arc::new(plan),
+ )?)));
+ }
+
let exprs = if alias.is_some() {
let mut alias_rewriter =
alias.as_ref().map(|alias_name| TableAliasRewriter {