@Override public String toString(final Object obj) { if (obj == null) { return null; } final ZoneId zoneId = (ZoneId)obj; return zoneId.getId(); } }
@Override public String getAsText() { ZoneId value = (ZoneId) getValue(); return (value != null ? value.getId() : ""); }
@Override public String getAsText() { ZoneId value = (ZoneId) getValue(); return (value != null ? value.getId() : ""); }
@Override protected DateTime evaluate(FunctionArgs args, EvaluationContext context, DateTimeZone timezone) { final Object datish = value.required(args, context); if (datish instanceof DateTime) { return (DateTime) datish; } if (datish instanceof Date) { return new DateTime(datish); } if (datish instanceof ZonedDateTime) { final ZonedDateTime zonedDateTime = (ZonedDateTime) datish; final DateTimeZone timeZone = DateTimeZone.forID(zonedDateTime.getZone().getId()); return new DateTime(zonedDateTime.toInstant().toEpochMilli(), timeZone); } return null; }
@Override public void writeDateTime( ZonedDateTime zonedDateTime ) { buf.putLong( zonedDateTime.toEpochSecond() ); buf.putInt( zonedDateTime.getNano() ); final ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { final int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); // lowest bit set to 0: it's a zone offset in seconds buf.putInt( offsetSeconds << 1 ); } else { // lowest bit set to 1: it's a zone id final int zoneId = (TimeZones.map( zone.getId() ) << 1) | 1; buf.putInt( zoneId ); } } }
private DateTimeValue( ZonedDateTime value ) { ZoneId zone = value.getZone(); if ( zone instanceof ZoneOffset ) { this.value = value; } else { // Do a 2-way lookup of the zone to make sure we only use the new name of renamed zones ZoneId mappedZone = ZoneId.of( TimeZones.map( TimeZones.map( zone.getId() ) ) ); this.value = value.withZoneSameInstant( mappedZone ); } this.epochSeconds = this.value.toEpochSecond(); }
private boolean checkEnterpriseFeaturesRequested() { boolean result = false; if (readBufferMode != BufferMode.None) result = onlyAvailableInEnterprise("Buffering"); if (writeBufferMode != BufferMode.None) result = onlyAvailableInEnterprise("Buffering"); if (rollTimeZone != null && !rollTimeZone.getId().equals("UTC") && !rollTimeZone.getId().equals("Z")) result = onlyAvailableInEnterprise("Non-UTC roll time zone"); if (wireType == WireType.DELTA_BINARY) result = onlyAvailableInEnterprise("Wire type " + wireType.name()); if (encodingSupplier != null) result = onlyAvailableInEnterprise("Encoding"); if (key != null) result = onlyAvailableInEnterprise("Encryption"); if (hasPretouchIntervalMillis()) result = onlyAvailableInEnterprise("Out of process pretouching"); return result; }
@Override public boolean equals( Value other ) { if ( other instanceof DateTimeValue ) { ZonedDateTime that = ((DateTimeValue) other).value; boolean res = value.toLocalDateTime().equals( that.toLocalDateTime() ); if ( res ) { ZoneId thisZone = value.getZone(); ZoneId thatZone = that.getZone(); boolean thisIsOffset = thisZone instanceof ZoneOffset; boolean thatIsOffset = thatZone instanceof ZoneOffset; if ( thisIsOffset && thatIsOffset ) { res = thisZone.equals( thatZone ); } else if ( !thisIsOffset && !thatIsOffset ) { res = TimeZones.map( thisZone.getId() ) == TimeZones.map( thatZone.getId() ); } else { res = false; } } return res; } return false; }
@Test public void shouldPackLocalDateTimeWithTimeZoneId() { LocalDateTime localDateTime = LocalDateTime.of( 1999, 12, 30, 9, 49, 20, 999999999 ); ZoneId zoneId = ZoneId.of( "Europe/Stockholm" ); ZonedDateTime zonedDateTime = ZonedDateTime.of( localDateTime, zoneId ); PackedOutputArray packedOutput = pack( datetime( zonedDateTime ) ); ByteBuffer buffer = ByteBuffer.wrap( packedOutput.bytes() ); buffer.getShort(); // skip struct header assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.toEpochSecond( UTC ), buffer.getInt() ); assertEquals( INT_32, buffer.get() ); assertEquals( localDateTime.getNano(), buffer.getInt() ); buffer.getShort(); // skip zoneId string header byte[] zoneIdBytes = new byte[zoneId.getId().getBytes( UTF_8 ).length]; buffer.get( zoneIdBytes ); assertEquals( zoneId.getId(), new String( zoneIdBytes, UTF_8 ) ); }
@Override public final void writeDateTime( ZonedDateTime zonedDateTime ) throws E { long epochSecondUTC = zonedDateTime.toEpochSecond(); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); writeDateTime( epochSecondUTC, nano, offsetSeconds ); } else { String zoneId = zone.getId(); writeDateTime( epochSecondUTC, nano, zoneId ); } }
private static RelOptPlanner createPlanner( HiveConf conf, Set<RelNode> corrScalarRexSQWithAgg, Set<RelNode> scalarAggNoGbyNoWin) { final Double maxSplitSize = (double) HiveConf.getLongVar( conf, HiveConf.ConfVars.MAPREDMAXSPLITSIZE); final Double maxMemory = (double) HiveConf.getLongVar( conf, HiveConf.ConfVars.HIVECONVERTJOINNOCONDITIONALTASKTHRESHOLD); HiveAlgorithmsConf algorithmsConf = new HiveAlgorithmsConf(maxSplitSize, maxMemory); HiveRulesRegistry registry = new HiveRulesRegistry(); Properties calciteConfigProperties = new Properties(); calciteConfigProperties.setProperty( CalciteConnectionProperty.TIME_ZONE.camelName(), conf.getLocalTimeZone().getId()); calciteConfigProperties.setProperty( CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.FALSE.toString()); CalciteConnectionConfig calciteConfig = new CalciteConnectionConfigImpl(calciteConfigProperties); boolean isCorrelatedColumns = HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVE_CBO_STATS_CORRELATED_MULTI_KEY_JOINS); boolean heuristicMaterializationStrategy = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_MATERIALIZED_VIEW_REWRITING_SELECTION_STRATEGY).equals("heuristic"); HivePlannerContext confContext = new HivePlannerContext(algorithmsConf, registry, calciteConfig, corrScalarRexSQWithAgg, scalarAggNoGbyNoWin, new HiveConfPlannerContext(isCorrelatedColumns, heuristicMaterializationStrategy)); return HiveVolcanoPlanner.createPlanner(confContext); }
@Override public void write(Kryo kryo, Output output, TimestampTZ object) { output.writeLong(object.getEpochSecond()); output.writeInt(object.getNanos()); output.writeString(object.getZonedDateTime().getZone().getId()); }
@Override public void writeMarshallable(@NotNull WireOut wire) { wire.write(RollFields.length).int32(length) .write(RollFields.format).text(format) .write(RollFields.epoch).int64(epoch); if (rollTime != null) wire.write(RollFields.rollTime).time(rollTime); if (rollTimeZone != null) wire.write(RollFields.rollTimeZone).text(rollTimeZone.getId()); }
@Test public void testDate() { // Note: there is identical test for MySQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, postgresCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }
@Test public void testDate() { // Note: there is identical test for PostgreSQL ZoneId jvmZone = ZoneId.systemDefault(); checkState(jvmZone.getId().equals("America/Bahia_Banderas"), "This test assumes certain JVM time zone"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInJvmZone = LocalDate.of(1970, 1, 1); verify(jvmZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInJvmZone.atStartOfDay()).isEmpty()); ZoneId someZone = ZoneId.of("Europe/Vilnius"); LocalDate dateOfLocalTimeChangeForwardAtMidnightInSomeZone = LocalDate.of(1983, 4, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeForwardAtMidnightInSomeZone.atStartOfDay()).isEmpty()); LocalDate dateOfLocalTimeChangeBackwardAtMidnightInSomeZone = LocalDate.of(1983, 10, 1); verify(someZone.getRules().getValidOffsets(dateOfLocalTimeChangeBackwardAtMidnightInSomeZone.atStartOfDay().minusMinutes(1)).size() == 2); DataTypeTest testCases = DataTypeTest.create() .addRoundTrip(dateDataType(), LocalDate.of(1952, 4, 3)) // before epoch .addRoundTrip(dateDataType(), LocalDate.of(1970, 1, 1)) .addRoundTrip(dateDataType(), LocalDate.of(1970, 2, 3)) .addRoundTrip(dateDataType(), LocalDate.of(2017, 7, 1)) // summer on northern hemisphere (possible DST) .addRoundTrip(dateDataType(), LocalDate.of(2017, 1, 1)) // winter on northern hemisphere (possible DST on southern hemisphere) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInJvmZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeForwardAtMidnightInSomeZone) .addRoundTrip(dateDataType(), dateOfLocalTimeChangeBackwardAtMidnightInSomeZone); for (String timeZoneId : ImmutableList.of(UTC_KEY.getId(), jvmZone.getId(), someZone.getId())) { Session session = Session.builder(getQueryRunner().getDefaultSession()) .setTimeZoneKey(TimeZoneKey.getTimeZoneKey(timeZoneId)) .build(); testCases.execute(getQueryRunner(), session, mysqlCreateAndInsert("tpch.test_date")); testCases.execute(getQueryRunner(), session, prestoCreateAsSelect("test_date")); } }
public static byte[] encodeDateTimeArray( ZonedDateTime[] dateTimes ) { // We could store this in dateTimes.length * 2.5 if we wanted long[] data = new long[dateTimes.length * BLOCKS_DATETIME]; int i; for ( i = 0; i < dateTimes.length; i++ ) { data[i * BLOCKS_DATETIME] = dateTimes[i].toEpochSecond(); data[i * BLOCKS_DATETIME + 1] = dateTimes[i].getNano(); if ( dateTimes[i].getZone() instanceof ZoneOffset ) { ZoneOffset offset = (ZoneOffset) dateTimes[i].getZone(); int secondOffset = offset.getTotalSeconds(); // Set lowest bit to 1 means offset data[i * BLOCKS_DATETIME + 2] = secondOffset << 1 | 1L; } else { String timeZoneId = dateTimes[i].getZone().getId(); short zoneNumber = TimeZones.map( timeZoneId ); // Set lowest bit to 0 means zone id data[i * BLOCKS_DATETIME + 2] = zoneNumber << 1; } } TemporalHeader header = new TemporalHeader( TemporalType.TEMPORAL_DATE_TIME.temporalType ); byte[] bytes = DynamicArrayStore.encodeFromNumbers( data, DynamicArrayStore.TEMPORAL_HEADER_SIZE ); header.writeArrayHeaderTo( bytes ); return bytes; }
@Override public void writeDateTime( ZonedDateTime zonedDateTime ) throws IOException { long epochSecondLocal = zonedDateTime.toLocalDateTime().toEpochSecond( UTC ); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); packStructHeader( DATE_TIME_WITH_ZONE_OFFSET_SIZE, DATE_TIME_WITH_ZONE_OFFSET ); pack( epochSecondLocal ); pack( nano ); pack( offsetSeconds ); } else { String zoneId = zone.getId(); packStructHeader( DATE_TIME_WITH_ZONE_NAME_SIZE, DATE_TIME_WITH_ZONE_NAME ); pack( epochSecondLocal ); pack( nano ); pack( zoneId ); } } }
@Override public void write( Object value, FlushableChannel into ) throws IOException { ZonedDateTime zonedDateTime = (ZonedDateTime) value; long epochSecondUTC = zonedDateTime.toEpochSecond(); int nano = zonedDateTime.getNano(); ZoneId zone = zonedDateTime.getZone(); if ( zone instanceof ZoneOffset ) { int offsetSeconds = ((ZoneOffset) zone).getTotalSeconds(); into.put( (byte) 0 ); into.putLong( epochSecondUTC ); into.putInt( nano ); into.putInt( offsetSeconds ); } else { String zoneId = zone.getId(); into.put( (byte) 1 ); into.putLong( epochSecondUTC ); into.putInt( nano ); into.putInt( TimeZones.map( zoneId ) ); } } } );