hsyuan commented on a change in pull request #2005:
URL: https://github.com/apache/calcite/pull/2005#discussion_r436291037
##########
File path:
core/src/main/java/org/apache/calcite/adapter/enumerable/EnumerableCorrelate.java
##########
@@ -81,6 +87,35 @@ public static EnumerableCorrelate create(
traitSet, left, right, correlationId, requiredColumns, joinType);
}
+ @Override public Pair<RelTraitSet, List<RelTraitSet>> passThroughTraits(
+ final RelTraitSet required) {
+ final RelCollation collation = required.getCollation();
+ if (collation == null || collation == RelCollations.EMPTY) {
+ return null;
+ }
+
+ // EnumerableCorrelate traits passdown shall only pass through collation
to left input.
+ // This is because for EnumerableCorrelate always uses left input as the
outer loop,
+ // thus only left input can preserve ordering.
+
+ for (RelFieldCollation relFieldCollation : collation.getFieldCollations())
{
+ // If field collation belongs to right input: bail out.
+ if (relFieldCollation.getFieldIndex() >=
getLeft().getRowType().getFieldCount()) {
+ return null;
+ }
+ }
+
+ final RelTraitSet passThroughTraitSet = traitSet.replace(collation);
+ return Pair.of(passThroughTraitSet,
+ ImmutableList.of(
+ passThroughTraitSet,
+ passThroughTraitSet.replace(RelCollations.EMPTY)));
+ }
+
+ @Override public DeriveMode getDeriveMode() {
+ return DeriveMode.LEFT_FIRST;
+ }
Review comment:
`deriveTraits` is still missing. If the left child can deliver some
collation, the correlate should be able to deliver the same collation.
##########
File path: core/src/test/java/org/apache/calcite/test/TopDownOptTest.java
##########
@@ -132,6 +134,72 @@
.check();
}
+ // Order by left field(s): push down sort to left input.
+ @Test void testCorrelateInnerJoinDeriveLeft() {
+ final String sql = "select * from emp e\n"
+ + "join dept d on e.deptno=d.deptno\n"
+ + "order by e.ename";
+ Query.create(sql)
+ .addRule(JoinToCorrelateRule.INSTANCE)
+ .removeRule(EnumerableRules.ENUMERABLE_JOIN_RULE)
+ .removeRule(EnumerableRules.ENUMERABLE_MERGE_JOIN_RULE)
+ .removeRule(EnumerableRules.ENUMERABLE_SORT_RULE)
+ .check();
+ }
Review comment:
You can also use lateral to generate correlate if you would like to. The
current tests are good enough, though.
e.g.
https://github.com/apache/calcite/blob/master/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml#L6581
----------------------------------------------------------------
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]