Skip to content

Commit

Permalink
Speed up getDate by parsing bytes instead of String (#3141)
Browse files Browse the repository at this point in the history
* WIP speed up getDate

fix LocalDate tests

fix infinity comparison

binary tests

fix PolyNull

* keep old String API, add in new binary API

* fix up javadoc and deprecate old string methods
  • Loading branch information
davecramer committed Apr 15, 2024
1 parent d9483ed commit 4e710d5
Show file tree
Hide file tree
Showing 5 changed files with 273 additions and 93 deletions.
6 changes: 3 additions & 3 deletions pgjdbc/src/main/java/org/postgresql/jdbc/ArrayDecoding.java
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ Object parseValue(String stringVal, BaseConnection connection) throws SQLExcepti

@Override
Object parseValue(String stringVal, BaseConnection connection) throws SQLException {
return connection.getTimestampUtils().toDate(null, stringVal);
return connection.getTimestampUtils().toDate(null, stringVal.getBytes());
}
};

Expand All @@ -345,7 +345,7 @@ Object parseValue(String stringVal, BaseConnection connection) throws SQLExcepti

@Override
Object parseValue(String stringVal, BaseConnection connection) throws SQLException {
return connection.getTimestampUtils().toTime(null, stringVal);
return connection.getTimestampUtils().toTime(null, stringVal.getBytes());
}
};

Expand All @@ -354,7 +354,7 @@ Object parseValue(String stringVal, BaseConnection connection) throws SQLExcepti

@Override
Object parseValue(String stringVal, BaseConnection connection) throws SQLException {
return connection.getTimestampUtils().toTimestamp(null, stringVal);
return connection.getTimestampUtils().toTimestamp(null, stringVal.getBytes());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ protected BatchResultHandler createBatchHandler(Query[] queries,
}

String value = result.toString();
return getTimestampUtils().toDate(cal, value);
return getTimestampUtils().toDate(cal, value.getBytes());
}

@Override
Expand All @@ -491,7 +491,7 @@ protected BatchResultHandler createBatchHandler(Query[] queries,
}

String value = result.toString();
return getTimestampUtils().toTime(cal, value);
return getTimestampUtils().toTime(cal, value.getBytes());
}

@Override
Expand All @@ -503,7 +503,7 @@ protected BatchResultHandler createBatchHandler(Query[] queries,
}

String value = result.toString();
return getTimestampUtils().toTimestamp(cal, value);
return getTimestampUtils().toTimestamp(cal, value.getBytes());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ public void setObject(@Positive int parameterIndex, @Nullable Object in,
setDate(parameterIndex, (LocalDate) in);
break;
} else {
tmpd = getTimestampUtils().toDate(getDefaultCalendar(), in.toString());
tmpd = getTimestampUtils().toDate(getDefaultCalendar(), in.toString().getBytes());
}
setDate(parameterIndex, tmpd);
}
Expand All @@ -665,7 +665,7 @@ public void setObject(@Positive int parameterIndex, @Nullable Object in,
setTime(parameterIndex, (OffsetTime) in);
break;
} else {
tmpt = getTimestampUtils().toTime(getDefaultCalendar(), in.toString());
tmpt = getTimestampUtils().toTime(getDefaultCalendar(), in.toString().getBytes());
}
setTime(parameterIndex, tmpt);
}
Expand All @@ -683,7 +683,7 @@ public void setObject(@Positive int parameterIndex, @Nullable Object in,
setTimestamp(parameterIndex, (LocalDateTime) in);
break;
} else {
tmpts = getTimestampUtils().toTimestamp(getDefaultCalendar(), in.toString());
tmpts = getTimestampUtils().toTimestamp(getDefaultCalendar(), in.toString().getBytes());
}
setTimestamp(parameterIndex, tmpts);
}
Expand Down
27 changes: 14 additions & 13 deletions pgjdbc/src/main/java/org/postgresql/jdbc/PgResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public int getConcurrency() throws SQLException {
}
}

