Github user sudheeshkatkam commented on a diff in the pull request: https://github.com/apache/drill/pull/377#discussion_r53538419 --- Diff: exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/DrillCalciteSqlAggFunctionWrapper.java --- @@ -0,0 +1,205 @@ +/** + * 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.sql; + +import com.google.common.collect.ArrayListMultimap; +import com.google.common.collect.Lists; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.sql.SqlAggFunction; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlCallBinding; +import org.apache.calcite.sql.SqlFunction; +import org.apache.calcite.sql.SqlFunctionCategory; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlOperandCountRange; +import org.apache.calcite.sql.SqlOperator; +import org.apache.calcite.sql.SqlOperatorBinding; +import org.apache.calcite.sql.SqlSyntax; +import org.apache.calcite.sql.SqlWriter; +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.sql.type.SqlTypeName; +import org.apache.calcite.sql.validate.SqlMonotonicity; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; +import org.apache.drill.exec.expr.fn.DrillFuncHolder; + +import java.util.List; + +public class DrillCalciteSqlAggFunctionWrapper extends SqlAggFunction implements DrillCalciteSqlWrapper { + private final SqlAggFunction operator; + private final ArrayListMultimap<String, SqlOperator> opMap; + private final RelDataType relDataType; + private final SqlOperandTypeChecker operandTypeChecker = new Checker(); + + @Override + public SqlOperator getOperator() { + return operator; + } + + private DrillCalciteSqlAggFunctionWrapper( + SqlAggFunction sqlAggFunction, + ArrayListMultimap<String, SqlOperator> opMap, + RelDataType relDataType) { + super(sqlAggFunction.getName(), + sqlAggFunction.getSqlIdentifier(), + sqlAggFunction.getKind(), + sqlAggFunction.getReturnTypeInference(), + sqlAggFunction.getOperandTypeInference(), + sqlAggFunction.getOperandTypeChecker(), + sqlAggFunction.getFunctionType(), + sqlAggFunction.requiresOrder(), + sqlAggFunction.requiresOver()); + this.operator = sqlAggFunction; + this.opMap = opMap; + this.relDataType = relDataType; + } + + public DrillCalciteSqlAggFunctionWrapper( + SqlAggFunction sqlAggFunction, + ArrayListMultimap<String, SqlOperator> opMap) { + this(sqlAggFunction, opMap, null); + } + + public DrillCalciteSqlAggFunctionWrapper( + SqlAggFunction sqlAggFunction, + RelDataType relDataType) { + this(sqlAggFunction, null, relDataType); + } + + @Override + public boolean validRexOperands(int count, boolean fail) { + return true; + } + + @Override + public String getAllowedSignatures(String opNameToUse) { + return operator.getAllowedSignatures(opNameToUse); + } + + @Override + public SqlOperandTypeInference getOperandTypeInference() { + return operator.getOperandTypeInference(); + } + + @Override + public boolean isAggregator() { + return operator.isAggregator(); + } + + @Override + public boolean allowsFraming() { + return operator.allowsFraming(); + } + + @Override + public SqlReturnTypeInference getReturnTypeInference() { + return operator.getReturnTypeInference(); + } + + @Override + public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) { + return operator.getMonotonicity(call); + } + + @Override + public boolean isDeterministic() { + return operator.isDeterministic(); + } + + @Override + public boolean isDynamicFunction() { + return operator.isDynamicFunction(); + } + + @Override + public boolean requiresDecimalExpansion() { + return operator.requiresDecimalExpansion(); + } + + @Override + public boolean argumentMustBeScalar(int ordinal) { + return operator.argumentMustBeScalar(ordinal); + } + + @Override + public SqlOperandTypeChecker getOperandTypeChecker() { + return operandTypeChecker; + } + + @Override + public SqlOperandCountRange getOperandCountRange() { + return operandTypeChecker.getOperandCountRange(); + } + + @Override + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { + if(relDataType != null) { + return relDataType; + } + + final List<DrillFuncHolder> functions = Lists.newArrayList(); + for(SqlOperator sqlOperator : opMap.get(getOperator().getName().toLowerCase())) { + if(sqlOperator instanceof DrillSqlAggOperator + && ((DrillSqlAggOperator) sqlOperator).getFunctions() != null) { + functions.addAll(((DrillSqlAggOperator) sqlOperator).getFunctions()); + } + } + + return TypeInferenceUtils.inferReturnType(opBinding, functions); + } + + @Override + public boolean checkOperandTypes( + SqlCallBinding callBinding, + boolean throwOnFailure) { + return true; --- End diff -- doc
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---