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());
}
}
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;
}
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;
}
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);
}
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;
}
Aggregations