Github user hsuanyi commented on a diff in the pull request:

    https://github.com/apache/drill/pull/397#discussion_r54963651
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
 ---
    @@ -0,0 +1,568 @@
    +/**
    + * 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.ImmutableMap;
    +import com.google.common.collect.Lists;
    +
    +import com.google.common.collect.Maps;
    +import org.apache.calcite.avatica.util.TimeUnit;
    +import org.apache.calcite.rel.type.RelDataType;
    +import org.apache.calcite.rel.type.RelDataTypeFactory;
    +import org.apache.calcite.sql.SqlCallBinding;
    +import org.apache.calcite.sql.SqlCharStringLiteral;
    +import org.apache.calcite.sql.SqlDynamicParam;
    +import org.apache.calcite.sql.SqlLiteral;
    +import org.apache.calcite.sql.SqlNode;
    +import org.apache.calcite.sql.SqlOperatorBinding;
    +import org.apache.calcite.sql.type.SqlReturnTypeInference;
    +import org.apache.calcite.sql.type.SqlTypeName;
    +
    +import org.apache.drill.common.expression.ExpressionPosition;
    +import org.apache.drill.common.expression.FunctionCall;
    +import org.apache.drill.common.expression.LogicalExpression;
    +import org.apache.drill.common.expression.MajorTypeInLogicalExpression;
    +import org.apache.drill.common.exceptions.UserException;
    +import org.apache.drill.common.types.TypeProtos;
    +import org.apache.drill.common.types.Types;
    +import org.apache.drill.exec.expr.TypeHelper;
    +import org.apache.drill.exec.expr.fn.DrillFuncHolder;
    +import org.apache.drill.exec.planner.logical.DrillConstExecutor;
    +import org.apache.drill.exec.resolver.FunctionResolver;
    +import org.apache.drill.exec.resolver.FunctionResolverFactory;
    +import org.apache.drill.exec.resolver.TypeCastRules;
    +
    +import java.util.List;
    +import java.util.Map;
    +
    +public class TypeInferenceUtils {
    +  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(TypeInferenceUtils.class);
    +
    +  public static final TypeProtos.MajorType UNKNOWN_TYPE = 
TypeProtos.MajorType.getDefaultInstance();
    +  private static ImmutableMap<TypeProtos.MinorType, SqlTypeName> 
DRILL_TO_CALCITE_TYPE_MAPPING =
    +      ImmutableMap.<TypeProtos.MinorType, SqlTypeName> builder()
    +          .put(TypeProtos.MinorType.INT, SqlTypeName.INTEGER)
    +          .put(TypeProtos.MinorType.BIGINT, SqlTypeName.BIGINT)
    +          .put(TypeProtos.MinorType.FLOAT4, SqlTypeName.FLOAT)
    +          .put(TypeProtos.MinorType.FLOAT8, SqlTypeName.DOUBLE)
    +          .put(TypeProtos.MinorType.VARCHAR, SqlTypeName.VARCHAR)
    +          .put(TypeProtos.MinorType.BIT, SqlTypeName.BOOLEAN)
    +          .put(TypeProtos.MinorType.DATE, SqlTypeName.DATE)
    +          .put(TypeProtos.MinorType.DECIMAL9, SqlTypeName.DECIMAL)
    +          .put(TypeProtos.MinorType.DECIMAL18, SqlTypeName.DECIMAL)
    +          .put(TypeProtos.MinorType.DECIMAL28SPARSE, SqlTypeName.DECIMAL)
    +          .put(TypeProtos.MinorType.DECIMAL38SPARSE, SqlTypeName.DECIMAL)
    +          .put(TypeProtos.MinorType.TIME, SqlTypeName.TIME)
    +          .put(TypeProtos.MinorType.TIMESTAMP, SqlTypeName.TIMESTAMP)
    +          .put(TypeProtos.MinorType.VARBINARY, SqlTypeName.VARBINARY)
    +          .put(TypeProtos.MinorType.INTERVALYEAR, 
SqlTypeName.INTERVAL_YEAR_MONTH)
    +          .put(TypeProtos.MinorType.INTERVALDAY, 
SqlTypeName.INTERVAL_DAY_TIME)
    +          .put(TypeProtos.MinorType.MAP, SqlTypeName.MAP)
    +          .put(TypeProtos.MinorType.LIST, SqlTypeName.ARRAY)
    +          .put(TypeProtos.MinorType.LATE, SqlTypeName.ANY)
    +          .build();
    +
    +  private static ImmutableMap<SqlTypeName, TypeProtos.MinorType> 
CALCITE_TO_DRILL_MAPPING =
    +      ImmutableMap.<SqlTypeName, TypeProtos.MinorType> builder()
    +          .put(SqlTypeName.INTEGER, TypeProtos.MinorType.INT)
    +          .put(SqlTypeName.BIGINT, TypeProtos.MinorType.BIGINT)
    +          .put(SqlTypeName.FLOAT, TypeProtos.MinorType.FLOAT4)
    +          .put(SqlTypeName.DOUBLE, TypeProtos.MinorType.FLOAT8)
    +          .put(SqlTypeName.VARCHAR, TypeProtos.MinorType.VARCHAR)
    +          .put(SqlTypeName.BOOLEAN, TypeProtos.MinorType.BIT)
    +          .put(SqlTypeName.DATE, TypeProtos.MinorType.DATE)
    +          .put(SqlTypeName.TIME, TypeProtos.MinorType.TIME)
    +          .put(SqlTypeName.TIMESTAMP, TypeProtos.MinorType.TIMESTAMP)
    +          .put(SqlTypeName.VARBINARY, TypeProtos.MinorType.VARBINARY)
    +          .put(SqlTypeName.INTERVAL_YEAR_MONTH, 
TypeProtos.MinorType.INTERVALYEAR)
    +          .put(SqlTypeName.INTERVAL_DAY_TIME, 
TypeProtos.MinorType.INTERVALDAY)
    +          .put(SqlTypeName.CHAR, TypeProtos.MinorType.VARCHAR)
    +          .put(SqlTypeName.DECIMAL, TypeProtos.MinorType.FLOAT8)
    +          .build();
    +
    +  private static Map<String, SqlReturnTypeInference> funcNameToInference = 
Maps.newHashMap();
    +  static {
    +    funcNameToInference.put("DATE_PART", 
DrillDatePartSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("SUM", 
DrillSumSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("COUNT", 
DrillCountSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("CONCAT", 
DrillConcatSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("LENGTH", 
DrillLengthSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("LPAD", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("RPAD", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("LTRIM", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("RTRIM", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("BTRIM", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("TRIM", 
DrillPadTrimSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("CONVERT_TO", 
DrillConvertToSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("EXTRACT", 
DrillExtractSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("SQRT", 
DrillSqrtSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("CAST", 
DrillCastSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("FLATTEN", 
DrillDeferToExecSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("KVGEN", 
DrillDeferToExecSqlReturnTypeInference.INSTANCE);
    +    funcNameToInference.put("CONVERT_FROM", 
DrillDeferToExecSqlReturnTypeInference.INSTANCE);
    +  }
    +
    +  /**
    +   * Given a Drill's TypeProtos.MinorType, return a Calcite's 
corresponding SqlTypeName
    +   */
    +  public static SqlTypeName getCalciteTypeFromDrillType(final 
TypeProtos.MinorType type) {
    +    return DRILL_TO_CALCITE_TYPE_MAPPING.get(type);
    +  }
    +
    +  /**
    +   * Given a Calcite's RelDataType, return a Drill's corresponding 
TypeProtos.MinorType
    +   */
    +  public static TypeProtos.MinorType getDrillTypeFromCalciteType(final 
RelDataType relDataType) {
    +    final SqlTypeName sqlTypeName = relDataType.getSqlTypeName();
    +    TypeProtos.MinorType minorType = 
CALCITE_TO_DRILL_MAPPING.get(sqlTypeName);
    +    if(minorType == null) {
    +      minorType = TypeProtos.MinorType.LATE;
    +    }
    +    return minorType;
    +  }
    +
    +  /**
    +   * Give the name and DrillFuncHolder list, return the inference 
mechanism.
    +   */
    +  public static SqlReturnTypeInference getDrillSqlReturnTypeInference(
    +      final String name,
    +      final List<DrillFuncHolder> functions) {
    +
    +    final String nameCap = name.toUpperCase();
    +    if(funcNameToInference.containsKey(nameCap)) {
    +      return funcNameToInference.get(nameCap);
    +    } else {
    +      return new DrillDefaultSqlReturnTypeInference(functions);
    +    }
    +  }
    +
    +  private static class DrillDefaultSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private final List<DrillFuncHolder> functions;
    +
    +    public DrillDefaultSqlReturnTypeInference(List<DrillFuncHolder> 
functions) {
    +      this.functions = functions;
    +    }
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      if (functions.isEmpty()) {
    +        return factory.createTypeWithNullability(
    +            factory.createSqlType(SqlTypeName.ANY),
    +            true);
    +      }
    +
    +      // This code for boolean output type is added for addressing 
