Search in sources :

Example 1 with TimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey in project presto by prestodb.

the class TestDateTimeFunctionsBase method testCurrentDateTimezone.

@Test
public void testCurrentDateTimezone() {
    TimeZoneKey kievTimeZoneKey = getTimeZoneKey("Europe/Kiev");
    // The zone has 'gap' on 1970-01-01
    TimeZoneKey bahiaBanderasTimeZoneKey = getTimeZoneKey("America/Bahia_Banderas");
    TimeZoneKey montrealTimeZoneKey = getTimeZoneKey("America/Montreal");
    long timeIncrement = TimeUnit.MINUTES.toMillis(53);
    // We expect UTC millis later on so we have to use UTC chronology
    for (long instant = ISOChronology.getInstanceUTC().getDateTimeMillis(2000, 6, 15, 0, 0, 0, 0); instant < ISOChronology.getInstanceUTC().getDateTimeMillis(2016, 6, 15, 0, 0, 0, 0); instant += timeIncrement) {
        assertCurrentDateAtInstant(kievTimeZoneKey, instant);
        assertCurrentDateAtInstant(bahiaBanderasTimeZoneKey, instant);
        assertCurrentDateAtInstant(montrealTimeZoneKey, instant);
        assertCurrentDateAtInstant(TIME_ZONE_KEY, instant);
    }
}
Also used : TimeZoneKey.getTimeZoneKey(com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) Test(org.testng.annotations.Test)

Example 2 with TimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey in project presto by prestodb.

the class TestDateTimeFunctionsBase method testTimeWithTimeZoneAtTimeZone.

@Test
public void testTimeWithTimeZoneAtTimeZone() {
    // this test does use hidden at_timezone function as it is equivalent of using SQL syntax AT TIME ZONE
    // but our test framework doesn't support that syntax directly.
    Session oldKathmanduTimeZoneOffsetSession = Session.builder(this.session).setTimeZoneKey(TIME_ZONE_KEY).setStartTime(new DateTime(1980, 1, 1, 10, 0, 0, DATE_TIME_ZONE).getMillis()).build();
    TimeZoneKey europeWarsawTimeZoneKey = getTimeZoneKey("Europe/Warsaw");
    DateTimeZone europeWarsawTimeZone = getDateTimeZone(europeWarsawTimeZoneKey);
    Session europeWarsawSessionWinter = Session.builder(this.session).setTimeZoneKey(europeWarsawTimeZoneKey).setStartTime(new DateTime(2017, 1, 1, 10, 0, 0, europeWarsawTimeZone).getMillis()).build();
    try (FunctionAssertions europeWarsawAssertionsWinter = new FunctionAssertions(europeWarsawSessionWinter);
        FunctionAssertions oldKathmanduTimeZoneOffsetAssertions = new FunctionAssertions(oldKathmanduTimeZoneOffsetSession)) {
        long millisTenOClockWarsawWinter = new DateTime(1970, 1, 1, 9, 0, 0, 0, UTC_TIME_ZONE).getMillis();
        // Simple shift to UTC
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, UTC_KEY));
        // Simple shift to fixed TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', '+00:45')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("+00:45")));
        // Simple shift to geographical TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'America/New_York')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("America/New_York")));
        // No shift but different time zone
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Berlin')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, getTimeZoneKey("Europe/Berlin")));
        // Noop on UTC
        assertFunction("at_timezone(TIME '10:00 UTC', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 10, 0, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Noop on other TZ
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, europeWarsawTimeZoneKey));
        // Noop on other TZ on different session TZ
        assertFunction("at_timezone(TIME '10:00 Europe/Warsaw', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(millisTenOClockWarsawWinter, europeWarsawTimeZoneKey));
        // Shift through days back
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '2:00 Europe/Warsaw', 'America/New_York')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("America/New_York"))).getMillis(), getTimeZoneKey("America/New_York")));
        // Shift through days forward
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '22:00 America/New_York', 'Europe/Warsaw')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 0, 0, 0, europeWarsawTimeZone).getMillis(), europeWarsawTimeZoneKey));
        // Shift backward on min value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '00:00 +14:00', '+13:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 23, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00")));
        // Shift backward on min value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '00:00 +14:00', '-14:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 20, 0, 0, 0, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00")));
        // Shift backward on max value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '23:59:59.999 +14:00', '+13:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 22, 59, 59, 999, getDateTimeZone(getTimeZoneKey("+13:00"))).getMillis(), getTimeZoneKey("+13:00")));
        // Shift forward on max value
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '23:59:59.999 +14:00', '-14:00')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 19, 59, 59, 999, getDateTimeZone(getTimeZoneKey("-14:00"))).getMillis(), getTimeZoneKey("-14:00")));
        // Asia/Kathmandu used +5:30 TZ until 1986 and than switched to +5:45
        // This test checks if we do use offset of time zone valid currently and not the historical one
        assertFunction("at_timezone(TIME '10:00 Asia/Kathmandu', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 15, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Noop when time zone doesn't change
        TimeZoneKey kabul = TimeZoneKey.getTimeZoneKey("Asia/Kabul");
        assertFunction("at_timezone(TIME '10:00 Asia/Kabul', 'Asia/Kabul')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 10, 0, 0, 0, getDateTimeZone(kabul)).getMillis(), kabul));
        // This test checks if the TZ offset isn't calculated on other fixed point in time by checking if
        // session started in 1980 would get historical Asia/Kathmandu offset.
        oldKathmanduTimeZoneOffsetAssertions.assertFunction("at_timezone(TIME '10:00 Asia/Kathmandu', 'UTC')", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 4, 30, 0, 0, UTC_TIME_ZONE).getMillis(), TimeZoneKey.UTC_KEY));
        // Check simple interval shift
        europeWarsawAssertionsWinter.assertFunction("at_timezone(TIME '10:00 +01:00', INTERVAL '2' HOUR)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 11, 0, 0, 0, getDateTimeZone(getTimeZoneKey("+02:00"))).getMillis(), getTimeZoneKey("+02:00")));
        // Check to high interval shift
        europeWarsawAssertionsWinter.assertInvalidFunction("at_timezone(TIME '10:00 +01:00', INTERVAL '60' HOUR)", StandardErrorCode.INVALID_FUNCTION_ARGUMENT, "Invalid offset minutes 3600");
    }
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) TimeZoneKey.getTimeZoneKey(com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone) TestingSession(com.facebook.presto.testing.TestingSession) Session(com.facebook.presto.Session) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) Test(org.testng.annotations.Test)

