tlfua opened a new pull request, #27783:
URL: https://github.com/apache/datafusion/pull/27783

   > [!IMPORTANT]
   >
   > **Fork CI setup:** For pull requests from a fork to `main`, enable 
workflows in your fork's **Actions** tab before you push.
   > See the [fork CI setup 
guide](https://datafusion.apache.org/contributor-guide/#pull-requests-from-forks).
   
   ## 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 #27691.
   
   ## 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.
   
   Please explain the problem you are trying to solve in terms of the 
user-visible
   behavior, rather than the implementation.
   
   For example, "The code in `foo.rs` doesn't handle nulls" is a symptom of the
   implementation. "COUNT(DISTINCT) returns wrong results when the column 
contains
   nulls" is the user-visible problem.
   -->
   
   A correlated subquery fails to plan with a schema error when its correlated 
filter sits inside a derived table that renames a column, and that derived 
table is joined with another relation inside the subquery. It affects scalar, 
`EXISTS`, `NOT EXISTS` and `IN` subqueries:
   
   ```sql
   CREATE TABLE t(a INT) AS VALUES (1), (2), (4);
   CREATE TABLE i(a INT) AS VALUES (1), (2), (2), (3);
   CREATE TABLE u(b INT) AS VALUES (10), (20);
   
   SELECT o.a, (
     SELECT count(*) FROM (SELECT a AS x FROM i WHERE a = o.a) e, u
   ) FROM t o;
   -- main: Optimizer rule 'scalar_subquery_to_join' failed
   --       Schema error: No field named e.a. Did you mean 'e.x'?
   -- this PR: (1, 2), (2, 4), (4, 0)
   ```
   
   When `PullUpCorrelatedExpr` moves the correlated filter up, the `Projection` 
and the `SubqueryAlias` below the join add the column the filter reads (`e.a`). 
A `Join` falls into the `_` arm of `PullUpCorrelatedExpr::pull_up` and keeps 
the schema it was built with. The node above it then looks up `e.a` in the join 
schema and fails.
   
   `f_down` already lets a correlated filter move above a `Join` or an 
`AsOfJoin` (when the join preserves the rows of the correlated side). Only the 
schema update on the way up is missing, the same as for `Unnest` before #25838.
   
   ## 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.
   -->
   
   - `PullUpCorrelatedExpr::pull_up` passes the pulled up columns through a 
`Join` and an `AsOfJoin` with `pass_through_pulled_up_cols`, as it already does 
for `Window` and `Unnest`. This recomputes the join schema. It also keeps the 
subquery correlated when a join input carries count bug handling, which only 
holds when nothing runs between the aggregate and the node that reads it.
   
   The query in #27691 uses a non-equality correlated predicate (`e.x < o.a`), 
which is not decorrelated (see `#non_equal_correlated_scalar_subquery` in 
`subquery.slt`). With this PR it now gets the same `This feature is not 
implemented: Physical plan does not support logical expression ScalarSubquery` 
error as the variants without the join or without the rename, instead of the 
schema error. On `main`, those variants also fail with this error, so the query 
cannot return `0` until non-equality correlated scalar subqueries are supported.
   
   Not changed by this PR:
   
   - If the derived table and the other join input both have a column with the 
name of the correlated column, the planner still returns `Unsupported 
correlated subquery: correlated column ... has the same name as an earlier 
column of the subquery output` (#27244).
   - The same query shape in a `LATERAL` subquery still fails with a schema 
error. This is #25837, which also happens without a join.
   
   ## What is the testing strategy for this PR?
   
   <!--
   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
   
   Briefly describe how this PR is tested, and point to the specific tests you 
added. For example: 'This new feature is covered by the `sqllogictest` cases 
added in `foo.slt`'.
   
   If this PR does not add tests, explain why. For example, if the change is 
already covered by existing tests, please mention it.
   
   You should also check the `codecov` bot reply on this PR to confirm the 
changed code is exercised.
   -->
   
   New `sqllogictest` cases at the end of `subquery.slt` (`t27691` tables) 
cover scalar (both join orders), `EXISTS`, `NOT EXISTS` and `IN` subqueries, 
including a row with no match (the count bug result `0`), and the non-equality 
query from the issue. All of them fail on `main` with the schema error.
   
   Commands run locally:
   
   - `cargo test -p datafusion-sqllogictest --test sqllogictests`: passed
   - `cargo test -p datafusion-optimizer --lib --tests`: passed
   - `cargo fmt --all -- --check`: passed
   - `cargo clippy -p datafusion-optimizer --all-targets --all-features -- -D 
warnings`: passed
   
   The full lint suite and the extended tests run in fork CI. I did not run the 
`sql_planner` benchmarks: the change only adds one arm for `Join`/`AsOfJoin` in 
`pull_up`, which recomputes the schema of joins that are on the path of a 
pulled up correlated filter.
   
   ## 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.
   -->
   
   The queries above now return results instead of a schema error. 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]

Reply via email to