zzwqqq commented on code in PR #5385:
URL: https://github.com/apache/calcite/pull/5385#discussion_r4514596912
##########
core/src/main/java/org/apache/calcite/sql2rel/RelDecorrelator.java:
##########
@@ -464,9 +497,48 @@ public RelNode removeCorrelationViaRule(RelNode root) {
* {@link org.apache.calcite.rel.core.Correlate}s might be removable in such
way).
*/
public RelNode removeCorrelationViaRule(RelNode root, RuleSet ruleSet) {
+ root = prepareCorrelates(root);
return applyHepProgram(root, ruleSetToHepProgram(ruleSet));
}
+ /** Removes unused RHS computations before matching rules or building frames.
+ * The checked input must also be the input used by the rewrite.
+ */
+ private RelNode prepareCorrelates(RelNode root) {
+ final RelNode prepared = root.accept(new RelHomogeneousShuttle() {
+ @Override public RelNode visit(RelNode other) {
+ final RelNode rel = super.visit(other);
+ if (!(rel instanceof Correlate)) {
+ return rel;
+ }
+ final Correlate correlate = (Correlate) rel;
+ RelNode right = correlate.getRight();
+ if (!isDeterministic(right)) {
+ final RelBuilder trimBuilder =
relBuilderFactory().create(right.getCluster(), null);
+ final RelNode trimmed = new RelFieldTrimmer(null,
trimBuilder).trim(right);
Review Comment:
Sorry, my earlier explanation was incomplete: Using `RelFieldTrimmer.trim`
on the whole plan before decorrelation can remove a key that is unused by the
query but needed by the scalar uniqueness check. `trimPreservingBindings`
leaves the Correlate's left input unchanged, so that key is preserved.
However, applying `trimPreservingBindings` globally can remove the `Project`
below a `SINGLE_VALUE` aggregate, preventing the scalar rule from eliminating
that aggregate. I'd prefer to keep trimming local in this PR. Does that sound
good?
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]