You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Phoenix500526 (via GitHub)" <gi...@apache.org> on 2026/06/25 03:56:56 UTC
[PR] fix: unparse columns of stacked pushdown projections unqualified [datafusion]
Phoenix500526 opened a new pull request, #23176:
URL: https://github.com/apache/datafusion/pull/23176
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
- Closes #23138 .
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
For an optimized plan, common subexpression elimination can leave a
`SubqueryAlias` over two stacked `Projection`s (it factors a shared expression
into an extra inner projection). The plan is correct; the unparser just cannot
render it accurately.
When unparsing such a subquery, `unparse_table_scan_pushdown` pushes the
subquery alias down to the aliased table scan and rebases each projection's
columns to that alias. That is correct for the projection directly above the
scan, but an *outer* stacked projection sits over a derived table where the
alias is no longer in scope, so rebasing its qualified pass-through columns
emits references the surrounding query cannot resolve.
For the reproducer in the issue the generated SQL is (note the middle
`"o"."order_id"`):
```sql
... INNER JOIN (
SELECT "o"."order_id", CASE WHEN "__common_expr_1" ... END AS "discount_pct_2"
FROM (SELECT CAST("o"."discount_pct" ...) AS "__common_expr_1", "o"."order_id"
FROM "warehouse"."main"."orders" AS "o")
) AS "o" ON ...
```
DuckDB rejects it with Binder Error: Referenced table "o" not found!, because
"o" is only the base-table alias one level deeper, not visible in the
intermediate derived table.
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
In unparse_table_scan_pushdown's Projection arm, detect the stacked case:
if the recursive pushdown result is itself a Projection, this projection
sits over a derived table rather than directly over the aliased scan.
In that case, strip the table qualifier from the projection's pass-through
columns so they reference the derived table's output unqualified, instead of
being rebased to the (out-of-scope) scan alias. The projection is built
directly (Projection::try_new) so the unqualified columns are not
re-normalized back to the alias.
The projection directly above the scan is unchanged and still rebases to the
alias.
After the fix the middle column is unqualified and the SQL is valid:
```sql
... INNER JOIN (
SELECT "order_id", CASE WHEN "__common_expr_1" ... END AS "discount_pct_2"
FROM (SELECT CAST("o"."discount_pct" ...) AS "__common_expr_1", "o"."order_id"
FROM "warehouse"."main"."orders" AS "o")
) AS "o" ON ...
```
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
New test `optimized_duckdb_unparse_qualifies_nested_passthrough_column` in
`datafusion/core/tests/sql/unparser.rs`, asserting the intermediate derived
table no longer carries the out-of-scope alias.
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
No public API changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: unparse columns of stacked pushdown projections unqualified [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #23176:
URL: https://github.com/apache/datafusion/pull/23176#discussion_r3508890070
##########
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
Review Comment:
I recommend we simply compare to the expected unparsed query -- would make it easier to understand what was actually being done / what was different
While the curernt checks for substrings might seem less brittle I think it will be very hard to understand if/how they should be updated in the future if the code changes
##########
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
Review Comment:
You can add extra asserts for important properties (like order_id should be unqualified)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: unparse columns of stacked pushdown projections unqualified [datafusion]
Posted by "Phoenix500526 (via GitHub)" <gi...@apache.org>.
Phoenix500526 commented on code in PR #23176:
URL: https://github.com/apache/datafusion/pull/23176#discussion_r3492547507
##########
datafusion/core/tests/sql/unparser.rs:
##########
@@ -343,6 +343,43 @@ 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.
+ assert!(sql.contains(r#"(SELECT "order_id", CASE WHEN"#));
Review Comment:
Done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: unparse columns of stacked pushdown projections unqualified [datafusion]
Posted by "kosiew (via GitHub)" <gi...@apache.org>.
kosiew commented on code in PR #23176:
URL: https://github.com/apache/datafusion/pull/23176#discussion_r3490324247
##########
datafusion/core/tests/sql/unparser.rs:
##########
@@ -343,6 +343,43 @@ 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.
+ assert!(sql.contains(r#"(SELECT "order_id", CASE WHEN"#));
Review Comment:
Nice regression test. One thought: would it make sense to make the positive assertion a little less tied to the exact emitted `CASE WHEN` formatting? For example, you could isolate the intermediate derived table fragment and check that `order_id` is unqualified there. That would still cover the alias scope bug while making the test a bit more resilient to unrelated formatting changes.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] fix: unparse columns of stacked pushdown projections unqualified [datafusion]
Posted by "Phoenix500526 (via GitHub)" <gi...@apache.org>.
Phoenix500526 commented on code in PR #23176:
URL: https://github.com/apache/datafusion/pull/23176#discussion_r3510122986
##########
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
Review Comment:
Done in commit 9693f66
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org