Search in sources :

Example 6 with TimestampTZ

use of org.apache.hadoop.hive.common.type.TimestampTZ 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) {
        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.applyPattern(patternVal);
                lasPattern = patternVal;
            }
        }
        try {
            retValue.set(formatter.parse(textVal).getTime() / 1000);
            return retValue;
        } catch (ParseException e) {
            return null;
        }
    } else if (inputDateOI != null) {
        retValue.set(inputDateOI.getPrimitiveWritableObject(arguments[0].get()).getTimeInSeconds());
        return retValue;
    } else if (inputTimestampLocalTzOI != null) {
        TimestampTZ timestampTZ = inputTimestampLocalTzOI.getPrimitiveJavaObject(arguments[0].get());
        retValue.set(timestampTZ.getEpochSecond());
        return retValue;
    }
    Timestamp timestamp = inputTimestampOI.getPrimitiveJavaObject(arguments[0].get());
    setValueFromTs(retValue, timestamp);
    return retValue;
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) VectorUDFUnixTimeStampString(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampString) TimestampWithTimeZoneString(org.apache.calcite.util.TimestampWithTimeZoneString) ParseException(java.text.ParseException) Timestamp(java.sql.Timestamp) VectorUDFUnixTimeStampTimestamp(org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampTimestamp)

Example 7 with TimestampTZ

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

the class DruidSerDe method deserialize.

@Override
public Object deserialize(Writable writable) throws SerDeException {
    final DruidWritable input = (DruidWritable) writable;
    final List<Object> output = Lists.newArrayListWithExpectedSize(columns.length);
    for (int i = 0; i < columns.length; i++) {
        final Object value = input.getValue().get(columns[i]);
        if (value == null) {
            output.add(null);
            continue;
        }
        switch(types[i].getPrimitiveCategory()) {
            case TIMESTAMP:
                output.add(new TimestampWritable(Timestamp.valueOf(ZonedDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), tsTZTypeInfo.timeZone()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).toString())));
                break;
            case TIMESTAMPLOCALTZ:
                output.add(new TimestampLocalTZWritable(new TimestampTZ(ZonedDateTime.ofInstant(Instant.ofEpochMilli(((Number) value).longValue()), ((TimestampLocalTZTypeInfo) types[i]).timeZone()))));
                break;
            case BYTE:
                output.add(new ByteWritable(((Number) value).byteValue()));
                break;
            case SHORT:
                output.add(new ShortWritable(((Number) value).shortValue()));
                break;
            case INT:
                output.add(new IntWritable(((Number) value).intValue()));
                break;
            case LONG:
                output.add(new LongWritable(((Number) value).longValue()));
                break;
            case FLOAT:
                output.add(new FloatWritable(((Number) value).floatValue()));
                break;
            case DOUBLE:
                output.add(new DoubleWritable(((Number) value).doubleValue()));
                break;
            case DECIMAL:
                output.add(new HiveDecimalWritable(HiveDecimal.create(((Number) value).doubleValue())));
                break;
            case CHAR:
                output.add(new HiveCharWritable(new HiveChar(value.toString(), ((CharTypeInfo) types[i]).getLength())));
                break;
            case VARCHAR:
                output.add(new HiveVarcharWritable(new HiveVarchar(value.toString(), ((VarcharTypeInfo) types[i]).getLength())));
                break;
            case STRING:
                output.add(new Text(value.toString()));
                break;
            case BOOLEAN:
                output.add(new BooleanWritable(Boolean.valueOf(value.toString())));
                break;
            default:
                throw new SerDeException("Unknown type: " + types[i].getPrimitiveCategory());
        }
    }
    return output;
}
Also used : HiveDecimalWritable(org.apache.hadoop.hive.serde2.io.HiveDecimalWritable) HiveChar(org.apache.hadoop.hive.common.type.HiveChar) TimestampWritable(org.apache.hadoop.hive.serde2.io.TimestampWritable) HiveCharWritable(org.apache.hadoop.hive.serde2.io.HiveCharWritable) HiveVarcharWritable(org.apache.hadoop.hive.serde2.io.HiveVarcharWritable) TimestampLocalTZTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo) DoubleWritable(org.apache.hadoop.hive.serde2.io.DoubleWritable) Text(org.apache.hadoop.io.Text) HiveVarchar(org.apache.hadoop.hive.common.type.HiveVarchar) ShortWritable(org.apache.hadoop.hive.serde2.io.ShortWritable) TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) FloatWritable(org.apache.hadoop.io.FloatWritable) BooleanWritable(org.apache.hadoop.io.BooleanWritable) TimestampLocalTZWritable(org.apache.hadoop.hive.serde2.io.TimestampLocalTZWritable) LongWritable(org.apache.hadoop.io.LongWritable) ByteWritable(org.apache.hadoop.hive.serde2.io.ByteWritable) IntWritable(org.apache.hadoop.io.IntWritable) SerDeException(org.apache.hadoop.hive.serde2.SerDeException)

Example 8 with TimestampTZ

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

the class MultiValueBoundaryScanner method isEqual.

@Override
public boolean isEqual(Object v1, Object v2) {
    if (v1 != null && v2 != null) {
        TimestampTZ l1 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v1, (PrimitiveObjectInspector) expressionDef.getOI(), null);
        TimestampTZ l2 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v2, (PrimitiveObjectInspector) expressionDef.getOI(), null);
        return l1.equals(l2);
    }
    // True if both are null
    return v1 == null && v2 == null;
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ)

