sbroeder commented on code in PR #5382:
URL: https://github.com/apache/calcite/pull/5382#discussion_r4621506780


##########
core/src/main/java/org/apache/calcite/rel/rules/AggregateUnionNullBranchRule.java:
##########
@@ -0,0 +1,281 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.calcite.rel.rules;
+
+import org.apache.calcite.plan.Convention;
+import org.apache.calcite.plan.RelOptPredicateList;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelRule;
+import org.apache.calcite.plan.volcano.RelSubset;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.Union;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.util.ImmutableBitSet;
+
+import com.google.common.collect.ImmutableList;
+
+import org.immutables.value.Value;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Planner rule that removes work from {@code UNION ALL} branches that cannot
+ * contribute to the result of an enclosing {@link Aggregate}.
+ *
+ * <p>A branch is irrelevant to the aggregate if every aggregate call ignores
+ * NULL inputs, for example {@code SUM}, {@code MIN}, {@code MAX}, {@code AVG}
+ * or {@code COUNT(x)}, and reads only columns that the branch produces as
+ * constant NULL. Then:
+ *
+ * <ul>
+ * <li>If the aggregate has no {@code GROUP BY}, the branch cannot change the
+ * result and is removed.  At least one branch is always kept.
+ * <li>Otherwise the branch still contributes group keys, so it is kept, but a
+ * pure {@code GROUP BY} with no aggregate calls inside it is redundant, 
because
+ * duplicate rows cannot change the enclosing aggregate, and is replaced by a
+ * projection of the grouping columns.
+ * </ul>
+ *
+ * @see CoreRules#AGGREGATE_UNION_NULL_BRANCH
+ */
[email protected]
+public class AggregateUnionNullBranchRule
+    extends RelRule<AggregateUnionNullBranchRule.Config>
+    implements SubstitutionRule {
+
+  /** Creates an AggregateUnionNullBranchRule. */
+  protected AggregateUnionNullBranchRule(Config config) {
+    super(config);
+  }
+
+  @Override public void onMatch(RelOptRuleCall call) {
+    final Aggregate aggregate = call.rel(0);
+    final Union union = call.rel(1);
+    final RelMetadataQuery mq = call.getMetadataQuery();
+    final RelBuilder relBuilder = call.builder();
+
+    final List<RelNode> inputs = union.getInputs();
+    final List<Boolean> irrelevant = new ArrayList<>();
+    boolean anyIrrelevant = false;
+    for (RelNode input : inputs) {
+      final ImmutableBitSet nullColumns = nullColumns(mq, input);
+      final boolean ignored = aggregate.getAggCallList().stream()
+          .allMatch(c -> ignoresRowsWithNullColumns(c, nullColumns));
+      irrelevant.add(ignored);
+      anyIrrelevant |= ignored;
+    }
+    if (!anyIrrelevant) {
+      return;
+    }
+
+    final List<RelNode> newInputs = new ArrayList<>();
+    if (aggregate.getGroupSet().isEmpty()) {
+      for (int i = 0; i < inputs.size(); i++) {
+        if (!irrelevant.get(i)) {
+          newInputs.add(inputs.get(i));
+        }
+      }
+      if (newInputs.isEmpty()) {
+        newInputs.add(inputs.get(0));
+      }
+      if (newInputs.size() == inputs.size()) {
+        return;
+      }
+    } else {
+      boolean changed = false;
+      for (int i = 0; i < inputs.size(); i++) {
+        final RelNode input = inputs.get(i);
+        final RelNode simplified =
+            irrelevant.get(i) ? removeRedundantGroupBy(input, relBuilder) : 
input;
+        changed |= simplified != input;
+        newInputs.add(simplified);
+      }
+      if (!changed) {
+        return;
+      }
+    }
+
+    relBuilder.pushAll(newInputs);
+    relBuilder.union(true, newInputs.size());
+    if (newInputs.size() == inputs.size()) {
+      // Same number of branches: only the branches themselves changed.
+      relBuilder.convert(union.getRowType(), false);
+      call.transformTo(
+          aggregate.copy(aggregate.getTraitSet(), 
ImmutableList.of(relBuilder.build())));
+      call.getPlanner().prune(aggregate);
+      return;
+    }
+    // Branches were removed. Re-derive the aggregate over the remaining input
+    // so that the types are inferred and only cast if they differ.
+    final List<RelBuilder.AggCall> aggCalls = new ArrayList<>();
+    for (AggregateCall c : aggregate.getAggCallList()) {
+      RelBuilder.AggCall aggCall =
+          relBuilder.aggregateCall(c.getAggregation(), 
relBuilder.fields(c.getArgList()))
+              .distinct(c.isDistinct())
+              .approximate(c.isApproximate())
+              .ignoreNulls(c.ignoreNulls())
+              .as(c.name);
+      if (c.hasFilter()) {
+        aggCall = aggCall.filter(relBuilder.field(c.filterArg));
+      }
+      if (!c.getCollation().getFieldCollations().isEmpty()) {
+        aggCall = aggCall.sort(relBuilder.fields(c.getCollation()));
+      }
+      aggCalls.add(aggCall);
+    }
+    relBuilder.aggregate(relBuilder.groupKey(aggregate.getGroupSet()), 
aggCalls);
+    relBuilder.convert(aggregate.getRowType(), false);
+    call.transformTo(relBuilder.build());
+    call.getPlanner().prune(aggregate);
+  }
+
+  /** Returns the columns that {@code input} always produces as NULL. */
+  private static ImmutableBitSet nullColumns(RelMetadataQuery mq, RelNode 
input) {
+    final ImmutableBitSet.Builder columns = ImmutableBitSet.builder();
+    final RelOptPredicateList predicates = mq.getPulledUpPredicates(input);
+    if (predicates == null) {
+      return columns.build();

Review Comment:
   Removed in 670e28a0d58c099ec98ccced1bacefb24b3ecf56



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

Reply via email to