Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with using Null as DateType #1544

Merged
merged 3 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
sfc-gh-igarish marked this conversation as resolved.
Show resolved Hide resolved
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 {
sfc-gh-ext-simba-jf marked this conversation as resolved.
Show resolved Hide resolved
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();
}
}