mihaibudiu commented on code in PR #4904: URL: https://github.com/apache/calcite/pull/4904#discussion_r3120694819
########## core/src/main/java/org/apache/calcite/rel/rules/AggregateRemoveDuplicateKeysRule.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.core.Aggregate; +import org.apache.calcite.rel.logical.LogicalAggregate; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.tools.RelBuilder; +import org.apache.calcite.util.ImmutableBitSet; + +import org.immutables.value.Value; + +import java.util.ArrayList; +import java.util.List; + +/** + * Planner rule that removes redundant grouping keys from an + * {@link Aggregate} when the later keys are functionally determined by the + * earlier retained keys. + * + * <p>The original output schema is preserved by adding {@code ANY_VALUE} + * aggregate calls for removed grouping keys and then projecting the row back + * into the original field order. + * + * <p>The original SQL: + * <pre>{@code + * SELECT deptno, name, count(*) AS c + * FROM sales.dept + * GROUP BY deptno, name + * }</pre> + * + * <p>The original logical plan: + * <pre> + * LogicalAggregate(group=[{0, 1}], C=[COUNT()]) + * LogicalTableScan(table=[[CATALOG, SALES, DEPT]]) + * </pre> + * + * <p>After optimization: + * <pre> + * LogicalProject(DEPTNO=[$0], NAME=[$1], C=[$2]) + * LogicalAggregate(group=[{0}], NAME=[ANY_VALUE($1)], C=[COUNT()]) Review Comment: To some degree ANY_VALUE and SINGLE_VALUE are equivalent in this context, so this implementation could work as well. We could merge this PR as is probably. But in general I like to have assertions as strong as possible. -- 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]
