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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]