Skip to content

Commit 066e25a

Browse files
SNOW-1232333 - add mapping for basic types in method getObject (#1669)
1 parent 2d14efc commit 066e25a

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1354,8 +1354,38 @@ public <T> T getObject(int columnIndex, Class<T> type) throws SQLException {
13541354
} else if (Map.class.isAssignableFrom(type)) {
13551355
JsonNode jsonNode = ((JsonSqlInput) getObject(columnIndex)).getInput();
13561356
return (T) OBJECT_MAPPER.convertValue(jsonNode, new TypeReference<Map<String, Object>>() {});
1357+
} else if (String.class.isAssignableFrom(type)) {
1358+
return (T) getString(columnIndex);
1359+
} else if (Boolean.class.isAssignableFrom(type)) {
1360+
return (T) (Boolean) getBoolean(columnIndex);
1361+
} else if (Byte.class.isAssignableFrom(type)) {
1362+
return (T) (Byte) getByte(columnIndex);
1363+
} else if (Short.class.isAssignableFrom(type)) {
1364+
return (T) (Short) getShort(columnIndex);
1365+
} else if (Integer.class.isAssignableFrom(type)) {
1366+
return (T) (Integer) getInt(columnIndex);
1367+
} else if (Long.class.isAssignableFrom(type)) {
1368+
return (T) (Long) getLong(columnIndex);
1369+
} else if (Float.class.isAssignableFrom(type)) {
1370+
return (T) (Float) getFloat(columnIndex);
1371+
} else if (Double.class.isAssignableFrom(type)) {
1372+
return (T) (Double) getDouble(columnIndex);
1373+
} else if (Date.class.isAssignableFrom(type)) {
1374+
return (T) getDate(columnIndex);
1375+
} else if (Date.class.isAssignableFrom(type)) {
1376+
return (T) getDate(columnIndex);
1377+
} else if (Time.class.isAssignableFrom(type)) {
1378+
return (T) getTime(columnIndex);
1379+
} else if (Timestamp.class.isAssignableFrom(type)) {
1380+
return (T) getTimestamp(columnIndex);
1381+
} else if (BigDecimal.class.isAssignableFrom(type)) {
1382+
return (T) getBigDecimal(columnIndex);
13571383
} else {
1358-
return (T) getObject(columnIndex);
1384+
logger.debug(
1385+
"Unsupported type passed to getObject(int columnIndex,Class<T> type): " + type.getName());
1386+
throw new SQLException(
1387+
"Type passed to 'getObject(int columnIndex,Class<T> type)' is unsupported. Type: "
1388+
+ type.getName());
13591389
}
13601390
}
13611391

src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java

