Search in sources :

Example 51 with UDFArgumentException

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

the class GenericUDFToTimestampLocalTZ method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1) {
        throw new UDFArgumentLengthException("The function CAST as TIMESTAMP WITH LOCAL TIME ZONE requires at least one argument, got " + arguments.length);
    }
    try {
        argumentOI = (PrimitiveObjectInspector) arguments[0];
        switch(argumentOI.getPrimitiveCategory()) {
            case CHAR:
            case VARCHAR:
            case STRING:
            case DATE:
            case TIMESTAMP:
            case TIMESTAMPLOCALTZ:
                break;
            default:
                throw new UDFArgumentException("CAST as TIMESTAMP WITH LOCAL TIME ZONE only allows" + "string/date/timestamp/timestamp with time zone types");
        }
    } catch (ClassCastException e) {
        throw new UDFArgumentException("The function CAST as TIMESTAMP WITH LOCAL TIME ZONE takes only primitive types");
    }
    SettableTimestampLocalTZObjectInspector outputOI = (SettableTimestampLocalTZObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
    converter = new TimestampLocalTZConverter(argumentOI, outputOI);
    return outputOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) SettableTimestampLocalTZObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.SettableTimestampLocalTZObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) TimestampLocalTZConverter(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampLocalTZConverter)

Example 52 with UDFArgumentException

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

the class GenericUDFOPDTIMinus method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentException(opName + " requires two arguments.");
    }
    PrimitiveObjectInspector resultOI = null;
    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");
        }
    }
    inputOIs = new PrimitiveObjectInspector[] { (PrimitiveObjectInspector) arguments[0], (PrimitiveObjectInspector) arguments[1] };
    PrimitiveObjectInspector leftOI = inputOIs[0];
    PrimitiveObjectInspector rightOI = inputOIs[1];
    // Date - Int = Date
    if (checkArgs(PrimitiveCategory.INTERVAL_YEAR_MONTH, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
        minusOpType = OperationType.INTERVALYM_MINUS_INTERVALYM;
        intervalArg1Idx = 0;
        intervalArg2Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalYearMonthTypeInfo);
    } else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
        minusOpType = OperationType.DATE_MINUS_INTERVALYM;
        dtArg1Idx = 0;
        intervalArg1Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.dateTypeInfo);
    } else if (checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.INTERVAL_YEAR_MONTH)) {
        minusOpType = OperationType.TIMESTAMP_MINUS_INTERVALYM;
        dtArg1Idx = 0;
        intervalArg1Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.timestampTypeInfo);
    } else if (checkArgs(PrimitiveCategory.INTERVAL_DAY_TIME, PrimitiveCategory.INTERVAL_DAY_TIME)) {
        minusOpType = OperationType.INTERVALDT_MINUS_INTERVALDT;
        intervalArg1Idx = 0;
        intervalArg2Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalDayTimeTypeInfo);
    } else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INTERVAL_DAY_TIME) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.INTERVAL_DAY_TIME)) {
        minusOpType = OperationType.TIMESTAMP_MINUS_INTERVALDT;
        dtArg1Idx = 0;
        intervalArg1Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.timestampTypeInfo);
        dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
    } else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.DATE) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.TIMESTAMP) || checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.TIMESTAMP) || checkArgs(PrimitiveCategory.TIMESTAMP, PrimitiveCategory.DATE)) {
        // Operands converted to timestamp, result as interval day-time
        minusOpType = OperationType.TIMESTAMP_MINUS_TIMESTAMP;
        dtArg1Idx = 0;
        dtArg2Idx = 1;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.intervalDayTimeTypeInfo);
        dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
        dt2Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
    } else if (checkArgs(PrimitiveCategory.DATE, PrimitiveCategory.INT)) {
        minusOpType = OperationType.DATE_MINUS_INT;
        intervalArg1Idx = 1;
        dtArg1Idx = 0;
        resultOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(TypeInfoFactory.dateTypeInfo);
        dt1Converter = ObjectInspectorConverters.getConverter(leftOI, resultOI);
    } else {
        // Unsupported types - error
        List<TypeInfo> argTypeInfos = new ArrayList<TypeInfo>(2);
        argTypeInfos.add(leftOI.getTypeInfo());
        argTypeInfos.add(rightOI.getTypeInfo());
        throw new NoMatchingMethodException(this.getClass(), argTypeInfos, null);
    }
    return resultOI;
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) NoMatchingMethodException(org.apache.hadoop.hive.ql.exec.NoMatchingMethodException) PrimitiveCategory(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory) Category(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) ArrayList(java.util.ArrayList) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)

Example 53 with UDFArgumentException

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

