From 10bac5d312bb96bbafd31427f6fa66cc0b8c1507 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Thu, 26 Oct 2023 02:02:23 -0700 Subject: [PATCH 1/3] Fix issue with using Null as DateType --- .../client/core/arrow/VarCharConverter.java | 15 +++++++++++++++ .../core/arrow/VarCharConverterTest.java | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java index 56a644467..df05fcbce 100644 --- a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java @@ -5,6 +5,9 @@ import java.math.BigDecimal; import java.nio.charset.StandardCharsets; +import java.sql.Date; +import java.util.TimeZone; + import net.snowflake.client.core.DataConversionContext; import net.snowflake.client.core.SFException; import net.snowflake.client.jdbc.ErrorCode; @@ -143,4 +146,16 @@ public boolean toBoolean(int index) throws SFException { SnowflakeUtil.BOOLEAN_STR, str); } } + + @Override + public Date toDate(int index, TimeZone jvmTz, boolean useDateFormat) throws SFException { + if (isNull(index)) + { + return null; + } else { + throw new SFException( + ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, + SnowflakeUtil.DATE_STR, ""); + } + } } diff --git a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java index da7f0b98c..c1664b74e 100644 --- a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java +++ b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java @@ -107,4 +107,23 @@ public void testGetBoolean() throws SFException { vector.close(); } + + @Test + public void testGetDate() throws SFException { + Map customFieldMeta = new HashMap<>(); + customFieldMeta.put("logicalType", "FIXED"); + + FieldType fieldType = + new FieldType(true, Types.MinorType.VARCHAR.getType(), null, customFieldMeta); + + VarCharVector vector = new VarCharVector("col_one", fieldType, allocator); + vector.setNull(0); + vector.setSafe(1, "abc".getBytes(StandardCharsets.UTF_8)); + + ArrowVectorConverter converter = new VarCharConverter(vector, 0, this); + assertThat(null, is(converter.toDate(0, null, false))); + TestUtil.assertSFException(invalidConversionErrorCode, () -> converter.toDate(1, null, false)); + + vector.close(); + } } From 99ef70001d3c686aa1b2038a540f9ed4b7fc8848 Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Thu, 26 Oct 2023 02:24:16 -0700 Subject: [PATCH 2/3] Check-style changes --- .../net/snowflake/client/core/arrow/VarCharConverter.java | 7 ++----- .../snowflake/client/core/arrow/VarCharConverterTest.java | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java index df05fcbce..a7cb30abd 100644 --- a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java @@ -7,7 +7,6 @@ import java.nio.charset.StandardCharsets; import java.sql.Date; import java.util.TimeZone; - import net.snowflake.client.core.DataConversionContext; import net.snowflake.client.core.SFException; import net.snowflake.client.jdbc.ErrorCode; @@ -149,13 +148,11 @@ public boolean toBoolean(int index) throws SFException { @Override public Date toDate(int index, TimeZone jvmTz, boolean useDateFormat) throws SFException { - if (isNull(index)) - { + if (isNull(index)) { return null; } else { throw new SFException( - ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, - SnowflakeUtil.DATE_STR, ""); + ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, SnowflakeUtil.DATE_STR, ""); } } } diff --git a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java index c1664b74e..e2c65f66a 100644 --- a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java +++ b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java @@ -114,7 +114,7 @@ public void testGetDate() throws SFException { customFieldMeta.put("logicalType", "FIXED"); FieldType fieldType = - new FieldType(true, Types.MinorType.VARCHAR.getType(), null, customFieldMeta); + new FieldType(true, Types.MinorType.VARCHAR.getType(), null, customFieldMeta); VarCharVector vector = new VarCharVector("col_one", fieldType, allocator); vector.setNull(0); From f1e86900de511ce45a3e7b7d894d7bd5482d6f8b Mon Sep 17 00:00:00 2001 From: Jelena Furundzic Date: Thu, 26 Oct 2023 15:07:25 -0700 Subject: [PATCH 3/3] Added logic for parsing date-formatted strings into sql Date values. --- .../client/core/arrow/VarCharConverter.java | 10 +++++++++- .../core/arrow/VarCharConverterTest.java | 18 +++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java index a7cb30abd..8a6ce64e5 100644 --- a/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java +++ b/src/main/java/net/snowflake/client/core/arrow/VarCharConverter.java @@ -6,6 +6,9 @@ import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.sql.Date; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.TimeZone; import net.snowflake.client.core.DataConversionContext; import net.snowflake.client.core.SFException; @@ -150,7 +153,12 @@ public boolean toBoolean(int index) throws SFException { public Date toDate(int index, TimeZone jvmTz, boolean useDateFormat) throws SFException { if (isNull(index)) { return null; - } else { + } + try { + DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + Date date = new Date(dateFormat.parse(toString(index)).getTime()); + return date; + } catch (ParseException e) { throw new SFException( ErrorCode.INVALID_VALUE_CONVERT, logicalTypeStr, SnowflakeUtil.DATE_STR, ""); } diff --git a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java index e2c65f66a..77bcef798 100644 --- a/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java +++ b/src/test/java/net/snowflake/client/core/arrow/VarCharConverterTest.java @@ -10,13 +10,8 @@ import static org.junit.Assert.assertTrue; import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Random; -import java.util.Set; +import java.sql.Date; +import java.util.*; import net.snowflake.client.TestUtil; import net.snowflake.client.core.SFException; import org.apache.arrow.memory.BufferAllocator; @@ -118,11 +113,16 @@ public void testGetDate() throws SFException { VarCharVector vector = new VarCharVector("col_one", fieldType, allocator); vector.setNull(0); - vector.setSafe(1, "abc".getBytes(StandardCharsets.UTF_8)); + vector.setSafe(1, "2023-10-26".getBytes(StandardCharsets.UTF_8)); + vector.setSafe(2, "abc".getBytes(StandardCharsets.UTF_8)); ArrowVectorConverter converter = new VarCharConverter(vector, 0, this); + Date expectedDate = new Date(123, 9, 26); + Date actualDate = converter.toDate(1, TimeZone.getDefault(), false); + assertThat(null, is(converter.toDate(0, null, false))); - TestUtil.assertSFException(invalidConversionErrorCode, () -> converter.toDate(1, null, false)); + assertThat(actualDate, is(expectedDate)); + TestUtil.assertSFException(invalidConversionErrorCode, () -> converter.toDate(2, null, false)); vector.close(); }