[ 
https://issues.apache.org/jira/browse/HIVE-21634?focusedWorklogId=230967&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-230967
 ]

ASF GitHub Bot logged work on HIVE-21634:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 22/Apr/19 23:55
            Start Date: 22/Apr/19 23:55
    Worklog Time Spent: 10m 
      Work Description: vineetgarg02 commented on pull request #602: HIVE-21634
URL: https://github.com/apache/hive/pull/602#discussion_r277472371
 
 

 ##########
 File path: 
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateSplitRule.java
 ##########
 @@ -0,0 +1,113 @@
+/*
+ * 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.hadoop.hive.ql.optimizer.calcite.rules;
+
+import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+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.sql.SqlAggFunction;
+import org.apache.calcite.tools.RelBuilder;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelBuilder;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
+import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate;
+import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveGroupingID;
+
+/**
+ * Rule that matches an aggregate with grouping sets and splits it into an 
aggregate
+ * without grouping sets (bottom) and an aggregate with grouping sets (top).
+ */
+public class HiveAggregateSplitRule extends RelOptRule {
+
+  public static final HiveAggregateSplitRule INSTANCE =
+      new HiveAggregateSplitRule(HiveAggregate.class, 
HiveRelFactories.HIVE_BUILDER);
+
+  private HiveAggregateSplitRule(Class<? extends Aggregate> aggregateClass,
+      RelBuilderFactory relBuilderFactory) {
+    super(
+        operandJ(aggregateClass, null, agg -> agg.getGroupType() != 
Group.SIMPLE, any()),
+        relBuilderFactory, null);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final Aggregate aggregate = call.rel(0);
+    final RelBuilder relBuilder = call.builder();
+
+    // If any aggregate is distinct, bail out
+    // If any aggregate is the grouping id, bail out
+    // If any aggregate call has a filter, bail out
+    // If any aggregate functions do not support splitting, bail out
+    final List<SqlAggFunction> topAggFunctions = new ArrayList<>();
+    for (AggregateCall aggregateCall : aggregate.getAggCallList()) {
+      if (aggregateCall.isDistinct()) {
+        return;
+      }
+      if (aggregateCall.getAggregation().equals(HiveGroupingID.INSTANCE)) {
+        return;
+      }
+      if (aggregateCall.filterArg >= 0) {
+        return;
+      }
+      SqlAggFunction aggFunction =
+          HiveRelBuilder.getRollup(aggregateCall.getAggregation());
+      if (aggFunction == null) {
+        return;
+      }
+      topAggFunctions.add(aggFunction);
+    }
+
+    final ImmutableBitSet bottomAggregateGroupSet = aggregate.getGroupSet();
+    if 
(aggregate.getCluster().getMetadataQuery().areColumnsUnique(aggregate.getInput(),
 bottomAggregateGroupSet)) {
+      // Nothing to do, probably already pushed
+      return;
+    }
+
+    final ImmutableBitSet topAggregateGroupSet = ImmutableBitSet.range(0, 
bottomAggregateGroupSet.cardinality());
+
+    final Map<Integer, Integer> map = new HashMap<>();
+    bottomAggregateGroupSet.forEach(k -> map.put(k, map.size()));
+    ImmutableList<ImmutableBitSet> topAggregateGroupSets = 
ImmutableBitSet.ORDERING.immutableSortedCopy(
+        ImmutableBitSet.permute(aggregate.groupSets, map));
+
+    final List<AggregateCall> topAggregateCalls = new ArrayList<>();
+    for (int i = 0; i < aggregate.getAggCallList().size(); i++) {
+      AggregateCall aggregateCall = aggregate.getAggCallList().get(i);
 
 Review comment:
   Instead of looping over agg call list again I believe you can do this in the 
first loop itself where `topAggFuctions` is created.
 
----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 230967)
    Time Spent: 20m  (was: 10m)

> Materialized view rewriting over aggregate operators containing with grouping 
> sets
> ----------------------------------------------------------------------------------
>
>                 Key: HIVE-21634
>                 URL: https://issues.apache.org/jira/browse/HIVE-21634
>             Project: Hive
>          Issue Type: Improvement
>          Components: Materialized views
>            Reporter: Jesus Camacho Rodriguez
>            Assignee: Jesus Camacho Rodriguez
>            Priority: Major
>              Labels: pull-request-available
>         Attachments: HIVE-21634.patch
>
>          Time Spent: 20m
>  Remaining Estimate: 0h
>
> A possible approach to support rewriting queries with an aggregate with 
> grouping sets is implementing a rule that splits the aggregate in the query 
> into an aggregate without grouping sets (bottom) and an aggregate with 
> grouping sets (top). Then the materialized view rewriting rule will trigger 
> on the former.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to