Search in sources :

Example 6 with UDFArgumentTypeException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException in project hive by apache.

the class GenericUDFFormatNumber method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("The function FORMAT_NUMBER(X, D or F) needs two arguments.");
    }
    switch(arguments[0].getCategory()) {
        case PRIMITIVE:
            break;
        default:
            throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
    }
    switch(arguments[1].getCategory()) {
        case PRIMITIVE:
            break;
        default:
            throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
    }
    PrimitiveObjectInspector xObjectInspector = (PrimitiveObjectInspector) arguments[0];
    PrimitiveObjectInspector dObjectInspector = (PrimitiveObjectInspector) arguments[1];
    switch(xObjectInspector.getPrimitiveCategory()) {
        case VOID:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case DOUBLE:
        case FLOAT:
        case DECIMAL:
            break;
        default:
            throw new UDFArgumentTypeException(0, "Argument 1" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DOUBLE_TYPE_NAME + "\"" + " or \"" + serdeConstants.FLOAT_TYPE_NAME + "\"" + " or \"" + serdeConstants.DECIMAL_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
    }
    dType = dObjectInspector.getPrimitiveCategory();
    switch(dType) {
        case VOID:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
            break;
        case STRING:
            if (!(arguments[1] instanceof ConstantObjectInspector)) {
                throw new UDFArgumentTypeException(1, "Format string passed must be a constant STRING." + arguments[1].toString());
            }
            ConstantObjectInspector constantOI = (ConstantObjectInspector) arguments[1];
            String fValue = constantOI.getWritableConstantValue().toString();
            DecimalFormat dFormat = new DecimalFormat(fValue);
            numberFormat.applyPattern(dFormat.toPattern());
            break;
        default:
            throw new UDFArgumentTypeException(1, "Argument 2" + " of function FORMAT_NUMBER must be \"" + serdeConstants.TINYINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.SMALLINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.INT_TYPE_NAME + "\"" + " or \"" + serdeConstants.BIGINT_TYPE_NAME + "\"" + " or \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[1].getTypeName() + "\" was found.");
    }
    argumentOIs = arguments;
    return PrimitiveObjectInspectorFactory.writableStringObjectInspector;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) DecimalFormat(java.text.DecimalFormat) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 7 with UDFArgumentTypeException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException in project hive by apache.

the class GenericUDFIf method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    argumentOIs = arguments;
    returnOIResolver = new GenericUDFUtils.ReturnObjectInspectorResolver(true);
    if (arguments.length != 3) {
        throw new UDFArgumentLengthException("The function IF(expr1,expr2,expr3) accepts exactly 3 arguments.");
    }
    boolean conditionTypeIsOk = (arguments[0].getCategory() == ObjectInspector.Category.PRIMITIVE);
    if (conditionTypeIsOk) {
        PrimitiveObjectInspector poi = ((PrimitiveObjectInspector) arguments[0]);
        conditionTypeIsOk = (poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.BOOLEAN || poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.VOID);
    }
    if (!conditionTypeIsOk) {
        throw new UDFArgumentTypeException(0, "The first argument of function IF should be \"" + serdeConstants.BOOLEAN_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" is found");
    }
    if (!(returnOIResolver.update(arguments[1]) && returnOIResolver.update(arguments[2]))) {
        throw new UDFArgumentTypeException(2, "The second and the third arguments of function IF should have the same type, " + "but they are different: \"" + arguments[1].getTypeName() + "\" and \"" + arguments[2].getTypeName() + "\"");
    }
    return returnOIResolver.get();
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 8 with UDFArgumentTypeException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException in project hive by apache.

the class GenericUDFPower method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentException(opName + " requires two arguments.");
    }
    for (int i = 0; i < 2; i++) {
        Category category = arguments[i].getCategory();
        if (category != Category.PRIMITIVE) {
            throw new UDFArgumentTypeException(i, "The " + GenericUDFUtils.getOrdinal(i + 1) + " argument of " + opName + "  is expected to a " + Category.PRIMITIVE.toString().toLowerCase() + " type, but " + category.toString().toLowerCase() + " is found");
        }
    }
    baseOI = (PrimitiveObjectInspector) arguments[0];
    if (!FunctionRegistry.isNumericType(baseOI.getTypeInfo())) {
        throw new UDFArgumentTypeException(0, "The " + GenericUDFUtils.getOrdinal(1) + " argument of " + opName + "  is expected to a " + "numeric type, but " + baseOI.getTypeName() + " is found");
    }
    powerOI = (PrimitiveObjectInspector) arguments[1];
    if (!FunctionRegistry.isNumericType(powerOI.getTypeInfo())) {
        throw new UDFArgumentTypeException(1, "The " + GenericUDFUtils.getOrdinal(2) + " argument of " + opName + "  is expected to a " + "numeric type, but " + powerOI.getTypeName() + " is found");
    }
    baseConverter = ObjectInspectorConverters.getConverter(baseOI, PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
    powerConverter = ObjectInspectorConverters.getConverter(powerOI, PrimitiveObjectInspectorFactory.writableDoubleObjectInspector);
    return resultOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)

