use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project cdap by caskdata.
the class StandardObjectInspectorsTest method doStandardObjectInspectorTest.
private void doStandardObjectInspectorTest(boolean testComments) {
ArrayList<String> fieldNames = new ArrayList<>();
fieldNames.add("firstInteger");
fieldNames.add("secondString");
fieldNames.add("thirdBoolean");
ArrayList<ObjectInspector> fieldObjectInspectors = new ArrayList<>();
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaIntObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaStringObjectInspector);
fieldObjectInspectors.add(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector);
ArrayList<String> fieldComments = new ArrayList<>(3);
if (testComments) {
fieldComments.add("firstInteger comment");
fieldComments.add("secondString comment");
fieldComments.add("thirdBoolean comment");
} else {
// should have null for non-specified comments
for (int i = 0; i < 3; i++) {
fieldComments.add(null);
}
}
StandardStructObjectInspector soi1 = testComments ? ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors, fieldComments) : ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldObjectInspectors);
StandardStructObjectInspector soi2 = testComments ? ObjectInspectorFactory.getStandardStructObjectInspector((ArrayList<String>) fieldNames.clone(), (ArrayList<ObjectInspector>) fieldObjectInspectors.clone(), (ArrayList<String>) fieldComments.clone()) : ObjectInspectorFactory.getStandardStructObjectInspector((ArrayList<String>) fieldNames.clone(), (ArrayList<ObjectInspector>) fieldObjectInspectors.clone());
Assert.assertEquals(soi1, soi2);
// metadata
Assert.assertEquals(Category.STRUCT, soi1.getCategory());
List<? extends StructField> fields = soi1.getAllStructFieldRefs();
Assert.assertEquals(3, fields.size());
for (int i = 0; i < 3; i++) {
Assert.assertEquals(fieldNames.get(i).toLowerCase(), fields.get(i).getFieldName());
Assert.assertEquals(fieldObjectInspectors.get(i), fields.get(i).getFieldObjectInspector());
Assert.assertEquals(fieldComments.get(i), fields.get(i).getFieldComment());
}
Assert.assertEquals(fields.get(1), soi1.getStructFieldRef("secondString"));
StringBuilder structTypeName = new StringBuilder();
structTypeName.append("struct<");
for (int i = 0; i < fields.size(); i++) {
if (i > 0) {
structTypeName.append(",");
}
structTypeName.append(fields.get(i).getFieldName());
structTypeName.append(":");
structTypeName.append(fields.get(i).getFieldObjectInspector().getTypeName());
}
structTypeName.append(">");
Assert.assertEquals(structTypeName.toString(), soi1.getTypeName());
// null
Assert.assertNull(soi1.getStructFieldData(null, fields.get(0)));
Assert.assertNull(soi1.getStructFieldData(null, fields.get(1)));
Assert.assertNull(soi1.getStructFieldData(null, fields.get(2)));
Assert.assertNull(soi1.getStructFieldsDataAsList(null));
// HashStruct
ArrayList<Object> struct = new ArrayList<>(3);
struct.add(1);
struct.add("two");
struct.add(true);
Assert.assertEquals(1, soi1.getStructFieldData(struct, fields.get(0)));
Assert.assertEquals("two", soi1.getStructFieldData(struct, fields.get(1)));
Assert.assertEquals(true, soi1.getStructFieldData(struct, fields.get(2)));
// Settable
Object struct3 = soi1.create();
soi1.setStructFieldData(struct3, fields.get(0), 1);
soi1.setStructFieldData(struct3, fields.get(1), "two");
soi1.setStructFieldData(struct3, fields.get(2), true);
Assert.assertEquals(struct, struct3);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFStringToMap method initGenericUDFWithNoDelimiters.
private void initGenericUDFWithNoDelimiters(GenericUDFStringToMap udf) throws UDFArgumentException {
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] arguments = { valueOI0 };
udf.initialize(arguments);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testTimestampToDateWithYearFormat.
public void testTimestampToDateWithYearFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("YYYY"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-01 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-14 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-31 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-02-02 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-02-28 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-03 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-28 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-29 00:00:00")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
// test timestamp string
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-01 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-14 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-01-31 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-02-02 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2014-02-28 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-03 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-28 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new TimestampWritable(Timestamp.valueOf("2016-02-29 10:30:45")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testStringToDateWithYearFormat.
public void testStringToDateWithYearFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("YEAR"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new Text("2014-01-01"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-14"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-31"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-02"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-28"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-03"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-28"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-29"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
// test timestamp string
valueObj0 = new DeferredJavaObject(new Text("2014-01-01 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-14 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-01-31 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-02 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2014-02-28 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-03 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-28 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new Text("2016-02-29 10:30:45"));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
}
use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaStringObjectInspector in project hive by apache.
the class TestGenericUDFTrunc method testDateWritableToDateWithQuarterFormat.
public void testDateWritableToDateWithQuarterFormat() throws HiveException {
GenericUDFTrunc udf = new GenericUDFTrunc();
ObjectInspector valueOI0 = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
ObjectInspector[] initArgs = { valueOI0, valueOI1 };
DeferredObject valueObjFmt = new DeferredJavaObject(new Text("Q"));
DeferredObject valueObj0;
DeferredObject[] evalArgs;
// test date string
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-01")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-14")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-01-31")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-02")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2014-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2014-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-03")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-28")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-02-29")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-01-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-05-11")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-04-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-07-01")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-07-01", udf, initArgs, evalArgs);
valueObj0 = new DeferredJavaObject(new DateWritable(Date.valueOf("2016-12-31")));
evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
runAndVerify("2016-10-01", udf, initArgs, evalArgs);
}
Aggregations