Search in sources :

Example 26 with PrimitiveObjectInspectorFactory.writableTimestampObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector in project hive by apache.

the class TestGenericUDFTrunc method testTimestampToDateWithYearFormat.

@Test
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 TimestampWritableV2(Timestamp.valueOf("2014-01-01 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-01-14 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-01-31 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-02-02 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-02-28 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2016-02-03 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2016-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2016-02-28 00:00:00")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2016-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(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 TimestampWritableV2(Timestamp.valueOf("2014-01-01 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-01-14 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-01-31 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-02-02 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2014-02-28 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2014-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2016-02-03 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2016-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2016-02-28 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2016-01-01", udf, initArgs, evalArgs);
    valueObj0 = new DeferredJavaObject(new TimestampWritableV2(Timestamp.valueOf("2016-02-29 10:30:45")));
    evalArgs = new DeferredObject[] { valueObj0, valueObjFmt };
    runAndVerify("2016-01-01", udf, initArgs, evalArgs);
}
Also used : ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) Text(org.apache.hadoop.io.Text) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Example 27 with PrimitiveObjectInspectorFactory.writableTimestampObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector in project hive by apache.

the class TestValueBoundaryScanner method testTimestampIsDistanceGreater.

@Test
public void testTimestampIsDistanceGreater() {
    PTFExpressionDef argDef = new PTFExpressionDef();
    argDef.setOI(PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
    TimestampValueBoundaryScanner scanner = new TimestampValueBoundaryScanner(null, null, new OrderExpressionDef(argDef), false);
    Timestamp ts = new Timestamp();
    // 1000s
    ts.setTimeInMillis(1000000);
    // 1000s
    TimestampWritableV2 w1 = new TimestampWritableV2(ts);
    // 1000s
    TimestampWritableV2 w2 = new TimestampWritableV2(ts);
    // empty == epoch == 0s
    TimestampWritableV2 w3 = new TimestampWritableV2();
    // equal timestamps, distance is not greater than 0
    Assert.assertFalse(scanner.isDistanceGreater(w1, w2, 0));
    Assert.assertFalse(scanner.isDistanceGreater(w2, w1, 0));
    // null comparison, true only if one value is null
    Assert.assertTrue(scanner.isDistanceGreater(w1, null, 100));
    Assert.assertTrue(scanner.isDistanceGreater(w2, null, 100));
    Assert.assertFalse(scanner.isDistanceGreater(null, null, 100));
    // 1000s distance
    // 1000 > 999
    Assert.assertTrue(scanner.isDistanceGreater(w1, w3, 999));
    Assert.assertFalse(scanner.isDistanceGreater(w1, w3, 1000));
    Assert.assertFalse(scanner.isDistanceGreater(w1, w3, 1001));
}
Also used : PTFExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) OrderExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.OrderExpressionDef) Test(org.junit.Test)

Example 28 with PrimitiveObjectInspectorFactory.writableTimestampObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector in project hive by apache.

the class TestValueBoundaryScanner method testTimestampEquals.

@Test
public void testTimestampEquals() {
    PTFExpressionDef argDef = new PTFExpressionDef();
    argDef.setOI(PrimitiveObjectInspectorFactory.writableTimestampObjectInspector);
    TimestampValueBoundaryScanner scanner = new TimestampValueBoundaryScanner(null, null, new OrderExpressionDef(argDef), false);
    Timestamp ts = new Timestamp();
    ts.setTimeInMillis(1000);
    TimestampWritableV2 w1 = new TimestampWritableV2(ts);
    TimestampWritableV2 w2 = new TimestampWritableV2(ts);
    // empty
    TimestampWritableV2 w3 = new TimestampWritableV2();
    Assert.assertTrue(scanner.isEqual(w1, w2));
    Assert.assertTrue(scanner.isEqual(w2, w1));
    // empty == epoch
    Assert.assertTrue(scanner.isEqual(w3, new TimestampWritableV2(new Timestamp())));
    // empty != another non-epoch
    Assert.assertFalse(scanner.isEqual(w3, w1));
    Assert.assertFalse(scanner.isEqual(null, w2));
    Assert.assertFalse(scanner.isEqual(w1, null));
    Assert.assertTrue(scanner.isEqual(null, null));
}
Also used : PTFExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) OrderExpressionDef(org.apache.hadoop.hive.ql.plan.ptf.OrderExpressionDef) Test(org.junit.Test)

