mihaibudiu commented on code in PR #4903: URL: https://github.com/apache/calcite/pull/4903#discussion_r3120111828
########## core/src/main/java/org/apache/calcite/rel/rules/AggregateFunctionOfGroupByKeysRule.java: ########## @@ -0,0 +1,182 @@ +/* + * 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.RelOptRuleCall; +import org.apache.calcite.plan.RelRule; +import org.apache.calcite.rel.RelCollations; +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.RelFactories; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexInputRef; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.tools.RelBuilder; + +import org.checkerframework.checker.nullness.qual.Nullable; +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; + +/** + * Planner rule that eliminates aggregate functions of GROUP BY keys. + * + * <p>For example, + * {@code SELECT sal, max(sal) FROM emp GROUP BY sal} + * can be simplified to + * {@code SELECT sal, sal FROM emp GROUP BY sal}. + * + * <p>Currently supports the following aggregate functions when their + * arguments exist in the aggregate's group set: + * <ul> + * <li>{@code MAX}</li> + * <li>{@code MIN}</li> + * <li>{@code AVG}</li> + * <li>{@code ANY_VALUE}</li> + * <li>{@code FIRST_VALUE}</li> Review Comment: I think FIRST_VALUE and LAST_VALUE only work for window aggregates. ########## core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java: ########## @@ -10060,6 +10060,41 @@ private RelOptFixture spatial(String sql) { sql(sql).withRule(CoreRules.AGGREGATE_REDUCE_FUNCTIONS, CoreRules.PROJECT_MERGE).check(); } + /** Test case for + * <a href="https://issues.apache.org/jira/browse/CALCITE-7484">[CALCITE-7484] + * Add AggregateFunctionOfGroupByKeysRule to eliminate redundant + * aggregates over GROUP BY keys</a>. + */ + @Test void testAggregateFunctionOfGroupByKeys() { + final String sql = "select sal, max(sal) as sal_max, min(sal) as sal_min,\n" + + "avg(sal) sal_avg, any_value(sal) as sal_val, first_value(sal) as sal_first,\n" Review Comment: This query should be illegal, since FIRST_VALUE and LAST_VALUE are only defined for window aggregates. Please file an issue - this program should not be accepted. -- 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]
