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



##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableHashJoin.java
##########
@@ -100,6 +106,81 @@ public static EnumerableHashJoin create(
         condition, variablesSet, joinType);
   }
 
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+      final RelTraitSet required) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    List<Integer> requiredKeys = RelCollations.ordinals(collation);
+    ImmutableBitSet requiredKeySet = ImmutableBitSet.of(requiredKeys);
+    ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
+
+    // EnumerableHashJoin traits passdown shall only pass through collation to 
left input.
+    // It is because for EnumerableHashJoin always builds hash table on right 
input,
+    // and only non-hashed side can preserve ordering.
+
+    // Only consider exact key match for now
+    if (requiredKeySet.equals(leftKeySet)) {

Review comment:
       hash join / nested loopjoin are different with MergeJoin. The required 
collation keys can be any keys other than join keys. 
   ```sql
   select * from foo join bar using (a) order by foo.b;
   ```
   We can still pass down the collation request to HashJoin's left child, 
because hashjoin doesn't require any collation from its left child.
   But for mergejoin, we can't pass down. because the join's parent requires 
collation on `b`, but the mergejoin requires its children to be sorted on `a`.

##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableHashJoin.java
##########
@@ -100,6 +106,45 @@ public static EnumerableHashJoin create(
         condition, variablesSet, joinType);
   }
 
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+      final RelTraitSet required) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    List<Integer> requiredKeys = RelCollations.ordinals(collation);
+    ImmutableBitSet requiredKeySet = ImmutableBitSet.of(requiredKeys);
+    ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
+
+    // EnumerableHashJoin traits passdown shall only pass through collation to 
left input.
+    // It is because for EnumerableHashJoin always builds hash table on right 
input,
+    // and only non-hashed side can preserve ordering.
+
+    // Only consider exact key match for now
+    if (requiredKeySet.equals(leftKeySet)) {
+      RelTraitSet passthroughTraitSet = traitSet.replace(collation);
+      return Pair.of(passthroughTraitSet,
+          ImmutableList.of(
+              passthroughTraitSet,
+              passthroughTraitSet.replace(RelCollations.EMPTY)));
+    }
+
+    // TODO: consider super keyset and sub keyset

Review comment:
       so this comment should be removed.

##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableHashJoin.java
##########
@@ -100,6 +106,81 @@ public static EnumerableHashJoin create(
         condition, variablesSet, joinType);
   }
 
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+      final RelTraitSet required) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    List<Integer> requiredKeys = RelCollations.ordinals(collation);
+    ImmutableBitSet requiredKeySet = ImmutableBitSet.of(requiredKeys);
+    ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
+
+    // EnumerableHashJoin traits passdown shall only pass through collation to 
left input.
+    // It is because for EnumerableHashJoin always builds hash table on right 
input,
+    // and only non-hashed side can preserve ordering.
+
+    // Only consider exact key match for now
+    if (requiredKeySet.equals(leftKeySet)) {
+      RelTraitSet passthroughTraitSet = traitSet.replace(collation);
+      return Pair.of(passthroughTraitSet,
+          ImmutableList.of(
+              passthroughTraitSet,
+              passthroughTraitSet.replace(RelCollations.EMPTY)));
+    }
+
+    // TODO: consider super keyset and sub keyset
+    return null;
+  }
+
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
+      final RelTraitSet childTraits, final int childId) {
+    // should only derive traits (limited to collation for now) from left join 
input.
+    if (childId > 0) {
+      return null;
+    }
+
+    RelCollation collation = childTraits.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    final int colCount = collation.getFieldCollations().size();
+    final int keyCount = joinInfo.leftKeys.size();
+    if (colCount < keyCount || keyCount == 0) {
+      return null;
+    }

Review comment:
       this is not needed. we can derive any collation from its left child. no 
matter what are the join keys.

##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableHashJoin.java
##########
@@ -100,6 +106,81 @@ public static EnumerableHashJoin create(
         condition, variablesSet, joinType);
   }
 
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+      final RelTraitSet required) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    List<Integer> requiredKeys = RelCollations.ordinals(collation);
+    ImmutableBitSet requiredKeySet = ImmutableBitSet.of(requiredKeys);
+    ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
+
+    // EnumerableHashJoin traits passdown shall only pass through collation to 
left input.
+    // It is because for EnumerableHashJoin always builds hash table on right 
input,
+    // and only non-hashed side can preserve ordering.
+
+    // Only consider exact key match for now
+    if (requiredKeySet.equals(leftKeySet)) {
+      RelTraitSet passthroughTraitSet = traitSet.replace(collation);
+      return Pair.of(passthroughTraitSet,
+          ImmutableList.of(
+              passthroughTraitSet,
+              passthroughTraitSet.replace(RelCollations.EMPTY)));
+    }
+
+    // TODO: consider super keyset and sub keyset
+    return null;
+  }
+
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
+      final RelTraitSet childTraits, final int childId) {
+    // should only derive traits (limited to collation for now) from left join 
input.
+    if (childId > 0) {
+      return null;
+    }
+
+    RelCollation collation = childTraits.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    final int colCount = collation.getFieldCollations().size();
+    final int keyCount = joinInfo.leftKeys.size();
+    if (colCount < keyCount || keyCount == 0) {
+      return null;
+    }
+
+    if (colCount > keyCount) {
+      collation = RelCollations.of(collation.getFieldCollations().subList(0, 
keyCount));
+    }

Review comment:
       same here.

##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableHashJoin.java
##########
@@ -100,6 +106,81 @@ public static EnumerableHashJoin create(
         condition, variablesSet, joinType);
   }
 
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+      final RelTraitSet required) {
+    RelCollation collation = required.getCollation();
+    if (collation == null
+        || collation == RelCollations.EMPTY
+        || joinType == JoinRelType.FULL
+        || joinType == JoinRelType.RIGHT) {
+      return null;
+    }
+
+    List<Integer> requiredKeys = RelCollations.ordinals(collation);
+    ImmutableBitSet requiredKeySet = ImmutableBitSet.of(requiredKeys);
+    ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
+
+    // EnumerableHashJoin traits passdown shall only pass through collation to 
left input.
+    // It is because for EnumerableHashJoin always builds hash table on right 
input,
+    // and only non-hashed side can preserve ordering.
+
+    // Only consider exact key match for now
+    if (requiredKeySet.equals(leftKeySet)) {
+      RelTraitSet passthroughTraitSet = traitSet.replace(collation);
+      return Pair.of(passthroughTraitSet,
+          ImmutableList.of(
+              passthroughTraitSet,
+              passthroughTraitSet.replace(RelCollations.EMPTY)));
+    }
+
+    // TODO: consider super keyset and sub keyset
+    return null;
+  }
+
+  @Override public Pair<RelTraitSet, List<RelTraitSet>> deriveTraits(
+      final RelTraitSet childTraits, final int childId) {
+    // should only derive traits (limited to collation for now) from left join 
input.
+    if (childId > 0) {

Review comment:
       should be `assert childId == 0`, it is illegal to be greater than 0




----------------------------------------------------------------
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:
[email protected]


Reply via email to