hsyuan commented on a change in pull request #2023:
URL: https://github.com/apache/calcite/pull/2023#discussion_r439578915



##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumTraitsUtils.java
##########
@@ -148,4 +149,52 @@ private static boolean isCollationOnTrivialExpr(
       return null;
     }
   }
+
+  // EnumerableNestedLoopJoin and EnumerableBatchNestedLoopJoin traits 
passdown shall only
+  // pass through collation to left input. It is because for 
EnumerableNestedLoopJoin always
+  // uses left input as the outer loop, thus only left input can preserve 
ordering.
+  // Push sort both to left and right inputs does not help right outer join. 
It's because in
+  // implementation, EnumerableNestedLoopJoin produces (null, right_unmatched) 
all together,
+  // which does not preserve ordering from right side.
+  static Pair<RelTraitSet, List<RelTraitSet>> 
passThroughTraitsForNestedLoopJoin(
+      RelTraitSet required, JoinRelType joinType,
+      int leftInputFieldCount, RelTraitSet joinTraitSet) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    for (RelFieldCollation fc : collation.getFieldCollations()) {
+      // If field collation belongs to right input: cannot push down collation.
+      if (fc.getFieldIndex() >= leftInputFieldCount) {
+        return null;
+      }
+    }
+
+    RelTraitSet passthroughTraitSet = joinTraitSet.replace(collation);
+    return Pair.of(passthroughTraitSet,
+        ImmutableList.of(
+            passthroughTraitSet,
+            passthroughTraitSet.replace(RelCollations.EMPTY)));
+  }
+
+  static Pair<RelTraitSet, List<RelTraitSet>> deriveTraitsForNestedLoopJoin(
+      RelTraitSet childTraits, int childId,
+      RelTraitSet joinTraitSet, RelTraitSet rightTraitSet) {
+    // should only derive traits (limited to collation for now) from left join 
input.
+    assert childId == 0;
+
+    RelCollation collation = childTraits.getCollation();
+    if (collation == null || collation == RelCollations.EMPTY) {

Review comment:
       Add check 
   ```
   || joinType == JoinRelType.FULL
           || joinType == JoinRelType.RIGHT
   ```

##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumTraitsUtils.java
##########
@@ -148,4 +149,52 @@ private static boolean isCollationOnTrivialExpr(
       return null;
     }
   }
+
+  // EnumerableNestedLoopJoin and EnumerableBatchNestedLoopJoin traits 
passdown shall only
+  // pass through collation to left input. It is because for 
EnumerableNestedLoopJoin always
+  // uses left input as the outer loop, thus only left input can preserve 
ordering.
+  // Push sort both to left and right inputs does not help right outer join. 
It's because in
+  // implementation, EnumerableNestedLoopJoin produces (null, right_unmatched) 
all together,
+  // which does not preserve ordering from right side.
+  static Pair<RelTraitSet, List<RelTraitSet>> 
passThroughTraitsForNestedLoopJoin(

Review comment:
       HashJoin and Correlate can reuse it too.




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

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


Reply via email to