Search in sources :

Example 6 with SqlTime

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

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

the class TestDateTimeOperatorsLegacy method testTimeRepresentation.

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

Aggregations

SqlTime (com.facebook.presto.common.type.SqlTime)7 SqlTimestamp (com.facebook.presto.common.type.SqlTimestamp)4 Type (com.facebook.presto.common.type.Type)4 SqlDate (com.facebook.presto.common.type.SqlDate)3 DateTime (org.joda.time.DateTime)3 Test (org.testng.annotations.Test)3 ArrayType (com.facebook.presto.common.type.ArrayType)2 CharType (com.facebook.presto.common.type.CharType)2 RowType (com.facebook.presto.common.type.RowType)2 TimestampType (com.facebook.presto.common.type.TimestampType)2 VarcharType (com.facebook.presto.common.type.VarcharType)2 PrestoException (com.facebook.presto.spi.PrestoException)2 ArrayList (java.util.ArrayList)2 RowPagesBuilder (com.facebook.presto.RowPagesBuilder)1 Block (com.facebook.presto.common.block.Block)1 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)1 SqlFunctionProperties (com.facebook.presto.common.function.SqlFunctionProperties)1 BigintType (com.facebook.presto.common.type.BigintType)1 BooleanType (com.facebook.presto.common.type.BooleanType)1 DateType (com.facebook.presto.common.type.DateType)1