Search in sources :

Example 1 with WritableConstantIntObjectInspector

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

the class MaskTransformer method getCharArg.

int getCharArg(ObjectInspector[] arguments, int index, int defaultValue) {
    int ret = defaultValue;
    ObjectInspector arg = (arguments != null && arguments.length > index) ? arguments[index] : null;
    if (arg != null) {
        if (arg instanceof WritableConstantIntObjectInspector) {
            IntWritable value = ((WritableConstantIntObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof WritableConstantLongObjectInspector) {
            LongWritable value = ((WritableConstantLongObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = (int) value.get();
            }
        } else if (arg instanceof WritableConstantShortObjectInspector) {
            ShortWritable value = ((WritableConstantShortObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof ConstantObjectInspector) {
            Object value = ((ConstantObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                String strValue = value.toString();
                if (strValue != null && strValue.length() > 0) {
                    ret = strValue.charAt(0);
                }
            }
        }
    }
    return ret;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 2 with WritableConstantIntObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector 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());
        }
        PrimitiveObjectInspector scaleOI = (PrimitiveObjectInspector) arguments[1];
        switch(scaleOI.getPrimitiveCategory()) {
            case VOID:
                break;
            case BYTE:
                if (!(scaleOI instanceof WritableConstantByteObjectInspector)) {
                    throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes constant");
                }
                scale = ((WritableConstantByteObjectInspector) scaleOI).getWritableConstantValue().get();
                break;
            case SHORT:
                if (!(scaleOI instanceof WritableConstantShortObjectInspector)) {
                    throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes constant");
                }
                scale = ((WritableConstantShortObjectInspector) scaleOI).getWritableConstantValue().get();
                break;
            case INT:
                if (!(scaleOI instanceof WritableConstantIntObjectInspector)) {
                    throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes constant");
                }
                scale = ((WritableConstantIntObjectInspector) scaleOI).getWritableConstantValue().get();
                break;
            case LONG:
                if (!(scaleOI instanceof WritableConstantLongObjectInspector)) {
                    throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes constant");
                }
                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;
                break;
            default:
                throw new UDFArgumentTypeException(1, getFuncName().toUpperCase() + " second argument only takes integer constant");
        }
    }
    inputType = inputOI.getPrimitiveCategory();
    ObjectInspector outputOI = null;
    switch(inputType) {
        case DECIMAL:
            DecimalTypeInfo inputTypeInfo = (DecimalTypeInfo) inputOI.getTypeInfo();
            DecimalTypeInfo typeInfo = getOutputTypeInfo(inputTypeInfo, scale);
            outputOI = PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(typeInfo);
            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) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) WritableConstantShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantShortObjectInspector)

Example 3 with WritableConstantIntObjectInspector

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

the class MaskTransformer method getIntArg.

int getIntArg(ObjectInspector[] arguments, int index, int defaultValue) {
    int ret = defaultValue;
    ObjectInspector arg = (arguments != null && arguments.length > index) ? arguments[index] : null;
    if (arg != null) {
        if (arg instanceof WritableConstantIntObjectInspector) {
            IntWritable value = ((WritableConstantIntObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof WritableConstantLongObjectInspector) {
            LongWritable value = ((WritableConstantLongObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = (int) value.get();
            }
        } else if (arg instanceof WritableConstantShortObjectInspector) {
            ShortWritable value = ((WritableConstantShortObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                ret = value.get();
            }
        } else if (arg instanceof ConstantObjectInspector) {
            Object value = ((ConstantObjectInspector) arg).getWritableConstantValue();
            if (value != null) {
                String strValue = value.toString();
                if (strValue != null && strValue.length() > 0) {
                    ret = Integer.parseInt(value.toString());
                }
            }
        }
    }
    return ret;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) IntWritable(org.apache.hadoop.io.IntWritable)

Example 4 with WritableConstantIntObjectInspector

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

the class GenericUDFGrouping method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (arguments.length != 2) {
        throw new UDFArgumentLengthException("grouping() requires 2 argument, got " + arguments.length);
    }
    if (arguments[0].getCategory() != Category.PRIMITIVE) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be primitive");
    }
    PrimitiveObjectInspector arg1OI = (PrimitiveObjectInspector) arguments[0];
    if (arg1OI.getPrimitiveCategory() != PrimitiveCategory.INT) {
        throw new UDFArgumentTypeException(0, "The first argument to grouping() must be an integer");
    }
    groupingIdOI = (IntObjectInspector) arguments[0];
    PrimitiveObjectInspector arg2OI = (PrimitiveObjectInspector) arguments[1];
    if (!(arg2OI instanceof WritableConstantIntObjectInspector)) {
        throw new UDFArgumentTypeException(1, "The second argument to grouping() must be a constant");
    }
    index = ((WritableConstantIntObjectInspector) arg2OI).getWritableConstantValue().get();
    return PrimitiveObjectInspectorFactory.writableByteObjectInspector;
}
Also used : 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) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)

Aggregations

ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)3 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)2 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)2 ShortWritable (org.apache.hadoop.hive.serde2.io.ShortWritable)2 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)2 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)2 WritableConstantIntObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantIntObjectInspector)2 IntWritable (org.apache.hadoop.io.IntWritable)2 LongWritable (org.apache.hadoop.io.LongWritable)2 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)1 WritableConstantByteObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantByteObjectInspector)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