Search in sources :

Example 6 with PrimitiveGrouping

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.

the class FunctionRegistry method getPrimitiveCommonCategory.

public static PrimitiveCategory getPrimitiveCommonCategory(TypeInfo a, TypeInfo b) {
    if (a.getCategory() != Category.PRIMITIVE || b.getCategory() != Category.PRIMITIVE) {
        return null;
    }
    PrimitiveCategory pcA = ((PrimitiveTypeInfo) a).getPrimitiveCategory();
    PrimitiveCategory pcB = ((PrimitiveTypeInfo) b).getPrimitiveCategory();
    PrimitiveGrouping pgA = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcA);
    PrimitiveGrouping pgB = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcB);
    // handle string types properly
    if (pgA == PrimitiveGrouping.STRING_GROUP && pgB == PrimitiveGrouping.STRING_GROUP) {
        return PrimitiveCategory.STRING;
    }
    if (pgA == PrimitiveGrouping.DATE_GROUP && pgB == PrimitiveGrouping.STRING_GROUP) {
        return PrimitiveCategory.STRING;
    }
    if (pgB == PrimitiveGrouping.DATE_GROUP && pgA == PrimitiveGrouping.STRING_GROUP) {
        return PrimitiveCategory.STRING;
    }
    Integer ai = TypeInfoUtils.numericTypes.get(pcA);
    Integer bi = TypeInfoUtils.numericTypes.get(pcB);
    if (ai == null || bi == null) {
        // If either is not a numeric type, return null.
        return null;
    }
    return (ai > bi) ? pcA : pcB;
}
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 7 with PrimitiveGrouping

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.

the class FunctionRegistry method getCommonClassForUnionAll.

/**
   * Find a common type for union-all operator. Only the common types for the same
   * type group will resolve to a common type. No implicit conversion across different
   * type groups will be done.
   */
public static TypeInfo getCommonClassForUnionAll(TypeInfo a, TypeInfo b) {
    if (a.equals(b)) {
        return a;
    }
    if (a.getCategory() != Category.PRIMITIVE || b.getCategory() != Category.PRIMITIVE) {
        return null;
    }
    PrimitiveCategory pcA = ((PrimitiveTypeInfo) a).getPrimitiveCategory();
    PrimitiveCategory pcB = ((PrimitiveTypeInfo) b).getPrimitiveCategory();
    if (pcA == pcB) {
        // Same primitive category but different qualifiers.
        return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, pcA);
    }
    PrimitiveGrouping pgA = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcA);
    PrimitiveGrouping pgB = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(pcB);
    // untyped nulls
    if (pgA == PrimitiveGrouping.VOID_GROUP) {
        return b;
    }
    if (pgB == PrimitiveGrouping.VOID_GROUP) {
        return a;
    }
    if (pgA != pgB) {
        return null;
    }
    switch(pgA) {
        case STRING_GROUP:
            return getTypeInfoForPrimitiveCategory((PrimitiveTypeInfo) a, (PrimitiveTypeInfo) b, PrimitiveCategory.STRING);
        case NUMERIC_GROUP:
            return TypeInfoUtils.implicitConvertible(a, b) ? b : a;
        case DATE_GROUP:
            return TypeInfoFactory.timestampTypeInfo;
        default:
            return null;
    }
}
Also used : 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 8 with PrimitiveGrouping

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping in project hive by apache.

the class FunctionRegistry method filterMethodsByTypeAffinity.

/**
   * Given a set of candidate methods and list of argument types, try to
   * select the best candidate based on how close the passed argument types are
   * to the candidate argument types.
   * For a varchar argument, we would prefer evaluate(string) over evaluate(double).
   * @param udfMethods  list of candidate methods
   * @param argumentsPassed list of argument types to match to the candidate methods
   */
static void filterMethodsByTypeAffinity(List<Method> udfMethods, List<TypeInfo> argumentsPassed) {
    if (udfMethods.size() > 1) {
        // Prefer methods with a closer signature based on the primitive grouping of each argument.
        // Score each method based on its similarity to the passed argument types.
        int currentScore = 0;
        int bestMatchScore = 0;
        Method bestMatch = null;
        for (Method m : udfMethods) {
            currentScore = 0;
            List<TypeInfo> argumentsAccepted = TypeInfoUtils.getParameterTypeInfos(m, argumentsPassed.size());
            Iterator<TypeInfo> argsPassedIter = argumentsPassed.iterator();
            for (TypeInfo acceptedType : argumentsAccepted) {
                // Check the affinity of the argument passed in with the accepted argument,
                // based on the PrimitiveGrouping
                TypeInfo passedType = argsPassedIter.next();
                if (acceptedType.getCategory() == Category.PRIMITIVE && passedType.getCategory() == Category.PRIMITIVE) {
                    PrimitiveGrouping acceptedPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(((PrimitiveTypeInfo) acceptedType).getPrimitiveCategory());
                    PrimitiveGrouping passedPg = PrimitiveObjectInspectorUtils.getPrimitiveGrouping(((PrimitiveTypeInfo) passedType).getPrimitiveCategory());
                    if (acceptedPg == passedPg) {
                        // The passed argument matches somewhat closely with an accepted argument
                        ++currentScore;
                    }
                }
            }
            // Check if the score for this method is any better relative to others
            if (currentScore > bestMatchScore) {
                bestMatchScore = currentScore;
                bestMatch = m;
            } else if (currentScore == bestMatchScore) {
                // no longer a best match if more than one.
                bestMatch = null;
            }
        }
        if (bestMatch != null) {
            // Found a best match during this processing, use it.
            udfMethods.clear();
            udfMethods.add(bestMatch);
        }
    }
}
Also used : Method(java.lang.reflect.Method) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) PrimitiveGrouping(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping)

Aggregations

PrimitiveGrouping (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveGrouping)8 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)5 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)4 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)2 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)2 Method (java.lang.reflect.Method)1 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)1 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)1 UDFToInteger (org.apache.hadoop.hive.ql.udf.UDFToInteger)1 UDFXPathInteger (org.apache.hadoop.hive.ql.udf.xml.UDFXPathInteger)1 DateConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.DateConverter)1 ListTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo)1 MapTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo)1 StructTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo)1 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)1