amaliujia commented on a change in pull request #2006:
URL: https://github.com/apache/calcite/pull/2006#discussion_r438534299



##########
File path: 
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableMergeJoin.java
##########
@@ -101,28 +103,55 @@ public static boolean isMergeJoinSupported(JoinRelType 
joinType) {
     // Required collation keys can be subset or superset of merge join keys.
     RelCollation collation = required.getTrait(RelCollationTraitDef.INSTANCE);
     List<Integer> reqKeys = RelCollations.ordinals(collation);
-    ImmutableBitSet reqKeySet = ImmutableBitSet.of(reqKeys);
+    // need to copy reqKeys because it is not sortable.
+    reqKeys = new ArrayList<>(reqKeys);
+    List<Integer> leftKeys = immutableIntListToList(joinInfo.leftKeys, 0);
+    List<Integer> rightKeys = immutableIntListToList(joinInfo.rightKeys,
+        left.getRowType().getFieldCount());
+    List<Integer> rightKeysNotShifted = 
immutableIntListToList(joinInfo.rightKeys, 0);
 
+    ImmutableBitSet reqKeySet = ImmutableBitSet.of(reqKeys);
     ImmutableBitSet leftKeySet = ImmutableBitSet.of(joinInfo.leftKeys);
     ImmutableBitSet rightKeySet = ImmutableBitSet.of(joinInfo.rightKeys)
         .shift(left.getRowType().getFieldCount());
 
     Map<Integer, Integer> keyMap = new HashMap<>();
-    final int keyCount = leftKeySet.cardinality();
+    final int keyCount = leftKeys.size();
     for (int i = 0; i < keyCount; i++) {
       keyMap.put(joinInfo.leftKeys.get(i), joinInfo.rightKeys.get(i));
     }
     Mappings.TargetMapping mapping = Mappings.target(keyMap,
         left.getRowType().getFieldCount(),
         right.getRowType().getFieldCount());
 
-    // Only consider exact key match for now
+
     if (reqKeySet.equals(leftKeySet)) {
+      // if sort keys equal to left join keys, we can pass through all 
collations directly.
       RelCollation rightCollation = RexUtil.apply(mapping, collation);
       return Pair.of(
           required, ImmutableList.of(required,
           required.replace(rightCollation)));
+    } else if (isSubset(reqKeys, leftKeys)) {
+      // if sort keys are subset of left join keys, we can extend collations 
to make sure all join
+      // keys are sorted.
+      collation = extendCollation(collation, leftKeys);
+      RelCollation rightCollation = RexUtil.apply(mapping, collation);
+      return Pair.of(
+          required, ImmutableList.of(required.replace(collation),
+              required.replace(rightCollation)));
+    } else if (isPrefixOrderingNotRequired(leftKeys, reqKeys)

Review comment:
       `containsOrderless` only consider if one is another's subset, for 
example:
   [1, 2, 3] is [2, 3, 4, 1]'s subset
   [3, 2, 1] is [2, 1, 3, 4] 's subset
   
   The prefix requirement is, still use examples above, but apply sublist:
   [2,3,4,1] -> [2,3,4], then containsOrderless([1,2,3]) = false.
   [2,1,3,4] ->[2,1,3], then containsOrderless([3,2,1]) = true.




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