Search in sources :

Example 6 with ConstantObjectInspector

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

the class TestGenericUDFInternalInterval method testDayIntervalConstant.

@Test
public void testDayIntervalConstant() throws Exception {
    try (GenericUDFInternalInterval udf = new GenericUDFInternalInterval()) {
        ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(HiveParser.TOK_INTERVAL_DAY_LITERAL)), PrimitiveObjectInspectorFactory.getPrimitiveWritableConstantObjectInspector(TypeInfoFactory.intTypeInfo, new IntWritable(3)) };
        PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
        Assert.assertEquals(TypeInfoFactory.intervalDayTimeTypeInfo, oi.getTypeInfo());
        ConstantObjectInspector coi = (ConstantObjectInspector) oi;
        HiveIntervalDayTimeWritable res = (HiveIntervalDayTimeWritable) coi.getWritableConstantValue();
        Assert.assertEquals(3, res.getHiveIntervalDayTime().getDays());
    }
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveIntervalDayTimeWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) IntWritable(org.apache.hadoop.io.IntWritable) Test(org.junit.Test)

Example 7 with ConstantObjectInspector

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

Example 8 with ConstantObjectInspector

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

the class GenericUDFRegExp method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    checkArgsSize(arguments, 2, 2);
    checkArgPrimitive(arguments, 0);
    checkArgPrimitive(arguments, 1);
    checkArgGroups(arguments, 0, inputTypes, STRING_GROUP);
    checkArgGroups(arguments, 1, inputTypes, STRING_GROUP);
    obtainStringConverter(arguments, 0, inputTypes, converters);
    obtainStringConverter(arguments, 1, inputTypes, converters);
    if (arguments[1] instanceof ConstantObjectInspector) {
        regexConst = getConstantStringValue(arguments, 1);
        if (regexConst != null) {
            patternConst = Pattern.compile(regexConst);
        }
        isRegexConst = true;
    }
    ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
    return outputOI;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)

Example 9 with ConstantObjectInspector

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

the class GenericUDFLeadLag method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    if (!(arguments.length >= 1 && arguments.length <= 3)) {
        throw new UDFArgumentTypeException(arguments.length - 1, "Incorrect invocation of " + _getFnName() + ": _FUNC_(expr, amt, default)");
    }
    amt = 1;
    if (arguments.length > 1) {
        ObjectInspector amtOI = arguments[1];
        if (!ObjectInspectorUtils.isConstantObjectInspector(amtOI) || (amtOI.getCategory() != ObjectInspector.Category.PRIMITIVE) || ((PrimitiveObjectInspector) amtOI).getPrimitiveCategory() != PrimitiveObjectInspector.PrimitiveCategory.INT) {
            throw new UDFArgumentTypeException(1, _getFnName() + " amount must be a integer value " + amtOI.getTypeName() + " was passed as parameter 1.");
        }
        Object o = ((ConstantObjectInspector) amtOI).getWritableConstantValue();
        amt = ((IntWritable) o).get();
        if (amt < 0) {
            throw new UDFArgumentTypeException(1, " amount can not be nagative. Specified: " + amt);
        }
    }
    if (arguments.length == 3) {
        defaultValueConverter = ObjectInspectorConverters.getConverter(arguments[2], arguments[0]);
    }
    firstArgOI = arguments[0];
    return ObjectInspectorUtils.getStandardObjectInspector(firstArgOI, ObjectInspectorCopyOption.WRITABLE);
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) 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 10 with ConstantObjectInspector

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

the class GenericUDFSha2 method initialize.

@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
    checkArgsSize(arguments, 2, 2);
    checkArgPrimitive(arguments, 0);
    checkArgPrimitive(arguments, 1);
    // the function should support both string and binary input types
    checkArgGroups(arguments, 0, inputTypes, STRING_GROUP, BINARY_GROUP);
    checkArgGroups(arguments, 1, inputTypes, NUMERIC_GROUP);
    if (PrimitiveObjectInspectorUtils.getPrimitiveGrouping(inputTypes[0]) == STRING_GROUP) {
        obtainStringConverter(arguments, 0, inputTypes, converters);
        isStr = true;
    } else {
        GenericUDFParamUtils.obtainBinaryConverter(arguments, 0, inputTypes, converters);
        isStr = false;
    }
    if (arguments[1] instanceof ConstantObjectInspector) {
        Integer lenObj = getConstantIntValue(arguments, 1);
        if (lenObj != null) {
            int len = lenObj.intValue();
            if (len == 0) {
                len = 256;
            }
            try {
                digest = MessageDigest.getInstance("SHA-" + len);
            } catch (NoSuchAlgorithmException e) {
            // ignore
            }
        }
    } else {
        throw new UDFArgumentTypeException(1, getFuncName() + " only takes constant as " + getArgOrder(1) + " argument");
    }
    ObjectInspector outputOI = PrimitiveObjectInspectorFactory.writableStringObjectInspector;
    return outputOI;
}
Also used : ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) ConstantObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)36 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)25 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)19 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)14 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)12 ExprNodeConstantDesc (org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc)7 ArrayList (java.util.ArrayList)6 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)6 HiveException (org.apache.hadoop.hive.ql.metadata.HiveException)5 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)4 StructObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)4 IntWritable (org.apache.hadoop.io.IntWritable)4 Text (org.apache.hadoop.io.Text)4 ExprNodeDesc (org.apache.hadoop.hive.ql.plan.ExprNodeDesc)3 StringObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.primitive.StringObjectInspector)3 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)3 BytesWritable (org.apache.hadoop.io.BytesWritable)3 LongWritable (org.apache.hadoop.io.LongWritable)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 List (java.util.List)2