Skip to content

Commit

Permalink
SNOW-1259709 - basic types for arrays and maps - fix for date/time types
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-pmotacki committed Mar 27, 2024
1 parent 36b61bc commit 5623506
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 26 deletions.
50 changes: 24 additions & 26 deletions src/main/java/net/snowflake/client/jdbc/SnowflakeBaseResultSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -1396,10 +1396,11 @@ public <T> List<T> getList(int columnIndex, Class<T> type) throws SQLException {
}

public <T> T[] getArray(int columnIndex, Class<T> type) throws SQLException {
int columnSubType = resultSetMetaData.getInternalColumnType(columnIndex);
int columnType = ColumnTypeHelper.getColumnType(columnSubType, session);
;
int scale = resultSetMetaData.getScale(columnIndex);
FieldMetadata fieldMetadata = sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1)
.getFields().get(0);
int columnSubType = fieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(fieldMetadata.getType(), session);
int scale = fieldMetadata.getScale();
TimeZone tz = sfBaseResultSet.getSessionTimeZone();
Object[] objects = (Object[]) getArray(columnIndex).getArray();
T[] arr = (T[]) java.lang.reflect.Array.newInstance(type, objects.length);
Expand Down Expand Up @@ -1499,27 +1500,24 @@ public <T> T[] getArray(int columnIndex, Class<T> type) throws SQLException {
mapSFExceptionToSQLException(
() ->
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getDate(value, columnType, columnSubType, tz, scale));
sfBaseResultSet
.getConverters()
.dateConverter(session).convert((String) value));
} else if (Time.class.isAssignableFrom(type)) {
arr[counter++] =
mapSFExceptionToSQLException(
() ->
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getTime(value, columnType, columnSubType, tz, scale));
sfBaseResultSet
.getConverters()
.timeConverter(session).convert((String) value));
} else if (Timestamp.class.isAssignableFrom(type)) {
mapSFExceptionToSQLException(
() ->
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getTimestamp(value, columnType, columnSubType, tz, scale));
sfBaseResultSet
.getConverters()
.timestampConverter(columnSubType, columnType, scale, session, null, tz).convert((String) value));
} else if (BigDecimal.class.isAssignableFrom(type)) {
arr[counter++] = (T) getBigDecimal(columnIndex);
} else {
Expand All @@ -1535,9 +1533,11 @@ public <T> T[] getArray(int columnIndex, Class<T> type) throws SQLException {
}

public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLException {
int columnType = resultSetMetaData.getInternalColumnType(columnIndex);
int columnSubType = resultSetMetaData.getInternalColumnType(columnIndex);
int scale = resultSetMetaData.getScale(columnIndex);
FieldMetadata valueFieldMetadata = sfBaseResultSet.getMetaData().getColumnMetadata().get(columnIndex - 1)
.getFields().get(1);
int columnSubType = valueFieldMetadata.getType();
int columnType = ColumnTypeHelper.getColumnType(valueFieldMetadata.getType(), session);
int scale = valueFieldMetadata.getScale();
TimeZone tz = sfBaseResultSet.getSessionTimeZone();
Object object = getObject(columnIndex);
JsonNode jsonNode = ((JsonSqlInput) object).getInput();
Expand Down Expand Up @@ -1658,9 +1658,7 @@ public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLExcep
() ->
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getDate(entry.getValue(), columnType, columnSubType, tz, scale)));
.getConverters().dateConverter(session).convert((String)entry.getValue())));
} else if (Time.class.isAssignableFrom(type)) {
resultMap.put(
entry.getKey(),
Expand All @@ -1669,8 +1667,7 @@ public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLExcep
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getTime(entry.getValue(), columnType, columnSubType, tz, scale)));
.timeConverter(session).convert((String) entry.getValue())));
} else if (Timestamp.class.isAssignableFrom(type)) {
resultMap.put(
entry.getKey(),
Expand All @@ -1679,8 +1676,9 @@ public <T> Map<String, T> getMap(int columnIndex, Class<T> type) throws SQLExcep
(T)
sfBaseResultSet
.getConverters()
.getDateTimeConverter()
.getTimestamp(entry.getValue(), columnType, columnSubType, tz, scale)));
.timestampConverter(columnSubType, columnType, scale, session, null, tz).convert((String) entry.getValue())
)
);
} else {
logger.debug(
"Unsupported type passed to getObject(int columnIndex,Class<T> type): "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public Connection init() throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute("alter session set ENABLE_STRUCTURED_TYPES_IN_CLIENT_RESPONSE = true");
stmt.execute("alter session set IGNORE_CLIENT_VESRION_IN_STRUCTURED_TYPES_RESPONSE = true");
stmt.execute("ALTER SESSION SET TIMEZONE = 'Europe/Warsaw'");
stmt.execute(
"alter session set jdbc_query_result_format = '"
+ queryResultFormat.sessionParameterTypeValue
Expand Down Expand Up @@ -262,6 +263,45 @@ public void testReturnAsMapOfLong() throws SQLException {
});
}

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testReturnAsMapOfTimestamp() throws SQLException {
withFirstRow(
"SELECT {'x':'2021-12-22 09:43:44.000 +0100', 'y':'2021-12-22 10:43:44.000 +0100'}::MAP(VARCHAR, TIMESTAMP)",
(resultSet) -> {
Map<String, Timestamp> map =
resultSet.unwrap(SnowflakeBaseResultSet.class).getMap(1, Timestamp.class);
assertEquals(Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 9, 43, 44)), map.get("x"));
assertEquals(Timestamp.valueOf(LocalDateTime.of(2021, 12, 22, 10, 43, 44)), map.get("y"));
});
}

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testReturnAsMapOfDate() throws SQLException {
withFirstRow(
"SELECT {'x':'2023-12-24', 'y':'2023-12-25'}::MAP(VARCHAR, DATE)",
(resultSet) -> {
Map<String, Date> map =
resultSet.unwrap(SnowflakeBaseResultSet.class).getMap(1, Date.class);
assertEquals( Date.valueOf(LocalDate.of(2023, 12, 24)), map.get("x"));
assertEquals( Date.valueOf(LocalDate.of(2023, 12, 25)), map.get("y"));
});
}

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testReturnAsMapOfTime() throws SQLException {
withFirstRow(
"SELECT {'x':'12:34:56', 'y':'12:34:58'}::MAP(VARCHAR, TIME)",
(resultSet) -> {
Map<String, Time> map =
resultSet.unwrap(SnowflakeBaseResultSet.class).getMap(1, Time.class);
assertEquals( Time.valueOf(LocalTime.of(12, 34, 56)), map.get("x"));
assertEquals( Time.valueOf(LocalTime.of(12, 34, 58)), map.get("y"));
});
}

@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void testReturnAsMapOfBoolean() throws SQLException {
Expand Down Expand Up @@ -516,6 +556,7 @@ private void withFirstRow(String sqlText, ThrowingConsumer<ResultSet, SQLExcepti
throws SQLException {
try (Connection connection = init();
Statement statement = connection.createStatement();

ResultSet rs = statement.executeQuery(sqlText); ) {
assertTrue(rs.next());
consumer.accept(rs);
Expand Down

0 comments on commit 5623506

Please sign in to comment.