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

SNOW-1636262: getDate triggers NPE with 3.18.0 (regression) #1877

Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -64,6 +64,7 @@ public abstract class SnowflakeBaseResultSet implements ResultSet {
protected Map<String, Object> parameters = new HashMap<>();
private int fetchSize = 0;
protected SFBaseSession session = null;
private SnowflakeResultSetSerializableV1 serializable = null;
sfc-gh-jszczerbinski marked this conversation as resolved.
Show resolved Hide resolved
private static final ObjectMapper OBJECT_MAPPER = ObjectMapperFactory.getObjectMapper();

SnowflakeBaseResultSet(Statement statement) throws SQLException {
Expand Down Expand Up @@ -95,6 +96,7 @@ public SnowflakeBaseResultSet(SnowflakeResultSetSerializableV1 resultSetSerializ
this.resultSetType = resultSetSerializable.getResultSetType();
this.resultSetConcurrency = resultSetSerializable.getResultSetConcurrency();
this.resultSetHoldability = resultSetSerializable.getResultSetHoldability();
this.serializable = resultSetSerializable;
}

/**
Expand Down Expand Up @@ -131,12 +133,22 @@ protected void raiseSQLExceptionIfResultSetIsClosed() throws SQLException {

public abstract Date getDate(int columnIndex, TimeZone tz) throws SQLException;

public boolean getGetDateUseNullTimezone() {
if (this.session != null) {
return this.session.getGetDateUseNullTimezone();
}

if (this.serializable != null) {
return this.serializable.getGetDateUseNullTimezone();
}

return false;
}

@Override
public Date getDate(int columnIndex) throws SQLException {
raiseSQLExceptionIfResultSetIsClosed();
return getDate(
columnIndex,
this.session.getGetDateUseNullTimezone() ? (TimeZone) null : TimeZone.getDefault());
return getDate(columnIndex, getGetDateUseNullTimezone() ? null : TimeZone.getDefault());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public String toString() {
int resultSetType;
int resultSetConcurrency;
int resultSetHoldability;
boolean treatNTZAsUTC;
boolean formatDateWithTimezone;
boolean useSessionTimezone;
boolean getDateUseNullTimezone;
sfc-gh-pmotacki marked this conversation as resolved.
Show resolved Hide resolved

// Below are some metadata fields parsed from the result JSON node
String queryId;
Expand All @@ -173,9 +177,6 @@ public String toString() {
long sendResultTime;
List<MetaDataOfBinds> metaDataOfBinds = new ArrayList<>();
QueryResultFormat queryResultFormat;
boolean treatNTZAsUTC;
boolean formatDateWithTimezone;
boolean useSessionTimezone;
int sessionClientMemoryLimit;

// Below fields are transient, they are generated from parameters
Expand Down Expand Up @@ -235,6 +236,7 @@ private SnowflakeResultSetSerializableV1(SnowflakeResultSetSerializableV1 toCopy
this.treatNTZAsUTC = toCopy.treatNTZAsUTC;
this.formatDateWithTimezone = toCopy.formatDateWithTimezone;
this.useSessionTimezone = toCopy.useSessionTimezone;
this.getDateUseNullTimezone = toCopy.getDateUseNullTimezone;

// Below are some metadata fields parsed from the result JSON node
this.queryId = toCopy.queryId;
Expand Down Expand Up @@ -438,6 +440,7 @@ protected SnowflakeResultSetSerializableV1(
this.treatNTZAsUTC = sfSession.getTreatNTZAsUTC();
this.formatDateWithTimezone = sfSession.getFormatDateWithTimezone();
this.useSessionTimezone = sfSession.getUseSessionTimezone();
this.getDateUseNullTimezone = sfSession.getGetDateUseNullTimezone();

// setup transient fields from parameter
this.setupFieldsFromParameters();
Expand Down Expand Up @@ -701,6 +704,10 @@ public boolean getUseSessionTimezone() {
return useSessionTimezone;
}

public boolean getGetDateUseNullTimezone() {
return getDateUseNullTimezone;
sfc-gh-dheyman marked this conversation as resolved.
Show resolved Hide resolved
}

public Optional<SFBaseSession> getSession() {
return possibleSession;
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/ConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import net.snowflake.client.ConditionalIgnoreRule.ConditionalIgnore;
import net.snowflake.client.RunningNotOnTestaccount;
import net.snowflake.client.RunningOnGithubAction;
Expand Down Expand Up @@ -800,6 +803,36 @@ public void testStatementsAndResultSetsClosedByConnection() throws SQLException
assertTrue(rs3.isClosed());
}

@Test
public void testReadDateAfterSplittingResultSet() throws Exception {
Connection conn = getConnection();
try (Statement statement = conn.createStatement()) {
statement.execute("create or replace table table_with_date (int_c int, date_c date)");
statement.execute("insert into table_with_date values (1, '2015-10-25')");

try (ResultSet rs = statement.executeQuery("select * from table_with_date")) {
final SnowflakeResultSet resultSet = rs.unwrap(SnowflakeResultSet.class);
final List<SnowflakeResultSetSerializable> serializables =
resultSet.getResultSetSerializables(1 << 20);
sfc-gh-jszczerbinski marked this conversation as resolved.
Show resolved Hide resolved
final List<Date> dates =
serializables.stream()
.map(
s -> {
try {
ResultSet srs = s.getResultSet();
srs.next();
return srs.getDate(2);
} catch (Exception e) {
throw new RuntimeException(e);
sfc-gh-jszczerbinski marked this conversation as resolved.
Show resolved Hide resolved
}
})
.collect(Collectors.toList());
assertEquals(1, dates.size());
assertEquals("2015-10-25", dates.get(0).toString());
}
}
}

@Test
public void testResultSetsClosedByStatement() throws SQLException {
Connection connection = getConnection();
Expand Down
Loading