Search in sources :

Example 1 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project flink by apache.

the class HiveParserBaseSemanticAnalyzer method getHiveAggInfo.

public static AggInfo getHiveAggInfo(HiveParserASTNode aggAst, int aggFnLstArgIndx, HiveParserRowResolver inputRR, HiveParserWindowingSpec.WindowFunctionSpec winFuncSpec, HiveParserSemanticAnalyzer semanticAnalyzer, FrameworkConfig frameworkConfig, RelOptCluster cluster) throws SemanticException {
    AggInfo aInfo;
    // 1 Convert UDAF Params to ExprNodeDesc
    ArrayList<ExprNodeDesc> aggParameters = new ArrayList<>();
    for (int i = 1; i <= aggFnLstArgIndx; i++) {
        HiveParserASTNode paraExpr = (HiveParserASTNode) aggAst.getChild(i);
        ExprNodeDesc paraExprNode = semanticAnalyzer.genExprNodeDesc(paraExpr, inputRR);
        aggParameters.add(paraExprNode);
    }
    // 2. Is this distinct UDAF
    boolean isDistinct = aggAst.getType() == HiveASTParser.TOK_FUNCTIONDI;
    // 3. Determine type of UDAF
    TypeInfo udafRetType = null;
    // 3.1 Obtain UDAF name
    String aggName = unescapeIdentifier(aggAst.getChild(0).getText());
    boolean isAllColumns = false;
    // 3.2 Rank functions type is 'int'/'double'
    if (FunctionRegistry.isRankingFunction(aggName)) {
        if (aggName.equalsIgnoreCase("percent_rank")) {
            udafRetType = TypeInfoFactory.doubleTypeInfo;
        } else {
            udafRetType = TypeInfoFactory.intTypeInfo;
        }
        // set arguments for rank functions
        for (OrderExpression orderExpr : winFuncSpec.windowSpec.getOrder().getExpressions()) {
            aggParameters.add(semanticAnalyzer.genExprNodeDesc(orderExpr.getExpression(), inputRR));
        }
    } else {
        // 3.3 Try obtaining UDAF evaluators to determine the ret type
        try {
            isAllColumns = aggAst.getType() == HiveASTParser.TOK_FUNCTIONSTAR;
            // 3.3.1 Get UDAF Evaluator
            GenericUDAFEvaluator.Mode amode = HiveParserUtils.groupByDescModeToUDAFMode(GroupByDesc.Mode.COMPLETE, isDistinct);
            GenericUDAFEvaluator genericUDAFEvaluator;
            if (aggName.toLowerCase().equals(FunctionRegistry.LEAD_FUNC_NAME) || aggName.toLowerCase().equals(FunctionRegistry.LAG_FUNC_NAME)) {
                ArrayList<ObjectInspector> originalParameterTypeInfos = HiveParserUtils.getWritableObjectInspector(aggParameters);
                genericUDAFEvaluator = FunctionRegistry.getGenericWindowingEvaluator(aggName, originalParameterTypeInfos, isDistinct, isAllColumns);
                HiveParserBaseSemanticAnalyzer.GenericUDAFInfo udaf = HiveParserUtils.getGenericUDAFInfo(genericUDAFEvaluator, amode, aggParameters);
                udafRetType = ((ListTypeInfo) udaf.returnType).getListElementTypeInfo();
            } else {
                genericUDAFEvaluator = HiveParserUtils.getGenericUDAFEvaluator(aggName, aggParameters, aggAst, isDistinct, isAllColumns, frameworkConfig.getOperatorTable());
                // 3.3.2 Get UDAF Info using UDAF Evaluator
                HiveParserBaseSemanticAnalyzer.GenericUDAFInfo udaf = HiveParserUtils.getGenericUDAFInfo(genericUDAFEvaluator, amode, aggParameters);
                if (HiveParserUtils.pivotResult(aggName)) {
                    udafRetType = ((ListTypeInfo) udaf.returnType).getListElementTypeInfo();
                } else {
                    udafRetType = udaf.returnType;
                }
            }
        } catch (Exception e) {
            LOG.debug("CBO: Couldn't Obtain UDAF evaluators for " + aggName + ", trying to translate to GenericUDF");
        }
        // 3.4 Try GenericUDF translation
        if (udafRetType == null) {
            HiveParserTypeCheckCtx tcCtx = new HiveParserTypeCheckCtx(inputRR, frameworkConfig, cluster);
            // We allow stateful functions in the SELECT list (but nowhere else)
            tcCtx.setAllowStatefulFunctions(true);
            tcCtx.setAllowDistinctFunctions(false);
            ExprNodeDesc exp = semanticAnalyzer.genExprNodeDesc((HiveParserASTNode) aggAst.getChild(0), inputRR, tcCtx);
            udafRetType = exp.getTypeInfo();
        }
    }
    // 4. Construct AggInfo
    aInfo = new AggInfo(aggParameters, udafRetType, aggName, isDistinct, isAllColumns, null);
    return aInfo;
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) OrderExpression(org.apache.flink.table.planner.delegation.hive.copy.HiveParserPTFInvocationSpec.OrderExpression) GenericUDAFEvaluator(org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator) ArrayList(java.util.ArrayList) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) SemanticException(org.apache.hadoop.hive.ql.parse.SemanticException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) InvalidTableException(org.apache.hadoop.hive.ql.metadata.InvalidTableException) ExprNodeDesc(org.apache.hadoop.hive.ql.plan.ExprNodeDesc)

