use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class GenericUDFCurrentDate method initialize.
@Override
public ObjectInspector initialize(ObjectInspector[] arguments) throws UDFArgumentException {
if (arguments.length != 0) {
throw new UDFArgumentLengthException("The function CURRENT_DATE does not take any arguments, but found " + arguments.length);
}
if (currentDate == null) {
Date dateVal = Date.valueOf(SessionState.get().getQueryCurrentTimestamp().toString().substring(0, 10));
currentDate = new DateWritable(dateVal);
}
return PrimitiveObjectInspectorFactory.writableDateObjectInspector;
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class GenericUDFCurrentDate method copyToNewInstance.
@Override
public void copyToNewInstance(Object newInstance) throws UDFArgumentException {
super.copyToNewInstance(newInstance);
// Need to preserve currentDate
GenericUDFCurrentDate other = (GenericUDFCurrentDate) newInstance;
if (this.currentDate != null) {
other.currentDate = new DateWritable(this.currentDate);
}
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class GenericUDFDateDiff method checkArguments.
private Converter checkArguments(ObjectInspector[] arguments, int i) throws UDFArgumentException {
if (arguments[i].getCategory() != ObjectInspector.Category.PRIMITIVE) {
throw new UDFArgumentTypeException(0, "Only primitive type arguments are accepted but " + arguments[i].getTypeName() + " is passed. as first arguments");
}
PrimitiveCategory inputType = ((PrimitiveObjectInspector) arguments[i]).getPrimitiveCategory();
Converter converter;
switch(inputType) {
case STRING:
case VARCHAR:
case CHAR:
converter = ObjectInspectorConverters.getConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableStringObjectInspector);
break;
case TIMESTAMP:
converter = new TimestampConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
break;
case DATE:
converter = ObjectInspectorConverters.getConverter((PrimitiveObjectInspector) arguments[i], PrimitiveObjectInspectorFactory.writableDateObjectInspector);
break;
default:
throw new UDFArgumentException(" DATEDIFF() only takes STRING/TIMESTAMP/DATEWRITABLE types as " + (i + 1) + "-th argument, got " + inputType);
}
return converter;
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFDate method testTimestampToDate.
public void testTimestampToDate() throws HiveException {
GenericUDFDate udf = new GenericUDFDate();
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
ObjectInspector[] arguments = { valueOI };
udf.initialize(arguments);
DeferredObject valueObj = new DeferredJavaObject(new TimestampWritable(new Timestamp(109, 06, 30, 4, 17, 52, 0)));
DeferredObject[] args = { valueObj };
DateWritable output = (DateWritable) udf.evaluate(args);
assertEquals("to_date() test for TIMESTAMP failed ", "2009-07-30", output.toString());
// Try with null args
DeferredObject[] nullArgs = { new DeferredJavaObject(null) };
output = (DateWritable) udf.evaluate(nullArgs);
assertNull("to_date() with null TIMESTAMP", output);
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFDate method testVoidToDate.
public void testVoidToDate() throws HiveException {
GenericUDFDate udf = new GenericUDFDate();
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableVoidObjectInspector;
ObjectInspector[] arguments = { valueOI };
udf.initialize(arguments);
DeferredObject[] args = { new DeferredJavaObject(null) };
DateWritable output = (DateWritable) udf.evaluate(args);
// Try with null VOID
assertNull("to_date() with null DATE ", output);
// Try with erroneously generated VOID
DeferredObject[] junkArgs = { new DeferredJavaObject(new Text("2015-11-22")) };
try {
udf.evaluate(junkArgs);
fail("to_date() test with VOID non-null failed");
} catch (UDFArgumentException udfae) {
assertEquals("TO_DATE() received non-null object of VOID type", udfae.getMessage());
}
}
Aggregations