jinxing64 commented on a change in pull request #1619: [CALCITE-3537] Add
NothingToCalcUnifyRule to match the top of MutableCalc in SubstitutionVisitor
URL: https://github.com/apache/calcite/pull/1619#discussion_r354646947
##########
File path: core/src/main/java/org/apache/calcite/plan/SubstitutionVisitor.java
##########
@@ -1070,6 +1071,70 @@ private ScanToCalcUnifyRule() {
}
}
+ /**
+ * A {@link SubstitutionVisitor.UnifyRule} that matches a {@link MutableCalc}
+ * which has {@link MutableAggregate} as child.
+ * We try to compensate the {@link MutableCalc},
+ * then match the query to the top of {@link MutableCalc} in target.
+ */
+ private static class NothingToCalcUnifyRule extends AbstractUnifyRule {
+
+ public static final NothingToCalcUnifyRule INSTANCE =
+ new NothingToCalcUnifyRule();
+
+ private NothingToCalcUnifyRule() {
+ super(any(MutableRel.class),
+ any(MutableCalc.class), 0);
+ }
+
+ public UnifyResult apply(UnifyRuleCall call) {
+ final MutableRel rel = call.query;
+ final MutableCalc target = (MutableCalc) call.target;
+ final MutableRel targetInput = target.getInput();
+ final Pair<RexNode, List<RexNode>> qInputExplained = explainCalc(target);
+ if (rel.getParent() != null || target.getParent() != null) {
+ return null;
+ }
+ final Mappings.TargetMapping mapping =
+ Project.getMapping(target.getInput().rowType.getFieldCount(),
qInputExplained.right);
+ if (mapping == null || Mappings.keepsOrdering(mapping)) {
+ return null;
+ }
+ MutableRel equalRel = rel.getInputs().get(0);
+ while (equalRel != targetInput) {
+ if (equalRel instanceof MutableScan) {
+ return null;
+ }
+ equalRel = equalRel.getInputs().get(0);
+ }
+ MutableRel parent = equalRel.getParent();
+ final RexBuilder rexBuilder = call.getCluster().getRexBuilder();
+
+ List<RexNode> rexNodes = (List<RexNode>)
rexBuilder.identityProjects(equalRel.rowType);
+ try {
+ RexShuttle shuttle = getRexShuttle(qInputExplained.right);
+ List<RexNode> compenProj = shuttle.apply(rexNodes);
+ RexProgram rexProgram = RexProgram.create(target.rowType, compenProj,
null,
+ equalRel.rowType, rexBuilder);
+ MutableCalc newProj = MutableCalc.of(target, rexProgram);
+ parent.setInput(0, newProj);
+ if (equalRel.getParent() instanceof MutableCalc) {
Review comment:
I don't think this check is right, we should use
`tryMergeParentCalcAndGenResult`
----------------------------------------------------------------
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