jinxing64 commented on a change in pull request #1688: [CALCITE-3626] Add 
SortOnCalcToSortUnifyRule in SubstitutionVisitor
URL: https://github.com/apache/calcite/pull/1688#discussion_r361453523
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
 ##########
 @@ -1406,6 +1410,48 @@ private JoinOnCalcsToJoinUnifyRule() {
     }
   }
 
+  /**
+   * A {@link SubstitutionVisitor.UnifyRule} that matches a {@link MutableSort}
+   * which has {@link MutableCalc} as child to a {@link MutableSort}.
+   * We try to pull up the {@link MutableCalc} to top of {@link MutableSort},
+   * then match the {@link MutableSort} in query to {@link MutableSort} in 
target.
+   */
+  private static class SortOnCalcToSortUnifyRule extends AbstractUnifyRule {
+
+    public static final SortOnCalcToSortUnifyRule INSTANCE =
+        new SortOnCalcToSortUnifyRule();
+
+    private SortOnCalcToSortUnifyRule() {
+      super(operand(MutableSort.class, operand(MutableCalc.class, query(0))),
+          operand(MutableSort.class, target(0)), 1);
+    }
+
+    @Override protected UnifyResult apply(UnifyRuleCall call) {
+      final MutableSort query = (MutableSort) call.query;
+      final MutableCalc qInput = (MutableCalc) query.getInput();
+      final Pair<RexNode, List<RexNode>> qInputExplained = explainCalc(qInput);
+      final List<RexNode> qInputProjs = qInputExplained.right;
+
+      final MutableSort target = (MutableSort) call.target;
+      if (!Objects.equals(query.fetch, target.fetch)
+          || !Objects.equals(query.offset, target.offset)) {
 
 Review comment:
   When collations are not exactly the same, the data semantics will be wrong 
if offset is specified, though the offsets are the same.

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


With regards,
Apache Git Services

Reply via email to