DRILL-1729
    +      // In summary, if we have a boolean output function in the 
WHERE-CLAUSE,
    +      // this logic can validate and execute user queries seamlessly
    +      boolean allBooleanOutput = true;
    +      for (DrillFuncHolder function : functions) {
    +        if (function.getReturnType().getMinorType() != 
TypeProtos.MinorType.BIT) {
    +          allBooleanOutput = false;
    +          break;
    +        }
    +      }
    +      if (allBooleanOutput) {
    +        return factory.createTypeWithNullability(
    +            factory.createSqlType(SqlTypeName.BOOLEAN), true);
    +      }
    +
    +      // The following logic is just a safe play:
    +      // Even if any of the input arguments has ANY type,
    +      // it "might" still be possible to determine the return type based 
on other non-ANY types
    +      for (RelDataType type : opBinding.collectOperandTypes()) {
    +        if (type.getSqlTypeName() == SqlTypeName.ANY) {
    +          return factory.createTypeWithNullability(
    +              factory.createSqlType(SqlTypeName.ANY),
    +              true);
    +        }
    +      }
    +
    +      final DrillFuncHolder func = resolveDrillFuncHolder(opBinding, 
functions);
    +      final RelDataType returnType = getReturnType(opBinding, func);
    +      return returnType;
    +    }
    +
    +    private static RelDataType getReturnType(final SqlOperatorBinding 
opBinding, final DrillFuncHolder func) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +
    +      // least restrictive type (nullable ANY type)
    +      final RelDataType nullableAnyType = 
