Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.calcite.avatica.remote.TypedValue;
import org.apache.calcite.avatica.util.ArrayFactoryImpl;
import org.apache.calcite.avatica.util.Cursor;
import org.apache.calcite.avatica.util.DateTimeUtils;

import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -77,7 +78,9 @@ public AvaticaResultSet(AvaticaStatement statement,
ResultSetMetaData resultSetMetaData,
TimeZone timeZone,
Meta.Frame firstFrame) throws SQLException {
super(timeZone);
// Initialize the parent ArrayFactory with the UTC time zone because otherwise an array of
// date/time values would get double-adjusted by a client that calls Array.getResultSet().
super(DateTimeUtils.UTC_ZONE);
this.statement = statement;
this.state = state;
this.signature = signature;
Expand Down
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 @@ -161,26 +162,31 @@ protected Accessor createAccessor(ColumnMetaData columnMetaData,
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,
// so avoid normalizing against the provided calendar for this type.
final boolean 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 +244,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 Down Expand Up @@ -1013,10 +1025,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 +1043,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 +1070,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 @@ -1154,7 +1172,7 @@ static class TimeAccessor extends ObjectAccessor {
if (time == null) {
return null;
}
final int unix = DateTimeUtils.sqlTimeToUnixTime(time, localCalendar);
final int unix = DateTimeUtils.sqlTimeToUnixTime(time, (TimeZone) null);
return timeAsString(unix, null);
}

Expand All @@ -1178,18 +1196,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 +1239,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 +1261,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 +1276,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 +1303,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