xiedeyantu commented on code in PR #4562:
URL: https://github.com/apache/calcite/pull/4562#discussion_r2388166682


##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectToSemiJoinRule.java:
##########


Review Comment:
   This should be deleted.



##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectToSemiJoinRule.java:
##########
@@ -112,33 +114,50 @@ protected IntersectToSemiJoinRule(Config config) {
 
     for (int i = 1; i < inputs.size(); i++) {
       RelNode next = inputs.get(i);
-      List<RexNode> conditions = new ArrayList<>();
-      int fieldCount = current.getRowType().getFieldCount();
 
-      for (int j = 0; j < fieldCount; j++) {
-        RelDataType leftFieldType = 
current.getRowType().getFieldList().get(j).getType();
-        RelDataType rightFieldType = 
next.getRowType().getFieldList().get(j).getType();
-        RelDataType leastFieldType = 
leastRowType.getFieldList().get(j).getType();
+      // cast columns of the join inputs to the least types (global)
+      final RelNode leftCasted = projectJoinInput(builder, leastRowType, 
current);
+      final RelNode rightCasted = projectJoinInput(builder, leastRowType, 
next);
+
+      builder.clear();
+      builder.push(leftCasted).push(rightCasted);
 
-        conditions.add(
+      // compute the join condition over plain fields from the projections of 
left/right inputs
+      final int fieldCount = leastRowType.getFieldCount();
+      final List<RexNode> joinPredicates = new ArrayList<>(fieldCount);
+      for (int j = 0; j < fieldCount; j++) {
+        joinPredicates.add(
             builder.isNotDistinctFrom(
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(leftFieldType, j)),
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(rightFieldType, j + fieldCount))));
+            builder.field(2, 0, j),
+            builder.field(2, 1, j)));
       }
-      RexNode condition = RexUtil.composeConjunction(rexBuilder, conditions);
 
-      builder.push(next)
-          .join(JoinRelType.SEMI, condition);
+      final RexNode condition = RexUtil.composeConjunction(rexBuilder, 
joinPredicates);
+      builder.join(JoinRelType.SEMI, condition);
       current = builder.peek();
     }
 
-    builder.distinct()
-        .convert(leastRowType, true);
+    builder.distinct().convert(leastRowType, true);
     call.transformTo(builder.build());
   }
 
+  private RelNode projectJoinInput(
+      RelBuilder builder, RelDataType leastRowType, RelNode joinInput) {
+    builder.clear();

Review Comment:
   The same as above.



##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectToSemiJoinRule.java:
##########
@@ -112,33 +114,50 @@ protected IntersectToSemiJoinRule(Config config) {
 
     for (int i = 1; i < inputs.size(); i++) {
       RelNode next = inputs.get(i);
-      List<RexNode> conditions = new ArrayList<>();
-      int fieldCount = current.getRowType().getFieldCount();
 
-      for (int j = 0; j < fieldCount; j++) {
-        RelDataType leftFieldType = 
current.getRowType().getFieldList().get(j).getType();
-        RelDataType rightFieldType = 
next.getRowType().getFieldList().get(j).getType();
-        RelDataType leastFieldType = 
leastRowType.getFieldList().get(j).getType();
+      // cast columns of the join inputs to the least types (global)
+      final RelNode leftCasted = projectJoinInput(builder, leastRowType, 
current);
+      final RelNode rightCasted = projectJoinInput(builder, leastRowType, 
next);
+
+      builder.clear();
+      builder.push(leftCasted).push(rightCasted);
 
-        conditions.add(
+      // compute the join condition over plain fields from the projections of 
left/right inputs
+      final int fieldCount = leastRowType.getFieldCount();
+      final List<RexNode> joinPredicates = new ArrayList<>(fieldCount);
+      for (int j = 0; j < fieldCount; j++) {
+        joinPredicates.add(
             builder.isNotDistinctFrom(
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(leftFieldType, j)),
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(rightFieldType, j + fieldCount))));
+            builder.field(2, 0, j),
+            builder.field(2, 1, j)));
       }
-      RexNode condition = RexUtil.composeConjunction(rexBuilder, conditions);
 
-      builder.push(next)
-          .join(JoinRelType.SEMI, condition);
+      final RexNode condition = RexUtil.composeConjunction(rexBuilder, 
joinPredicates);
+      builder.join(JoinRelType.SEMI, condition);

Review Comment:
   ```suggestion
         current = builder.join(JoinRelType.SEMI, condition).build();
   ```



##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectToSemiJoinRule.java:
##########
@@ -112,33 +114,50 @@ protected IntersectToSemiJoinRule(Config config) {
 
     for (int i = 1; i < inputs.size(); i++) {
       RelNode next = inputs.get(i);
-      List<RexNode> conditions = new ArrayList<>();
-      int fieldCount = current.getRowType().getFieldCount();
 
-      for (int j = 0; j < fieldCount; j++) {
-        RelDataType leftFieldType = 
current.getRowType().getFieldList().get(j).getType();
-        RelDataType rightFieldType = 
next.getRowType().getFieldList().get(j).getType();
-        RelDataType leastFieldType = 
leastRowType.getFieldList().get(j).getType();
+      // cast columns of the join inputs to the least types (global)
+      final RelNode leftCasted = projectJoinInput(builder, leastRowType, 
current);
+      final RelNode rightCasted = projectJoinInput(builder, leastRowType, 
next);
+
+      builder.clear();
+      builder.push(leftCasted).push(rightCasted);
 
-        conditions.add(
+      // compute the join condition over plain fields from the projections of 
left/right inputs
+      final int fieldCount = leastRowType.getFieldCount();
+      final List<RexNode> joinPredicates = new ArrayList<>(fieldCount);
+      for (int j = 0; j < fieldCount; j++) {
+        joinPredicates.add(
             builder.isNotDistinctFrom(
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(leftFieldType, j)),
-                rexBuilder.makeCast(leastFieldType,
-                    rexBuilder.makeInputRef(rightFieldType, j + fieldCount))));
+            builder.field(2, 0, j),
+            builder.field(2, 1, j)));
       }
-      RexNode condition = RexUtil.composeConjunction(rexBuilder, conditions);
 
-      builder.push(next)
-          .join(JoinRelType.SEMI, condition);
+      final RexNode condition = RexUtil.composeConjunction(rexBuilder, 
joinPredicates);
+      builder.join(JoinRelType.SEMI, condition);
       current = builder.peek();

Review Comment:
   And remove it.



##########
core/src/main/java/org/apache/calcite/rel/rules/IntersectToSemiJoinRule.java:
##########
@@ -112,33 +114,50 @@ protected IntersectToSemiJoinRule(Config config) {
 
     for (int i = 1; i < inputs.size(); i++) {
       RelNode next = inputs.get(i);
-      List<RexNode> conditions = new ArrayList<>();
-      int fieldCount = current.getRowType().getFieldCount();
 
-      for (int j = 0; j < fieldCount; j++) {
-        RelDataType leftFieldType = 
current.getRowType().getFieldList().get(j).getType();
-        RelDataType rightFieldType = 
next.getRowType().getFieldList().get(j).getType();
-        RelDataType leastFieldType = 
leastRowType.getFieldList().get(j).getType();
+      // cast columns of the join inputs to the least types (global)
+      final RelNode leftCasted = projectJoinInput(builder, leastRowType, 
current);
+      final RelNode rightCasted = projectJoinInput(builder, leastRowType, 
next);
+
+      builder.clear();

Review Comment:
   Also can remove.



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