Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CALCITE-5446] Add support for TIMESTAMP WITH LOCAL TIME ZONE #207

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 71 additions & 30 deletions core/src/main/java/org/apache/calcite/avatica/util/AbstractCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

/**
* Base class for implementing a cursor.
Expand Down Expand Up @@ -150,37 +151,45 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.TIME:
// TIME WITH LOCAL TIME ZONE is a standard ISO type without proper JDBC support.
// It represents a global instant in time, as opposed to local clock parameters.
boolean fixedInstant =
"TIME_WITH_LOCAL_TIME_ZONE".equals(columnMetaData.type.getName());
switch (columnMetaData.type.rep) {
case PRIMITIVE_INT:
case INTEGER:
case NUMBER:
return new TimeFromNumberAccessor(getter, localCalendar);
return new TimeFromNumberAccessor(getter, localCalendar, fixedInstant);
case JAVA_SQL_TIME:
return new TimeAccessor(getter, localCalendar);
return new TimeAccessor(getter, localCalendar, fixedInstant);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.TIMESTAMP:
// TIMESTAMP WITH LOCAL TIME ZONE is a standard ISO type without proper JDBC support.
// It represents a global instant in time, as opposed to local clock/calendar parameters.
fixedInstant =
"TIMESTAMP_WITH_LOCAL_TIME_ZONE".equals(columnMetaData.type.getName());
switch (columnMetaData.type.rep) {
case PRIMITIVE_LONG:
case LONG:
case NUMBER:
return new TimestampFromNumberAccessor(getter, localCalendar);
return new TimestampFromNumberAccessor(getter, localCalendar, fixedInstant);
case JAVA_SQL_TIMESTAMP:
return new TimestampAccessor(getter, localCalendar);
return new TimestampAccessor(getter, localCalendar, fixedInstant);
case JAVA_UTIL_DATE:
return new TimestampFromUtilDateAccessor(getter, localCalendar);
return new TimestampFromUtilDateAccessor(getter, localCalendar, fixedInstant);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case 2013: // TIME_WITH_TIMEZONE
case Types.TIME_WITH_TIMEZONE:
switch (columnMetaData.type.rep) {
case STRING:
return new StringAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case 2014: // TIMESTAMP_WITH_TIMEZONE
case Types.TIMESTAMP_WITH_TIMEZONE:
switch (columnMetaData.type.rep) {
case STRING:
return new StringAccessor(getter);
Expand Down Expand Up @@ -238,12 +247,18 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,

public abstract boolean next();

/** Accesses a timestamp value as a string.
/**
* Accesses a timestamp value as a string.
* The timestamp is in SQL format (e.g. "2013-09-22 22:30:32"),
* not Java format ("2013-09-22 22:30:32.123"). */
* not Java format ("2013-09-22 22:30:32.123").
*
* <p>Note that, when a TIMESTAMP is adjusted to a calendar, the offset is subtracted.
* Here, on the other hand, to adjust the string to the calendar (which only happens for type
* TIMESTAMP WITH LOCAL TIME ZONE), the offset is added. These are meant to be inverse operations.
*/
private static String timestampAsString(long v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
v += calendar.getTimeZone().getOffset(v);
}
return DateTimeUtils.unixTimestampToString(v);
}
Expand All @@ -254,12 +269,18 @@ private static String dateAsString(int v, Calendar calendar) {
return DateTimeUtils.unixDateToString(v);
}

/** Accesses a time value as a string, e.g. "22:30:32". */
/**
* Accesses a time value as a string, e.g. "22:30:32".
*
* <p>Note that, when a TIME is adjusted to a calendar, the offset is subtracted.
* Here, on the other hand, to adjust the string to the calendar (which only happens for type
* TIME WITH LOCAL TIME ZONE), the offset is added. These are meant to be inverse operations.
*/
private static String timeAsString(int v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
v += calendar.getTimeZone().getOffset(v);
}
return DateTimeUtils.unixTimeToString(v);
return DateTimeUtils.unixTimeToString(v % (int) DateTimeUtils.MILLIS_PER_DAY);
}