Example 2 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class ComparisonOpMethodResolver method getEvalMethod.

/*
   * (non-Javadoc)
   *
   * @see
   * org.apache.hadoop.hive.ql.exec.UDFMethodResolver#getEvalMethod(java.util
   * .List)
   */
@Override
public Method getEvalMethod(List<TypeInfo> argTypeInfos) throws UDFArgumentException {
    assert (argTypeInfos.size() == 2);
    List<TypeInfo> pTypeInfos = null;
    if (argTypeInfos.get(0).equals(TypeInfoFactory.voidTypeInfo) || argTypeInfos.get(1).equals(TypeInfoFactory.voidTypeInfo)) {
        pTypeInfos = new ArrayList<TypeInfo>();
        pTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
        pTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
    } else if (argTypeInfos.get(0).equals(TypeInfoFactory.booleanTypeInfo) && argTypeInfos.get(1).equals(TypeInfoFactory.booleanTypeInfo)) {
        pTypeInfos = new ArrayList<TypeInfo>();
        pTypeInfos.add(TypeInfoFactory.intTypeInfo);
        pTypeInfos.add(TypeInfoFactory.intTypeInfo);
    } else if (argTypeInfos.get(0) == argTypeInfos.get(1)) {
        pTypeInfos = argTypeInfos;
    } else {
        pTypeInfos = new ArrayList<TypeInfo>();
        pTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
        pTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
    }
    Method udfMethod = null;
    List<Method> evaluateMethods = new ArrayList<Method>();
    for (Method m : Arrays.asList(udfClass.getMethods())) {
        if (m.getName().equals("evaluate")) {
            evaluateMethods.add(m);
            List<TypeInfo> acceptedTypeInfos = TypeInfoUtils.getParameterTypeInfos(m, pTypeInfos.size());
            if (acceptedTypeInfos == null) {
                // null means the method does not accept number of arguments passed.
                continue;
            }
            boolean match = (acceptedTypeInfos.size() == pTypeInfos.size());
            for (int i = 0; i < pTypeInfos.size() && match; i++) {
                TypeInfo accepted = acceptedTypeInfos.get(i);
                if (accepted != pTypeInfos.get(i)) {
                    match = false;
                }
            }
            if (match) {
                if (udfMethod != null) {
                    throw new AmbiguousMethodException(udfClass, argTypeInfos, Arrays.asList(new Method[] { udfMethod, m }));
                } else {
                    udfMethod = m;
                }
            }
        }
    }
    if (udfMethod == null) {
        throw new NoMatchingMethodException(udfClass, argTypeInfos, evaluateMethods);
    }
    return udfMethod;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 3 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class NumericOpMethodResolver method getEvalMethod.

/*
   * (non-Javadoc)
   *
   * @see
   * org.apache.hadoop.hive.ql.exec.UDFMethodResolver#getEvalMethod(java.util
   * .List)
   */
@Override
public Method getEvalMethod(List<TypeInfo> argTypeInfos) throws UDFArgumentException {
    assert (argTypeInfos.size() == 2);
    List<TypeInfo> pTypeInfos = null;
    List<TypeInfo> modArgTypeInfos = new ArrayList<TypeInfo>();
    // in string form should always be convertible into either of those
    if (argTypeInfos.get(0).equals(TypeInfoFactory.stringTypeInfo) || argTypeInfos.get(1).equals(TypeInfoFactory.stringTypeInfo)) {
        // complete the operation in that type.
        if (argTypeInfos.get(0).equals(TypeInfoFactory.decimalTypeInfo) || argTypeInfos.get(1).equals(TypeInfoFactory.decimalTypeInfo)) {
            modArgTypeInfos.add(TypeInfoFactory.decimalTypeInfo);
            modArgTypeInfos.add(TypeInfoFactory.decimalTypeInfo);
        } else {
            modArgTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
            modArgTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
        }
    } else {
        // resolve to type T
        for (int i = 0; i < 2; i++) {
            if (argTypeInfos.get(i).equals(TypeInfoFactory.voidTypeInfo)) {
                modArgTypeInfos.add(TypeInfoFactory.byteTypeInfo);
            } else {
                modArgTypeInfos.add(argTypeInfos.get(i));
            }
        }
    }
    TypeInfo commonType = FunctionRegistry.getCommonClass(modArgTypeInfos.get(0), modArgTypeInfos.get(1));
    if (commonType == null) {
        throw new UDFArgumentException("Unable to find a common class between" + "types " + modArgTypeInfos.get(0).getTypeName() + " and " + modArgTypeInfos.get(1).getTypeName());
    }
    pTypeInfos = new ArrayList<TypeInfo>();
    pTypeInfos.add(commonType);
    pTypeInfos.add(commonType);
    Method udfMethod = null;
    for (Method m : Arrays.asList(udfClass.getMethods())) {
        if (m.getName().equals("evaluate")) {
            List<TypeInfo> argumentTypeInfos = TypeInfoUtils.getParameterTypeInfos(m, pTypeInfos.size());
            if (argumentTypeInfos == null) {
                // null means the method does not accept number of arguments passed.
                continue;
            }
            boolean match = (argumentTypeInfos.size() == pTypeInfos.size());
            for (int i = 0; i < pTypeInfos.size() && match; i++) {
                TypeInfo accepted = argumentTypeInfos.get(i);
                if (!accepted.accept(pTypeInfos.get(i))) {
                    match = false;
                }
            }
            if (match) {
                if (udfMethod != null) {
                    throw new AmbiguousMethodException(udfClass, argTypeInfos, Arrays.asList(new Method[] { udfMethod, m }));
                } else {
                    udfMethod = m;
                }
            }
        }
    }
    if (udfMethod == null) {
        throw new NoMatchingMethodException(udfClass, argTypeInfos, null);
    }
    return udfMethod;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 4 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class FunctionRegistry method getCommonClassForComparison.

/**
 * Find a common class that objects of both TypeInfo a and TypeInfo b can
 * convert to. This is used for comparing objects of type a and type b.
 *
 * When we are comparing string and double, we will always convert both of
 * them to double and then compare.
 *
 * @return null if no common class could be found.
 */
public static TypeInfo getCommonClassForComparison(TypeInfo a, TypeInfo b) {
    // If same return one of them
    if (a.equals(b)) {
        return a;
    }
    if (a.getCategory() != Category.PRIMITIVE || b.getCategory() != Category.PRIMITIVE) {
        // It is not primitive; check if it is a struct and we can infer a common class
        if (a.getCategory() == Category.STRUCT && b.getCategory() == Category.STRUCT) {
            return getCommonClassForStruct((StructTypeInfo) a, (StructTypeInfo) b, (type1, type2) -> getCommonClassForComparison(type1, type2));
        }
        return null;
    }
    PrimitiveCategory pcA = ((PrimitiveTypeInfo) a).getPrimitiveCategory();
    PrimitiveCategory pcB = ((PrimitiveTypeInfo) b).getPrimitiveCategory();
    if (pcA == pcB) {
        // Rely on getTypeInfoForPrimitiveCategory() to sort out the type params.
        return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, pcA);
    }
    if (pcA == PrimitiveCategory.VOID) {
        // Handle NULL, we return the type of pcB
        return b;
    }
    if (pcB == PrimitiveCategory.VOID) {
        // Handle NULL, we return the type of pcA
        return a;
    }
    PrimitiveGrouping pgA = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcA);
    PrimitiveGrouping pgB = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcB);
    if (pgA == pgB) {
        // grouping is same, but category is not.
        if (pgA == PrimitiveGrouping.DATE_GROUP) {
            Integer ai = TypeInfoUtils.dateTypes.get(pcA);
            Integer bi = TypeInfoUtils.dateTypes.get(pcB);
            return (ai > bi) ? a : b;
        }
    }
    // handle string types properly
    if (pgA == PrimitiveGrouping.STRING_GROUP && pgB == PrimitiveGrouping.STRING_GROUP) {
        // Compare as strings. Char comparison semantics may be different if/when implemented.
        return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, PrimitiveCategory.STRING);
    }
    // timestamp/date is higher precedence than String_GROUP
    if (pgA == PrimitiveGrouping.STRING_GROUP && pgB == PrimitiveGrouping.DATE_GROUP) {
        return b;
    }
    // date/timestamp is higher precedence than String_GROUP
    if (pgB == PrimitiveGrouping.STRING_GROUP && pgA == PrimitiveGrouping.DATE_GROUP) {
        return a;
    }
    // Another special case, because timestamp is not implicitly convertible to numeric types.
    if ((pgA == PrimitiveGrouping.NUMERIC_GROUP || pgB == PrimitiveGrouping.NUMERIC_GROUP) && (pcA == PrimitiveCategory.TIMESTAMP || pcB == PrimitiveCategory.TIMESTAMP)) {
        return TypeInfoFactory.doubleTypeInfo;
    }
    for (PrimitiveCategory t : TypeInfoUtils.numericTypeList) {
        if (TypeInfoUtils.implicitConvertible(pcA, t) && TypeInfoUtils.implicitConvertible(pcB, t)) {
            return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, t);
        }
    }
    return null;
}
Also used : UDFToInteger(org.apache.hadoop.hive.ql.udf.UDFToInteger) UDFXPathInteger(org.apache.hadoop.hive.ql.udf.xml.UDFXPathInteger) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) PrimitiveGrouping(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping)

