cgivre commented on code in PR #3026: URL: https://github.com/apache/drill/pull/3026#discussion_r2444990546
########## exec/java-exec/src/main/java/org/apache/drill/exec/planner/logical/DrillAggregateExpandGroupingSetsRule.java: ########## @@ -0,0 +1,475 @@ +/* + * 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.drill.exec.planner.logical; + +import com.google.common.collect.ImmutableList; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptRule; +import org.apache.calcite.plan.RelOptRuleCall; +import org.apache.calcite.rel.InvalidRelException; +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.logical.LogicalAggregate; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexBuilder; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.fun.SqlStdOperatorTable; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.calcite.util.ImmutableBitSet; + +import java.util.ArrayList; +import java.util.List; + +/** + * Planner rule that expands GROUPING SETS, ROLLUP, and CUBE into a UNION ALL + * of multiple aggregates, each with a single grouping set. + * + * This rule converts: + * SELECT a, b, SUM(c) FROM t GROUP BY GROUPING SETS ((a, b), (a), ()) + * + * Into: + * SELECT a, b, SUM(c), 0 AS $g FROM t GROUP BY a, b + * UNION ALL + * SELECT a, null, SUM(c), 1 AS $g FROM t GROUP BY a + * UNION ALL + * SELECT null, null, SUM(c), 3 AS $g FROM t GROUP BY () + * + * The $g column is the grouping ID that can be used by GROUPING() and GROUPING_ID() functions. + * Currently, the $g column is generated internally but stripped from the final output. + */ +public class DrillAggregateExpandGroupingSetsRule extends RelOptRule { + + public static final DrillAggregateExpandGroupingSetsRule INSTANCE = + new DrillAggregateExpandGroupingSetsRule(); + public static String GROUPING_ID_COLUMN_NAME = "$g"; + public static String GROUP_ID_COLUMN_NAME = "$group_id"; + public static String EXPRESSION_COLUMN_PLACEHOLDER = "EXPR$"; Review Comment: Good catch. Fixed. -- 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]
