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

   ## Which issue does this PR close?
   
   - Closes #26622.
   
   ## Rationale for this change
   
   A quantified comparison such as `x > ALL (SELECT y FROM s)` fails to plan 
when it appears unaliased in the SELECT list, with `Optimizer rule 
'decorrelate_predicate_subquery' failed ... Failed due to a difference in 
schemas`. The same query works when the expression is aliased or used in a 
`WHERE` clause.
   
   `RewriteSetComparison` replaces the `SetComparison` with a `CASE WHEN EXISTS 
... END` expression. In a projection this changes the output column name from 
`t.x > ALL` to the `CASE ...` display name, so the plan's output schema changes 
and the optimizer's schema invariant check fails.
   
   ## What changes are included in this PR?
   
   - `RewriteSetComparison::rewrite_plan` now uses `NamePreserver` (as 
`ApplyFunctionRewrites` and `TypeCoercion` already do) to keep the original 
expression name when a `SetComparison` is rewritten.
   
   The aggregate variant in the issue (`bool_or(x > ALL (...))`) hits a 
separate limitation (`Physical plan does not support logical expression 
Exists`) and is not addressed here.
   
   ## What is the testing strategy for this PR?
   
   Added sqllogictest cases to `subquery.slt` covering unaliased `> ALL`, `= 
ANY`, and `< ANY` in the SELECT list, plus the aliased form. Without the fix, 
the three unaliased queries fail with the schema error above. With it, all of 
them return the expected results (matching DuckDB for the query in the issue). 
The full sqllogictest suite and `datafusion-optimizer` unit tests pass, and 
`cargo fmt` and `cargo clippy -D warnings` are clean.
   
   ## Are there any user-facing changes?
   
   Queries with an unaliased `ANY`/`ALL` subquery comparison in the SELECT list 
now run instead of failing. The output column keeps its original name (for 
example `t.x > ALL`). No API changes.
   
   ---
   
   This fix was developed with AI assistance (Claude Code) and reviewed by me 
before submission.
   


-- 
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