liuyongvs commented on code in PR #25900:
URL: https://github.com/apache/flink/pull/25900#discussion_r1903784779


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/plan/rules/logical/PruneAggregateCallRule.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.flink.table.planner.plan.rules.logical;
+
+import org.apache.flink.util.Preconditions;
+
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Aggregate.Group;
+import org.apache.calcite.rel.core.AggregateCall;
+import org.apache.calcite.rel.core.Calc;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexProgram;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.runtime.Utilities;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.mapping.Mappings;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+/** Planner rule that removes unreferenced AggregateCall from Aggregate. */
+public abstract class PruneAggregateCallRule<T extends RelNode> extends 
RelOptRule {
+
+    public static final ProjectPruneAggregateCallRule PROJECT_ON_AGGREGATE =
+            new ProjectPruneAggregateCallRule();
+    public static final CalcPruneAggregateCallRule CALC_ON_AGGREGATE =
+            new CalcPruneAggregateCallRule();
+
+    protected PruneAggregateCallRule(Class<T> topClass) {
+        super(
+                operand(topClass, operand(Aggregate.class, any())),
+                RelFactories.LOGICAL_BUILDER,
+                "PruneAggregateCallRule_" + topClass.getCanonicalName());
+    }
+
+    protected abstract ImmutableBitSet getInputRefs(T relOnAgg);
+
+    @Override
+    public boolean matches(RelOptRuleCall call) {
+        T relOnAgg = call.rel(0);
+        Aggregate agg = call.rel(1);
+        if (agg.getGroupType() != Group.SIMPLE
+                || agg.getAggCallList().isEmpty()
+                ||
+                // at least output one column
+                (agg.getGroupCount() == 0 && agg.getAggCallList().size() == 
1)) {
+            return false;
+        }
+        ImmutableBitSet inputRefs = getInputRefs(relOnAgg);
+        int[] unrefAggCallIndices = getUnrefAggCallIndices(inputRefs, agg);
+        return unrefAggCallIndices.length > 0;
+    }
+
+    private int[] getUnrefAggCallIndices(ImmutableBitSet inputRefs, Aggregate 
agg) {
+        int groupCount = agg.getGroupCount();
+        return IntStream.range(0, agg.getAggCallList().size())
+                .filter(index -> !inputRefs.get(groupCount + index))
+                .toArray();
+    }
+
+    @Override
+    public void onMatch(RelOptRuleCall call) {
+        T relOnAgg = call.rel(0);
+        Aggregate agg = call.rel(1);
+        ImmutableBitSet inputRefs = getInputRefs(relOnAgg);
+        int[] unrefAggCallIndices = getUnrefAggCallIndices(inputRefs, agg);
+        Preconditions.checkArgument(unrefAggCallIndices.length > 0, 
"requirement failed");
+
+        List<AggregateCall> newAggCalls = new 
ArrayList<>(agg.getAggCallList());
+        // remove unreferenced AggCall from original aggCalls
+        Arrays.stream(unrefAggCallIndices)
+                .boxed()
+                .sorted(Comparator.reverseOrder())
+                .forEach(index -> newAggCalls.remove((int) index));
+

Review Comment:
   comments: newAggCalls.remove((int) index) , we need this int cast here. 
because it will remove the value instead of   removing index when doesn't have 
int cast 



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to