/** Implementation of {@link Cursor.Accessor}. */
Expand Down Expand Up @@ -955,10 +976,12 @@ protected Number getNumber() throws SQLException {
*/
static class TimeFromNumberAccessor extends NumberAccessor {
private final Calendar localCalendar;
private final boolean fixedInstant;

TimeFromNumberAccessor(Getter getter, Calendar localCalendar) {
TimeFromNumberAccessor(Getter getter, Calendar localCalendar, boolean fixedInstant) {
super(getter, 0);
this.localCalendar = localCalendar;
this.fixedInstant = fixedInstant;
}

@Override public Object getObject() throws SQLException {
Expand All @@ -970,6 +993,9 @@ static class TimeFromNumberAccessor extends NumberAccessor {
if (v == null) {
return null;
}
if (fixedInstant) {
calendar = null;
}
return DateTimeUtils.unixTimeToSqlTime(v.intValue(), calendar);
}

Expand All @@ -978,6 +1004,9 @@ static class TimeFromNumberAccessor extends NumberAccessor {
if (v == null) {
return null;
}
if (fixedInstant) {
calendar = null;
}
return DateTimeUtils.unixTimestampToSqlTimestamp(v.longValue(), calendar);
}

Expand All @@ -986,7 +1015,7 @@ static class TimeFromNumberAccessor extends NumberAccessor {
if (v == null) {
return null;
}
return timeAsString(v.intValue(), null);
return timeAsString(v.intValue(), fixedInstant ? localCalendar : null);
}

protected Number getNumber() throws SQLException {
Expand All @@ -1013,10 +1042,13 @@ protected Number getNumber() throws SQLException {
*/
static class TimestampFromNumberAccessor extends NumberAccessor {
private final Calendar localCalendar;
private final boolean fixedInstant;

TimestampFromNumberAccessor(Getter getter, Calendar localCalendar) {
TimestampFromNumberAccessor(
Getter getter, Calendar localCalendar, boolean fixedInstant) {
super(getter, 0);
this.localCalendar = localCalendar;
this.fixedInstant = fixedInstant;
}

@Override public Object getObject() throws SQLException {
Expand All @@ -1028,6 +1060,9 @@ static class TimestampFromNumberAccessor extends NumberAccessor {
if (v == null) {
return null;
}
if (fixedInstant) {
calendar = null;
}
return DateTimeUtils.unixTimestampToSqlTimestamp(v.longValue(), calendar);
}

Expand All @@ -1052,7 +1087,7 @@ static class TimestampFromNumberAccessor extends NumberAccessor {
if (v == null) {
return null;
}
return timestampAsString(v.longValue(), null);
return timestampAsString(v.longValue(), fixedInstant ? localCalendar : null);
}

protected Number getNumber() throws SQLException {
Expand Down Expand Up @@ -1130,18 +1165,20 @@ static class DateAccessor extends ObjectAccessor {
*/
static class TimeAccessor extends ObjectAccessor {
private final Calendar localCalendar;
private final boolean fixedInstant;

TimeAccessor(Getter getter, Calendar localCalendar) {
TimeAccessor(Getter getter, Calendar localCalendar, boolean fixedInstant) {
super(getter);
this.localCalendar = localCalendar;
this.fixedInstant = fixedInstant;
}

@Override public Time getTime(Calendar calendar) throws SQLException {
Time date = (Time) getObject();
if (date == null) {
return null;
}
if (calendar != null) {
if (calendar != null && !fixedInstant) {
long v = date.getTime();
v -= calendar.getTimeZone().getOffset(v);
date = new Time(v);
Expand All @@ -1154,8 +1191,8 @@ static class TimeAccessor extends ObjectAccessor {
if (time == null) {
return null;
}
final int unix = DateTimeUtils.sqlTimeToUnixTime(time, localCalendar);
return timeAsString(unix, null);
final int unix = DateTimeUtils.sqlTimeToUnixTime(time, (TimeZone) null);
return timeAsString(unix, fixedInstant ? localCalendar : null);
}

@Override public long getLong() throws SQLException {
Expand All @@ -1178,18 +1215,20 @@ static class TimeAccessor extends ObjectAccessor {
*/
static class TimestampAccessor extends ObjectAccessor {
private final Calendar localCalendar;
private final boolean fixedInstant;

TimestampAccessor(Getter getter, Calendar localCalendar) {
TimestampAccessor(Getter getter, Calendar localCalendar, boolean fixedInstant) {
super(getter);
this.localCalendar = localCalendar;
this.fixedInstant = fixedInstant;
}

@Override public Timestamp getTimestamp(Calendar calendar) throws SQLException {
Timestamp timestamp = (Timestamp) getObject();
if (timestamp == null) {
return null;
}
if (calendar != null) {
if (calendar != null && !fixedInstant) {
long v = timestamp.getTime();
v -= calendar.getTimeZone().getOffset(v);
timestamp = new Timestamp(v);
Expand Down Expand Up @@ -1219,8 +1258,8 @@ static class TimestampAccessor extends ObjectAccessor {
return null;
}
final long unix =
DateTimeUtils.sqlTimestampToUnixTimestamp(timestamp, localCalendar);
return timestampAsString(unix, null);
DateTimeUtils.sqlTimestampToUnixTimestamp(timestamp, (TimeZone) null);
return timestampAsString(unix, fixedInstant ? localCalendar : null);
}

@Override public long getLong() throws SQLException {
Expand All @@ -1241,11 +1280,13 @@ static class TimestampAccessor extends ObjectAccessor {
*/
static class TimestampFromUtilDateAccessor extends ObjectAccessor {
private final Calendar localCalendar;
private final boolean fixedInstant;

TimestampFromUtilDateAccessor(Getter getter,
Calendar localCalendar) {
TimestampFromUtilDateAccessor(
Getter getter, Calendar localCalendar, boolean fixedInstant) {
super(getter);
this.localCalendar = localCalendar;
this.fixedInstant = fixedInstant;
}

@Override public Timestamp getTimestamp(Calendar calendar) throws SQLException {
Expand All @@ -1254,7 +1295,7 @@ static class TimestampFromUtilDateAccessor extends ObjectAccessor {
return null;
}
long v = date.getTime();
if (calendar != null) {
if (calendar != null && !fixedInstant) {
v -= calendar.getTimeZone().getOffset(v);
}
return new Timestamp(v);
Expand All @@ -1281,8 +1322,8 @@ static class TimestampFromUtilDateAccessor extends ObjectAccessor {
if (date == null) {
return null;
}
final long unix = DateTimeUtils.utilDateToUnixTimestamp(date, localCalendar);
return timestampAsString(unix, null);
final long unix = DateTimeUtils.utilDateToUnixTimestamp(date, (TimeZone) null);
return timestampAsString(unix, fixedInstant ? localCalendar : null);
}

@Override public long getLong() throws SQLException {
Expand Down
Loading