Example 9 with UDFArgumentTypeException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException in project hive by apache.

the class GenericUDFPrintf method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1) {
        throw new UDFArgumentLengthException("The function PRINTF(String format, Obj... args) needs at least one arguments.");
    }
    WritableStringObjectInspector resultOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    if (arguments[0].getCategory() == ObjectInspector.Category.PRIMITIVE) {
        PrimitiveObjectInspector poi = ((PrimitiveObjectInspector) arguments[0]);
        if (poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.STRING || poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.CHAR || poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.VARCHAR || poi.getPrimitiveCategory() == PrimitiveObjectInspector.PrimitiveCategory.VOID) {
            converterFormat = ObjectInspectorConverters.getConverter(arguments[0], resultOI);
        } else {
            throw new UDFArgumentTypeException(0, "Argument 1" + " of function PRINTF must be \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
        }
    } else {
        throw new UDFArgumentTypeException(0, "Argument 1" + " of function PRINTF must be \"" + serdeConstants.STRING_TYPE_NAME + "\", but \"" + arguments[0].getTypeName() + "\" was found.");
    }
    for (int i = 1; i < arguments.length; i++) {
        if (!arguments[i].getCategory().equals(Category.PRIMITIVE)) {
            throw new UDFArgumentTypeException(i, "Argument " + (i + 1) + " of function PRINTF must be \"" + Category.PRIMITIVE + "\", but \"" + arguments[i].getTypeName() + "\" was found.");
        }
    }
    argumentOIs = arguments;
    return resultOI;
}
Also used : WritableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableStringObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Example 10 with UDFArgumentTypeException

use of org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException in project hive by apache.

the class GenericUDFReflect2 method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 2) {
        throw new UDFArgumentLengthException("The function GenericUDFReflect2(arg0,method[,arg1[,arg2]...])" + " accepts 2 or more arguments.");
    }
    if (arguments[0].getCategory() != ObjectInspector.Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(1, "The target instance should be a primitive type.");
    }
    targetOI = (PrimitiveObjectInspector) arguments[0];
    if (!(arguments[1] instanceof StringObjectInspector)) {
        throw new UDFArgumentTypeException(1, "The method name should be string type.");
    }
    if (!(arguments[1] instanceof ConstantObjectInspector)) {
        throw new UDFArgumentTypeException(1, "The method name should be a constant.");
    }
    Text methodName = (Text) ((ConstantObjectInspector) arguments[1]).getWritableConstantValue();
    if (methodName.toString().equals("hashCode") && arguments.length == 2) {
        // it's non-deterministic
        throw new UDFArgumentTypeException(1, "Use hash() UDF instead of this.");
    }
    setupParameterOIs(arguments, 2);
    Class<?> targetClass = PrimitiveObjectInspectorUtils.getTypeEntryFromPrimitiveCategory(targetOI.getPrimitiveCategory()).primitiveJavaClass;
    try {
        method = findMethod(targetClass, methodName.toString(), null, true);
        // Note: type param is not available here.
        PrimitiveTypeEntry typeEntry = getTypeFor(method.getReturnType());
        returnOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeEntry.primitiveCategory);
        returnObj = (Writable) returnOI.getPrimitiveWritableClass().newInstance();
    } catch (Exception e) {
        throw new UDFArgumentException(e);
    }
    return returnOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) PrimitiveTypeEntry(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils.PrimitiveTypeEntry) Text(org.apache.hadoop.io.Text) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) StringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector) UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) InvocationTargetException(java.lang.reflect.InvocationTargetException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException)

Aggregations

UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)56 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)29 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)28 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)20 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)15 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)14 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)12 Category (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category)8 ListObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector)5 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)5 Converter (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorConverters.Converter)4 ParseException (java.text.ParseException)3 ArrayList (java.util.ArrayList)3 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)3 MapObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector)3 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)3 TimestampConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter)3 VoidObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.VoidObjectInspector)3 Timestamp (java.sql.Timestamp)2 Date (java.util.Date)2