factory.createTypeWithNullability(
    +          factory.createSqlType(SqlTypeName.ANY),
    +          true);
    +
    +      final TypeProtos.MajorType returnType = func.getReturnType();
    +      if (UNKNOWN_TYPE.equals(returnType)) {
    +        return nullableAnyType;
    +      }
    +
    +      final TypeProtos.MinorType minorType = returnType.getMinorType();
    +      final SqlTypeName sqlTypeName = 
getCalciteTypeFromDrillType(minorType);
    +      if (sqlTypeName == null) {
    +        return nullableAnyType;
    +      }
    +
    +      final boolean isNullable;
    +      switch (returnType.getMode()) {
    +        case REPEATED:
    +        case OPTIONAL:
    +          isNullable = true;
    +          break;
    +
    +        case REQUIRED:
    +          switch (func.getNullHandling()) {
    +            case INTERNAL:
    +              isNullable = false;
    +              break;
    +
    +            case NULL_IF_NULL:
    +              boolean isNull = false;
    +              for (int i = 0; i < opBinding.getOperandCount(); ++i) {
    +                if (opBinding.getOperandType(i).isNullable()) {
    +                  isNull = true;
    +                  break;
    +                }
    +              }
    +
    +              isNullable = isNull;
    +              break;
    +            default:
    +              throw new UnsupportedOperationException();
    +          }
    +          break;
    +
    +        default:
    +          throw new UnsupportedOperationException();
    +      }
    +
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          sqlTypeName,
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillDeferToExecSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillDeferToExecSqlReturnTypeInference INSTANCE = new 
DrillDeferToExecSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      return factory.createTypeWithNullability(
    +          factory.createSqlType(SqlTypeName.ANY),
    +          true);
    +    }
    +  }
    +
    +  private static class DrillSumSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillSumSqlReturnTypeInference INSTANCE = new 
DrillSumSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      // If there is group-by and the imput type is Non-nullable,
    +      // the output is Non-nullable;
    +      // Otherwise, the output is nullable.
    +      final boolean isNullable = opBinding.getGroupCount() == 0
    +          || opBinding.getOperandType(0).isNullable();
    +
    +      final SqlTypeName sqlTypeName = 
