Commit 3496b9e
authored
fix: unparse columns of stacked pushdown projections unqualified (#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.
---------
Signed-off-by: Jiawei Zhao <Phoenix500526@163.com>1 parent 4d898e9 commit 3496b9e
2 files changed
Lines changed: 98 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
343 | 343 | | |
344 | 344 | | |
345 | 345 | | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
346 | 402 | | |
347 | 403 | | |
348 | 404 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1992 | 1992 | | |
1993 | 1993 | | |
1994 | 1994 | | |
| 1995 | + | |
| 1996 | + | |
| 1997 | + | |
| 1998 | + | |
| 1999 | + | |
| 2000 | + | |
| 2001 | + | |
| 2002 | + | |
| 2003 | + | |
| 2004 | + | |
| 2005 | + | |
| 2006 | + | |
| 2007 | + | |
| 2008 | + | |
| 2009 | + | |
1995 | 2010 | | |
1996 | 2011 | | |
1997 | 2012 | | |
| |||
2121 | 2136 | | |
2122 | 2137 | | |
2123 | 2138 | | |
| 2139 | + | |
| 2140 | + | |
| 2141 | + | |
| 2142 | + | |
| 2143 | + | |
| 2144 | + | |
| 2145 | + | |
| 2146 | + | |
| 2147 | + | |
| 2148 | + | |
| 2149 | + | |
| 2150 | + | |
| 2151 | + | |
| 2152 | + | |
| 2153 | + | |
| 2154 | + | |
| 2155 | + | |
| 2156 | + | |
| 2157 | + | |
| 2158 | + | |
| 2159 | + | |
| 2160 | + | |
| 2161 | + | |
| 2162 | + | |
| 2163 | + | |
| 2164 | + | |
| 2165 | + | |
2124 | 2166 | | |
2125 | 2167 | | |
2126 | 2168 | | |
| |||
0 commit comments