the class GenericUDFRound method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length < 1 || arguments.length > 2) {
        throw new UDFArgumentLengthException("ROUND requires one or two argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "ROUND input only takes primitive types, got " + arguments[0].getTypeName());
    }
    inputOI = (PrimitiveObjectInspector) arguments[0];
    if (arguments.length == 2) {
        if (arguments[1].getCategory() != Category.PRIMITIVE) {
            throw new UDFArgumentTypeException(1, "ROUND second argument only takes primitive types, got " + arguments[1].getTypeName());
        }
        scaleOI = (PrimitiveObjectInspector) arguments[1];
        switch(scaleOI.getPrimitiveCategory()) {
            case VOID:
                break;
            case BYTE:
                if (scaleOI instanceof WritableConstantByteObjectInspector) {
                    scale = ((WritableConstantByteObjectInspector) scaleOI).getWritableConstantValue().get();
                } else {
                    constantScale = false;
                }
                break;
            case SHORT:
                if (scaleOI instanceof WritableConstantShortObjectInspector) {
                    scale = ((WritableConstantShortObjectInspector) scaleOI).getWritableConstantValue().get();
                } else {
                    constantScale = false;
                }
                break;
            case INT:
                if (scaleOI instanceof WritableConstantIntObjectInspector) {
                    scale = ((WritableConstantIntObjectInspector) scaleOI).getWritableConstantValue().get();
                } else {
                    constantScale = false;
                }
                break;
            case LONG:
                if (scaleOI instanceof WritableConstantLongObjectInspector) {
                    long l = ((WritableConstantLongObjectInspector) scaleOI).getWritableConstantValue().get();
                    if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
                        throw new UDFArgumentException(getFuncName().toUpperCase() + " scale argument out of allowed range");
                    }
                    scale = (int) l;
                } else {
                    constantScale = false;
                }
                break;
            default:
                throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes numeric type");
        }
    }
    inputType = inputOI.getPrimitiveCategory();
    ObjectInspector outputOI = null;
    switch(inputType) {
        case DECIMAL:
            DecimalTypeInfo inputTypeInfo = (DecimalTypeInfo) inputOI.getTypeInfo();
            DecimalTypeInfo typeInfo = getOutputTypeInfo(inputTypeInfo, scale);
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            if (!constantScale) {
                throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " scale argument for " + "decimal must be constant");
            }
            break;
        case VOID:
        case BYTE:
        case SHORT:
        case INT:
        case LONG:
        case FLOAT:
        case DOUBLE:
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(inputType);
            break;
        case STRING:
        case VARCHAR:
        case CHAR:
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(PrimitiveCategory.DOUBLE);
            converterFromString = ObjectInspectorConverters.getConverter(inputOI, outputOI);
            break;
        default:
            throw new UDFArgumentTypeException(0, "Only numeric or string group data types are allowed for ROUND function. Got " + inputType.name());
    }
    return outputOI;
}
Also used : WritableConstantByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantByteObjectInspector) UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) WritableConstantLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantLongObjectInspector) WritableConstantByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantByteObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) WritableConstantLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantLongObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) WritableConstantShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantShortObjectInspector) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) WritableConstantIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) WritableConstantShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantShortObjectInspector)

Example 54 with UDFArgumentException

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

the class TestGenericUDFNextDay method testNextDayErrorArg1.

@Test
public void testNextDayErrorArg1() throws HiveException {
    @SuppressWarnings("resource") GenericUDFNextDay udf = new GenericUDFNextDay();
    ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableLongObjectInspector;
    ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    ObjectInspector[] arguments = { valueOI0, valueOI1 };
    try {
        udf.initialize(arguments);
        assertTrue("UDFArgumentException expected", false);
    } catch (UDFArgumentException e) {
        assertEquals("next_day only takes STRING_GROUP, DATE_GROUP, VOID_GROUP types as 1st argument, got LONG", e.getMessage());
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Test(org.junit.Test)

Example 55 with UDFArgumentException

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

the class TestGenericUDFNextDay method testNextDayErrorArg2.

@Test
public void testNextDayErrorArg2() throws HiveException {
    @SuppressWarnings("resource") GenericUDFNextDay udf = new GenericUDFNextDay();
    ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.writableIntObjectInspector;
    ObjectInspector[] arguments = { valueOI0, valueOI1 };
    try {
        udf.initialize(arguments);
        assertTrue("UDFArgumentException expected", false);
    } catch (UDFArgumentException e) {
        assertEquals("next_day only takes STRING_GROUP, VOID_GROUP types as 2nd argument, got INT", e.getMessage());
    }
}
Also used : UDFArgumentException(org.apache.hadoop.hive.ql.exec.UDFArgumentException) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) Test(org.junit.Test)

Aggregations

UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)72 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)31 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)27 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)24 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)18 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)11 ArrayList (java.util.ArrayList)9 Category (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector.Category)7 TypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TypeInfo)7 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)6 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)6 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)6 Test (org.junit.Test)6 PrimitiveObjectInspectorConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter)5 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)5 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)4 HiveConf (org.apache.hadoop.hive.conf.HiveConf)3 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)3 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)3 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)3