xiedeyantu commented on PR #4897:
URL: https://github.com/apache/calcite/pull/4897#issuecomment-4613884911

   Current PR Change Management Process:
   1. `MinusMergeRule`
   Merges the nested binary `Minus` operators into a single multi-input 
`LogicalMinus`:
   ```text
   LogicalMinus(all=[false])
     t1
     t2
     t3
   ```
   2. `MinusToAntiJoinRule`
   Rewrites the multi-input `Minus` into a chain of anti joins with a top-level 
distinct aggregate:
   ```text
   LogicalAggregate(group=[{0}])
     LogicalJoin(joinType=[anti])
       LogicalJoin(joinType=[anti])
         t1
         t2
       t3
   ```
   3. `JoinPushExpressionsRule`
   Extracts the `CAST` expressions from the join condition into projection 
columns (`A0`), allowing the join to use direct column references:
   text
   LogicalProject(A=[$0])
     LogicalJoin(condition=[=($1, $3)], joinType=[anti])
       LogicalProject(A=[$0], A0=[CAST($0)])
       LogicalProject(A=[$0], A0=[CAST($0)])
   ```
   4. `EnumerableJoinRule`
   Converts the logical anti joins into physical join implementations. The 
inner anti join becomes a nested-loop join, while the outer anti join is 
implemented as a hash join:
   ```text
   EnumerableHashJoin(joinType=[anti])
     EnumerableProject(A=[$0], A0=[CAST($0)])
       EnumerableNestedLoopJoin(joinType=[anti])
         ...
   ```
   5. `EnumerableAggregateRule`
   Converts the top-level logical aggregate into a physical enumerable 
aggregate:
   ```text
   EnumerableAggregate(group=[{0}])
     EnumerableHashJoin(joinType=[anti])
       ...
   ```
   The final selected plan is:
   ```text
   EnumerableAggregate
     EnumerableHashJoin (anti)
       EnumerableProject(A0=CAST(...))
         EnumerableNestedLoopJoin (anti)
           ...
   ```
   
   The original plan took this specific form because, within Volcano, every 
rule generates a corresponding candidate RelNode. A particular scenario arises 
with nested Minus operations: for each level of nesting, an equivalent 
Aggregation-plus-Join structure is generated. Subsequently, during the final 
cost-based selection phase, this specific structure was deemed to have the 
lowest cost and was therefore selected. Please refer to the plan diagram below.
   
   <img width="11864" height="4105" alt="graphviz" 
src="https://github.com/user-attachments/assets/525d306a-9569-4e78-aece-28e1889fd201";
 />
   
   This is my current understanding; please take another look to see if it is 
clearly explained. @silundong @mihaibudiu 


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