@Override public Object create(byte[] bytes, int offset) { TimestampTZ t = new TimestampTZ(); TimestampLocalTZWritable.setTimestampTZ( t, bytes, offset, ((TimestampLocalTZTypeInfo) typeInfo).timeZone()); return t; }
public void set(TimestampTZ tstz) { if (tstz == null) { timestampTZ.setZonedDateTime(null); return; } timestampTZ = tstz; timeZone = timestampTZ.getZonedDateTime().getZone(); bytesEmpty = true; timestampTZEmpty = false; }
@Override public boolean equals(Object other) { if (other instanceof TimestampTZ) { return compareTo((TimestampTZ) other) == 0; } return false; }
@Override public void write(Kryo kryo, Output output, TimestampTZ object) { output.writeLong(object.getEpochSecond()); output.writeInt(object.getNanos()); output.writeString(object.getZonedDateTime().getZone().getId()); }
public TimestampLocalTZWritable evaluate(TimestampLocalTZWritable t) { if (t == null) { return null; } final ZonedDateTime localZDT = t.getTimestampTZ().getZonedDateTime(); // default final long originalTimestampUTC = localZDT.withZoneSameLocal(ZoneOffset.UTC) .toInstant().toEpochMilli(); // default -> utc final long newTimestampUTC = granularity.truncate(originalTimestampUTC); // utc final ZonedDateTime newLocalZDT = ZonedDateTime.of( LocalDateTime.ofInstant(Instant.ofEpochMilli(newTimestampUTC), ZoneOffset.UTC), localZDT.getZone()); // utc -> default resultTSLTZ.set(new TimestampTZ(newLocalZDT)); return resultTSLTZ; }
@Test public void testFromToInstant() { String s1 = "2017-04-14 18:00:00 UTC"; TimestampTZ tstz = TimestampTZUtil.parse(s1); long seconds = tstz.getEpochSecond(); int nanos = tstz.getNanos(); Assert.assertEquals(tstz, new TimestampTZ(seconds, nanos, ZoneId.of("UTC"))); nanos += 123000000; Assert.assertEquals("2017-04-14 18:00:00.123 UTC", new TimestampTZ(seconds, nanos, ZoneId.of("UTC")).toString()); seconds -= 3; Assert.assertEquals("2017-04-14 17:59:57.123 UTC", new TimestampTZ(seconds, nanos, ZoneId.of("UTC")).toString()); }
@Override public Object evaluate(DeferredObject[] arguments) throws HiveException { Object a0 = arguments[0].get(); if (a0 == null) { return null; } result.set(tsOi == null ? tsWithLocalTzOi.getPrimitiveJavaObject(a0).getZonedDateTime().toInstant().toEpochMilli() : tsOi.getPrimitiveJavaObject(a0).toEpochMilli()); return result; }
private void populateBytes() { Arrays.fill(internalBytes, (byte) 0); long seconds = timestampTZ.getEpochSecond(); int nanos = timestampTZ.getNanos(); boolean hasSecondVInt = seconds < 0 || seconds > Integer.MAX_VALUE; boolean hasDecimal = setNanosBytes(nanos, internalBytes, offset + 4, hasSecondVInt); int firstInt = (int) seconds; if (hasDecimal || hasSecondVInt) { firstInt |= DECIMAL_OR_SECOND_VINT_FLAG; } intToBytes(firstInt, internalBytes, offset); if (hasSecondVInt) { LazyBinaryUtils.writeVLongToByteArray(internalBytes, offset + 4 + WritableUtils.decodeVIntSize(internalBytes[offset + 4]), seconds >> 31); } }
@Override public String toString() { populateTimestampTZ(); return timestampTZ.toString(); }
public long getSeconds() { if (!timestampTZEmpty) { return timestampTZ.getEpochSecond(); } else if (!bytesEmpty) { return getSeconds(currentBytes, offset); } throw new IllegalStateException("Both timestamp and bytes are empty"); }
@Test public void testComparison() { String s1 = "2017-04-14 18:00:00 Asia/Shanghai"; String s2 = "2017-04-14 10:00:00.00 GMT"; String s3 = "2017-04-14 18:00:00 UTC+08:00"; String s4 = "2017-04-14 18:00:00 Europe/London"; TimestampTZ tstz1 = TimestampTZUtil.parse(s1); TimestampTZ tstz2 = TimestampTZUtil.parse(s2); TimestampTZ tstz3 = TimestampTZUtil.parse(s3); TimestampTZ tstz4 = TimestampTZUtil.parse(s4); Assert.assertEquals(tstz1, tstz2); Assert.assertEquals(tstz1, tstz3); Assert.assertEquals(tstz1.hashCode(), tstz2.hashCode()); Assert.assertEquals(tstz1.hashCode(), tstz3.hashCode()); Assert.assertTrue(tstz1.compareTo(tstz4) < 0); }
public TimestampTZ(long seconds, int nanos, ZoneId timeZone) { set(seconds, nanos, timeZone); }
public int getNanos() { if (!timestampTZEmpty) { return timestampTZ.getNanos(); } else if (!bytesEmpty) { return hasDecimalOrSecondVInt(currentBytes[offset]) ? getNanos(currentBytes, offset + 4) : 0; } throw new IllegalStateException("Both timestamp and bytes are empty"); }
@Override public int hashCode() { return getTimestampTZ().hashCode(); }
/** * Initialize current timestamp, other necessary query initialization. */ public void setupQueryCurrentTimestamp() { queryCurrentTimestamp = Instant.now(); // Provide a facility to set current timestamp during tests if (sessionConf.getBoolVar(ConfVars.HIVE_IN_TEST)) { String overrideTimestampString = HiveConf.getVar(sessionConf, HiveConf.ConfVars.HIVETESTCURRENTTIMESTAMP, (String)null); if (overrideTimestampString != null && overrideTimestampString.length() > 0) { TimestampTZ zonedDateTime = TimestampTZUtil.convert( Timestamp.valueOf(overrideTimestampString), sessionConf.getLocalTimeZone()); queryCurrentTimestamp = zonedDateTime.getZonedDateTime().toInstant(); } } }
private void populateBytes() { Arrays.fill(internalBytes, (byte) 0); long seconds = timestampTZ.getEpochSecond(); int nanos = timestampTZ.getNanos(); boolean hasSecondVInt = seconds < 0 || seconds > Integer.MAX_VALUE; boolean hasDecimal = setNanosBytes(nanos, internalBytes, offset + 4, hasSecondVInt); int firstInt = (int) seconds; if (hasDecimal || hasSecondVInt) { firstInt |= DECIMAL_OR_SECOND_VINT_FLAG; } intToBytes(firstInt, internalBytes, offset); if (hasSecondVInt) { LazyBinaryUtils.writeVLongToByteArray(internalBytes, offset + 4 + WritableUtils.decodeVIntSize(internalBytes[offset + 4]), seconds >> 31); } }
private static TColumnValue timestampTZValue(TimestampTZ value) { TStringValue tStringValue = new TStringValue(); if (value != null) { tStringValue.setValue(value.toString()); } return TColumnValue.stringVal(tStringValue); }
@Override public boolean isDistanceGreater(Object v1, Object v2, int amt) { if (v1 != null && v2 != null) { long l1 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v1, (PrimitiveObjectInspector) expressionDef.getOI(), null).getEpochSecond(); long l2 = PrimitiveObjectInspectorUtils.getTimestampLocalTZ(v2, (PrimitiveObjectInspector) expressionDef.getOI(), null).getEpochSecond(); return (l1 -l2) > amt; // TODO: lossy conversion, distance is considered seconds similar to timestamp } return v1 != null || v2 != null; // True if only one value is null }
public void fromBinarySortable(byte[] bytes, int binSortOffset, ZoneId timeZone) { // Flip the sign bit (and unused bits of the high-order byte) of the seven-byte long back. long seconds = readSevenByteLong(bytes, binSortOffset) ^ SEVEN_BYTE_LONG_SIGN_FLIP; int nanos = bytesToInt(bytes, binSortOffset + 7); timestampTZ.set(seconds, nanos, timeZone); timestampTZEmpty = false; bytesEmpty = true; }
public int getNanos() { if (!timestampTZEmpty) { return timestampTZ.getNanos(); } else if (!bytesEmpty) { return hasDecimalOrSecondVInt(currentBytes[offset]) ? getNanos(currentBytes, offset + 4) : 0; } throw new IllegalStateException("Both timestamp and bytes are empty"); }