mihaibudiu commented on code in PR #4904: URL: https://github.com/apache/calcite/pull/4904#discussion_r3120048231
########## core/src/test/resources/org/apache/calcite/test/AggregateRemoveDuplicateKeysRuleTest.xml: ########## @@ -0,0 +1,73 @@ +<?xml version="1.0" ?> Review Comment: I thought the plan was to move away from xml and to offload more and more to quidem tests. ########## core/src/test/resources/sql/misc.iq: ########## @@ -1381,12 +1381,12 @@ from v; # ORDER BY expression with SELECT DISTINCT select distinct deptno, job from "scott".emp -order by substring(job from 2 for 1), -deptno; +order by substring(job from 2 for 1), -deptno, job; Review Comment: this seems unrelated, I guess it's to make the test deterministic ########## 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: So you switched to ANY_VALUE instead of SINGLE_VALUE? Why is this better? SINGLE_VALUE will catch bugs if they exist... -- 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]