Example 5 with TypeInfoFactory.doubleTypeInfo

use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.doubleTypeInfo in project hive by apache.

the class TestColumnPrunerProcCtx method setup.

@BeforeClass
public static void setup() {
    List<String> ns = new ArrayList<>();
    ns.add("a");
    ns.add("b");
    List<TypeInfo> tis = new ArrayList<>();
    TypeInfo aType = TypeInfoFactory.booleanTypeInfo;
    TypeInfo bType = TypeInfoFactory.doubleTypeInfo;
    tis.add(aType);
    tis.add(bType);
    col1Type = TypeInfoFactory.getStructTypeInfo(ns, tis);
    col2Type = TypeInfoFactory.doubleTypeInfo;
    List<String> names = new ArrayList<>();
    names.add("col1");
    names.add("col2");
    List<TypeInfo> typeInfos = new ArrayList<>();
    typeInfos.add(col1Type);
    typeInfos.add(col2Type);
    col3Type = TypeInfoFactory.getStructTypeInfo(names, typeInfos);
}
Also used : ArrayList(java.util.ArrayList) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) BeforeClass(org.junit.BeforeClass)

Aggregations

Test (org.junit.Test)35 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)34 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)31 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)29 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)29 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)29 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)12 ArrayList (java.util.ArrayList)7 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)7 HiveVarcharWritable (org.apache.hadoop.hive.serde2.io.HiveVarcharWritable)7 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)6 LongWritable (org.apache.hadoop.io.LongWritable)6 IntWritable (org.apache.hadoop.io.IntWritable)5 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)3 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)3 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)3 Method (java.lang.reflect.Method)2 Random (java.util.Random)2 SemanticException (org.apache.hadoop.hive.ql.parse.SemanticException)2 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)2