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

    https://github.com/apache/drill/pull/397#discussion_r54924830
  
    --- 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(
    --- End diff --
    
    Move createCalciteTypeWithNullability to this class.


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