Search in sources :

Example 6 with SqlTimeWithTimeZone

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

the class TestTimestampWithTimeZoneBase method testCastToTimeWithTimeZone.

@Test
public void testCastToTimeWithTimeZone() {
    assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321 +07:09' as time with time zone)", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    functionAssertions.assertFunctionString("cast(TIMESTAMP '2001-1-22 03:04:05.321 +07:09' as time with time zone)", TIME_WITH_TIME_ZONE, "03:04:05.321 +07:09");
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 7 with SqlTimeWithTimeZone

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

the class MaterializedResult method convertToTestTypes.

private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow) {
    List<Object> convertedValues = new ArrayList<>();
    for (int field = 0; field < prestoRow.getFieldCount(); field++) {
        Object prestoValue = prestoRow.getField(field);
        Object convertedValue;
        if (prestoValue instanceof SqlDate) {
            convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays());
        } else if (prestoValue instanceof SqlTime) {
            convertedValue = DateTimeFormatter.ISO_LOCAL_TIME.parse(prestoValue.toString(), LocalTime::from);
        } else if (prestoValue instanceof SqlTimeWithTimeZone) {
            // Political timezone cannot be represented in OffsetTime and there isn't any better representation.
            long millisUtc = ((SqlTimeWithTimeZone) prestoValue).getMillisUtc();
            ZoneOffset zone = toZoneOffset(((SqlTimeWithTimeZone) prestoValue).getTimeZoneKey());
            convertedValue = OffsetTime.of(LocalTime.ofNanoOfDay(MILLISECONDS.toNanos(millisUtc) + SECONDS.toNanos(zone.getTotalSeconds())), zone);
        } else if (prestoValue instanceof SqlTimestamp) {
            convertedValue = SqlTimestamp.JSON_FORMATTER.parse(prestoValue.toString(), LocalDateTime::from);
        } else if (prestoValue instanceof SqlTimestampWithTimeZone) {
            convertedValue = Instant.ofEpochMilli(((SqlTimestampWithTimeZone) prestoValue).getMillisUtc()).atZone(ZoneId.of(((SqlTimestampWithTimeZone) prestoValue).getTimeZoneKey().getId()));
        } else if (prestoValue instanceof SqlDecimal) {
            convertedValue = ((SqlDecimal) prestoValue).toBigDecimal();
        } else {
            convertedValue = prestoValue;
        }
        convertedValues.add(convertedValue);
    }
    return new MaterializedRow(prestoRow.getPrecision(), convertedValues);
}
Also used : LocalDateTime(java.time.LocalDateTime) LocalTime(java.time.LocalTime) ArrayList(java.util.ArrayList) SqlTime(com.facebook.presto.common.type.SqlTime) SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) SqlDecimal(com.facebook.presto.common.type.SqlDecimal) ZoneOffset(java.time.ZoneOffset) SqlTimestampWithTimeZone(com.facebook.presto.common.type.SqlTimestampWithTimeZone) SqlDate(com.facebook.presto.common.type.SqlDate)

Example 8 with SqlTimeWithTimeZone

use of com.facebook.presto.common.type.SqlTimeWithTimeZone 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)

Example 9 with SqlTimeWithTimeZone

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

the class TestDateTimeOperatorsLegacy method testTimeWithTimeZoneRepresentation.

@Test
public void testTimeWithTimeZoneRepresentation() {
    // PST -> PDT date
    testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(37800000, getTimeZoneKey("America/Los_Angeles")));
    testTimeRepresentationOnDate(new DateTime(2017, 3, 12, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(41400000, getTimeZoneKey("America/Los_Angeles")));
    // PDT -> PST date
    testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(37800000, getTimeZoneKey("America/Los_Angeles")));
    testTimeRepresentationOnDate(new DateTime(2017, 10, 4, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(41400000, getTimeZoneKey("America/Los_Angeles")));
    // PDT date
    testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(37800000, getTimeZoneKey("America/Los_Angeles")));
    testTimeRepresentationOnDate(new DateTime(2017, 6, 6, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(41400000, getTimeZoneKey("America/Los_Angeles")));
    // PST date
    testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '02:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(37800000, getTimeZoneKey("America/Los_Angeles")));
    testTimeRepresentationOnDate(new DateTime(2017, 11, 1, 10, 0, 0, 0, DateTimeZone.UTC), "TIME '03:30:00.000 America/Los_Angeles'", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(41400000, getTimeZoneKey("America/Los_Angeles")));
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Example 10 with SqlTimeWithTimeZone

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

the class TestDateTimeOperatorsBase method testTimeMinusInterval.

@Test
public void testTimeMinusInterval() {
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' hour", TIME, sqlTimeOf(0, 4, 5, 321, session));
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' day", TIME, sqlTimeOf(3, 4, 5, 321, session));
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' month", TIME, sqlTimeOf(3, 4, 5, 321, session));
    assertFunction("TIME '03:04:05.321' - INTERVAL '3' year", TIME, sqlTimeOf(3, 4, 5, 321, session));
    assertFunction("TIME '03:04:05.321' - INTERVAL '6' hour", TIME, sqlTimeOf(21, 4, 5, 321, session));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 0, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' day", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' month", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '3' year", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 3, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
    assertFunction("TIME '03:04:05.321 +05:09' - INTERVAL '6' hour", TIME_WITH_TIME_ZONE, new SqlTimeWithTimeZone(new DateTime(1970, 1, 1, 21, 4, 5, 321, WEIRD_TIME_ZONE).getMillis(), WEIRD_TIME_ZONE_KEY));
}
Also used : SqlTimeWithTimeZone(com.facebook.presto.common.type.SqlTimeWithTimeZone) DateTime(org.joda.time.DateTime) Test(org.testng.annotations.Test)

Aggregations

SqlTimeWithTimeZone (com.facebook.presto.common.type.SqlTimeWithTimeZone)10 DateTime (org.joda.time.DateTime)9 Test (org.testng.annotations.Test)9 Session (com.facebook.presto.Session)2 TimeZoneKey (com.facebook.presto.common.type.TimeZoneKey)2 TimeZoneKey.getTimeZoneKey (com.facebook.presto.common.type.TimeZoneKey.getTimeZoneKey)2 TestingSession (com.facebook.presto.testing.TestingSession)2 DateTimeZoneIndex.getDateTimeZone (com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone)2 DateTimeZone (org.joda.time.DateTimeZone)2 SqlDate (com.facebook.presto.common.type.SqlDate)1 SqlDecimal (com.facebook.presto.common.type.SqlDecimal)1 SqlTime (com.facebook.presto.common.type.SqlTime)1 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)1 SqlTimestampWithTimeZone (com.facebook.presto.common.type.SqlTimestampWithTimeZone)1 FunctionAssertions (com.facebook.presto.operator.scalar.FunctionAssertions)1 TestingConnectorSession (com.facebook.presto.testing.TestingConnectorSession)1 LocalDateTime (java.time.LocalDateTime)1 LocalTime (java.time.LocalTime)1 ZoneOffset (java.time.ZoneOffset)1 ArrayList (java.util.ArrayList)1