use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo in project hive by apache.
the class NumericOpMethodResolver method getEvalMethod.
/*
* (non-Javadoc)
*
* @see
* org.apache.hadoop.hive.ql.exec.UDFMethodResolver#getEvalMethod(java.util
* .List)
*/
@Override
public Method getEvalMethod(List<TypeInfo> argTypeInfos) throws UDFArgumentException {
assert (argTypeInfos.size() == 2);
List<TypeInfo> pTypeInfos = null;
List<TypeInfo> modArgTypeInfos = new ArrayList<TypeInfo>();
// in string form should always be convertible into either of those
if (argTypeInfos.get(0).equals(TypeInfoFactory.stringTypeInfo) || argTypeInfos.get(1).equals(TypeInfoFactory.stringTypeInfo)) {
// complete the operation in that type.
if (argTypeInfos.get(0).equals(TypeInfoFactory.decimalTypeInfo) || argTypeInfos.get(1).equals(TypeInfoFactory.decimalTypeInfo)) {
modArgTypeInfos.add(TypeInfoFactory.decimalTypeInfo);
modArgTypeInfos.add(TypeInfoFactory.decimalTypeInfo);
} else {
modArgTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
modArgTypeInfos.add(TypeInfoFactory.doubleTypeInfo);
}
} else {
// resolve to type T
for (int i = 0; i < 2; i++) {
if (argTypeInfos.get(i).equals(TypeInfoFactory.voidTypeInfo)) {
modArgTypeInfos.add(TypeInfoFactory.byteTypeInfo);
} else {
modArgTypeInfos.add(argTypeInfos.get(i));
}
}
}
TypeInfo commonType = FunctionRegistry.getCommonClass(modArgTypeInfos.get(0), modArgTypeInfos.get(1));
if (commonType == null) {
throw new UDFArgumentException("Unable to find a common class between" + "types " + modArgTypeInfos.get(0).getTypeName() + " and " + modArgTypeInfos.get(1).getTypeName());
}
pTypeInfos = new ArrayList<TypeInfo>();
pTypeInfos.add(commonType);
pTypeInfos.add(commonType);
Method udfMethod = null;
for (Method m : Arrays.asList(udfClass.getMethods())) {
if (m.getName().equals("evaluate")) {
List<TypeInfo> argumentTypeInfos = TypeInfoUtils.getParameterTypeInfos(m, pTypeInfos.size());
if (argumentTypeInfos == null) {
// null means the method does not accept number of arguments passed.
continue;
}
boolean match = (argumentTypeInfos.size() == pTypeInfos.size());
for (int i = 0; i < pTypeInfos.size() && match; i++) {
TypeInfo accepted = argumentTypeInfos.get(i);
if (!accepted.accept(pTypeInfos.get(i))) {
match = false;
}
}
if (match) {
if (udfMethod != null) {
throw new AmbiguousMethodException(udfClass, argTypeInfos, Arrays.asList(new Method[] { udfMethod, m }));
} else {
udfMethod = m;
}
}
}
}
if (udfMethod == null) {
throw new NoMatchingMethodException(udfClass, argTypeInfos, null);
}
return udfMethod;
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo in project hive by apache.
the class TestGenericUDFNullif method testByteTypeEq.
@Test
public void testByteTypeEq() throws HiveException {
GenericUDFNullif udf = new GenericUDFNullif();
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableByteObjectInspector, PrimitiveObjectInspectorFactory.writableByteObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(new ByteWritable((byte) 4)), new DeferredJavaObject(new ByteWritable((byte) 4)) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.byteTypeInfo, oi.getTypeInfo());
ByteWritable res = (ByteWritable) udf.evaluate(args);
Assert.assertEquals(null, res);
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo in project hive by apache.
the class TestGenericUDFOPPositive method testByte.
@Test
public void testByte() throws HiveException {
GenericUDFOPPositive udf = new GenericUDFOPPositive();
ByteWritable input = new ByteWritable((byte) 4);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableByteObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(input) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.byteTypeInfo, oi.getTypeInfo());
ByteWritable res = (ByteWritable) udf.evaluate(args);
Assert.assertEquals((byte) 4, res.get());
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo in project hive by apache.
the class TestGenericUDFOPNegative method testByte.
@Test
public void testByte() throws HiveException {
GenericUDFOPNegative udf = new GenericUDFOPNegative();
ByteWritable input = new ByteWritable((byte) 4);
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableByteObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(input) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.byteTypeInfo, oi.getTypeInfo());
ByteWritable res = (ByteWritable) udf.evaluate(args);
Assert.assertEquals((byte) -4, res.get());
}
use of org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.byteTypeInfo in project hive by apache.
the class GenericUDFBaseCompare method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentException(opName + " requires two arguments.");
}
argumentOIs = arguments;
for (int i = 0; i < arguments.length; 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");
}
}
if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.stringTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.stringTypeInfo)) {
soi0 = (StringObjectInspector) arguments[0];
soi1 = (StringObjectInspector) arguments[1];
if (soi0.preferWritable() || soi1.preferWritable()) {
compareType = CompareType.COMPARE_TEXT;
} else {
compareType = CompareType.COMPARE_STRING;
}
} else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.intTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.intTypeInfo)) {
compareType = CompareType.COMPARE_INT;
ioi0 = (IntObjectInspector) arguments[0];
ioi1 = (IntObjectInspector) arguments[1];
} else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.longTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.longTypeInfo)) {
compareType = CompareType.COMPARE_LONG;
loi0 = (LongObjectInspector) arguments[0];
loi1 = (LongObjectInspector) arguments[1];
} else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.byteTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.byteTypeInfo)) {
compareType = CompareType.COMPARE_BYTE;
byoi0 = (ByteObjectInspector) arguments[0];
byoi1 = (ByteObjectInspector) arguments[1];
} else if (TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]).equals(TypeInfoFactory.booleanTypeInfo) && TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]).equals(TypeInfoFactory.booleanTypeInfo)) {
compareType = CompareType.COMPARE_BOOL;
boi0 = (BooleanObjectInspector) arguments[0];
boi1 = (BooleanObjectInspector) arguments[1];
} else {
TypeInfo oiTypeInfo0 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[0]);
TypeInfo oiTypeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(arguments[1]);
if (oiTypeInfo0 == oiTypeInfo1 || TypeInfoUtils.doPrimitiveCategoriesMatch(oiTypeInfo0, oiTypeInfo1)) {
compareType = CompareType.SAME_TYPE;
} else {
compareType = CompareType.NEED_CONVERT;
TypeInfo compareType = FunctionRegistry.getCommonClassForComparison(oiTypeInfo0, oiTypeInfo1);
// For now, we always convert to double if we can't find a common type
compareOI = TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo((compareType == null) ? TypeInfoFactory.doubleTypeInfo : compareType);
converter0 = ObjectInspectorConverters.getConverter(arguments[0], compareOI);
converter1 = ObjectInspectorConverters.getConverter(arguments[1], compareOI);
}
}
return PrimitiveObjectInspectorFactory.writableBooleanObjectInspector;
}
Aggregations