|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.drill.exec.planner.sql; |
| 19 | + |
| 20 | +import org.apache.calcite.rel.type.RelDataType; |
| 21 | +import org.apache.calcite.sql.SqlAggFunction; |
| 22 | +import org.apache.calcite.sql.SqlCall; |
| 23 | +import org.apache.calcite.sql.SqlCallBinding; |
| 24 | +import org.apache.calcite.sql.SqlOperator; |
| 25 | +import org.apache.calcite.sql.SqlOperatorBinding; |
| 26 | +import org.apache.calcite.sql.SqlSyntax; |
| 27 | +import org.apache.calcite.sql.fun.SqlSumEmptyIsZeroAggFunction; |
| 28 | +import org.apache.calcite.sql.type.SqlReturnTypeInference; |
| 29 | +import org.apache.calcite.sql.validate.SqlMonotonicity; |
| 30 | +import org.apache.calcite.sql.validate.SqlValidator; |
| 31 | +import org.apache.calcite.sql.validate.SqlValidatorScope; |
| 32 | +import org.apache.calcite.util.Litmus; |
| 33 | +import org.apache.calcite.util.Util; |
| 34 | +import org.apache.drill.exec.expr.fn.DrillFuncHolder; |
| 35 | + |
| 36 | +import java.util.List; |
| 37 | + |
| 38 | +/** |
| 39 | + * This class serves as a wrapper class for {@link SqlSumEmptyIsZeroAggFunction} |
| 40 | + * with the same goal as {@link DrillCalciteSqlAggFunctionWrapper} |
| 41 | + * but extends {@link SqlSumEmptyIsZeroAggFunction} to allow using |
| 42 | + * additional Calcite functionality designated for {@link SqlSumEmptyIsZeroAggFunction}. |
| 43 | + */ |
| 44 | +public class DrillCalciteSqlSumEmptyIsZeroAggFunctionWrapper |
| 45 | + extends SqlSumEmptyIsZeroAggFunction implements DrillCalciteSqlWrapper { |
| 46 | + |
| 47 | + private final SqlAggFunction operator; |
| 48 | + |
| 49 | + private final SqlReturnTypeInference sqlReturnTypeInference; |
| 50 | + |
| 51 | + private DrillCalciteSqlSumEmptyIsZeroAggFunctionWrapper( |
| 52 | + SqlSumEmptyIsZeroAggFunction sqlAggFunction, |
| 53 | + SqlReturnTypeInference sqlReturnTypeInference) { |
| 54 | + this.sqlReturnTypeInference = sqlReturnTypeInference; |
| 55 | + this.operator = sqlAggFunction; |
| 56 | + } |
| 57 | + |
| 58 | + public DrillCalciteSqlSumEmptyIsZeroAggFunctionWrapper( |
| 59 | + SqlSumEmptyIsZeroAggFunction sqlAggFunction, |
| 60 | + List<DrillFuncHolder> functions) { |
| 61 | + this(sqlAggFunction, |
| 62 | + TypeInferenceUtils.getDrillSqlReturnTypeInference( |
| 63 | + sqlAggFunction.getName(), |
| 64 | + functions)); |
| 65 | + } |
| 66 | + |
| 67 | + public DrillCalciteSqlSumEmptyIsZeroAggFunctionWrapper( |
| 68 | + SqlSumEmptyIsZeroAggFunction sqlAggFunction, |
| 69 | + RelDataType relDataType) { |
| 70 | + this(sqlAggFunction, opBinding -> relDataType); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public SqlOperator getOperator() { |
| 75 | + return operator; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public SqlReturnTypeInference getReturnTypeInference() { |
| 80 | + return this.sqlReturnTypeInference; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public RelDataType inferReturnType(SqlOperatorBinding opBinding) { |
| 85 | + if (this.sqlReturnTypeInference != null) { |
| 86 | + RelDataType returnType = this.sqlReturnTypeInference.inferReturnType(opBinding); |
| 87 | + if (returnType == null) { |
| 88 | + throw new IllegalArgumentException(String.format( |
| 89 | + "Cannot infer return type for %s; operand types: %s", |
| 90 | + opBinding.getOperator(), opBinding.collectOperandTypes())); |
| 91 | + } else { |
| 92 | + return returnType; |
| 93 | + } |
| 94 | + } else { |
| 95 | + throw Util.needToImplement(this); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean validRexOperands(int count, Litmus litmus) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public String getAllowedSignatures(String opNameToUse) { |
| 106 | + return operator.getAllowedSignatures(opNameToUse); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public boolean isAggregator() { |
| 111 | + return operator.isAggregator(); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean allowsFraming() { |
| 116 | + return operator.allowsFraming(); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public SqlMonotonicity getMonotonicity(SqlOperatorBinding call) { |
| 121 | + return operator.getMonotonicity(call); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public boolean isDeterministic() { |
| 126 | + return operator.isDeterministic(); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean isDynamicFunction() { |
| 131 | + return operator.isDynamicFunction(); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public boolean requiresDecimalExpansion() { |
| 136 | + return operator.requiresDecimalExpansion(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public boolean argumentMustBeScalar(int ordinal) { |
| 141 | + return operator.argumentMustBeScalar(ordinal); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public boolean checkOperandTypes( |
| 146 | + SqlCallBinding callBinding, |
| 147 | + boolean throwOnFailure) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public SqlSyntax getSyntax() { |
| 153 | + return operator.getSyntax(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public List<String> getParamNames() { |
| 158 | + return operator.getParamNames(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public String getSignatureTemplate(final int operandsCount) { |
| 163 | + return operator.getSignatureTemplate(operandsCount); |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public RelDataType deriveType( |
| 168 | + SqlValidator validator, |
| 169 | + SqlValidatorScope scope, |
| 170 | + SqlCall call) { |
| 171 | + return operator.deriveType(validator, scope, call); |
| 172 | + } |
| 173 | +} |
0 commit comments