assertEquals(UTC_KEY.getKey(), 0); assertEquals(UTC_KEY.getId(), "UTC"); assertSame(TimeZoneKey.getTimeZoneKey((short) 0), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Z"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("zulu"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("ZULU"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UCT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("Universal"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/utc"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt+00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/gmt-00:00"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/ut"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UT"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("etc/UCT"), UTC_KEY);
@Override protected Object getGreaterValue(Object value) { // time zone doesn't matter for ordering return packDateTimeWithZone(unpackMillisUtc((Long) value) + 10, getTimeZoneKeyForOffset(33)); } }
public static void assertDateTimeZoneEquals(String zoneId, DateTimeZone actualTimeZone) { DateTimeZone expectedDateTimeZone; if (isUtcZoneId(zoneId)) { expectedDateTimeZone = DateTimeZone.UTC; } else { expectedDateTimeZone = DateTimeZone.forID(zoneId); } assertEquals(actualTimeZone, expectedDateTimeZone); } }
@Test public void testZoneKeyLookup() { for (TimeZoneKey timeZoneKey : TimeZoneKey.getTimeZoneKeys()) { assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getKey()), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId()), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId().toUpperCase(ENGLISH)), timeZoneKey); assertSame(TimeZoneKey.getTimeZoneKey(timeZoneKey.getId().toLowerCase(ENGLISH)), timeZoneKey); } }
@Test public void testMaxTimeZoneKey() { boolean foundMax = false; for (TimeZoneKey timeZoneKey : TimeZoneKey.getTimeZoneKeys()) { assertTrue(timeZoneKey.getKey() <= MAX_TIME_ZONE_KEY, timeZoneKey + " key is larger than max key " + MAX_TIME_ZONE_KEY); foundMax = foundMax || (timeZoneKey.getKey() == MAX_TIME_ZONE_KEY); } assertTrue(foundMax, "Did not find a time zone with the MAX_TIME_ZONE_KEY"); }
@Test public void testSetTimeZoneId() throws Exception { TimeZoneKey defaultZoneKey = TimeZoneKey.getTimeZoneKey(TimeZone.getDefault().getID()); DateTimeZone defaultZone = DateTimeZone.forTimeZone(TimeZone.getDefault()); String sql = "SELECT current_timezone() zone, TIMESTAMP '2001-02-03 3:04:05' ts"; try (Connection connection = createConnection()) { try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), defaultZoneKey.getId()); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, defaultZone).getMillis())); } connection.unwrap(PrestoConnection.class).setTimeZoneId("UTC"); try (Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery(sql)) { assertTrue(rs.next()); assertEquals(rs.getString("zone"), "UTC"); assertEquals(rs.getTimestamp("ts"), new Timestamp(new DateTime(2001, 2, 3, 3, 4, 5, DateTimeZone.UTC).getMillis())); } } }
@Test public void test() TimeZoneKey.getTimeZoneKey("GMT-13:00"); TreeSet<String> jodaZones = new TreeSet<>(DateTimeZone.getAvailableIDs()); TreeSet<String> jdkZones = new TreeSet<>(ZoneId.getAvailableZoneIds()); assertEquals(jodaZones, jdkZones); DateTimeZone dateTimeZone = DateTimeZone.forID(zoneId); DateTimeZone indexedZone = getDateTimeZone(TimeZoneKey.getTimeZoneKey(zoneId)); DateTimeZone dateTimeZone = DateTimeZone.forOffsetHoursMinutes(offsetHours, offsetMinutes); assertTimeZone(dateTimeZone.getID(), dateTimeZone);
@Test public void testCastToTimeWithTimeZoneDSTIsNotAppliedWhenTimeCrossesDST() { // Australia/Sydney will switch DST a second after session start // For simplicity we have to use time zone that is going forward when entering DST zone with 1970-01-01 Session session = Session.builder(this.session) .setTimeZoneKey(getTimeZoneKey("Australia/Sydney")) .setStartTime(new DateTime(2017, 10, 1, 1, 59, 59, 999, getDateTimeZone(getTimeZoneKey("Australia/Sydney"))).getMillis()) .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(session)) { localAssertions.assertFunctionString("cast(TIME '12:00:00.000' as time with time zone)", TIME_WITH_TIME_ZONE, "12:00:00.000 Australia/Sydney"); } }
@Test public void testCreateSession() Session session = sessionSupplier.createSession(new QueryId("test_query_id"), context); assertEquals(session.getQueryId(), new QueryId("test_query_id")); assertEquals(session.getUser(), "testUser"); assertEquals(session.getSource().get(), "testSource"); assertEquals(session.getCatalog().get(), "testCatalog"); assertEquals(session.getSchema().get(), "testSchema"); assertEquals(session.getPath().getRawPath().get(), "testPath"); assertEquals(session.getLocale(), Locale.TAIWAN); assertEquals(session.getTimeZoneKey(), getTimeZoneKey("Asia/Taipei")); assertEquals(session.getRemoteUserAddress().get(), "testRemote"); assertEquals(session.getClientInfo().get(), "client-info");
@Test public void test() { TypeManager typeManager = new TypeRegistry(); FunctionRegistry functionRegistry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig()); ArrayType arrayOfBigintType = new ArrayType(BIGINT); FieldSetFilteringRecordSet fieldSetFilteringRecordSet = new FieldSetFilteringRecordSet( functionRegistry, new InMemoryRecordSet( ImmutableList.of(BIGINT, BIGINT, TIMESTAMP_WITH_TIME_ZONE, TIMESTAMP_WITH_TIME_ZONE, arrayOfBigintType, arrayOfBigintType), ImmutableList.of( ImmutableList.of( 100L, 100L, // test same time in different time zone to make sure equal check was done properly packDateTimeWithZone(100, getTimeZoneKeyForOffset(123)), packDateTimeWithZone(100, getTimeZoneKeyForOffset(234)), // test structural type arrayBlockOf(BIGINT, 12, 34, 56), arrayBlockOf(BIGINT, 12, 34, 56)))), ImmutableList.of(ImmutableSet.of(0, 1), ImmutableSet.of(2, 3), ImmutableSet.of(4, 5))); RecordCursor recordCursor = fieldSetFilteringRecordSet.cursor(); assertTrue(recordCursor.advanceNextPosition()); } }
@Test public void testZoneKeyData() { Hasher hasher = Hashing.murmur3_128().newHasher(); SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>() { @Override public int compare(TimeZoneKey left, TimeZoneKey right) { return Short.compare(left.getKey(), right.getKey()); } }, TimeZoneKey.getTimeZoneKeys()); for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) { hasher.putShort(timeZoneKey.getKey()); hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8); } // Zone file should not (normally) be changed, so let's make this more difficult assertEquals(hasher.hash().asLong(), -4582158485614614451L, "zone-index.properties file contents changed!"); }
@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")); } }
@Test public void testTimeZone() { assertFunction("hour(" + TIMESTAMP_LITERAL + ")", BIGINT, (long) TIMESTAMP.getHourOfDay()); assertFunction("minute(" + TIMESTAMP_LITERAL + ")", BIGINT, (long) TIMESTAMP.getMinuteOfHour()); assertFunction("hour(" + WEIRD_TIMESTAMP_LITERAL + ")", BIGINT, (long) WEIRD_TIMESTAMP.getHourOfDay()); assertFunction("minute(" + WEIRD_TIMESTAMP_LITERAL + ")", BIGINT, (long) WEIRD_TIMESTAMP.getMinuteOfHour()); assertFunction("current_timezone()", VARCHAR, TIME_ZONE_KEY.getId()); }
@Test public void testFromUnixTimeWithOffset() { DateTime dateTime = new DateTime(2001, 1, 22, 3, 4, 5, 0, DATE_TIME_ZONE); double seconds = dateTime.getMillis() / 1000.0; int timeZoneHoursOffset = 1; int timezoneMinutesOffset = 10; DateTime expected = new DateTime(dateTime, getDateTimeZone(getTimeZoneKeyForOffset((timeZoneHoursOffset * 60L) + timezoneMinutesOffset))); assertFunction("from_unixtime(" + seconds + ", " + timeZoneHoursOffset + ", " + timezoneMinutesOffset + ")", TIMESTAMP_WITH_TIME_ZONE, toTimestampWithTimeZone(expected)); // test invalid minute offsets assertInvalidFunction("from_unixtime(0, 1, 10000)", INVALID_FUNCTION_ARGUMENT); assertInvalidFunction("from_unixtime(0, 10000, 0)", INVALID_FUNCTION_ARGUMENT); assertInvalidFunction("from_unixtime(0, -100, 100)", INVALID_FUNCTION_ARGUMENT); }
assertSame(TimeZoneKey.getTimeZoneKey("GMT0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-0"), UTC_KEY); assertTimeZoneNotSupported("GMT7"); assertSame(TimeZoneKey.getTimeZoneKey("GMT+7"), PLUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-7"), MINUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT+7"), PLUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("GMT-7"), MINUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT-0"), UTC_KEY); assertTimeZoneNotSupported("UT7"); assertSame(TimeZoneKey.getTimeZoneKey("UT+7"), PLUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT-7"), MINUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT+7"), PLUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UT-7"), MINUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC-0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC+0"), UTC_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC-0"), UTC_KEY); assertTimeZoneNotSupported("UTC7"); assertSame(TimeZoneKey.getTimeZoneKey("UTC+7"), PLUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC-7"), MINUS_7_KEY); assertSame(TimeZoneKey.getTimeZoneKey("UTC+7"), PLUS_7_KEY);
@Test public void testCastToTimeWithTimeZoneWithTZWithRulesChanged() { TimeZoneKey timeZoneThatChangedSince1970 = getTimeZoneKey("Asia/Kathmandu"); DateTimeZone dateTimeZoneThatChangedSince1970 = getDateTimeZone(timeZoneThatChangedSince1970); Session session = Session.builder(this.session) .setTimeZoneKey(timeZoneThatChangedSince1970) .build(); try (FunctionAssertions localAssertions = new FunctionAssertions(session)) { localAssertions.assertFunction( "cast(TIME '03:04:05.321' as time with time zone)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, dateTimeZoneThatChangedSince1970).getMillis(), dateTimeZoneThatChangedSince1970.toTimeZone())); } }
@Override public FormatWriter createFileFormatWriter( ConnectorSession session, File targetFile, List<String> columnNames, List<Type> columnTypes, HiveCompressionCodec compressionCodec) throws IOException { return new PrestoDwrfFormatWriter( targetFile, columnNames, columnTypes, DateTimeZone.forID(session.getTimeZoneKey().getId()), compressionCodec); }
public void assertTimeZoneNotSupported(String zoneId) { try { TimeZoneKey.getTimeZoneKey(zoneId); fail("expect TimeZoneNotSupportedException"); } catch (TimeZoneNotSupportedException e) { // expected } } }
private static ZoneOffset toZoneOffset(TimeZoneKey timeZoneKey) { requireNonNull(timeZoneKey, "timeZoneKey is null"); if (Objects.equals("UTC", timeZoneKey.getId())) { return ZoneOffset.UTC; } checkArgument(timeZoneKey.getId().matches("[+-]\\d\\d:\\d\\d"), "Not a zone-offset timezone: %s", timeZoneKey); return ZoneOffset.of(timeZoneKey.getId()); }
public static long packDateTimeWithZone(long millisUtc, String zoneId) { return packDateTimeWithZone(millisUtc, getTimeZoneKey(zoneId)); }