Example 29 with PrimitiveObjectInspectorFactory.writableTimestampObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector in project hive by apache.

the class TestGenericUDFToUnixTimestamp method testTimestamp.

@Test
public void testTimestamp() throws HiveException {
    GenericUDFToUnixTimeStamp udf = new GenericUDFToUnixTimeStamp();
    ObjectInspector valueOI = PrimitiveObjectInspectorFactory.writableTimestampObjectInspector;
    ObjectInspector[] arguments = { valueOI };
    udf.initialize(arguments);
    Timestamp ts = Timestamp.valueOf("1970-01-01 00:00:00");
    TimestampTZ tstz = TimestampTZUtil.convert(ts, ZoneId.systemDefault());
    runAndVerify(udf, new TimestampWritableV2(ts), new LongWritable(tstz.getEpochSecond()));
    ts = Timestamp.valueOf("2001-02-03 01:02:03");
    tstz = TimestampTZUtil.convert(ts, ZoneId.systemDefault());
    runAndVerify(udf, new TimestampWritableV2(ts), new LongWritable(tstz.getEpochSecond()));
    // test null values
    runAndVerify(udf, null, null);
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) LongWritable(org.apache.hadoop.io.LongWritable) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Example 30 with PrimitiveObjectInspectorFactory.writableTimestampObjectInspector

use of org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector in project hive by apache.

the class TestGenericUDFOPMinus method testTimestampMinusIntervalYearMonth.

@Test
public void testTimestampMinusIntervalYearMonth() throws Exception {
    GenericUDFOPMinus udf = new GenericUDFOPMinus();
    TimestampWritableV2 left = new TimestampWritableV2(Timestamp.valueOf("2004-01-15 01:02:03.123456789"));
    HiveIntervalYearMonthWritable right = new HiveIntervalYearMonthWritable(HiveIntervalYearMonth.valueOf("2-2"));
    ObjectInspector[] inputOIs = { PrimitiveObjectInspectorFactory.writableTimestampObjectInspector, PrimitiveObjectInspectorFactory.writableHiveIntervalYearMonthObjectInspector };
    DeferredObject[] args = { new DeferredJavaObject(left), new DeferredJavaObject(right) };
    PrimitiveObjectInspector oi = (PrimitiveObjectInspector) udf.initialize(inputOIs);
    Assert.assertEquals(TypeInfoFactory.timestampTypeInfo, oi.getTypeInfo());
    TimestampWritableV2 res = (TimestampWritableV2) udf.evaluate(args);
    Assert.assertEquals(Timestamp.valueOf("2001-11-15 01:02:03.123456789"), res.getTimestamp());
}
Also used : PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) DeferredJavaObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject) DeferredObject(org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject) PrimitiveObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector) HiveIntervalYearMonthWritable(org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Aggregations

TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)27 ObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector)27 Test (org.junit.Test)25 DeferredJavaObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredJavaObject)13 DeferredObject (org.apache.hadoop.hive.ql.udf.generic.GenericUDF.DeferredObject)13 PrimitiveObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector)11 IntWritable (org.apache.hadoop.io.IntWritable)10 UDFArgumentLengthException (org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException)6 UDFArgumentException (org.apache.hadoop.hive.ql.exec.UDFArgumentException)5 TimestampConverter (org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter)5 Text (org.apache.hadoop.io.Text)5 UDFArgumentTypeException (org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException)4 Timestamp (org.apache.hadoop.hive.common.type.Timestamp)3 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)3 HiveIntervalDayTimeWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalDayTimeWritable)3 HiveIntervalYearMonthWritable (org.apache.hadoop.hive.serde2.io.HiveIntervalYearMonthWritable)3 ConstantObjectInspector (org.apache.hadoop.hive.serde2.objectinspector.ConstantObjectInspector)3 PrimitiveCategory (org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector.PrimitiveCategory)3 OrderExpressionDef (org.apache.hadoop.hive.ql.plan.ptf.OrderExpressionDef)2 PTFExpressionDef (org.apache.hadoop.hive.ql.plan.ptf.PTFExpressionDef)2