opBinding.getOperandType(0).getSqlTypeName();
    +      if(sqlTypeName == SqlTypeName.ANY) {
    +        return DrillConstExecutor.createCalciteTypeWithNullability(
    +            factory,
    +            SqlTypeName.ANY,
    +            isNullable);
    +      }
    +
    +      final TypeProtos.MinorType inputMinorType = 
getDrillTypeFromCalciteType(opBinding.getOperandType(0));
    +      
if(TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, 
TypeProtos.MinorType.BIGINT))
    +          == TypeProtos.MinorType.BIGINT) {
    +        return DrillConstExecutor.createCalciteTypeWithNullability(
    +            factory,
    +            SqlTypeName.BIGINT,
    +            isNullable);
    +      } else 
if(TypeCastRules.getLeastRestrictiveType(Lists.newArrayList(inputMinorType, 
TypeProtos.MinorType.FLOAT8))
    +          == TypeProtos.MinorType.FLOAT8) {
    +        return DrillConstExecutor.createCalciteTypeWithNullability(
    +            factory,
    +            SqlTypeName.DOUBLE,
    +            isNullable);
    +      } else {
    +        throw UserException
    +            .functionError()
    +            .message(String.format("%s does not support operand types 
(%s)",
    +                opBinding.getOperator().getName(),
    +                sqlTypeName))
    +            .build(logger);
    +      }
    +    }
    +  }
    +
    +  private static class DrillCountSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillCountSqlReturnTypeInference INSTANCE = new 
DrillCountSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final SqlTypeName type = SqlTypeName.BIGINT;
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          type,
    +          false);
    +    }
    +  }
    +
    +  private static class DrillConcatSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillConcatSqlReturnTypeInference INSTANCE = new 
DrillConcatSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +
    +      boolean isNullable = true;
    +      int precision = 0;
    +      for(RelDataType relDataType : opBinding.collectOperandTypes()) {
    +        if(!relDataType.isNullable()) {
    +          isNullable = false;
    +        }
    +
    +        // If the underlying columns cannot offer information regarding 
the precision (i.e., the length) of the VarChar,
    +        // Drill uses the largest to represent it
    +        if(relDataType.getPrecision() == 
TypeHelper.VARCHAR_DEFAULT_CAST_LEN
    +            || relDataType.getPrecision() == 
RelDataType.PRECISION_NOT_SPECIFIED) {
    +          precision = TypeHelper.VARCHAR_DEFAULT_CAST_LEN;
    +        } else {
    +          precision += relDataType.getPrecision();
    +        }
    +      }
    +
    +      return factory.createTypeWithNullability(
    +          factory.createSqlType(SqlTypeName.VARCHAR, precision),
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillLengthSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillLengthSqlReturnTypeInference INSTANCE = new 
DrillLengthSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final SqlTypeName sqlTypeName = SqlTypeName.BIGINT;
    +
    +      // We need to check only the first argument because
    +      // the second one is used to represent encoding type
    +      final boolean isNullable = opBinding.getOperandType(0).isNullable();
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          sqlTypeName,
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillPadTrimSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillPadTrimSqlReturnTypeInference INSTANCE = new 
DrillPadTrimSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final SqlTypeName sqlTypeName = SqlTypeName.VARCHAR;
    +
    +      for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    +        if(opBinding.getOperandType(i).isNullable()) {
    +          return DrillConstExecutor.createCalciteTypeWithNullability(
    +              factory, sqlTypeName, true);
    +        }
    +      }
    +
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory, sqlTypeName, false);
    +    }
    +  }
    +
    +  private static class DrillConvertToSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillConvertToSqlReturnTypeInference INSTANCE = new 
DrillConvertToSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final SqlTypeName type = SqlTypeName.VARBINARY;
    +
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory, type, opBinding.getOperandType(0).isNullable());
    +    }
    +  }
    +
    +  private static class DrillExtractSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillExtractSqlReturnTypeInference INSTANCE = new 
DrillExtractSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final TimeUnit timeUnit = 
opBinding.getOperandType(0).getIntervalQualifier().getStartUnit();
    +      final boolean isNullable = opBinding.getOperandType(1).isNullable();
    +
    +      final SqlTypeName sqlTypeName = 
