Search in sources :

Example 71 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class UDFMinute method evaluate.

@Override
public Object evaluate(GenericUDF.DeferredObject[] arguments) throws HiveException {
    switch(inputTypes[0]) {
        case INTERVAL_DAY_TIME:
            HiveIntervalDayTime intervalDayTime = getIntervalDayTimeValue(arguments, 0, inputTypes, converters);
            if (intervalDayTime == null) {
                return null;
            }
            output.set(intervalDayTime.getMinutes());
            break;
        case STRING:
        case CHAR:
        case VARCHAR:
        case DATE:
        case TIMESTAMP:
        case TIMESTAMPLOCALTZ:
        case VOID:
            Timestamp ts = getTimestampValue(arguments, 0, converters);
            if (ts == null) {
                return null;
            }
            calendar.setTimeInMillis(ts.toEpochMilli());
            output.set(calendar.get(Calendar.MINUTE));
    }
    return output;
}
Also used : VectorUDFMinuteTimestamp(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFMinuteTimestamp) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime)

Example 72 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp 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 73 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp 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 74 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp 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 75 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp in project hive by apache.

the class TestDateTimeMath method checkTsArithmetic.

private static void checkTsArithmetic(String left, String right, String expected) throws Exception {
    Timestamp leftTs = null;
    if (left != null) {
        leftTs = Timestamp.valueOf(left);
    }
    Timestamp rightTs = null;
    if (left != null) {
        rightTs = Timestamp.valueOf(right);
    }
    HiveIntervalDayTime expectedResult = null;
    if (expected != null) {
        expectedResult = HiveIntervalDayTime.valueOf(expected);
    }
    DateTimeMath dtm = new DateTimeMath();
    HiveIntervalDayTime testResult = dtm.subtract(leftTs, rightTs);
    assertEquals(String.format("%s - %s", leftTs, rightTs), expectedResult, testResult);
}
Also used : DateTimeMath(org.apache.hadoop.hive.ql.util.DateTimeMath) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) HiveIntervalDayTime(org.apache.hadoop.hive.common.type.HiveIntervalDayTime)

Aggregations

Timestamp (org.apache.hadoop.hive.common.type.Timestamp)116 Test (org.junit.Test)36 TimestampWritableV2 (org.apache.hadoop.hive.serde2.io.TimestampWritableV2)32 Date (org.apache.hadoop.hive.common.type.Date)27 BytesWritable (org.apache.hadoop.io.BytesWritable)25 LongWritable (org.apache.hadoop.io.LongWritable)25 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)24 Text (org.apache.hadoop.io.Text)22 HiveDecimal (org.apache.hadoop.hive.common.type.HiveDecimal)21 IntWritable (org.apache.hadoop.io.IntWritable)21 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)20 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)20 BooleanWritable (org.apache.hadoop.io.BooleanWritable)19 FloatWritable (org.apache.hadoop.io.FloatWritable)19 PrimitiveTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo)18 ArrayList (java.util.ArrayList)17 DateWritableV2 (org.apache.hadoop.hive.serde2.io.DateWritableV2)17 HiveIntervalDayTime (org.apache.hadoop.hive.common.type.HiveIntervalDayTime)16 List (java.util.List)15 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)12