Example 3 with TimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey in project presto by prestodb.

the class TestPrestoDriver method testSetTimeZoneId.

@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()));
        }
    }
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) Timestamp(java.sql.Timestamp) DateTimeZone(org.joda.time.DateTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 4 with TimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey in project presto by prestodb.

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillisUtc();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(com.facebook.presto.common.type.VarcharType) SqlTime(com.facebook.presto.common.type.SqlTime) RowType(com.facebook.presto.common.type.RowType) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) ArrayType(com.facebook.presto.common.type.ArrayType) VarcharType(com.facebook.presto.common.type.VarcharType) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) Entry(java.util.Map.Entry) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(com.facebook.presto.common.type.CharType) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(com.facebook.presto.common.block.BlockBuilder)

Example 5 with TimeZoneKey

use of com.facebook.presto.common.type.TimeZoneKey in project presto by prestodb.

the class TestTimeBase method testCastToTimeWithTimeZoneWithTZWithRulesChanged.

@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(), TimeZone.getTimeZone(ZoneId.of(dateTimeZoneThatChangedSince1970.getID()))));
    }
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) FunctionAssertions(com.facebook.presto.operator.scalar.FunctionAssertions) TimeZoneKey.getTimeZoneKey(com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey) TimeZoneKey(com.facebook.presto.common.type.TimeZoneKey) DateTimeZone(org.joda.time.DateTimeZone) DateTimeZoneIndex.getDateTimeZone(com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone) DateTime(org.joda.time.DateTime) TestingSession(com.facebook.presto.testing.TestingSession) Session(com.facebook.presto.Session) Test(org.testng.annotations.Test)

Aggregations

TimeZoneKey (com.facebook.presto.common.type.TimeZoneKey)5 Test (org.testng.annotations.Test)4 TimeZoneKey.getTimeZoneKey (com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey)3 DateTime (org.joda.time.DateTime)3 DateTimeZone (org.joda.time.DateTimeZone)3 Session (com.facebook.presto.Session)2 SqlTimeWithTimeZone (com.facebook.presto.common.type.SqlTimeWithTimeZone)2 TestingSession (com.facebook.presto.testing.TestingSession)2 DateTimeZoneIndex.getDateTimeZone (com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone)2 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 ArrayType (com.facebook.presto.common.type.ArrayType)1 CharType (com.facebook.presto.common.type.CharType)1 MapType (com.facebook.presto.common.type.MapType)1 RowType (com.facebook.presto.common.type.RowType)1 SqlTime (com.facebook.presto.common.type.SqlTime)1 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)1 Type (com.facebook.presto.common.type.Type)1 VarcharType (com.facebook.presto.common.type.VarcharType)1 FunctionAssertions (com.facebook.presto.operator.scalar.FunctionAssertions)1 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)1