drill-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hsuanyi <...@git.apache.org>
Subject [GitHub] drill pull request: Drill 4372 for review
Date Tue, 23 Feb 2016 17:21:21 GMT
Github user hsuanyi commented on a diff in the pull request:

    https://github.com/apache/drill/pull/377#discussion_r53815227
  
    --- 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);
    --- End diff --
    
    If it is Null, a user exception should be thrown. I just enhanced the error message in
the new patch.
    
    Regarding implicit casting, it happens in DefaultFunctionResolver. For example, say, a
DrillFuncHolder Fn which accepts BIGINT, but we have INT in the operands. 
    
    DefaultFunctionResolver knows how to deal with this operands with implicit casting, as
opposed to just failing out due to type mismatch.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message