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..8a6ce64e5 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,11 @@ 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; import net.snowflake.client.jdbc.ErrorCode; @@ -143,4 +148,19 @@ 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; + } + 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 da7f0b98c..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; @@ -107,4 +102,28 @@ 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, "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))); + assertThat(actualDate, is(expectedDate)); + TestUtil.assertSFException(invalidConversionErrorCode, () -> converter.toDate(2, null, false)); + + vector.close(); + } }