Skip to content

Commit

Permalink
Align TIMESTAMP -> TIMESTAMP WITH TIME ZONE cast with standard
Browse files Browse the repository at this point in the history
  • Loading branch information
fiedukow committed Mar 10, 2017
1 parent bf1e739 commit 9dc288e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.type.AbstractLongType;
import com.facebook.presto.spi.type.StandardTypes;
import com.facebook.presto.spi.type.TimeZoneKey;
import io.airlift.slice.Slice;
import org.joda.time.chrono.ISOChronology;

Expand All @@ -39,11 +40,13 @@
import static com.facebook.presto.spi.function.OperatorType.LESS_THAN_OR_EQUAL;
import static com.facebook.presto.spi.function.OperatorType.NOT_EQUAL;
import static com.facebook.presto.spi.type.DateTimeEncoding.packDateTimeWithZone;
import static com.facebook.presto.spi.type.TimeZoneKey.getTimeZoneKeyForOffset;
import static com.facebook.presto.type.DateTimeOperators.modulo24Hour;
import static com.facebook.presto.util.DateTimeUtils.parseTimestampWithTimeZone;
import static com.facebook.presto.util.DateTimeUtils.parseTimestampWithoutTimeZone;
import static com.facebook.presto.util.DateTimeUtils.printTimestampWithoutTimeZone;
import static com.facebook.presto.util.DateTimeZoneIndex.getChronology;
import static com.facebook.presto.util.DateTimeZoneIndex.getDateTimeZone;
import static io.airlift.slice.SliceUtf8.trim;
import static io.airlift.slice.Slices.utf8Slice;

Expand Down Expand Up @@ -159,11 +162,14 @@ public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlTyp
return packDateTimeWithZone(value, session.getTimeZoneKey());
}
else {
ISOChronology localChronology = getChronology(session.getTimeZoneKey());
long millisOffset = getDateTimeZone(session.getTimeZoneKey()).getOffset(session.getStartTime());
TimeZoneKey fixedTimeZoneKey = getTimeZoneKeyForOffset(TimeUnit.MILLISECONDS.toMinutes(millisOffset));

ISOChronology localChronology = getChronology(fixedTimeZoneKey);

// This cast does treat TIMESTAMP as wall time in session TZ. This means that in order to get
// its UTC representation we need to shift the value by the offset of TZ.
return packDateTimeWithZone(value - localChronology.getZone().getOffset(value), session.getTimeZoneKey());
return packDateTimeWithZone(value - localChronology.getZone().getOffset(value), fixedTimeZoneKey);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.type.SqlDate;
import com.facebook.presto.spi.type.SqlTimeWithTimeZone;
import com.facebook.presto.spi.type.SqlTimestampWithTimeZone;
import com.facebook.presto.spi.type.TimeZoneKey;
import com.facebook.presto.spi.type.Type;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -200,9 +199,15 @@ public void testCastToTimeWithTimeZone()
@Test
public void testCastToTimestampWithTimeZone()
{
assertFunction("cast(TIMESTAMP '2001-1-22 03:04:05.321' as timestamp with time zone)",
Session localSession = testSessionBuilder()
.setTimeZoneKey(TIME_ZONE_KEY)
.setStartTime(new DateTime(2017, 3, 1, 12, 34, 56, 789, UTC).getMillis())
.build();
FunctionAssertions localAssertions = new FunctionAssertions(localSession);

localAssertions.assertFunctionString("cast(TIMESTAMP '2001-1-22 03:04:05.321' as timestamp with time zone)",
TIMESTAMP_WITH_TIME_ZONE,
new SqlTimestampWithTimeZone(new DateTime(2001, 1, 22, 3, 4, 5, 321, DATE_TIME_ZONE).getMillis(), DATE_TIME_ZONE.toTimeZone()));
"2001-01-22 03:04:05.321 +01:00");
}

@Test
Expand Down

0 comments on commit 9dc288e

Please sign in to comment.