libenchao commented on code in PR #2848:
URL: https://github.com/apache/calcite/pull/2848#discussion_r915379084


##########
core/src/main/java/org/apache/calcite/rel/rules/SemiJoinRule.java:
##########
@@ -42,14 +43,18 @@
 /**
  * Planner rule that creates a {@code SemiJoin} from a
  * {@link org.apache.calcite.rel.core.Join} on top of a
- * {@link org.apache.calcite.rel.logical.LogicalAggregate}.
+ * {@link org.apache.calcite.rel.logical.LogicalAggregate} or
+ * on a {@link org.apache.calcite.rel.RelNode} which is
+ * unique for join's right keys.
  */
 public abstract class SemiJoinRule
     extends RelRule<SemiJoinRule.Config>
     implements TransformationRule {
   private static boolean isJoinTypeSupported(Join join) {
     final JoinRelType type = join.getJoinType();
-    return type == JoinRelType.INNER || type == JoinRelType.LEFT;
+    return type == JoinRelType.INNER
+        || type == JoinRelType.LEFT
+        || type == JoinRelType.SEMI;
   }

Review Comment:
   It's good to know there is already a discussion about this before in 
CALCITE-4623.
   
   Consider this case, we have a plan below:
   ```xml
   LogicalJoin(condition=[=($7, $9)], joinType=[inner])
     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
     LogicalAggregate(group=[{0}], agg#0=[MIN($1)])
       LogicalProject(DEPTNO=[$0], $f0=[true])
         LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
   ```
   After `JoinOnUniqueToSemiJoinRule`, it would be transformed to:
   ```xml
   LogicalJoin(condition=[=($7, $9)], joinType=[semi])
     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
     LogicalAggregate(group=[{0}], agg#0=[MIN($1)])
       LogicalProject(DEPTNO=[$0], $f0=[true])
         LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
   ```
   And we can still use `JoinToSemiJoinRule` to remove the `Aggregate`:
   ```xml
   LogicalJoin(condition=[=($7, $9)], joinType=[semi])
     LogicalTableScan(table=[[CATALOG, SALES, EMP]])
     LogicalProject(DEPTNO=[$0], $f0=[true])
       LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
   ```
   
   In many cases, we would not expect the rules firing orders, especially in 
`HepPlanner`, hence the above transforming order is valid and should be valid 
IMHO.



-- 
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: commits-unsubscr...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to