[ https://issues.apache.org/jira/browse/HIVE-23031?focusedWorklogId=426882&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-426882 ]
ASF GitHub Bot logged work on HIVE-23031: ----------------------------------------- Author: ASF GitHub Bot Created on: 24/Apr/20 07:40 Start Date: 24/Apr/20 07:40 Worklog Time Spent: 10m Work Description: kgyrtkirk commented on a change in pull request #988: URL: https://github.com/apache/hive/pull/988#discussion_r414363412 ########## 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: I just followed the "usual practice" by passing the `HiveConf` :D but since there is nothing else needed - I've removed it... About the factory stuff: I've moved it to the constructor/etc but this is not entirely clear to me: * we the `HiveRelFactories` - to construct things * in the meantime there is also `call.builder()` which could be probably could be used to do the same Other rules seemed to utilize `HiveRelFactories` so I've followed that - but I feel that it would be better to use the builder - and fix the issues we might encounter along the way...what do you think? ---------------------------------------------------------------- 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: 426882) Time Spent: 40m (was: 0.5h) > 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: 40m > Remaining Estimate: 0h > -- This message was sent by Atlassian Jira (v8.3.4#803005)