Skip to content

Commit

Permalink
Rename SqlTime#millisUtc to millis
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Jun 27, 2018
1 parent d7792fc commit 828f3c6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
Expand Up @@ -44,6 +44,7 @@
import java.time.OffsetTime; import java.time.OffsetTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
Expand Down Expand Up @@ -273,8 +274,13 @@ else if (DATE.equals(type)) {
type.writeLong(blockBuilder, days); type.writeLong(blockBuilder, days);
} }
else if (TIME.equals(type)) { else if (TIME.equals(type)) {
long millisUtc = ((SqlTime) value).getMillisUtc(); SqlTime time = (SqlTime) value;
type.writeLong(blockBuilder, millisUtc); if (time.isLegacyTimestamp()) {
type.writeLong(blockBuilder, time.getMillisUtc());
}
else {
type.writeLong(blockBuilder, time.getMillis());
}
} }
else if (TIME_WITH_TIME_ZONE.equals(type)) { else if (TIME_WITH_TIME_ZONE.equals(type)) {
long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc(); long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
Expand Down Expand Up @@ -350,7 +356,7 @@ private static MaterializedRow convertToTestTypes(MaterializedRow prestoRow)
convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays()); convertedValue = LocalDate.ofEpochDay(((SqlDate) prestoValue).getDays());
} }
else if (prestoValue instanceof SqlTime) { else if (prestoValue instanceof SqlTime) {
convertedValue = LocalTime.ofNanoOfDay(MILLISECONDS.toNanos(((SqlTime) prestoValue).getMillisUtc())); convertedValue = DateTimeFormatter.ISO_LOCAL_TIME.parse(prestoValue.toString(), LocalTime::from);
} }
else if (prestoValue instanceof SqlTimeWithTimeZone) { else if (prestoValue instanceof SqlTimeWithTimeZone) {
// Political timezone cannot be represented in OffsetTime and there isn't any better representation. // Political timezone cannot be represented in OffsetTime and there isn't any better representation.
Expand Down
35 changes: 25 additions & 10 deletions presto-spi/src/main/java/com/facebook/presto/spi/type/SqlTime.java
Expand Up @@ -26,25 +26,33 @@ public final class SqlTime
{ {
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS"); private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss.SSS");


private final long millisUtc; private final long millis;
private final Optional<TimeZoneKey> sessionTimeZoneKey; private final Optional<TimeZoneKey> sessionTimeZoneKey;


public SqlTime(long millisUtc) public SqlTime(long millis)
{ {
this.millisUtc = millisUtc; this.millis = millis;
this.sessionTimeZoneKey = Optional.empty(); this.sessionTimeZoneKey = Optional.empty();
} }


@Deprecated @Deprecated
public SqlTime(long millisUtc, TimeZoneKey sessionTimeZoneKey) public SqlTime(long millisUtc, TimeZoneKey sessionTimeZoneKey)
{ {
this.millisUtc = millisUtc; this.millis = millisUtc;
this.sessionTimeZoneKey = Optional.of(sessionTimeZoneKey); this.sessionTimeZoneKey = Optional.of(sessionTimeZoneKey);
} }


public long getMillis()
{
checkState(!isLegacyTimestamp(), "getMillis() can be called in new timestamp semantics only");
return millis;
}

@Deprecated
public long getMillisUtc() public long getMillisUtc()
{ {
return millisUtc; checkState(isLegacyTimestamp(), "getMillisUtc() can be called in legacy timestamp semantics only");
return millis;
} }


@Deprecated @Deprecated
Expand All @@ -54,15 +62,15 @@ public Optional<TimeZoneKey> getSessionTimeZoneKey()
} }


@Deprecated @Deprecated
private boolean isLegacyTimestamp() public boolean isLegacyTimestamp()
{ {
return sessionTimeZoneKey.isPresent(); return sessionTimeZoneKey.isPresent();
} }


@Override @Override
public int hashCode() public int hashCode()
{ {
return Objects.hash(millisUtc, sessionTimeZoneKey); return Objects.hash(millis, sessionTimeZoneKey);
} }


@Override @Override
Expand All @@ -75,7 +83,7 @@ public boolean equals(Object obj)
return false; return false;
} }
SqlTime other = (SqlTime) obj; SqlTime other = (SqlTime) obj;
return Objects.equals(this.millisUtc, other.millisUtc) && return Objects.equals(this.millis, other.millis) &&
Objects.equals(this.sessionTimeZoneKey, other.sessionTimeZoneKey); Objects.equals(this.sessionTimeZoneKey, other.sessionTimeZoneKey);
} }


Expand All @@ -84,10 +92,17 @@ public boolean equals(Object obj)
public String toString() public String toString()
{ {
if (isLegacyTimestamp()) { if (isLegacyTimestamp()) {
return Instant.ofEpochMilli(millisUtc).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter); return Instant.ofEpochMilli(millis).atZone(ZoneId.of(sessionTimeZoneKey.get().getId())).format(formatter);
} }
else { else {
return Instant.ofEpochMilli(millisUtc).atZone(ZoneOffset.UTC).format(formatter); return Instant.ofEpochMilli(millis).atZone(ZoneOffset.UTC).format(formatter);
}
}

private static void checkState(boolean condition, String message)
{
if (!condition) {
throw new IllegalStateException(message);
} }
} }
} }

0 comments on commit 828f3c6

Please sign in to comment.