mihaibudiu commented on PR #5385:
URL: https://github.com/apache/calcite/pull/5385#issuecomment-6869109550

   The agent also found the following counterexample:
   ```
   select count(*) from (values 1) as t(k),
     lateral (select a.x from <1000 rows> where a.x + t.k > 0) as s
   where rand() < 0.5
   ```
   
   The plan moves the filter into the left input. `RAND()` then runs once per 
outer row, not once per joined row, so the query keeps all 1,000 rows or none:
   
   ```
   EnumerableCorrelate(correlation=[$cor0], joinType=[inner], ...)
     EnumerableCalc(expr#1=[RAND()], ..., $condition=[$t3])   -- the filter, 
now below the Correlate
       EnumerableValues(tuples=[[{ 1 }]])
   ```
   
   The cause is `FilterCorrelateRule`, which the decorrelator runs before it 
decorrelates. The rule only refuses conjuncts that contain a correlation 
variable. It does not refuse non-deterministic ones, as FilterJoinRule does.
   
   Here the PR refuses to decorrelate, because the copied left input now 
contains RAND(), and keeps the Correlate. But decorrelate() returns the plan 
that the early rules already changed. So "keep the Correlate" does not mean 
"keep the original plan".
   
   Suggested changes for the non-deterministic part:
   
   Fix FilterCorrelateRule: skip non-deterministic conjuncts, as FilterJoinRule 
does. Add T1 as a test. A RAND() count is random, so the test should check the 
plan: the RAND() filter must stay above the Correlate.
   
   Optional: the PR keeps the whole Correlate when any part of the inner input 
is non-deterministic. In T4, the RAND() projection could run after the join 
back, and the rest could still be decorrelated. This is an optimization, not a 
correctness issue.
   FilterProjectTransposeRule also has no determinism check, and the 
decorrelator runs it too. It is also in Calcite's default Volcano rule set, so 
that is a general rule bug, not part of this PR.
   


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