use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class GenericUDFTrunc method evaluateDate.
private Object evaluateDate(DeferredObject[] arguments) throws UDFArgumentLengthException, HiveException, UDFArgumentTypeException, UDFArgumentException {
if (arguments.length != 2) {
throw new UDFArgumentLengthException("trunc() requires 2 argument, got " + arguments.length);
}
if (arguments[0].get() == null || arguments[1].get() == null) {
return null;
}
if (textConverter2 != null) {
fmtInput = textConverter2.convert(arguments[1].get()).toString();
}
Date date;
switch(inputType1) {
case STRING:
String dateString = textConverter1.convert(arguments[0].get()).toString();
try {
date = formatter.parse(dateString.toString());
} catch (ParseException e) {
return null;
}
break;
case TIMESTAMP:
Timestamp ts = ((TimestampWritable) timestampConverter.convert(arguments[0].get())).getTimestamp();
date = ts;
break;
case DATE:
DateWritable dw = (DateWritable) dateWritableConverter.convert(arguments[0].get());
date = dw.get();
break;
default:
throw new UDFArgumentTypeException(0, "TRUNC() only takes STRING/TIMESTAMP/DATEWRITABLE types, got " + inputType1);
}
if (evalDate(date) == null) {
return null;
}
Date newDate = calendar.getTime();
output.set(formatter.format(newDate));
return output;
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFSortArray method testSortStruct.
@Test
public void testSortStruct() throws HiveException {
ObjectInspector[] inputOIs = { ObjectInspectorFactory.getStandardListObjectInspector(ObjectInspectorFactory.getStandardStructObjectInspector(asList("f1", "f2", "f3", "f4"), asList(PrimitiveObjectInspectorFactory.writableStringObjectInspector, PrimitiveObjectInspectorFactory.writableDoubleObjectInspector, PrimitiveObjectInspectorFactory.writableDateObjectInspector, ObjectInspectorFactory.getStandardListObjectInspector(PrimitiveObjectInspectorFactory.writableIntObjectInspector)))) };
udf.initialize(inputOIs);
Object i1 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 26)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
Object i2 = asList(new Text("b"), new DoubleWritable(3.14), new DateWritable(new Date(2015, 5, 26)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
Object i3 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 25)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(5)));
Object i4 = asList(new Text("a"), new DoubleWritable(3.1415), new DateWritable(new Date(2015, 5, 25)), asList(new IntWritable(1), new IntWritable(3), new IntWritable(2), new IntWritable(4)));
runAndVerify(asList(i1, i2, i3, i4), asList(i4, i3, i1, i2));
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFOPPlus method testIntervalYearMonthPlusDate.
@Test
public void testIntervalYearMonthPlusDate() throws Exception {
GenericUDFOPPlus udf = new GenericUDFOPPlus();
HiveIntervalYearMonthWritable left = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-8"));
DateWritable right = new DateWritable(Date.valueOf("2001-06-15"));
ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector, PrimitiveObjectInspectorFactory.writableDateObjectInspector };
DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
Assert.assertEquals(TypeInfoFactory.dateTypeInfo, oi.getTypeInfo());
DateWritable res = (DateWritable) udf.evaluate(args);
Assert.assertEquals(Date.valueOf("2004-02-15"), res.get());
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFQuarter method runAndVerifyDt.
private void runAndVerifyDt(String str, Integer expResult, GenericUDF udf) throws HiveException {
DeferredObject valueObj0 = new DeferredJavaObject(str != null ? new DateWritable(Date.valueOf(str)) : null);
DeferredObject[] args = { valueObj0 };
IntWritable output = (IntWritable) udf.evaluate(args);
if (expResult == null) {
assertNull(output);
} else {
assertNotNull(output);
assertEquals("quarter() test ", expResult.intValue(), output.get());
}
}
use of org.apache.hadoop.hive.serde2.io.DateWritable in project hive by apache.
the class TestGenericUDFToUnixTimestamp method testDate.
public void testDate() throws HiveException {
GenericUDFToUnixTimeStamp udf = new GenericUDFToUnixTimeStamp();
ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableDateObjectInspector;
ObjectInspector[] arguments = { valueOI };
udf.initialize(arguments);
Date date = Date.valueOf("1970-01-01");
runAndVerify(udf, new DateWritable(date), new LongWritable(date.getTime() / 1000));
// test null values
runAndVerify(udf, null, null);
}
Aggregations