+87
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.sql.Clob;
2323
import java.sql.Connection;
2424
import java.sql.DatabaseMetaData;
25+
import java.sql.Date;
2526
import java.sql.PreparedStatement;
2627
import java.sql.ResultSet;
2728
import java.sql.ResultSetMetaData;
@@ -31,6 +32,9 @@
3132
import java.sql.Time;
3233
import java.sql.Timestamp;
3334
import java.sql.Types;
35+
import java.time.LocalDate;
36+
import java.time.LocalDateTime;
37+
import java.time.LocalTime;
3438
import java.util.ArrayList;
3539
import java.util.LinkedList;
3640
import java.util.List;
@@ -1069,4 +1073,87 @@ public void testGetObjectForJSONResultFormatUsingJDBCDecimalAsInt() throws SQLEx
10691073
}
10701074
}
10711075
}
1076+
1077+
@Test
1078+
public void testGetObjectWithType() throws SQLException {
1079+
try (Connection connection = init();
1080+
Statement statement = connection.createStatement()) {
1081+
statement.execute(
1082+
" CREATE OR REPLACE TABLE test_all_types ("
1083+
+ " string VARCHAR, "
1084+
+ " b TINYINT, "
1085+
+ " s SMALLINT, "
1086+
+ " i INTEGER, "
1087+
+ " l BIGINT, "
1088+
+ " f FLOAT, "
1089+
+ " d DOUBLE, "
1090+
+ " bd DOUBLE, "
1091+
+ " bool BOOLEAN, "
1092+
+ " timestampLtz TIMESTAMP_LTZ, "
1093+
+ " timestampNtz TIMESTAMP_NTZ, "
1094+
+ " timestampTz TIMESTAMP_TZ, "
1095+
+ " date DATE,"
1096+
+ " time TIME "
1097+
+ " )");
1098+
statement.execute(
1099+
"insert into test_all_types values('aString',1,2,3,4,1.1,2.2,3.3, false, "
1100+
+ "'2021-12-22 09:43:44','2021-12-22 09:43:44','2021-12-22 09:43:44', "
1101+
+ "'2023-12-24','12:34:56')");
1102+
1103+
assertResultValueAndType(statement, "aString", "string", String.class);
1104+
assertResultValueAndType(statement, new Byte("1"), "b", Byte.class);
1105+
assertResultValueAndType(statement, Short.valueOf("2"), "s", Short.class);
1106+
assertResultValueAndType(statement, Integer.valueOf("2"), "s", Integer.class);
1107+
assertResultValueAndType(statement, Integer.valueOf("3"), "i", Integer.class);
1108+
assertResultValueAndType(statement, Long.valueOf("4"), "l", Long.class);
1109+
assertResultValueAndType(statement, BigDecimal.valueOf(4), "l", BigDecimal.class);
1110+
assertResultValueAndType(statement, Float.valueOf("1.1"), "f", Float.class);
1111+
assertResultValueAndType(statement, Double.valueOf("1.1"), "f", Double.class);
1112+
assertResultValueAndType(statement, Double.valueOf("2.2"), "d", Double.class);
1113+
assertResultValueAndType(statement, BigDecimal.valueOf(3.3), "bd", BigDecimal.class);
1114+
assertResultValueAndType(statement, "FALSE", "bool", String.class);
1115+
assertResultValueAndType(statement, Boolean.FALSE, "bool", Boolean.class);
1116+
assertResultValueAndType(statement, Long.valueOf(0), "bool", Long.class);
1117+
assertResultValueAsString(
1118+
statement,
1119+
new SnowflakeTimestampWithTimezone(
1120+
Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()),
1121+
"timestampLtz",
1122+
Timestamp.class);
1123+
assertResultValueAsString(
1124+
statement,
1125+
new SnowflakeTimestampWithTimezone(
1126+
Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()),
1127+
"timestampNtz",
1128+
Timestamp.class);
1129+
assertResultValueAsString(
1130+
statement,
1131+
new SnowflakeTimestampWithTimezone(
1132+
Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), TimeZone.getDefault()),
1133+
"timestampTz",
1134+
Timestamp.class);
1135+
assertResultValueAndType(
1136+
statement, Date.valueOf(LocalDate.of(2023, 12, 24)), "date", Date.class);
1137+
assertResultValueAndType(
1138+
statement, Time.valueOf(LocalTime.of(12, 34, 56)), "time", Time.class);
1139+
}
1140+
}
1141+
1142+
private void assertResultValueAndType(
1143+
Statement statement, Object expected, String columnName, Class<?> type) throws SQLException {
1144+
try (ResultSet resultSetString =
1145+
statement.executeQuery(String.format("select %s from test_all_types", columnName))) {
1146+
resultSetString.next();
1147+
assertEquals(expected, resultSetString.getObject(1, type));
1148+
}
1149+
}
1150+
1151+
private void assertResultValueAsString(
1152+
Statement statement, Object expected, String columnName, Class type) throws SQLException {
1153+
try (ResultSet resultSetString =
1154+
statement.executeQuery(String.format("select %s from test_all_types", columnName))) {
1155+
resultSetString.next();
1156+
assertEquals(expected.toString(), resultSetString.getObject(1, type).toString());
1157+
}
1158+
}
10721159
}

0 commit comments

Comments
 (0)