use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFEncode method verifyEncode.
public void verifyEncode(String string, String charsetName) throws UnsupportedEncodingException, HiveException {
GenericUDFEncode udf = new GenericUDFEncode();
byte[] expected = string.getBytes(charsetName);
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector charsetOI = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArguments = { valueOI, charsetOI };
udf.initialize(initArguments);
DeferredObject valueObj = new DeferredJavaObject(string);
DeferredObject charsetObj = new DeferredJavaObject(charsetName);
DeferredObject[] arguments = { valueObj, charsetObj };
BytesWritable outputWritable = (BytesWritable) udf.evaluate(arguments);
byte[] output = outputWritable.getBytes();
assertTrue("Encoding failed for CharSet: " + charsetName, expected.length == outputWritable.getLength());
for (int i = 0; i < expected.length; i++) {
assertEquals("Encoding failed for CharSet: " + charsetName, expected[i], output[i]);
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFDateAdd method testStringToDate.
public void testStringToDate() throws HiveException {
GenericUDFDateAdd udf = new GenericUDFDateAdd();
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.javaIntObjectInspector;
ObjectInspector[] arguments = { valueOI1, valueOI2 };
udf.initialize(arguments);
DeferredObject valueObj1 = new DeferredJavaObject(new Text("2009-07-20 04:17:52"));
DeferredObject valueObj2 = new DeferredJavaObject(new Integer("2"));
DeferredObject[] args = { valueObj1, valueObj2 };
DateWritable output = (DateWritable) udf.evaluate(args);
assertEquals("date_add() test for STRING failed ", "2009-07-22", output.toString());
// Test with null args
args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
assertNull("date_add() 1st arg null", udf.evaluate(args));
args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
assertNull("date_add() 2nd arg null", udf.evaluate(args));
args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null) };
assertNull("date_add() both args null", udf.evaluate(args));
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFDateDiff method testStringToDate.
public void testStringToDate() throws HiveException {
GenericUDFDateDiff udf = new GenericUDFDateDiff();
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] arguments = { valueOI1, valueOI2 };
udf.initialize(arguments);
DeferredObject valueObj1 = new DeferredJavaObject(new Text("2009-07-20"));
DeferredObject valueObj2 = new DeferredJavaObject(new Text("2009-07-22"));
DeferredObject[] args = { valueObj1, valueObj2 };
IntWritable output = (IntWritable) udf.evaluate(args);
assertEquals("date_iff() test for STRING failed ", "-2", output.toString());
// Test with null args
args = new DeferredObject[] { new DeferredJavaObject(null), valueObj2 };
assertNull("date_add() 1st arg null", udf.evaluate(args));
args = new DeferredObject[] { valueObj1, new DeferredJavaObject(null) };
assertNull("date_add() 2nd arg null", udf.evaluate(args));
args = new DeferredObject[] { new DeferredJavaObject(null), new DeferredJavaObject(null) };
assertNull("date_add() both args null", udf.evaluate(args));
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project cdap by caskdata.
the class StandardObjectInspectorsTest method testStandardUnionObjectInspector.
@Test
public void testStandardUnionObjectInspector() throws Throwable {
try {
ArrayList<ObjectInspector> objectInspectors = new ArrayList<>();
// add primitive types
objectInspectors.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
objectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
objectInspectors.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
// add a list
objectInspectors.add(ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector));
// add a map
objectInspectors.add(ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaIntObjectInspector, PrimitiveObjectInspectorFactory.javaStringObjectInspector));
// add a struct
List<String> fieldNames = new ArrayList<>();
fieldNames.add("myDouble");
fieldNames.add("myLong");
ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<>();
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaLongObjectInspector);
objectInspectors.add(ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors));
StandardUnionObjectInspector uoi1 = ObjectInspectorFactory.getStandardUnionObjectInspector(objectInspectors);
StandardUnionObjectInspector uoi2 = ObjectInspectorFactory.getStandardUnionObjectInspector((ArrayList<ObjectInspector>) objectInspectors.clone());
Assert.assertEquals(uoi1, uoi2);
Assert.assertEquals(ObjectInspectorUtils.getObjectInspectorName(uoi1), ObjectInspectorUtils.getObjectInspectorName(uoi2));
Assert.assertTrue(ObjectInspectorUtils.compareTypes(uoi1, uoi2));
// compareSupported returns false because Union can contain
// an object of Map
Assert.assertFalse(ObjectInspectorUtils.compareSupported(uoi1));
// construct unionObjectInspector without Map field.
ArrayList<ObjectInspector> ois = (ArrayList<ObjectInspector>) objectInspectors.clone();
ois.set(4, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
Assert.assertTrue(ObjectInspectorUtils.compareSupported(ObjectInspectorFactory.getStandardUnionObjectInspector(ois)));
// metadata
Assert.assertEquals(Category.UNION, uoi1.getCategory());
List<? extends ObjectInspector> uois = uoi1.getObjectInspectors();
Assert.assertEquals(6, uois.size());
for (int i = 0; i < 6; i++) {
Assert.assertEquals(objectInspectors.get(i), uois.get(i));
}
StringBuilder unionTypeName = new StringBuilder();
unionTypeName.append("uniontype<");
for (int i = 0; i < uois.size(); i++) {
if (i > 0) {
unionTypeName.append(",");
}
unionTypeName.append(uois.get(i).getTypeName());
}
unionTypeName.append(">");
Assert.assertEquals(unionTypeName.toString(), uoi1.getTypeName());
// TypeInfo
TypeInfo typeInfo1 = TypeInfoUtils.getTypeInfoFromObjectInspector(uoi1);
Assert.assertEquals(Category.UNION, typeInfo1.getCategory());
Assert.assertEquals(UnionTypeInfo.class.getName(), typeInfo1.getClass().getName());
Assert.assertEquals(typeInfo1.getTypeName(), uoi1.getTypeName());
Assert.assertEquals(typeInfo1, TypeInfoUtils.getTypeInfoFromTypeString(uoi1.getTypeName()));
TypeInfo typeInfo2 = TypeInfoUtils.getTypeInfoFromObjectInspector(uoi2);
Assert.assertEquals(typeInfo1, typeInfo2);
Assert.assertEquals(TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(typeInfo1), TypeInfoUtils.getStandardJavaObjectInspectorFromTypeInfo(typeInfo2));
Assert.assertEquals(TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(typeInfo1), TypeInfoUtils.getStandardWritableObjectInspectorFromTypeInfo(typeInfo2));
// null
Assert.assertNull(uoi1.getField(null));
Assert.assertEquals(-1, uoi1.getTag(null));
// Union
UnionObject union = new StandardUnionObjectInspector.StandardUnion((byte) 0, 1);
Assert.assertEquals(0, uoi1.getTag(union));
Assert.assertEquals(1, uoi1.getField(union));
Assert.assertEquals("{0:1}", SerDeUtils.getJSONString(union, uoi1));
Assert.assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 0, 1), uoi2));
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(1));
union = new StandardUnionObjectInspector.StandardUnion((byte) 1, "two");
Assert.assertEquals(1, uoi1.getTag(union));
Assert.assertEquals("two", uoi1.getField(union));
Assert.assertEquals("{1:\"two\"}", SerDeUtils.getJSONString(union, uoi1));
Assert.assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 1, "two"), uoi2));
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals("two"));
union = new StandardUnionObjectInspector.StandardUnion((byte) 2, true);
Assert.assertEquals(2, uoi1.getTag(union));
Assert.assertEquals(true, uoi1.getField(union));
Assert.assertEquals("{2:true}", SerDeUtils.getJSONString(union, uoi1));
Assert.assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 2, true), uoi2));
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(true));
ArrayList<Integer> iList = new ArrayList<>();
iList.add(4);
iList.add(5);
union = new StandardUnionObjectInspector.StandardUnion((byte) 3, iList);
Assert.assertEquals(3, uoi1.getTag(union));
Assert.assertEquals(iList, uoi1.getField(union));
Assert.assertEquals("{3:[4,5]}", SerDeUtils.getJSONString(union, uoi1));
Assert.assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 3, iList.clone()), uoi2));
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(iList));
HashMap<Integer, String> map = new HashMap<>();
map.put(6, "six");
map.put(7, "seven");
map.put(8, "eight");
union = new StandardUnionObjectInspector.StandardUnion((byte) 4, map);
Assert.assertEquals(4, uoi1.getTag(union));
Assert.assertEquals(map, uoi1.getField(union));
Assert.assertEquals("{4:{6:\"six\",7:\"seven\",8:\"eight\"}}", SerDeUtils.getJSONString(union, uoi1));
Throwable th = null;
try {
ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 4, map.clone()), uoi2, null);
} catch (Throwable t) {
th = t;
}
Assert.assertNotNull(th);
Assert.assertEquals("Compare on map type not supported!", th.getMessage());
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(map));
ArrayList<Object> struct = new ArrayList<>(2);
struct.add(9.0);
struct.add(10L);
union = new StandardUnionObjectInspector.StandardUnion((byte) 5, struct);
Assert.assertEquals(5, uoi1.getTag(union));
Assert.assertEquals(struct, uoi1.getField(union));
Assert.assertEquals("{5:{\"mydouble\":9.0,\"mylong\":10}}", SerDeUtils.getJSONString(union, uoi1));
Assert.assertEquals(0, ObjectInspectorUtils.compare(union, uoi1, new StandardUnionObjectInspector.StandardUnion((byte) 5, struct.clone()), uoi2));
Assert.assertTrue(ObjectInspectorUtils.copyToStandardObject(union, uoi1).equals(struct));
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project cdap by caskdata.
the class StandardObjectInspectorsTest method testStandardMapObjectInspector.
@Test
public void testStandardMapObjectInspector() throws Throwable {
try {
StandardMapObjectInspector moi1 = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
StandardMapObjectInspector moi2 = ObjectInspectorFactory.getStandardMapObjectInspector(PrimitiveObjectInspectorFactory.javaStringObjectInspector, PrimitiveObjectInspectorFactory.javaIntObjectInspector);
Assert.assertEquals(moi1, moi2);
// metadata
Assert.assertEquals(Category.MAP, moi1.getCategory());
Assert.assertEquals(moi1.getMapKeyObjectInspector(), PrimitiveObjectInspectorFactory.javaStringObjectInspector);
Assert.assertEquals(moi2.getMapValueObjectInspector(), PrimitiveObjectInspectorFactory.javaIntObjectInspector);
// null
Assert.assertNull(moi1.getMap(null));
Assert.assertNull(moi1.getMapValueElement(null, null));
Assert.assertNull(moi1.getMapValueElement(null, "nokey"));
Assert.assertEquals(-1, moi1.getMapSize(null));
Assert.assertEquals("map<" + PrimitiveObjectInspectorFactory.javaStringObjectInspector.getTypeName() + "," + PrimitiveObjectInspectorFactory.javaIntObjectInspector.getTypeName() + ">", moi1.getTypeName());
// HashMap
HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
Assert.assertEquals(map, moi1.getMap(map));
Assert.assertEquals(3, moi1.getMapSize(map));
Assert.assertEquals(1, moi1.getMapValueElement(map, "one"));
Assert.assertEquals(2, moi1.getMapValueElement(map, "two"));
Assert.assertEquals(3, moi1.getMapValueElement(map, "three"));
Assert.assertNull(moi1.getMapValueElement(map, null));
Assert.assertNull(moi1.getMapValueElement(map, "null"));
// Settable
Object map3 = moi1.create();
moi1.put(map3, "one", 1);
moi1.put(map3, "two", 2);
moi1.put(map3, "three", 3);
Assert.assertEquals(map, map3);
moi1.clear(map3);
Assert.assertEquals(0, moi1.getMapSize(map3));
} catch (Throwable e) {
e.printStackTrace();
throw e;
}
}
Aggregations