getSqlTypeNameForTimeUnit(timeUnit.name());
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          sqlTypeName,
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillSqrtSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillSqrtSqlReturnTypeInference INSTANCE = new 
DrillSqrtSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final boolean isNullable = opBinding.getOperandType(0).isNullable();
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          SqlTypeName.DOUBLE,
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillDatePartSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillDatePartSqlReturnTypeInference INSTANCE = new 
DrillDatePartSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +
    +      final SqlNode firstOperand = ((SqlCallBinding) opBinding).operand(0);
    +      if(!(firstOperand instanceof SqlCharStringLiteral)) {
    +        return DrillConstExecutor.createCalciteTypeWithNullability(factory,
    +            SqlTypeName.ANY,
    +            opBinding.getOperandType(1).isNullable());
    +      }
    +
    +      final String part = ((SqlCharStringLiteral) firstOperand)
    +          .getNlsString()
    +          .getValue()
    +          .toUpperCase();
    +
    +      final SqlTypeName sqlTypeName = getSqlTypeNameForTimeUnit(part);
    +      final boolean isNullable = opBinding.getOperandType(1).isNullable();
    +      return DrillConstExecutor.createCalciteTypeWithNullability(
    +          factory,
    +          sqlTypeName,
    +          isNullable);
    +    }
    +  }
    +
    +  private static class DrillCastSqlReturnTypeInference implements 
SqlReturnTypeInference {
    +    private static DrillCastSqlReturnTypeInference INSTANCE = new 
DrillCastSqlReturnTypeInference();
    +
    +    @Override
    +    public RelDataType inferReturnType(SqlOperatorBinding opBinding) {
    +      final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +      final boolean isNullable = opBinding
    +          .getOperandType(0)
    +          .isNullable();
    +
    +      RelDataType ret = factory.createTypeWithNullability(
    +          opBinding.getOperandType(1),
    +          isNullable);
    +
    +      if (opBinding instanceof SqlCallBinding) {
    +        SqlCallBinding callBinding = (SqlCallBinding) opBinding;
    +        SqlNode operand0 = callBinding.operand(0);
    +
    +        // dynamic parameters and null constants need their types assigned
    +        // to them using the type they are casted to.
    +        if (((operand0 instanceof SqlLiteral)
    +                && (((SqlLiteral) operand0).getValue() == null))
    +                || (operand0 instanceof SqlDynamicParam)) {
    +          callBinding.getValidator().setValidatedNodeType(
    +                  operand0,
    +                  ret);
    +        }
    +      }
    +
    +      return ret;
    +    }
    +  }
    +
    +  private static DrillFuncHolder resolveDrillFuncHolder(final 
SqlOperatorBinding opBinding, final List<DrillFuncHolder> functions) {
    +    final List<LogicalExpression> args = Lists.newArrayList();
    +    for (final RelDataType type : opBinding.collectOperandTypes()) {
    +      final TypeProtos.MinorType minorType = 
getDrillTypeFromCalciteType(type);
    +      final TypeProtos.MajorType majorType;
    +      if (type.isNullable()) {
    +        majorType =  Types.optional(minorType);
    +      } else {
    +        majorType = Types.required(minorType);
    +      }
    +
    +      args.add(new MajorTypeInLogicalExpression(majorType));
    +    }
    +    final FunctionCall functionCall = new 
FunctionCall(opBinding.getOperator().getName(), args, 
ExpressionPosition.UNKNOWN);
    +    final FunctionResolver functionResolver = 
FunctionResolverFactory.getResolver();
    +    final DrillFuncHolder func = functionResolver.getBestMatch(functions, 
functionCall);
    +
    +    // Throw an exception
    +    // if no DrillFuncHolder matched for the given list of operand types
    +    if(func == null) {
    +      String operandTypes = "";
    +      for(int i = 0; i < opBinding.getOperandCount(); ++i) {
    +        operandTypes += opBinding.getOperandType(i).getSqlTypeName();
    +        if(i < opBinding.getOperandCount() - 1) {
    +          operandTypes += ",";
    +        }
    +      }
    +
    +      throw UserException
    +          .functionError()
    +          .message(String.format("%s does not support operand types (%s)",
    +              opBinding.getOperator().getName(),
    +              operandTypes))
    +          .build(logger);
    +    }
    +    return func;
    +  }
    +
    +  /**
    +   * This class is not intended to be initiated
    --- End diff --
    
    addressed


---
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.
---

Reply via email to