Skip to content

Commit 04cd704

Browse files
committed
JDBC: Fix stackoverflow on getObject and timestamp conversion (#31735)
StackOverflowError fix in JdbcResultSet getObject method. Fix Timestamp conversion bug when getting the value of a time column.
1 parent 2fb2bb2 commit 04cd704

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/JdbcResultSet.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
344344
throw new SQLException("type is null");
345345
}
346346

347-
return getObject(columnIndex, type);
347+
return convert(columnIndex, type);
348348
}
349349

350350
private <T> T convert(int columnIndex, Class<T> type) throws SQLException {

x-pack/plugin/sql/jdbc/src/main/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ static Object convert(Object v, JDBCType columnType) throws SQLException {
206206
case REAL:
207207
return floatValue(v); // Float might be represented as string for infinity and NaN values
208208
case TIMESTAMP:
209-
return ((Number) v).longValue();
209+
return new Timestamp(((Number) v).longValue());
210210
default:
211211
throw new SQLException("Unexpected column type [" + columnType.getName() + "]");
212212

x-pack/plugin/sql/jdbc/src/test/java/org/elasticsearch/xpack/sql/jdbc/jdbc/TypeConverterTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.joda.time.DateTime;
1616

1717
import java.sql.JDBCType;
18+
import java.sql.Timestamp;
1819

1920
import static org.hamcrest.Matchers.instanceOf;
2021

@@ -42,8 +43,8 @@ public void testDoubleAsNative() throws Exception {
4243

4344
public void testTimestampAsNative() throws Exception {
4445
DateTime now = DateTime.now();
45-
assertThat(convertAsNative(now, JDBCType.TIMESTAMP), instanceOf(Long.class));
46-
assertEquals(now.getMillis(), convertAsNative(now, JDBCType.TIMESTAMP));
46+
assertThat(convertAsNative(now, JDBCType.TIMESTAMP), instanceOf(Timestamp.class));
47+
assertEquals(now.getMillis(), ((Timestamp) convertAsNative(now, JDBCType.TIMESTAMP)).getTime());
4748
}
4849

4950
private Object convertAsNative(Object value, JDBCType type) throws Exception {

0 commit comments

Comments
 (0)