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

    https://github.com/apache/drill/pull/377#discussion_r53540420
  
    --- Diff: 
exec/java-exec/src/main/java/org/apache/drill/exec/planner/sql/TypeInferenceUtils.java
 ---
    @@ -0,0 +1,196 @@
    +/**
    + * 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.Lists;
    +import com.google.common.collect.Sets;
    +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.SqlIntervalQualifier;
    +import org.apache.calcite.sql.SqlOperatorBinding;
    +import org.apache.calcite.sql.parser.SqlParserPos;
    +import org.apache.calcite.sql.type.SqlTypeName;
    +import org.apache.drill.common.expression.DumbLogicalExpression;
    +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.types.TypeProtos;
    +import org.apache.drill.common.types.Types;
    +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 java.util.List;
    +import java.util.Set;
    +
    +public class TypeInferenceUtils {
    +  public static final TypeProtos.MajorType NONE = 
TypeProtos.MajorType.getDefaultInstance();
    +  public static final int MAX_VARCHAR_LENGTH = 65535;
    +
    +  private static final Set<String> setFnWithDynamicTypes = 
Sets.newHashSet();
    +  static {
    +    setFnWithDynamicTypes.add("FALTTEN");
    +    setFnWithDynamicTypes.add("KVGEN");
    +    setFnWithDynamicTypes.add("CONVERT_FROM");
    +  }
    +
    +  public static RelDataType inferReturnType(final SqlOperatorBinding 
opBinding, final List<DrillFuncHolder> functions) {
    +    if(functions == null
    +        || functions.isEmpty()
    +            || 
setFnWithDynamicTypes.contains(opBinding.getOperator().getName().toUpperCase()))
 {
    +      return opBinding.getTypeFactory()
    +          
.createTypeWithNullability(opBinding.getTypeFactory().createSqlType(SqlTypeName.ANY),
 true);
    +    }
    +
    +    final RelDataTypeFactory factory = opBinding.getTypeFactory();
    +    final String name = opBinding.getOperator().getName().toUpperCase();
    +    if(name.equals("CONCAT")) {
    +    final RelDataType type = factory.createSqlType(SqlTypeName.VARCHAR, 
MAX_VARCHAR_LENGTH);
    +      if(opBinding.getOperandType(0).isNullable()) {
    +        return factory.createTypeWithNullability(type, true);
    +      } else {
    +        return type;
    +      }
    +    }
    +
    +    boolean allBooleanOutput = true;
    +    for(DrillFuncHolder function : functions) {
    +      if(function.getReturnType().getMinorType() != 
TypeProtos.MinorType.BIT) {
    +        allBooleanOutput = false;
    +        break;
    +      }
    +    }
    +    if(allBooleanOutput) {
    +      return opBinding.getTypeFactory().createSqlType(SqlTypeName.BOOLEAN);
    +    }
    +
    +    // 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 || 
type.getSqlTypeName() == SqlTypeName.DECIMAL) {
    +        return opBinding.getTypeFactory()
    +            
.createTypeWithNullability(opBinding.getTypeFactory().createSqlType(SqlTypeName.ANY),
 true);
    +      }
    +    }
    +
    +    final List<LogicalExpression> args = Lists.newArrayList();
    +    for(final RelDataType type : opBinding.collectOperandTypes()) {
    +      final TypeProtos.MajorType majorType = getMajorType(type);
    +      args.add(new DumbLogicalExpression(majorType));
    +    }
    +    final FunctionCall functionCall = new 
FunctionCall(opBinding.getOperator().getName(), args, 
ExpressionPosition.UNKNOWN);
    +    final FunctionResolver functionResolver = 
FunctionResolverFactory.getResolver();
    +    final DrillFuncHolder func = functionResolver.getBestMatch(functions, 
functionCall);
    +
    +    // If the return type is VarChar,
    +    // set the precision as the maximum
    +    RelDataType returnType = getReturnType(opBinding, func);
    +      if(returnType.getSqlTypeName() == SqlTypeName.VARCHAR) {
    +      final boolean isNullable = returnType.isNullable();
    +      returnType = factory.createSqlType(SqlTypeName.VARCHAR, 
MAX_VARCHAR_LENGTH);
    +
    +      if(isNullable) {
    +        returnType = factory.createTypeWithNullability(returnType, true);
    +      }
    +    }
    +
    +    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 anyType = factory.createSqlType(SqlTypeName.ANY);
    +    final RelDataType nullableAnyType = 
factory.createTypeWithNullability(anyType, true);
    +
    +    final TypeProtos.MajorType returnType = func.getReturnType();
    +    if (NONE.equals(returnType)) {
    +      return nullableAnyType;
    +    }
    +
    +    final TypeProtos.MinorType minorType = returnType.getMinorType();
    +    final SqlTypeName sqlTypeName = 
DrillConstExecutor.DRILL_TO_CALCITE_TYPE_MAPPING.get(minorType);
    +    if (sqlTypeName == null) {
    +      return factory.createTypeWithNullability(nullableAnyType, true);
    +    }
    +
    +    final RelDataType relReturnType;
    +    switch (sqlTypeName) {
    +      case INTERVAL_DAY_TIME:
    +        relReturnType = factory.createSqlIntervalType(
    +            new SqlIntervalQualifier(
    +                TimeUnit.DAY,
    +                    TimeUnit.MINUTE,
    --- End diff --
    
    Why only minutes, not seconds for INTERVAL_DAY_TIME?



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