From 93e98ea90a2f3d0f20eb6761ffd4c8025a0c3730 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Kubik?= Date: Tue, 29 Oct 2024 18:57:19 +0100 Subject: [PATCH] return toStringResult for getObject and getBytes called on structured type --- .../client/core/SFArrowResultSet.java | 12 ++---------- .../client/core/arrow/ArrayConverter.java | 7 ++++++- .../client/core/arrow/MapConverter.java | 11 ++++++----- .../client/core/arrow/StructConverter.java | 7 ++++++- .../client/core/arrow/VectorTypeConverter.java | 5 +++++ ...TypesGetStringArrowJsonCompatibilityIT.java | 18 ++++++++++++++++++ .../StructuredTypesGetStringBaseIT.java | 17 ++++++++++++++++- 7 files changed, 59 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/SFArrowResultSet.java b/src/main/java/net/snowflake/client/core/SFArrowResultSet.java index 14f21e8d1..7ebca741b 100644 --- a/src/main/java/net/snowflake/client/core/SFArrowResultSet.java +++ b/src/main/java/net/snowflake/client/core/SFArrowResultSet.java @@ -573,16 +573,8 @@ public Object getObject(int columnIndex) throws SFException { converter.setTreatNTZAsUTC(treatNTZAsUTC); converter.setUseSessionTimezone(useSessionTimezone); converter.setSessionTimeZone(sessionTimeZone); - Object obj = converter.toObject(index); - boolean isStructuredType = resultSetMetaData.isStructuredTypeColumn(columnIndex); - if (type == Types.STRUCT && isStructuredType) { - if (converter instanceof VarCharConverter) { - return createJsonSqlInput(columnIndex, obj); - } else if (converter instanceof StructConverter) { - return createArrowSqlInput(columnIndex, (Map) obj); - } - } - return obj; + + return converter.toString(index); } private Object createJsonSqlInput(int columnIndex, Object obj) throws SFException { diff --git a/src/main/java/net/snowflake/client/core/arrow/ArrayConverter.java b/src/main/java/net/snowflake/client/core/arrow/ArrayConverter.java index 48b8fa083..dfafb14dc 100644 --- a/src/main/java/net/snowflake/client/core/arrow/ArrayConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/ArrayConverter.java @@ -19,7 +19,12 @@ public ArrayConverter(ListVector valueVector, int vectorIndex, DataConversionCon @Override public Object toObject(int index) throws SFException { - return vector.getObject(index); + return toString(index); + } + + @Override + public byte[] toBytes(int index) throws SFException { + return toString(index).getBytes(); } @Override diff --git a/src/main/java/net/snowflake/client/core/arrow/MapConverter.java b/src/main/java/net/snowflake/client/core/arrow/MapConverter.java index 8ef1cdccf..4b982ad84 100644 --- a/src/main/java/net/snowflake/client/core/arrow/MapConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/MapConverter.java @@ -22,11 +22,12 @@ public MapConverter(MapVector valueVector, int columnIndex, DataConversionContex @Override public Object toObject(int index) throws SFException { - List> entriesList = - (List>) vector.getObject(index); - return entriesList.stream() - .collect( - Collectors.toMap(entry -> entry.get("key").toString(), entry -> entry.get("value"))); + return toString(index); + } + + @Override + public byte[] toBytes(int index) throws SFException { + return toString(index).getBytes(); } @Override diff --git a/src/main/java/net/snowflake/client/core/arrow/StructConverter.java b/src/main/java/net/snowflake/client/core/arrow/StructConverter.java index 4c0516c51..fea1136ba 100644 --- a/src/main/java/net/snowflake/client/core/arrow/StructConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/StructConverter.java @@ -21,7 +21,12 @@ public StructConverter(StructVector vector, int columnIndex, DataConversionConte @Override public Object toObject(int index) throws SFException { - return structVector.getObject(index); + return toString(index); + } + + @Override + public byte[] toBytes(int index) throws SFException { + return toString(index).getBytes(); } @Override diff --git a/src/main/java/net/snowflake/client/core/arrow/VectorTypeConverter.java b/src/main/java/net/snowflake/client/core/arrow/VectorTypeConverter.java index ae7a492a0..b95b23a6f 100644 --- a/src/main/java/net/snowflake/client/core/arrow/VectorTypeConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/VectorTypeConverter.java @@ -21,6 +21,11 @@ public Object toObject(int index) throws SFException { return vector.getObject(index); } + @Override + public byte[] toBytes(int index) throws SFException { + return toString(index).getBytes(); + } + @Override public String toString(int index) throws SFException { List object = vector.getObject(index); diff --git a/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringArrowJsonCompatibilityIT.java b/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringArrowJsonCompatibilityIT.java index 352d2b1a4..c3677b54f 100644 --- a/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringArrowJsonCompatibilityIT.java +++ b/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringArrowJsonCompatibilityIT.java @@ -62,6 +62,24 @@ public void testRunAsGetString() throws SQLException { (resultSet) -> assertGetStringIsCompatible(resultSet, expectedStructureTypeRepresentation)); } + @Test + @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) + public void testRunAsGetObject() throws SQLException { + withFirstRow( + connections.get(queryResultFormat), + selectSql, + (resultSet) -> assertGetObjectIsCompatible(resultSet, expectedStructureTypeRepresentation)); + } + + @Test + @ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class) + public void testRunAsGetBytes() throws SQLException { + withFirstRow( + connections.get(queryResultFormat), + selectSql, + (resultSet) -> assertGetBytesIsCompatible(resultSet, expectedStructureTypeRepresentation)); + } + @Parameterized.Parameters(name = "format={0},sql={1}") public static Collection data() { Map samples = new LinkedHashMap<>(); diff --git a/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringBaseIT.java b/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringBaseIT.java index d9d5c15e2..cabb81a3b 100644 --- a/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringBaseIT.java +++ b/src/test/java/net/snowflake/client/jdbc/structuredtypes/StructuredTypesGetStringBaseIT.java @@ -2,6 +2,7 @@ import static org.junit.Assert.assertTrue; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; @@ -50,12 +51,26 @@ protected static Connection initConnection(ResultSetFormatType queryResultFormat return conn; } + protected void assertGetBytesIsCompatible(ResultSet resultSet, String expected) + throws SQLException { + String result = new String(resultSet.getBytes(1), StandardCharsets.UTF_8); + TestUtil.assertEqualsIgnoringWhitespace(expected, result); + } + protected void assertGetStringIsCompatible(ResultSet resultSet, String expected) - throws SQLException { + throws SQLException { String result = resultSet.getString(1); TestUtil.assertEqualsIgnoringWhitespace(expected, result); } + protected void assertGetObjectIsCompatible(ResultSet resultSet, String expected) + throws SQLException { + String result = resultSet.getObject(1, String.class); + String resultCasted = (String) resultSet.getObject(1); + TestUtil.assertEqualsIgnoringWhitespace(expected, result); + TestUtil.assertEqualsIgnoringWhitespace(expected, resultCasted); + } + protected void withFirstRow( Connection connection, String sqlText, ThrowingConsumer consumer) throws SQLException {