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

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

                Author: ASF GitHub Bot
            Created on: 24/Apr/20 15:20
            Start Date: 24/Apr/20 15:20
    Worklog Time Spent: 10m 
      Work Description: jcamachor commented on a change in pull request #988:
URL: https://github.com/apache/hive/pull/988#discussion_r414658473



##########
File path: 
ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveRewriteCountDistinctToDataSketches.java
##########
@@ -0,0 +1,175 @@
+/*
+ * 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 java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.rel.RelCollation;
+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.AggregateFactory;
+import org.apache.calcite.rel.core.RelFactories.ProjectFactory;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.sql.SqlAggFunction;
+import org.apache.calcite.sql.SqlOperator;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.apache.hadoop.hive.ql.exec.DataSketchesFunctions;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
+import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveAggregate;
+import org.apache.hive.plugin.api.HiveUDFPlugin.UDFDescriptor;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableList;
+
+/**
+ * This rule could rewrite {@code count(distinct(x))} calls to be calculated 
using sketch based functions.
+ */
+public final class HiveRewriteCountDistinctToDataSketches extends RelOptRule {
+
+  protected static final Logger LOG = 
LoggerFactory.getLogger(HiveRewriteCountDistinctToDataSketches.class);
+  private String sketchClass;
+
+  public HiveRewriteCountDistinctToDataSketches(HiveConf conf) {
+    super(operand(HiveAggregate.class, any()));
+    sketchClass = 
conf.getVar(ConfVars.HIVE_OPTIMIZE_REWRITE_COUNT_DISTINCT_SKETCHCLASS);
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final Aggregate aggregate = call.rel(0);
+
+    if (aggregate.getGroupSets().size() != 1) {
+      // not yet supported
+      return;
+    }
+
+    List<AggregateCall> newAggCalls = new ArrayList<AggregateCall>();
+
+    AggregateFactory f = HiveRelFactories.HIVE_AGGREGATE_FACTORY;

Review comment:
       About `HiveConf`, I suggested this because it makes rules easier to 
instantiate by passing a well defined parameter.
   
   About the factories, one could make a case that 1) if the rule is a final 
static instance, you can pass the `HIVE_REL_BUILDER` to the RelOptRule 
constructor since you will not incur any additional instantiation cost per 
query, and thus use call.builder, and 2) if the rule is parameterized (as it is 
the case for this one), you have to instantiate it for every query compilation, 
thus your initial implementation using the factory directly would work better 
from performance point of view (instantiation of a builder based on 
`HIVE_REL_BUILDER` adds some overhead). If factories are not needed at all, I 
guess `copy` may be an option too.




----------------------------------------------------------------
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: 427002)
    Time Spent: 1h  (was: 50m)

> Add option to enable transparent rewrite of count(distinct) into sketch 
> functions
> ---------------------------------------------------------------------------------
>
>                 Key: HIVE-23031
>                 URL: https://issues.apache.org/jira/browse/HIVE-23031
>             Project: Hive
>          Issue Type: Sub-task
>            Reporter: Zoltan Haindrich
>            Assignee: Zoltan Haindrich
>            Priority: Major
>         Attachments: HIVE-23031.01.patch, HIVE-23031.02.patch, 
> HIVE-23031.03.patch, HIVE-23031.03.patch, HIVE-23031.03.patch
>
>          Time Spent: 1h
>  Remaining Estimate: 0h
>




--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to