From 7765d55d8010b5f291e10fe3907e2fb7c52a39ef Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Mon, 11 Mar 2024 01:37:47 -0700 Subject: [PATCH 1/5] Created new connection property jdbc_arrow_treat_decimal_as_int --- .../snowflake/client/core/SFBaseSession.java | 11 +++ .../net/snowflake/client/core/SFSession.java | 6 ++ .../client/core/SFSessionProperty.java | 4 +- .../core/arrow/BigIntToFixedConverter.java | 5 +- .../core/arrow/IntToFixedConverter.java | 7 +- .../core/arrow/SmallIntToFixedConverter.java | 2 + .../core/arrow/TinyIntToFixedConverter.java | 7 +- .../client/jdbc/ResultSetArrowLatestIT.java | 73 +++++++++++++++++++ .../client/jdbc/ResultSetLatestIT.java | 69 ++++++++++++++++++ 9 files changed, 179 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/SFBaseSession.java b/src/main/java/net/snowflake/client/core/SFBaseSession.java index 364a45ad9..e0fd7ca6e 100644 --- a/src/main/java/net/snowflake/client/core/SFBaseSession.java +++ b/src/main/java/net/snowflake/client/core/SFBaseSession.java @@ -139,6 +139,8 @@ public abstract class SFBaseSession { private Map commonParameters; + private boolean isJdbcArrowTreatDecimalAsInt = true; + protected SFBaseSession(SFConnectionHandler sfConnectionHandler) { this.sfConnectionHandler = sfConnectionHandler; } @@ -267,6 +269,15 @@ public void setJdbcTreatDecimalAsInt(boolean jdbcTreatDecimalAsInt) { isJdbcTreatDecimalAsInt = jdbcTreatDecimalAsInt; } + public boolean isJdbcArrowTreatDecimalAsInt() { + return isJdbcArrowTreatDecimalAsInt; + } + + public void setJdbcArrowTreatDecimalAsInt(boolean jdbcArrowTreatDecimalAsInt) { + isJdbcArrowTreatDecimalAsInt = jdbcArrowTreatDecimalAsInt; + } + + public String getServerUrl() { if (connectionPropertiesMap.containsKey(SFSessionProperty.SERVER_URL)) { return (String) connectionPropertiesMap.get(SFSessionProperty.SERVER_URL); diff --git a/src/main/java/net/snowflake/client/core/SFSession.java b/src/main/java/net/snowflake/client/core/SFSession.java index 20850b4f0..a0c25c213 100644 --- a/src/main/java/net/snowflake/client/core/SFSession.java +++ b/src/main/java/net/snowflake/client/core/SFSession.java @@ -470,6 +470,12 @@ public void addSFSessionProperty(String propertyName, Object propertyValue) thro } break; + case JDBC_ARROW_TREAT_DECIMAL_AS_INT: + if (propertyValue != null) { + setJdbcArrowTreatDecimalAsInt(getBooleanValue(propertyValue)); + } + break; + default: break; } diff --git a/src/main/java/net/snowflake/client/core/SFSessionProperty.java b/src/main/java/net/snowflake/client/core/SFSessionProperty.java index 0d42edfef..0dc6841db 100644 --- a/src/main/java/net/snowflake/client/core/SFSessionProperty.java +++ b/src/main/java/net/snowflake/client/core/SFSessionProperty.java @@ -78,7 +78,9 @@ public enum SFSessionProperty { RETRY_TIMEOUT("retryTimeout", false, Integer.class), - ENABLE_PATTERN_SEARCH("enablePatternSearch", false, Boolean.class); + ENABLE_PATTERN_SEARCH("enablePatternSearch", false, Boolean.class), + + JDBC_ARROW_TREAT_DECIMAL_AS_INT("jdbc_arrow_treat_decimal_as_int", false, Boolean.class); // property key in string private String propertyKey; diff --git a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java index f05a9960d..080d5e8fa 100644 --- a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java @@ -137,9 +137,10 @@ public BigDecimal toBigDecimal(int index) { public Object toObject(int index) throws SFException { if (bigIntVector.isNull(index)) { return null; - } else { - return getLong(index); + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + return BigDecimal.valueOf(getLong(index), sfScale); } + return getLong(index); } @Override diff --git a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java index a5faa2215..271468e1a 100644 --- a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java @@ -105,7 +105,12 @@ public BigDecimal toBigDecimal(int index) throws SFException { @Override public Object toObject(int index) throws SFException { - return isNull(index) ? null : (long) getInt(index); + if (isNull(index)){ + return null; + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + return BigDecimal.valueOf((long) getInt(index), sfScale); + } + return (long) getInt(index); } @Override diff --git a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java index ad44cd224..f85a920fd 100644 --- a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java @@ -99,6 +99,8 @@ public double toDouble(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + return BigDecimal.valueOf((long) getShort(index), sfScale); } return (long) getShort(index); } diff --git a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java index 5cfbd3e5d..16ac652a7 100644 --- a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java @@ -90,7 +90,12 @@ public BigDecimal toBigDecimal(int index) throws SFException { @Override public Object toObject(int index) throws SFException { - return isNull(index) ? null : (long) toByte(index); + if (isNull(index)) { + return null; + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + return BigDecimal.valueOf((long) getByte(index), sfScale); + } + return (long) toByte(index); } @Override diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java index 4ea7f7d8f..78b80fe40 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java @@ -4,8 +4,17 @@ package net.snowflake.client.jdbc; import net.snowflake.client.category.TestCategoryArrow; +import org.junit.Test; import org.junit.experimental.categories.Category; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Properties; + +import static org.junit.Assert.assertEquals; + /** * ResultSet integration tests for the latest JDBC driver. This doesn't work for the oldest * supported driver. Drop this file when ResultSetLatestIT is dropped. @@ -15,4 +24,68 @@ public class ResultSetArrowLatestIT extends ResultSetLatestIT { public ResultSetArrowLatestIT() { super("arrow"); } + + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { + Properties properties = new Properties(); + properties.put("jdbc_arrow_treat_decimal_as_int", false); + try (Connection con = init(properties); + Statement stmt = con.createStatement()) { + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); + } + } + } + } + + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { + try (Connection con = init(); + Statement stmt = con.createStatement()) { + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + } + } + } + } } diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index f9e5f0293..1e4fb75cb 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -73,6 +73,11 @@ public ResultSetLatestIT() { super(queryResultFormat); } + public String createTableSql = "Create or replace table get_object_for_numeric_types (c1 INT, c2 BIGINT, c3 SMALLINT, c4 TINYINT) "; + public String insertStmt = "Insert into get_object_for_numeric_types (c1, c2, c3, c4) values (1000000000, 2000000000000000000000000, 3, 4)"; + public String selectQuery = "Select * from get_object_for_numeric_types"; + public String setJdbcTreatDecimalAsIntFalse = "alter session set JDBC_TREAT_DECIMAL_AS_INT = false"; + /** * Test that when closing of results is interrupted by Thread.Interrupt(), the memory is released * safely before driver execution ends. @@ -971,4 +976,68 @@ public void testLargeStringRetrieval() throws SQLException { fail("executeQuery should not fail"); } } + + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { + Properties properties = new Properties(); + properties.put("jdbc_arrow_treat_decimal_as_int", false); + try (Connection con = init(properties); + Statement stmt = con.createStatement()) { + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); + } + } + } + } + + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { + try (Connection con = init(); + Statement stmt = con.createStatement()) { + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); + assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); + } + } + } + } } From 0648ab903bd9672ce294fb698e9ae9671433781e Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Mon, 11 Mar 2024 01:38:45 -0700 Subject: [PATCH 2/5] Fix check-style --- .../snowflake/client/core/SFBaseSession.java | 1 - .../core/arrow/BigIntToFixedConverter.java | 3 ++- .../core/arrow/IntToFixedConverter.java | 5 ++-- .../core/arrow/SmallIntToFixedConverter.java | 3 ++- .../core/arrow/TinyIntToFixedConverter.java | 3 ++- .../client/jdbc/ResultSetArrowLatestIT.java | 23 ++++++++++--------- .../client/jdbc/ResultSetLatestIT.java | 23 +++++++++++-------- 7 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/SFBaseSession.java b/src/main/java/net/snowflake/client/core/SFBaseSession.java index e0fd7ca6e..d0bfeda9d 100644 --- a/src/main/java/net/snowflake/client/core/SFBaseSession.java +++ b/src/main/java/net/snowflake/client/core/SFBaseSession.java @@ -277,7 +277,6 @@ public void setJdbcArrowTreatDecimalAsInt(boolean jdbcArrowTreatDecimalAsInt) { isJdbcArrowTreatDecimalAsInt = jdbcArrowTreatDecimalAsInt; } - public String getServerUrl() { if (connectionPropertiesMap.containsKey(SFSessionProperty.SERVER_URL)) { return (String) connectionPropertiesMap.get(SFSessionProperty.SERVER_URL); diff --git a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java index 080d5e8fa..dad9fae3a 100644 --- a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java @@ -137,7 +137,8 @@ public BigDecimal toBigDecimal(int index) { public Object toObject(int index) throws SFException { if (bigIntVector.isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() + && !context.getSession().isJdbcTreatDecimalAsInt()) { return BigDecimal.valueOf(getLong(index), sfScale); } return getLong(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java index 271468e1a..073b12f81 100644 --- a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java @@ -105,9 +105,10 @@ public BigDecimal toBigDecimal(int index) throws SFException { @Override public Object toObject(int index) throws SFException { - if (isNull(index)){ + if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() + && !context.getSession().isJdbcTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getInt(index), sfScale); } return (long) getInt(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java index f85a920fd..84d1902a1 100644 --- a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java @@ -99,7 +99,8 @@ public double toDouble(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() + && !context.getSession().isJdbcTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getShort(index), sfScale); } return (long) getShort(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java index 16ac652a7..35cb0a59c 100644 --- a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java @@ -92,7 +92,8 @@ public BigDecimal toBigDecimal(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() + && !context.getSession().isJdbcTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getByte(index), sfScale); } return (long) toByte(index); diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java index 78b80fe40..ab07ae3d5 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java @@ -3,17 +3,16 @@ */ package net.snowflake.client.jdbc; -import net.snowflake.client.category.TestCategoryArrow; -import org.junit.Test; -import org.junit.experimental.categories.Category; +import static org.junit.Assert.assertEquals; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; - -import static org.junit.Assert.assertEquals; +import net.snowflake.client.category.TestCategoryArrow; +import org.junit.Test; +import org.junit.experimental.categories.Category; /** * ResultSet integration tests for the latest JDBC driver. This doesn't work for the oldest @@ -25,19 +24,20 @@ public ResultSetArrowLatestIT() { super("arrow"); } - // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property + // introduced after version 3.15.0. @Test public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { Properties properties = new Properties(); properties.put("jdbc_arrow_treat_decimal_as_int", false); try (Connection con = init(properties); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { + while (rs.next()) { assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); @@ -58,17 +58,18 @@ public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws } } - // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. + // Connection property introduced after version 3.15.0. @Test public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { try (Connection con = init(); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { + while (rs.next()) { assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index 1e4fb75cb..5a416ab8a 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -73,10 +73,13 @@ public ResultSetLatestIT() { super(queryResultFormat); } - public String createTableSql = "Create or replace table get_object_for_numeric_types (c1 INT, c2 BIGINT, c3 SMALLINT, c4 TINYINT) "; - public String insertStmt = "Insert into get_object_for_numeric_types (c1, c2, c3, c4) values (1000000000, 2000000000000000000000000, 3, 4)"; + public String createTableSql = + "Create or replace table get_object_for_numeric_types (c1 INT, c2 BIGINT, c3 SMALLINT, c4 TINYINT) "; + public String insertStmt = + "Insert into get_object_for_numeric_types (c1, c2, c3, c4) values (1000000000, 2000000000000000000000000, 3, 4)"; public String selectQuery = "Select * from get_object_for_numeric_types"; - public String setJdbcTreatDecimalAsIntFalse = "alter session set JDBC_TREAT_DECIMAL_AS_INT = false"; + public String setJdbcTreatDecimalAsIntFalse = + "alter session set JDBC_TREAT_DECIMAL_AS_INT = false"; /** * Test that when closing of results is interrupted by Thread.Interrupt(), the memory is released @@ -977,19 +980,20 @@ public void testLargeStringRetrieval() throws SQLException { } } - // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property + // introduced after version 3.15.0. @Test public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { Properties properties = new Properties(); properties.put("jdbc_arrow_treat_decimal_as_int", false); try (Connection con = init(properties); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { + while (rs.next()) { assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); @@ -1010,17 +1014,18 @@ public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws } } - // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. + // Connection property introduced after version 3.15.0. @Test public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { try (Connection con = init(); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { + while (rs.next()) { assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); From ed20be538a950b09768e9816f26b870da46de3ff Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Tue, 12 Mar 2024 00:10:16 -0700 Subject: [PATCH 3/5] Trying fix for failing tests --- .../client/jdbc/ResultSetArrowLatestIT.java | 75 +----------- .../client/jdbc/ResultSetLatestIT.java | 112 ++++++++++++++---- 2 files changed, 95 insertions(+), 92 deletions(-) diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java index ab07ae3d5..5ac08c118 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java @@ -3,16 +3,17 @@ */ package net.snowflake.client.jdbc; -import static org.junit.Assert.assertEquals; +import net.snowflake.client.category.TestCategoryArrow; +import org.junit.Test; +import org.junit.experimental.categories.Category; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; -import net.snowflake.client.category.TestCategoryArrow; -import org.junit.Test; -import org.junit.experimental.categories.Category; + +import static org.junit.Assert.assertEquals; /** * ResultSet integration tests for the latest JDBC driver. This doesn't work for the oldest @@ -23,70 +24,4 @@ public class ResultSetArrowLatestIT extends ResultSetLatestIT { public ResultSetArrowLatestIT() { super("arrow"); } - - // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property - // introduced after version 3.15.0. - @Test - public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { - Properties properties = new Properties(); - properties.put("jdbc_arrow_treat_decimal_as_int", false); - try (Connection con = init(properties); - Statement stmt = con.createStatement()) { - stmt.execute(createTableSql); - stmt.execute(insertStmt); - - // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); - } - } - - // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false - stmt.execute(setJdbcTreatDecimalAsIntFalse); - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); - } - } - } - } - - // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. - // Connection property introduced after version 3.15.0. - @Test - public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { - try (Connection con = init(); - Statement stmt = con.createStatement()) { - stmt.execute(createTableSql); - stmt.execute(insertStmt); - - // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); - } - } - - // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false - stmt.execute(setJdbcTreatDecimalAsIntFalse); - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); - } - } - } - } } diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index 5a416ab8a..cd97a5817 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -73,12 +73,12 @@ public ResultSetLatestIT() { super(queryResultFormat); } - public String createTableSql = + private String createTableSql = "Create or replace table get_object_for_numeric_types (c1 INT, c2 BIGINT, c3 SMALLINT, c4 TINYINT) "; - public String insertStmt = + private String insertStmt = "Insert into get_object_for_numeric_types (c1, c2, c3, c4) values (1000000000, 2000000000000000000000000, 3, 4)"; - public String selectQuery = "Select * from get_object_for_numeric_types"; - public String setJdbcTreatDecimalAsIntFalse = + private String selectQuery = "Select * from get_object_for_numeric_types"; + private String setJdbcTreatDecimalAsIntFalse = "alter session set JDBC_TREAT_DECIMAL_AS_INT = false"; /** @@ -980,24 +980,91 @@ public void testLargeStringRetrieval() throws SQLException { } } + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { + Properties properties = new Properties(); + properties.put("jdbc_arrow_treat_decimal_as_int", false); + try (Connection con = getConnection(properties); + Statement stmt = con.createStatement()) { + stmt.execute("alter session set jdbc_query_result_format = 'ARROW'"); + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); + } + } + } + } + + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + @Test + public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { + try (Connection con = BaseJDBCTest.getConnection(); + Statement stmt = con.createStatement()) { + stmt.execute("alter session set jdbc_query_result_format = 'ARROW'"); + stmt.execute(createTableSql); + stmt.execute(insertStmt); + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while(rs.next()) { + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); + } + } + + // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false + stmt.execute(setJdbcTreatDecimalAsIntFalse); + try (ResultSet rs = stmt.executeQuery(selectQuery)) { + while (rs.next()) { + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); + } + } + } + } + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property // introduced after version 3.15.0. @Test public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { Properties properties = new Properties(); properties.put("jdbc_arrow_treat_decimal_as_int", false); - try (Connection con = init(properties); + try (Connection con = getConnection(properties); Statement stmt = con.createStatement()) { + stmt.execute("alter session set jdbc_query_result_format = 'JSON'"); stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); } } @@ -1005,10 +1072,10 @@ public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws stmt.execute(setJdbcTreatDecimalAsIntFalse); try (ResultSet rs = stmt.executeQuery(selectQuery)) { while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); + assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); } } } @@ -1018,18 +1085,19 @@ public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws // Connection property introduced after version 3.15.0. @Test public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { - try (Connection con = init(); + try (Connection con = BaseJDBCTest.getConnection(); Statement stmt = con.createStatement()) { + stmt.execute("alter session set jdbc_query_result_format = 'JSON'"); stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.lang.Long"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.lang.Long"); + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); } } @@ -1037,10 +1105,10 @@ public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntTrue() throws S stmt.execute(setJdbcTreatDecimalAsIntFalse); try (ResultSet rs = stmt.executeQuery(selectQuery)) { while (rs.next()) { - assertEquals(rs.getObject(1).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(2).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(3).getClass().toString(), "class java.math.BigDecimal"); - assertEquals(rs.getObject(4).getClass().toString(), "class java.math.BigDecimal"); + assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); } } } From 33468eebfd72f70299fb973fa440c566b00b05a2 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Tue, 12 Mar 2024 23:28:53 -0700 Subject: [PATCH 4/5] Updated tests --- .../arrow/AbstractArrowVectorConverter.java | 8 ++ .../core/arrow/BigIntToFixedConverter.java | 3 +- .../core/arrow/IntToFixedConverter.java | 3 +- .../core/arrow/SmallIntToFixedConverter.java | 3 +- .../core/arrow/TinyIntToFixedConverter.java | 3 +- .../client/jdbc/ResultSetArrowLatestIT.java | 9 -- .../client/jdbc/ResultSetLatestIT.java | 97 ++++++------------- 7 files changed, 39 insertions(+), 87 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java b/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java index 03b2cace6..e290831b8 100644 --- a/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java @@ -146,6 +146,14 @@ public BigDecimal toBigDecimal(int index) throws SFException { ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, SnowflakeUtil.BIG_DECIMAL_STR, ""); } + public boolean shouldTreatDecimalAsInt(){ + if (!context.getSession().isJdbcArrowTreatDecimalAsInt() + && !context.getSession().isJdbcTreatDecimalAsInt()) { + return false; + } + return true; + } + @Override public void setTreatNTZAsUTC(boolean isUTC) { this.treatNTZasUTC = isUTC; diff --git a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java index dad9fae3a..71bd123a0 100644 --- a/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/BigIntToFixedConverter.java @@ -137,8 +137,7 @@ public BigDecimal toBigDecimal(int index) { public Object toObject(int index) throws SFException { if (bigIntVector.isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() - && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!shouldTreatDecimalAsInt()) { return BigDecimal.valueOf(getLong(index), sfScale); } return getLong(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java index 073b12f81..8055081ef 100644 --- a/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/IntToFixedConverter.java @@ -107,8 +107,7 @@ public BigDecimal toBigDecimal(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() - && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!shouldTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getInt(index), sfScale); } return (long) getInt(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java index 84d1902a1..bfa398d88 100644 --- a/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/SmallIntToFixedConverter.java @@ -99,8 +99,7 @@ public double toDouble(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() - && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!shouldTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getShort(index), sfScale); } return (long) getShort(index); diff --git a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java index 35cb0a59c..26c90c228 100644 --- a/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/TinyIntToFixedConverter.java @@ -92,8 +92,7 @@ public BigDecimal toBigDecimal(int index) throws SFException { public Object toObject(int index) throws SFException { if (isNull(index)) { return null; - } else if (!context.getSession().isJdbcArrowTreatDecimalAsInt() - && !context.getSession().isJdbcTreatDecimalAsInt()) { + } else if (!shouldTreatDecimalAsInt()) { return BigDecimal.valueOf((long) getByte(index), sfScale); } return (long) toByte(index); diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java index 5ac08c118..4ea7f7d8f 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetArrowLatestIT.java @@ -4,17 +4,8 @@ package net.snowflake.client.jdbc; import net.snowflake.client.category.TestCategoryArrow; -import org.junit.Test; import org.junit.experimental.categories.Category; -import java.sql.Connection; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Statement; -import java.util.Properties; - -import static org.junit.Assert.assertEquals; - /** * ResultSet integration tests for the latest JDBC driver. This doesn't work for the oldest * supported driver. Drop this file when ResultSetLatestIT is dropped. diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index cd97a5817..5efa4534f 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -980,6 +980,24 @@ public void testLargeStringRetrieval() throws SQLException { } } + private static void assertAllColumnsAreLongButBigIntIsBigDecimal(ResultSet rs) throws SQLException { + while(rs.next()) { + assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); + assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); + } + } + + private static void assertAllColumnsAreBigDecimal(ResultSet rs) throws SQLException { + while (rs.next()) { + assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); + assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); + } + } + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. @Test public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { @@ -993,23 +1011,13 @@ public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { - assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); - } + assertAllColumnsAreLongButBigIntIsBigDecimal(rs); } // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false stmt.execute(setJdbcTreatDecimalAsIntFalse); try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); - } + assertAllColumnsAreBigDecimal(rs); } } } @@ -1036,80 +1044,29 @@ public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false stmt.execute(setJdbcTreatDecimalAsIntFalse); try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); - } + assertAllColumnsAreLongButBigIntIsBigDecimal(rs); } } } - // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property - // introduced after version 3.15.0. + // Test getObject for numeric types when JDBC_TREAT_DECIMAL_AS_INT is set and using JSON result format. @Test - public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { - Properties properties = new Properties(); - properties.put("jdbc_arrow_treat_decimal_as_int", false); - try (Connection con = getConnection(properties); - Statement stmt = con.createStatement()) { - stmt.execute("alter session set jdbc_query_result_format = 'JSON'"); - stmt.execute(createTableSql); - stmt.execute(insertStmt); - - // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=true - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); - } - } - - // Test with jdbc_arrow_treat_decimal_as_int=false and JDBC_TREAT_DECIMAL_AS_INT=false - stmt.execute(setJdbcTreatDecimalAsIntFalse); - try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); - } - } - } - } - - // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. - // Connection property introduced after version 3.15.0. - @Test - public void testGetObjectForJSONResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { + public void testGetObjectForJSONResultFormatJDBCDecimalAsIntFalse() throws SQLException { try (Connection con = BaseJDBCTest.getConnection(); Statement stmt = con.createStatement()) { stmt.execute("alter session set jdbc_query_result_format = 'JSON'"); stmt.execute(createTableSql); stmt.execute(insertStmt); - // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true + // Test with JDBC_TREAT_DECIMAL_AS_INT=true (default value) try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); - assertEquals("class java.lang.Long", rs.getObject(4).getClass().toString()); - } + assertAllColumnsAreLongButBigIntIsBigDecimal(rs); } - // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=false + // Test with JDBC_TREAT_DECIMAL_AS_INT=false stmt.execute(setJdbcTreatDecimalAsIntFalse); try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while (rs.next()) { - assertEquals("class java.math.BigDecimal", rs.getObject(1).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(3).getClass().toString()); - assertEquals("class java.math.BigDecimal", rs.getObject(4).getClass().toString()); - } + assertAllColumnsAreBigDecimal(rs); } } } From 75f3c74fa5ba67380429ad2e6d92f89ec14906cb Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Tue, 12 Mar 2024 23:30:04 -0700 Subject: [PATCH 5/5] check-style fix --- .../arrow/AbstractArrowVectorConverter.java | 4 ++-- .../client/jdbc/ResultSetLatestIT.java | 20 +++++++++++-------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java b/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java index e290831b8..c47050e64 100644 --- a/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/AbstractArrowVectorConverter.java @@ -146,9 +146,9 @@ public BigDecimal toBigDecimal(int index) throws SFException { ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, SnowflakeUtil.BIG_DECIMAL_STR, ""); } - public boolean shouldTreatDecimalAsInt(){ + public boolean shouldTreatDecimalAsInt() { if (!context.getSession().isJdbcArrowTreatDecimalAsInt() - && !context.getSession().isJdbcTreatDecimalAsInt()) { + && !context.getSession().isJdbcTreatDecimalAsInt()) { return false; } return true; diff --git a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java index 5efa4534f..aa3bb9909 100644 --- a/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java +++ b/src/test/java/net/snowflake/client/jdbc/ResultSetLatestIT.java @@ -980,8 +980,9 @@ public void testLargeStringRetrieval() throws SQLException { } } - private static void assertAllColumnsAreLongButBigIntIsBigDecimal(ResultSet rs) throws SQLException { - while(rs.next()) { + private static void assertAllColumnsAreLongButBigIntIsBigDecimal(ResultSet rs) + throws SQLException { + while (rs.next()) { assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); @@ -998,13 +999,14 @@ private static void assertAllColumnsAreBigDecimal(ResultSet rs) throws SQLExcept } } - // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property introduced after version 3.15.0. + // Test setting new connection property jdbc_arrow_treat_decimal_as_int=false. Connection property + // introduced after version 3.15.0. @Test public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws SQLException { Properties properties = new Properties(); properties.put("jdbc_arrow_treat_decimal_as_int", false); try (Connection con = getConnection(properties); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute("alter session set jdbc_query_result_format = 'ARROW'"); stmt.execute(createTableSql); stmt.execute(insertStmt); @@ -1022,18 +1024,19 @@ public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntFalse() throws } } - // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. Connection property introduced after version 3.15.0. + // Test default setting of new connection property jdbc_arrow_treat_decimal_as_int=true. + // Connection property introduced after version 3.15.0. @Test public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws SQLException { try (Connection con = BaseJDBCTest.getConnection(); - Statement stmt = con.createStatement()) { + Statement stmt = con.createStatement()) { stmt.execute("alter session set jdbc_query_result_format = 'ARROW'"); stmt.execute(createTableSql); stmt.execute(insertStmt); // Test with jdbc_arrow_treat_decimal_as_int=true and JDBC_TREAT_DECIMAL_AS_INT=true try (ResultSet rs = stmt.executeQuery(selectQuery)) { - while(rs.next()) { + while (rs.next()) { assertEquals("class java.lang.Long", rs.getObject(1).getClass().toString()); assertEquals("class java.math.BigDecimal", rs.getObject(2).getClass().toString()); assertEquals("class java.lang.Long", rs.getObject(3).getClass().toString()); @@ -1049,7 +1052,8 @@ public void testGetObjectForArrowResultFormatJDBCArrowDecimalAsIntTrue() throws } } - // Test getObject for numeric types when JDBC_TREAT_DECIMAL_AS_INT is set and using JSON result format. + // Test getObject for numeric types when JDBC_TREAT_DECIMAL_AS_INT is set and using JSON result + // format. @Test public void testGetObjectForJSONResultFormatJDBCDecimalAsIntFalse() throws SQLException { try (Connection con = BaseJDBCTest.getConnection();