diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java index 5cbfb5dbe0cf..b1b8003e21fd 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveCalciteUtil.java @@ -954,7 +954,7 @@ public static AggregateCall createSingleArgAggCall(String funcName, RelOptCluste PrimitiveTypeInfo typeInfo, Integer pos, RelDataType aggFnRetType) { ImmutableList.Builder aggArgRelDTBldr = new ImmutableList.Builder(); aggArgRelDTBldr.add(TypeConverter.convert(typeInfo, cluster.getTypeFactory())); - SqlAggFunction aggFunction = SqlFunctionConverter.getCalciteAggFn(funcName, false, + SqlAggFunction aggFunction = SqlFunctionConverter.getCalciteAggFn(funcName, aggArgRelDTBldr.build(), aggFnRetType); List argList = new ArrayList(); argList.add(pos); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/CanAggregateDistinct.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/CanAggregateDistinct.java deleted file mode 100644 index efccaca95f3d..000000000000 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/CanAggregateDistinct.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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.functions; - -/** - * This is the UDAF interface to support DISTINCT function. - * - */ -public interface CanAggregateDistinct { - boolean isDistinct(); -} diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlAverageAggFunction.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlAverageAggFunction.java index 361611e9d0c1..f0a8c3e2e1f5 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlAverageAggFunction.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlAverageAggFunction.java @@ -22,15 +22,15 @@ import org.apache.calcite.sql.SqlFunctionCategory; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlSplittableAggFunction; +import org.apache.calcite.sql.SqlSyntax; import org.apache.calcite.sql.type.SqlOperandTypeChecker; import org.apache.calcite.sql.type.SqlOperandTypeInference; import org.apache.calcite.sql.type.SqlReturnTypeInference; import org.apache.calcite.util.Optionality; -public class HiveSqlAverageAggFunction extends SqlAggFunction implements CanAggregateDistinct { - private final boolean isDistinct; +public class HiveSqlAverageAggFunction extends SqlAggFunction { - public HiveSqlAverageAggFunction(boolean isDistinct, SqlReturnTypeInference returnTypeInference, + public HiveSqlAverageAggFunction(SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker) { super( "avg", @@ -43,7 +43,6 @@ public HiveSqlAverageAggFunction(boolean isDistinct, SqlReturnTypeInference retu false, false, Optionality.FORBIDDEN); - this.isDistinct = isDistinct; } @Override @@ -55,7 +54,7 @@ public T unwrap(Class clazz) { } @Override - public boolean isDistinct() { - return isDistinct; + public SqlSyntax getSyntax() { + return SqlSyntax.FUNCTION_STAR; } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlCountAggFunction.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlCountAggFunction.java index 7318676928fa..348045cdd1db 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlCountAggFunction.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlCountAggFunction.java @@ -41,14 +41,12 @@ import com.google.common.collect.ImmutableList; -public class HiveSqlCountAggFunction extends SqlAggFunction implements CanAggregateDistinct { - - final boolean isDistinct; +public class HiveSqlCountAggFunction extends SqlAggFunction { final SqlReturnTypeInference returnTypeInference; final SqlOperandTypeInference operandTypeInference; final SqlOperandTypeChecker operandTypeChecker; - public HiveSqlCountAggFunction(boolean isDistinct, SqlReturnTypeInference returnTypeInference, + public HiveSqlCountAggFunction(SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker) { super( "count", @@ -57,17 +55,11 @@ public HiveSqlCountAggFunction(boolean isDistinct, SqlReturnTypeInference return operandTypeInference, operandTypeChecker, SqlFunctionCategory.NUMERIC); - this.isDistinct = isDistinct; this.returnTypeInference = returnTypeInference; this.operandTypeChecker = operandTypeChecker; this.operandTypeInference = operandTypeInference; } - @Override - public boolean isDistinct() { - return isDistinct; - } - @Override public SqlSyntax getSyntax() { return SqlSyntax.FUNCTION_STAR; @@ -91,7 +83,7 @@ class HiveCountSplitter extends CountSplitter { @Override public AggregateCall other(RelDataTypeFactory typeFactory, AggregateCall e) { return AggregateCall.create( - new HiveSqlCountAggFunction(isDistinct, returnTypeInference, operandTypeInference, operandTypeChecker), + new HiveSqlCountAggFunction(returnTypeInference, operandTypeInference, operandTypeChecker), false, ImmutableIntList.of(), -1, typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), true), "count"); } @@ -122,14 +114,14 @@ public AggregateCall topSplit(RexBuilder rexBuilder, } int ordinal = extra.register(node); return AggregateCall.create( - new HiveSqlSumEmptyIsZeroAggFunction(isDistinct, returnTypeInference, operandTypeInference, operandTypeChecker), + new HiveSqlSumEmptyIsZeroAggFunction(returnTypeInference, operandTypeInference, operandTypeChecker), false, ImmutableList.of(ordinal), -1, aggregateCall.type, aggregateCall.name); } } @Override public @Nullable SqlAggFunction getRollup() { - return new HiveSqlSumEmptyIsZeroAggFunction(isDistinct(), getReturnTypeInference(), getOperandTypeInference(), + return new HiveSqlSumEmptyIsZeroAggFunction(getReturnTypeInference(), getOperandTypeInference(), getOperandTypeChecker()); } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumAggFunction.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumAggFunction.java index 0b749c3e524c..20793ebdaf9b 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumAggFunction.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumAggFunction.java @@ -30,6 +30,7 @@ import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlSplittableAggFunction; import org.apache.calcite.sql.SqlSplittableAggFunction.SumSplitter; +import org.apache.calcite.sql.SqlSyntax; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.type.ReturnTypes; import org.apache.calcite.sql.type.SqlOperandTypeChecker; @@ -46,15 +47,14 @@ * long, float, double), and the result * is the same type. */ -public class HiveSqlSumAggFunction extends SqlAggFunction implements CanAggregateDistinct{ - final boolean isDistinct; +public class HiveSqlSumAggFunction extends SqlAggFunction { final SqlReturnTypeInference returnTypeInference; final SqlOperandTypeInference operandTypeInference; final SqlOperandTypeChecker operandTypeChecker; //~ Constructors ----------------------------------------------------------- - public HiveSqlSumAggFunction(boolean isDistinct, SqlReturnTypeInference returnTypeInference, + public HiveSqlSumAggFunction(SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker) { super( "sum", @@ -66,14 +66,9 @@ public HiveSqlSumAggFunction(boolean isDistinct, SqlReturnTypeInference returnTy this.returnTypeInference = returnTypeInference; this.operandTypeChecker = operandTypeChecker; this.operandTypeInference = operandTypeInference; - this.isDistinct = isDistinct; } //~ Methods ---------------------------------------------------------------- - @Override - public boolean isDistinct() { - return isDistinct; - } @Override public T unwrap(Class clazz) { @@ -89,7 +84,7 @@ class HiveSumSplitter extends SumSplitter { public AggregateCall other(RelDataTypeFactory typeFactory, AggregateCall e) { RelDataType countRetType = typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.BIGINT), true); return AggregateCall.create( - new HiveSqlCountAggFunction(isDistinct, ReturnTypes.explicit(countRetType), operandTypeInference, operandTypeChecker), + new HiveSqlCountAggFunction(ReturnTypes.explicit(countRetType), operandTypeInference, operandTypeChecker), false, ImmutableIntList.of(), -1, countRetType, "count"); } @@ -120,11 +115,16 @@ public AggregateCall topSplit(RexBuilder rexBuilder, throw new AssertionError("unexpected count " + merges); } int ordinal = extra.register(node); - return AggregateCall.create(new HiveSqlSumAggFunction(isDistinct, returnTypeInference, operandTypeInference, operandTypeChecker), + return AggregateCall.create(new HiveSqlSumAggFunction(returnTypeInference, operandTypeInference, operandTypeChecker), false, ImmutableList.of(ordinal), -1, aggregateCall.type, aggregateCall.name); } } + @Override + public SqlSyntax getSyntax() { + return SqlSyntax.FUNCTION_STAR; + } + @Override public SqlAggFunction getRollup() { return this; diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumEmptyIsZeroAggFunction.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumEmptyIsZeroAggFunction.java index 1d41dd2f0cc1..e9ff99cb41e0 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumEmptyIsZeroAggFunction.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/functions/HiveSqlSumEmptyIsZeroAggFunction.java @@ -42,7 +42,7 @@ public class HiveSqlSumEmptyIsZeroAggFunction extends SqlAggFunction { //~ Constructors ----------------------------------------------------------- - public HiveSqlSumEmptyIsZeroAggFunction(boolean isDistinct, SqlReturnTypeInference returnTypeInference, + public HiveSqlSumEmptyIsZeroAggFunction(SqlReturnTypeInference returnTypeInference, SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker) { super("$SUM0", null, diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateReduceFunctionsRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateReduceFunctionsRule.java index 731b34340fb9..2ae3da004087 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateReduceFunctionsRule.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveAggregateReduceFunctionsRule.java @@ -300,7 +300,6 @@ private RexNode reduceSum0( final AggregateCall sumCall = AggregateCall.create( new HiveSqlSumAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), oldCall.getAggregation().getOperandTypeInference(), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.SUM, @@ -346,7 +345,6 @@ private RexNode reduceAvg( final AggregateCall sumCall = AggregateCall.create( new HiveSqlSumAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), oldCall.getAggregation().getOperandTypeInference(), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.SUM, @@ -364,7 +362,6 @@ private RexNode reduceAvg( final AggregateCall countCall = AggregateCall.create( new HiveSqlCountAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(countRetType), oldCall.getAggregation().getOperandTypeInference(), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.COUNT, @@ -445,7 +442,6 @@ private RexNode reduceStddev( final AggregateCall sumArgSquaredAggCall = createAggregateCallWithBinding(typeFactory, new HiveSqlSumAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(sumSquaredReturnType), InferTypes.explicit(Collections.singletonList(argSquared.getType())), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.SUM, @@ -461,7 +457,6 @@ private RexNode reduceStddev( final AggregateCall sumArgAggCall = AggregateCall.create( new HiveSqlSumAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(sumReturnType), InferTypes.explicit(Collections.singletonList(argRef.getType())), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.SUM, @@ -490,7 +485,6 @@ private RexNode reduceStddev( final AggregateCall countArgAggCall = AggregateCall.create( new HiveSqlCountAggFunction( - oldCall.isDistinct(), ReturnTypes.explicit(countRetType), oldCall.getAggregation().getOperandTypeInference(), oldCall.getAggregation().getOperandTypeChecker()), //SqlStdOperatorTable.COUNT, diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveWindowingLastValueRewrite.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveWindowingLastValueRewrite.java index 6b166352bc7f..a8d86ca20973 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveWindowingLastValueRewrite.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveWindowingLastValueRewrite.java @@ -36,8 +36,8 @@ import org.apache.calcite.rex.RexWindow; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.fun.SqlBasicAggFunction; import org.apache.commons.collections.CollectionUtils; -import org.apache.hadoop.hive.ql.optimizer.calcite.translator.SqlFunctionConverter; /** * Rule to rewrite a window function containing a last value clause. @@ -103,9 +103,8 @@ public RexNode visitOver(RexOver over) { newOrderKeys.add(new RexFieldCollation(orderKey.left, flags)); } SqlAggFunction s = (SqlAggFunction) over.op; - SqlFunctionConverter.CalciteUDAF newSqlAggFunction = new SqlFunctionConverter.CalciteUDAF( - over.isDistinct(), FIRST_VALUE_FUNC, s.getReturnTypeInference(), s.getOperandTypeInference(), - s.getOperandTypeChecker()); + SqlAggFunction newSqlAggFunction = SqlBasicAggFunction.create( + FIRST_VALUE_FUNC, SqlKind.OTHER_FUNCTION, s.getReturnTypeInference(), s.getOperandTypeChecker()); List clonedOperands = visitList(over.operands, new boolean[] {false}); RexWindow window = visitWindow(over.getWindow()); return rexBuilder.makeOver(over.type, newSqlAggFunction, clonedOperands, diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/jdbc/JDBCAggregationPushDownRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/jdbc/JDBCAggregationPushDownRule.java index e21963912b40..2e282ca80143 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/jdbc/JDBCAggregationPushDownRule.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/jdbc/JDBCAggregationPushDownRule.java @@ -62,8 +62,7 @@ public boolean matches(RelOptRuleCall call) { SqlAggFunction f = relOptRuleOperand.getAggregation(); if (f instanceof HiveSqlCountAggFunction) { //count distinct with more that one argument is not supported - HiveSqlCountAggFunction countAgg = (HiveSqlCountAggFunction)f; - if (countAgg.isDistinct() && 1 < relOptRuleOperand.getArgList().size()) { + if (relOptRuleOperand.isDistinct() && 1 < relOptRuleOperand.getArgList().size()) { return false; } } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java index 8baa14c265ad..49afcc902030 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/ASTConverter.java @@ -66,6 +66,7 @@ import org.apache.calcite.rex.RexWindowBound; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlSyntax; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.type.SqlTypeName; import org.apache.calcite.util.ImmutableBitSet; @@ -1000,7 +1001,18 @@ public ASTNode visitOver(RexOver over) { } // 1. Translate the UDAF - final ASTNode wUDAFAst = visitCall(over); + ASTNode wUDAFAst = ASTBuilder.createAST(HiveParser.TOK_FUNCTION, "TOK_FUNCTION"); + if (over.getOperands().isEmpty() && over.op.getSyntax() == SqlSyntax.FUNCTION_STAR) { + wUDAFAst = ASTBuilder.createAST(HiveParser.TOK_FUNCTIONSTAR, "TOK_FUNCTIONSTAR"); + } + if (over.isDistinct()) { + wUDAFAst = ASTBuilder.createAST(HiveParser.TOK_FUNCTIONDI, "TOK_FUNCTIONDI"); + } + wUDAFAst.addChild(ASTBuilder.createAST(HiveParser.Identifier, over.op.getName())); + wUDAFAst.setTypeInfo(TypeConverter.convert(over.type)); + for (RexNode operand : over.getOperands()) { + wUDAFAst.addChild(operand.accept(this)); + } // 2. Add TOK_WINDOW as child of UDAF ASTNode wSpec = ASTBuilder.createAST(HiveParser.TOK_WINDOWSPEC, "TOK_WINDOWSPEC"); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java index 1875538e089f..7c0d29ec5607 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/PlanModifierForASTConv.java @@ -493,8 +493,7 @@ private static void replaceEmptyGroupAggr(final RelNode rel, RelNode parent) { RelDataType longType = TypeConverter.convert(TypeInfoFactory.longTypeInfo, typeFactory); RelDataType intType = TypeConverter.convert(TypeInfoFactory.intTypeInfo, typeFactory); // Create the dummy aggregation. - SqlAggFunction countFn = SqlFunctionConverter.getCalciteAggFn("count", false, - ImmutableList.of(intType), longType); + SqlAggFunction countFn = SqlFunctionConverter.getCalciteAggFn("count", ImmutableList.of(intType), longType); // TODO: Using 0 might be wrong; might need to walk down to find the // proper index of a dummy. List argList = ImmutableList.of(0); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/SqlFunctionConverter.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/SqlFunctionConverter.java index 156cc9d8ac07..80baf63740ff 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/SqlFunctionConverter.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/translator/SqlFunctionConverter.java @@ -27,6 +27,7 @@ import org.apache.calcite.sql.SqlFunctionCategory; import org.apache.calcite.sql.SqlKind; import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.fun.SqlBasicAggFunction; import org.apache.calcite.sql.fun.SqlMonotonicBinaryOperator; import org.apache.calcite.sql.fun.SqlStdOperatorTable; import org.apache.calcite.sql.type.InferTypes; @@ -44,7 +45,6 @@ import org.apache.hadoop.hive.ql.exec.FunctionRegistry; import org.apache.hadoop.hive.ql.exec.UDFArgumentException; import org.apache.hadoop.hive.ql.optimizer.calcite.CalciteSemanticException; -import org.apache.hadoop.hive.ql.optimizer.calcite.functions.CanAggregateDistinct; import org.apache.hadoop.hive.ql.optimizer.calcite.functions.HiveSqlAverageAggFunction; import org.apache.hadoop.hive.ql.optimizer.calcite.functions.HiveSqlCountAggFunction; import org.apache.hadoop.hive.ql.optimizer.calcite.functions.HiveSqlMinMaxAggFunction; @@ -272,21 +272,6 @@ public static ASTNode buildAST(SqlOperator op, List children, RelDataTy } else if (op.kind == SqlKind.PLUS_PREFIX) { node = (ASTNode) ParseDriver.adaptor.create(HiveParser.PLUS, "PLUS"); } else { - // Handle COUNT/SUM/AVG function for the case of COUNT(*) and COUNT(DISTINCT) - if (op instanceof HiveSqlCountAggFunction || - op instanceof HiveSqlSumAggFunction || - op instanceof HiveSqlAverageAggFunction) { - if (children.size() == 0) { - node = (ASTNode) ParseDriver.adaptor.create(HiveParser.TOK_FUNCTIONSTAR, - "TOK_FUNCTIONSTAR"); - } else { - CanAggregateDistinct distinctFunction = (CanAggregateDistinct) op; - if (distinctFunction.isDistinct()) { - node = (ASTNode) ParseDriver.adaptor.create(HiveParser.TOK_FUNCTIONDI, - "TOK_FUNCTIONDI"); - } - } - } node.addChild((ASTNode) ParseDriver.adaptor.create(HiveParser.Identifier, op.getName())); } } @@ -528,22 +513,6 @@ private static HiveToken hToken(int type, String text) { return new HiveToken(type, text); } - // UDAF is assumed to be deterministic - public static class CalciteUDAF extends SqlAggFunction implements CanAggregateDistinct { - private final boolean isDistinct; - public CalciteUDAF(boolean isDistinct, String opName, SqlReturnTypeInference returnTypeInference, - SqlOperandTypeInference operandTypeInference, SqlOperandTypeChecker operandTypeChecker) { - super(opName, SqlKind.OTHER_FUNCTION, returnTypeInference, operandTypeInference, - operandTypeChecker, SqlFunctionCategory.USER_DEFINED_FUNCTION); - this.isDistinct = isDistinct; - } - - @Override - public boolean isDistinct() { - return isDistinct; - } - } - private static class CalciteUDFInfo { private String udfName; private SqlReturnTypeInference returnTypeInference; @@ -596,8 +565,7 @@ public static SqlOperator getCalciteFn(String hiveUdfName, return calciteOp; } - public static SqlAggFunction getCalciteAggFn(String hiveUdfName, boolean isDistinct, - ImmutableList calciteArgTypes, RelDataType calciteRetType) { + public static SqlAggFunction getCalciteAggFn(String hiveUdfName, ImmutableList calciteArgTypes, RelDataType calciteRetType) { SqlAggFunction calciteAggFn = (SqlAggFunction) hiveToCalcite.get(hiveUdfName); if (calciteAggFn == null) { @@ -606,21 +574,18 @@ public static SqlAggFunction getCalciteAggFn(String hiveUdfName, boolean isDisti switch (hiveUdfName.toLowerCase()) { case "sum": calciteAggFn = new HiveSqlSumAggFunction( - isDistinct, udfInfo.returnTypeInference, udfInfo.operandTypeInference, udfInfo.operandTypeChecker); break; case "$sum0": calciteAggFn = new HiveSqlSumEmptyIsZeroAggFunction( - isDistinct, udfInfo.returnTypeInference, udfInfo.operandTypeInference, udfInfo.operandTypeChecker); break; case "count": calciteAggFn = new HiveSqlCountAggFunction( - isDistinct, udfInfo.returnTypeInference, udfInfo.operandTypeInference, udfInfo.operandTypeChecker); @@ -639,7 +604,6 @@ public static SqlAggFunction getCalciteAggFn(String hiveUdfName, boolean isDisti break; case "avg": calciteAggFn = new HiveSqlAverageAggFunction( - isDistinct, udfInfo.returnTypeInference, udfInfo.operandTypeInference, udfInfo.operandTypeChecker); @@ -680,11 +644,10 @@ public static SqlAggFunction getCalciteAggFn(String hiveUdfName, boolean isDisti udfInfo.operandTypeChecker); break; default: - calciteAggFn = new CalciteUDAF( - isDistinct, + calciteAggFn = SqlBasicAggFunction.create( udfInfo.udfName, + SqlKind.OTHER_FUNCTION, udfInfo.returnTypeInference, - udfInfo.operandTypeInference, udfInfo.operandTypeChecker); break; } diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java index 00e96773fa83..eb24496743fb 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java @@ -3542,7 +3542,7 @@ private AggregateCall convertGBAgg(AggregateInfo agg, List gbChildProjL // 3. Get Aggregation FN from Calcite given name, ret type and input arg // type - final SqlAggFunction aggregation = SqlFunctionConverter.getCalciteAggFn(agg.getAggregateName(), agg.isDistinct(), + final SqlAggFunction aggregation = SqlFunctionConverter.getCalciteAggFn(agg.getAggregateName(), aggArgRelDTBldr.build(), aggFnRetType); List collationList = new ArrayList<>(agg.getCollation().size()); @@ -4125,7 +4125,7 @@ private Pair genWindowingProj(WindowExpressionSpec wExpSpec, // 5. Get Calcite Agg Fn final SqlAggFunction calciteAggFn = SqlFunctionConverter.getCalciteAggFn( - hiveAggInfo.getAggregateName(), hiveAggInfo.isDistinct(), calciteAggFnArgsType, calciteAggFnRetType); + hiveAggInfo.getAggregateName(), calciteAggFnArgsType, calciteAggFnRetType); // 6. Translate Window spec RowResolver inputRR = relToHiveRR.get(srcRel);