Example 9 with TimestampTZ

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

the class TestUDFDateFormatGranularity method testTimestampWithLocalTZGranularity.

@Test
public void testTimestampWithLocalTZGranularity() throws Exception {
    // Running example
    // Friday 30th August 1985 02:47:02 AM
    final TimestampLocalTZWritable t = new TimestampLocalTZWritable(new TimestampTZ(Instant.ofEpochMilli(494243222000L).atZone(ZoneId.of("America/Los_Angeles"))));
    UDFDateFloor g;
    // Year granularity
    // Tuesday 1st January 1985 12:00:00 AM
    g = new UDFDateFloorYear();
    TimestampLocalTZWritable i1 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(473414400000L).atZone(ZoneId.of("America/Los_Angeles"))), i1.getTimestampTZ());
    // Quarter granularity
    // Monday 1st July 1985 12:00:00 AM
    g = new UDFDateFloorQuarter();
    TimestampLocalTZWritable i2 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(489049200000L).atZone(ZoneId.of("America/Los_Angeles"))), i2.getTimestampTZ());
    // Month granularity
    // Thursday 1st August 1985 12:00:00 AM
    g = new UDFDateFloorMonth();
    TimestampLocalTZWritable i3 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(491727600000L).atZone(ZoneId.of("America/Los_Angeles"))), i3.getTimestampTZ());
    // Week granularity
    // Monday 26th August 1985 12:00:00 AM
    g = new UDFDateFloorWeek();
    TimestampLocalTZWritable i4 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(493887600000L).atZone(ZoneId.of("America/Los_Angeles"))), i4.getTimestampTZ());
    // Day granularity
    // Friday 30th August 1985 12:00:00 AM
    g = new UDFDateFloorDay();
    TimestampLocalTZWritable i5 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(494233200000L).atZone(ZoneId.of("America/Los_Angeles"))), i5.getTimestampTZ());
    // Hour granularity
    // Friday 30th August 1985 02:00:00 AM
    g = new UDFDateFloorHour();
    TimestampLocalTZWritable i6 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(494240400000L).atZone(ZoneId.of("America/Los_Angeles"))), i6.getTimestampTZ());
    // Minute granularity
    // Friday 30th August 1985 02:47:00 AM
    g = new UDFDateFloorMinute();
    TimestampLocalTZWritable i7 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(494243220000L).atZone(ZoneId.of("America/Los_Angeles"))), i7.getTimestampTZ());
    // Second granularity
    // Friday 30th August 1985 02:47:02 AM
    g = new UDFDateFloorSecond();
    TimestampLocalTZWritable i8 = g.evaluate(t);
    assertEquals(new TimestampTZ(Instant.ofEpochMilli(494243222000L).atZone(ZoneId.of("America/Los_Angeles"))), i8.getTimestampTZ());
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) TimestampLocalTZWritable(org.apache.hadoop.hive.serde2.io.TimestampLocalTZWritable) Test(org.junit.Test)

Example 10 with TimestampTZ

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

the class LazyTimestampLocalTZObjectInspector method getPrimitiveJavaObject.

@Override
public TimestampTZ getPrimitiveJavaObject(Object o) {
    if (o == null) {
        return null;
    }
    TimestampTZ t = ((LazyTimestampLocalTZ) o).getWritableObject().getTimestampTZ();
    TimestampLocalTZTypeInfo timestampTZTypeInfo = (TimestampLocalTZTypeInfo) typeInfo;
    if (!t.getZonedDateTime().getZone().equals(timestampTZTypeInfo.timeZone())) {
        t.setZonedDateTime(t.getZonedDateTime().withZoneSameInstant(timestampTZTypeInfo.timeZone()));
    }
    return t;
}
Also used : TimestampTZ(org.apache.hadoop.hive.common.type.TimestampTZ) TimestampLocalTZTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo)

Aggregations

TimestampTZ (org.apache.hadoop.hive.common.type.TimestampTZ)13 TimestampLocalTZTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.TimestampLocalTZTypeInfo)4 TimestampLocalTZWritable (org.apache.hadoop.hive.serde2.io.TimestampLocalTZWritable)3 Test (org.junit.Test)3 Repeating (com.google.code.tempusfugit.concurrency.annotations.Repeating)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Timestamp (java.sql.Timestamp)1 ParseException (java.text.ParseException)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeParseException (java.time.format.DateTimeParseException)1 TimestampWithTimeZoneString (org.apache.calcite.util.TimestampWithTimeZoneString)1 HiveChar (org.apache.hadoop.hive.common.type.HiveChar)1 HiveVarchar (org.apache.hadoop.hive.common.type.HiveVarchar)1 VectorUDFUnixTimeStampString (org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampString)1 VectorUDFUnixTimeStampTimestamp (org.apache.hadoop.hive.ql.exec.vector.expressions.VectorUDFUnixTimeStampTimestamp)1 SerDeException (org.apache.hadoop.hive.serde2.SerDeException)1 ByteWritable (org.apache.hadoop.hive.serde2.io.ByteWritable)1 DoubleWritable (org.apache.hadoop.hive.serde2.io.DoubleWritable)1 HiveCharWritable (org.apache.hadoop.hive.serde2.io.HiveCharWritable)1 HiveDecimalWritable (org.apache.hadoop.hive.serde2.io.HiveDecimalWritable)1