amaliujia commented on a change in pull request #2006: URL: https://github.com/apache/calcite/pull/2006#discussion_r437890122
########## 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: For supset case, for example if required collation is [d, b, a ,c] then join keys [d, b, a] is valid join keys [d,a,c] is not (if push down [d,b,a,c], it does not guarantee the ordering of [d,a,c] ---------------------------------------------------------------- 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