Search in sources :

Example 1 with WritableConstantByteObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantByteObjectInspector 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)

Aggregations

UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)1 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)1 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)1 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)1 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)1 WritableConstantByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantByteObjectInspector)1 WritableConstantIntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector)1 WritableConstantLongObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantLongObjectInspector)1 WritableConstantShortObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantShortObjectInspector)1 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)1