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


##########
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));

Review Comment:
   Yes, I think that's correct and probably a better change.  I will work on 
it.  



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