return getTimestampUtils().toDate(cal, castNonNull(getString(i)));
return getTimestampUtils().toDate(cal, castNonNull(value));
}

@Override
Expand Down Expand Up @@ -589,8 +589,7 @@ public int getConcurrency() throws SQLException {
}
}

String string = getString(i);
return getTimestampUtils().toTime(cal, string);
return getTimestampUtils().toTime(cal, value);
}

@Pure
Expand Down Expand Up @@ -631,7 +630,7 @@ public int getConcurrency() throws SQLException {
tsUnixEpochDate.setNanos(tsWithMicros.getNanos());
return tsUnixEpochDate;
} else if (oid == Oid.DATE) {
new Timestamp(castNonNull(getDate(i, cal)).getTime());
return new Timestamp(castNonNull(getDate(i, cal)).getTime());
} else {
throw new PSQLException(
GT.tr("Cannot convert the column of type {0} to requested type {1}.",
Expand All @@ -643,16 +642,15 @@ public int getConcurrency() throws SQLException {
// If this is actually a timestamptz, the server-provided timezone will override
// the one we pass in, which is the desired behaviour. Otherwise, we'll
// interpret the timezone-less value in the provided timezone.
String string = castNonNull(getString(i));
if (oid == Oid.TIME || oid == Oid.TIMETZ) {
// If server sends us a TIME, we ensure java counterpart has date of 1970-01-01
Timestamp tsWithMicros = getTimestampUtils().toTimestamp(cal, string);
Timestamp tsUnixEpochDate = new Timestamp(getTimestampUtils().toTime(cal, string).getTime());
Timestamp tsWithMicros = getTimestampUtils().toTimestamp(cal, value);
Timestamp tsUnixEpochDate = new Timestamp(getTimestampUtils().toTime(cal, value).getTime());
tsUnixEpochDate.setNanos(tsWithMicros.getNanos());
return tsUnixEpochDate;
}

return getTimestampUtils().toTimestamp(cal, string);
return getTimestampUtils().toTimestamp(cal, value);

}

Expand Down Expand Up @@ -681,7 +679,7 @@ public int getConcurrency() throws SQLException {

if (oid == Oid.TIMESTAMPTZ || oid == Oid.TIMESTAMP ) {

OffsetDateTime offsetDateTime = getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
OffsetDateTime offsetDateTime = getTimestampUtils().toOffsetDateTime(value);
if ( offsetDateTime != OffsetDateTime.MAX && offsetDateTime != OffsetDateTime.MIN ) {
return offsetDateTime.withOffsetSameInstant(ZoneOffset.UTC);
} else {
Expand All @@ -690,7 +688,7 @@ public int getConcurrency() throws SQLException {

}
if ( oid == Oid.TIMETZ ) {
return getTimestampUtils().toOffsetDateTime(castNonNull(getString(i)));
return getTimestampUtils().toOffsetDateTime(value);
}
}

Expand All @@ -713,7 +711,7 @@ public int getConcurrency() throws SQLException {
if (isBinary(i)) {
return getTimestampUtils().toOffsetTimeBin(value);
} else {
return getTimestampUtils().toOffsetTime(castNonNull(getString(i)));
return getTimestampUtils().toOffsetTime(castNonNull(getRawValue(i)));
}
}

Expand Down Expand Up @@ -763,8 +761,11 @@ public int getConcurrency() throws SQLException {
}
} else {
// string
if (oid == Oid.DATE || oid == Oid.TIMESTAMP) {
return getTimestampUtils().toLocalDateTime(castNonNull(getString(i))).toLocalDate();
if (oid == Oid.DATE ) {
return getTimestampUtils().toLocalDate(value);
}
if (oid == Oid.TIMESTAMP) {
return getTimestampUtils().toLocalDateTime(castNonNull(getRawValue(i))).toLocalDate();
}
}

Expand Down

0 comments on commit 4e710d5

Please sign in to comment.