Skip to content

Commit

Permalink
Fix issue with using Null as DateType (#1544)
Browse files Browse the repository at this point in the history
* Fix issue with using Null as DateType
* Added logic for parsing date-formatted (yyyy-MM-dd) strings into sql Date values.
  • Loading branch information
sfc-gh-ext-simba-jf authored Oct 27, 2023
1 parent 8720b4c commit 9fbaa7a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, "");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,4 +102,28 @@ public void testGetBoolean() throws SFException {

vector.close();
}

@Test
public void testGetDate() throws SFException {
Map<String, String> 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();
}
}

0 comments on commit 9fbaa7a

Please sign in to comment.