Search in sources :

Example 1 with MajorTypeInLogicalExpression

use of org.apache.drill.common.expression.MajorTypeInLogicalExpression in project drill by apache.

the class TypeCastRules method getCost.

/*
   * code decide whether it's legal to do implicit cast. -1 : not allowed for
   * implicit cast > 0: cost associated with implicit cast. ==0: parms are
   * exactly same type of arg. No need of implicit.
   */
public static int getCost(List<MajorType> argumentTypes, DrillFuncHolder holder) {
    int cost = 0;
    if (argumentTypes.size() != holder.getParamCount()) {
        return -1;
    }
    // Indicates whether we used secondary cast rules
    boolean secondaryCast = false;
    // number of arguments that could implicitly casts using precedence map or didn't require casting at all
    int nCasts = 0;
    /*
     * If we are determining function holder for decimal data type, we need to make sure the output type of
     * the function can fit the precision that we need based on the input types.
     */
    if (holder.checkPrecisionRange() == true) {
        List<LogicalExpression> logicalExpressions = Lists.newArrayList();
        for (MajorType majorType : argumentTypes) {
            logicalExpressions.add(new MajorTypeInLogicalExpression(majorType));
        }
        if (DecimalUtility.getMaxPrecision(holder.getReturnType().getMinorType()) < holder.getReturnType(logicalExpressions).getPrecision()) {
            return -1;
        }
    }
    final int numOfArgs = holder.getParamCount();
    for (int i = 0; i < numOfArgs; i++) {
        final MajorType argType = argumentTypes.get(i);
        final MajorType parmType = holder.getParmMajorType(i);
        //@Param FieldReader will match any type
        if (holder.isFieldReader(i)) {
            //        if (Types.isComplex(call.args.get(i).getMajorType()) ||Types.isRepeated(call.args.get(i).getMajorType()) )
            // add the max cost when encountered with a field reader considering that it is the most expensive factor
            // contributing to the cost.
            cost += ResolverTypePrecedence.MAX_IMPLICIT_CAST_COST;
            continue;
        //        else
        //          return -1;
        }
        if (!TypeCastRules.isCastableWithNullHandling(argType, parmType, holder.getNullHandling())) {
            return -1;
        }
        Integer parmVal = ResolverTypePrecedence.precedenceMap.get(parmType.getMinorType());
        Integer argVal = ResolverTypePrecedence.precedenceMap.get(argType.getMinorType());
        if (parmVal == null) {
            throw new RuntimeException(String.format("Precedence for type %s is not defined", parmType.getMinorType().name()));
        }
        if (argVal == null) {
            throw new RuntimeException(String.format("Precedence for type %s is not defined", argType.getMinorType().name()));
        }
        if (parmVal - argVal < 0) {
            /* Precedence rules does not allow to implicitly cast, however check
         * if the seconday rules allow us to cast
         */
            Set<MinorType> rules;
            if ((rules = (ResolverTypePrecedence.secondaryImplicitCastRules.get(parmType.getMinorType()))) != null && rules.contains(argType.getMinorType()) != false) {
                secondaryCast = true;
            } else {
                return -1;
            }
        }
        // Otherwise, the function implementation is not a match.
        if (argType.getMode() != parmType.getMode()) {
            // this allows for a non-nullable implementation to be preferred
            if (holder.getNullHandling() == NullHandling.INTERNAL) {
                // a function that expects required output, but nullable was provided
                if (parmType.getMode() == DataMode.REQUIRED && argType.getMode() == DataMode.OPTIONAL) {
                    return -1;
                } else if (parmType.getMode() == DataMode.OPTIONAL && argType.getMode() == DataMode.REQUIRED) {
                    cost += DATAMODE_CAST_COST;
                }
            }
        }
        int castCost;
        if ((castCost = (parmVal - argVal)) >= 0) {
            nCasts++;
            cost += castCost;
        }
    }
    if (secondaryCast) {
        // We have a secondary cast for one or more of the arguments, determine the cost associated
        int secondaryCastCost = Integer.MAX_VALUE - 1;
        // Subtract maximum possible implicit costs from the secondary cast cost
        secondaryCastCost -= (nCasts * (ResolverTypePrecedence.MAX_IMPLICIT_CAST_COST + DATAMODE_CAST_COST));
        // Add cost of implicitly casting the rest of the arguments that didn't use secondary casting
        secondaryCastCost += cost;
        return secondaryCastCost;
    }
    return cost;
}
Also used : MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) MinorType(org.apache.drill.common.types.TypeProtos.MinorType)

Example 2 with MajorTypeInLogicalExpression

use of org.apache.drill.common.expression.MajorTypeInLogicalExpression in project drill by apache.

the class TypeInferenceUtils method convertSqlOperatorBindingToFunctionCall.

/**
   * Given a SqlOperatorBinding, convert it to FunctionCall
   * @param  opBinding    the given SqlOperatorBinding
   * @return FunctionCall the converted FunctionCall
   */
public static FunctionCall convertSqlOperatorBindingToFunctionCall(final SqlOperatorBinding opBinding) {
    final List<LogicalExpression> args = Lists.newArrayList();
    for (int i = 0; i < opBinding.getOperandCount(); ++i) {
        final RelDataType type = opBinding.getOperandType(i);
        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 String drillFuncName = FunctionCallFactory.replaceOpWithFuncName(opBinding.getOperator().getName());
    final FunctionCall functionCall = new FunctionCall(drillFuncName, args, ExpressionPosition.UNKNOWN);
    return functionCall;
}
Also used : MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) MajorTypeInLogicalExpression(org.apache.drill.common.expression.MajorTypeInLogicalExpression) RelDataType(org.apache.calcite.rel.type.RelDataType) FunctionCall(org.apache.drill.common.expression.FunctionCall) TypeProtos(org.apache.drill.common.types.TypeProtos)

Aggregations

LogicalExpression (org.apache.drill.common.expression.LogicalExpression)2 MajorTypeInLogicalExpression (org.apache.drill.common.expression.MajorTypeInLogicalExpression)2 RelDataType (org.apache.calcite.rel.type.RelDataType)1 FunctionCall (org.apache.drill.common.expression.FunctionCall)1 TypeProtos (org.apache.drill.common.types.TypeProtos)1 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)1 MinorType (org.apache.drill.common.types.TypeProtos.MinorType)1