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

   ## Which issue does this PR close?
   
   - Closes #26620.
   
   ## Rationale for this change
   
   When two window expressions share a common `ORDER BY` sort key such as `a + 
1`, and one of the window expressions is wrapped in an outer `Expr::Alias` (for 
example after `simplify_expressions` folds `1 + 1` and preserves the original 
display name) while the sibling window expression has no outer `Expr::Alias`:
   
   ```sql
   SELECT sum(1 + 1) OVER (ORDER BY a + 1), sum(b) OVER (ORDER BY a + 1) FROM t;
   ```
   
   `ExprCSEController` previously only incremented `alias_counter` when 
descending into `Expr::Alias`. As a result, the extracted sort key under the 
unaliased window expression received a name-preserving alias (`__common_expr_1 
AS t.a + Int64(1)`), while the extracted sort key under the aliased sibling 
received bare `__common_expr_1`. That caused the two window expressions in the 
same `WindowAggr` group to have unequal sort keys, triggering the debug 
assertion `all window expressions shall have the same sort keys` in 
`DefaultPhysicalPlanner`.
   
   Because `try_optimize_window` already wraps each top-level window expression 
with `NamePreserver`, child expressions inside `Expr::WindowFunction` never 
need name-preserving aliases.
   
   ## What changes are included in this PR?
   
   - Treat `Expr::WindowFunction` alongside `Expr::Alias` in 
`ExprCSEController::rewrite_f_down` and `rewrite_f_up` so common subexpressions 
extracted inside window expressions always emit bare `__common_expr_*` column 
references.
   - Add unit and sqllogictest coverage for window functions sharing extracted 
`ORDER BY` and `PARTITION BY` expressions.
   
   ## Are these changes tested?
   
   Yes, covered by `common_subexpr_eliminate` unit tests in 
`datafusion-optimizer` and `window.slt` in `datafusion-sqllogictest`.
   
   ## Are there any user-facing changes?
   
   No, other than fixing the debug assertion panic on queries with multiple 
window functions sharing extracted sort keys.
   


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