xuzifu666 commented on code in PR #5040:
URL: https://github.com/apache/calcite/pull/5040#discussion_r3464468086


##########
core/src/test/resources/sql/sub-query.iq:
##########
@@ -2540,11 +2541,12 @@ EnumerableCalc(expr#0..5=[{inputs}], expr#6=[NOT($t3)], 
expr#7=[IS NOT NULL($t3)
     EnumerableSort(sort0=[$2], dir0=[ASC])
       EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], 
DEPTNO=[$t7])
         EnumerableTableScan(table=[[scott, EMP]])
-    EnumerableSort(sort0=[$1], dir0=[ASC])
-      EnumerableCalc(expr#0..1=[{inputs}], expr#2=[false], expr#3=[1], 
expr#4=[<=($t1, $t3)], cs=[$t2], DEPTNO=[$t0], rn=[$t1], $condition=[$t4])
-        EnumerableWindow(window#0=[window(partition {0} rows between UNBOUNDED 
PRECEDING and CURRENT ROW aggs [ROW_NUMBER()])], constants=[[false]])
-          EnumerableCalc(expr#0..2=[{inputs}], DEPTNO=[$t0])
-            EnumerableTableScan(table=[[scott, DEPT]])
+    EnumerableCalc(expr#0..2=[{inputs}], expr#3=[1], expr#4=[<=($t2, $t3)], 
proj#0..2=[{exprs}], $condition=[$t4])

Review Comment:
   This fix altered the sorting metadata derivation for the window operator, 
resulting in a legitimate change to the execution plan structure of two 
existing !plan tests, but the computation results remain unchanged.
   
   Specific changes: Previously, the collation derivation for 
`EnumerableWindow` was optimistic, claiming to retain the input sorting. With 
`CalcRelSplitter` inheriting the original `Calc`'s collation, the optimizer 
would push `Sort` down onto the `Window` and merge it with the outer `Calc`. 
Therefore, the original plan was:
   ```
   EnumerableSort
     EnumerableCalc(condition + window output)
         EnumerableWindow
   ```
   
   After the fix, `EnumerableWindow` no longer claims any sorting, and 
`CalcRelSplitter` no longer inherits the contaminated collation. `Sort` can 
only stop at the `Window`, wrapped in an outer `Calc` for projection. The plan 
becomes:
   ```
   EnumerableCalc(projection)
     EnumerableSort
       EnumerableCalc(condition)
         EnumerableWindow
   ```
   
   Both are logically equivalent; only the relative positions of `Sort` and the 
projected `Calc` have changed. Therefore, only the expected output of these two 
!plan blocks was updated, without modifying any result data.This also ensures 
complete consistency with the PostgreSQL sorting results.



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

Reply via email to