Search in sources :

Example 31 with Timestamp

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

the class TestETypeConverter method testGetInt64MillisTimestampProlepticConverter.

@Test
public void testGetInt64MillisTimestampProlepticConverter() throws Exception {
    Timestamp timestamp = Timestamp.valueOf("1572-07-15 15:12:20.112");
    PrimitiveType primitiveType = createInt64TimestampType(false, TimeUnit.MILLIS);
    Writable writable = getWritableFromPrimitiveConverter(null, primitiveType, timestamp.toEpochMilli());
    TimestampWritableV2 timestampWritable = (TimestampWritableV2) writable;
    assertEquals(timestamp.toEpochMilli(), timestampWritable.getTimestamp().toEpochMilli());
}
Also used : Writable(org.apache.hadoop.io.Writable) DoubleWritable(org.apache.hadoop.io.DoubleWritable) LongWritable(org.apache.hadoop.io.LongWritable) BytesWritable(org.apache.hadoop.io.BytesWritable) IntWritable(org.apache.hadoop.io.IntWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) FloatWritable(org.apache.hadoop.io.FloatWritable) PrimitiveType(org.apache.parquet.schema.PrimitiveType) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Test(org.junit.Test)

Example 32 with Timestamp

use of org.apache.hadoop.hive.common.type.Timestamp 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 d;
    switch(inputType1) {
        case STRING:
            String dateString = textConverter1.convert(arguments[0].get()).toString();
            d = DateParser.parseDate(dateString);
            if (d == null) {
                return null;
            }
            break;
        case TIMESTAMP:
            Timestamp ts = ((TimestampWritableV2) timestampConverter.convert(arguments[0].get())).getTimestamp();
            d = Date.ofEpochMilli(ts.toEpochMilli());
            break;
        case DATE:
            DateWritableV2 dw = (DateWritableV2) dateWritableConverter.convert(arguments[0].get());
            d = dw.get();
            break;
        default:
            throw new UDFArgumentTypeException(0, "TRUNC() only takes STRING/TIMESTAMP/DATEWRITABLE types, got " + inputType1);
    }
    if (evalDate(d) == null) {
        return null;
    }
    output.set(date.toString());
    return output;
}
Also used : UDFArgumentLengthException(org.apache.hadoop.hive.ql.exec.UDFArgumentLengthException) UDFArgumentTypeException(org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException) DateWritableV2(org.apache.hadoop.hive.serde2.io.DateWritableV2) TruncDateFromString(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromString) TruncDateFromTimestamp(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromTimestamp) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) TimestampWritableV2(org.apache.hadoop.hive.serde2.io.TimestampWritableV2) Date(org.apache.hadoop.hive.common.type.Date) TruncDateFromDate(org.apache.hadoop.hive.ql.exec.vector.expressions.TruncDateFromDate)

Example 33 with Timestamp

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

the class GenericUDFToUnixTimeStamp method evaluate.

@Override
public Object evaluate(DeferredObject[] arguments) throws HiveException {
    if (arguments[0].get() == null) {
        return null;
    }
    if (inputTextConverter != null) {
        Timestamp timestamp;
        String textVal = (String) inputTextConverter.convert(arguments[0].get());
        if (textVal == null) {
            return null;
        }
        if (patternConverter != null) {
            if (arguments[1].get() == null) {
                return null;
            }
            String patternVal = (String) patternConverter.convert(arguments[1].get());
            if (patternVal == null) {
                return null;
            }
            if (!patternVal.equals(lasPattern)) {
                formatter = getFormatter(patternVal);
                lasPattern = patternVal;
            }
            try {
                ZonedDateTime zonedDateTime = ZonedDateTime.parse(textVal, formatter.withZone(timeZone)).withZoneSameInstant(timeZone);
                timestamp = new Timestamp(zonedDateTime.toLocalDateTime());
            } catch (DateTimeException e1) {
                try {
                    LocalDate localDate = LocalDate.parse(textVal, formatter);
                    timestamp = new Timestamp(localDate.atStartOfDay());
                } catch (DateTimeException e3) {
                    return null;
                }
            }
        } else {
            try {
                timestamp = Timestamp.valueOf(textVal);
            } catch (IllegalArgumentException e) {
                return null;
            }
        }
        TimestampTZ timestampTZ = TimestampTZUtil.convert(timestamp, timeZone);
        retValue.set(timestampTZ.getEpochSecond());
    } else if (inputDateOI != null) {
        TimestampTZ timestampTZ = TimestampTZUtil.convert(inputDateOI.getPrimitiveJavaObject(arguments[0].get()), timeZone);
        retValue.set(timestampTZ.getEpochSecond());
    } else if (inputTimestampOI != null) {
        TimestampTZ timestampTZ = TimestampTZUtil.convert(inputTimestampOI.getPrimitiveJavaObject(arguments[0].get()), timeZone);
        retValue.set(timestampTZ.getEpochSecond());
    } else {
        TimestampTZ timestampTZ = inputTimestampLocalTzOI.getPrimitiveJavaObject(arguments[0].get());
        retValue.set(timestampTZ.getEpochSecond());
    }
    return retValue;
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) DateTimeException(java.time.DateTimeException) ZonedDateTime(java.time.ZonedDateTime) VectorUDFUnixTimeStampString(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampString) Timestamp(org.apache.hadoop.hive.common.type.Timestamp) VectorUDFUnixTimeStampTimestamp(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampTimestamp) LocalDate(java.time.LocalDate)

Example 34 with Timestamp

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

the class DateTimeMath method add.

public Timestamp add(HiveIntervalDayTime interval, Timestamp ts) {
    if (ts == null || interval == null) {
        return null;
    }
    Timestamp tsResult = new Timestamp();
    add(interval, ts, tsResult);
    return tsResult;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

Example 35 with Timestamp

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

the class DateTimeMath method subtract.

public Timestamp subtract(Timestamp left, HiveIntervalYearMonth right) {
    if (left == null || right == null) {
        return null;
    }
    Timestamp tsResult = new Timestamp();
    subtract(left, right, tsResult);
    return tsResult;
}
Also used : Timestamp(org.apache.hadoop.hive.common.type.Timestamp)

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