Skip to content

Commit

Permalink
Make session and serializable fields final, cleanup the test
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jszczerbinski committed Aug 29, 2024
1 parent faf1c25 commit 62fe3d7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,28 @@ public abstract class SnowflakeBaseResultSet implements ResultSet {
protected SnowflakeResultSetMetaDataV1 resultSetMetaData = null;
protected Map<String, Object> parameters = new HashMap<>();
private int fetchSize = 0;
protected SFBaseSession session = null;
private SnowflakeResultSetSerializableV1 serializable = null;
protected final SFBaseSession session;
private final SnowflakeResultSetSerializableV1 serializable;
private static final ObjectMapper OBJECT_MAPPER = ObjectMapperFactory.getObjectMapper();

SnowflakeBaseResultSet(Statement statement) throws SQLException {
this.statement = statement;
this.resultSetType = statement.getResultSetType();
this.resultSetConcurrency = statement.getResultSetConcurrency();
this.resultSetHoldability = statement.getResultSetHoldability();
this.session = maybeGetSession(statement);
this.serializable = null;
}

private static SFBaseSession maybeGetSession(Statement statement) {
try {
this.session = statement.unwrap(SnowflakeStatementV1.class).connection.getSFBaseSession();
return statement.unwrap(SnowflakeStatementV1.class).connection.getSFBaseSession();
} catch (SQLException e) {
// This exception shouldn't be hit. Statement class should be able to be unwrapped.
logger.error(
"Unable to unwrap SnowflakeStatementV1 class to retrieve session. Session is null.",
false);
this.session = null;
return null;
}
}

Expand All @@ -96,6 +101,7 @@ public SnowflakeBaseResultSet(SnowflakeResultSetSerializableV1 resultSetSerializ
this.resultSetType = resultSetSerializable.getResultSetType();
this.resultSetConcurrency = resultSetSerializable.getResultSetConcurrency();
this.resultSetHoldability = resultSetSerializable.getResultSetHoldability();
this.session = null;
this.serializable = resultSetSerializable;
}

Expand All @@ -109,6 +115,8 @@ protected SnowflakeBaseResultSet() throws SQLException {
this.resultSetConcurrency = 0;
this.resultSetHoldability = 0;
this.statement = new SnowflakeStatementV1.NoOpSnowflakeStatementV1();
this.session = null;
this.serializable = null;
}

@Override
Expand Down
24 changes: 9 additions & 15 deletions src/test/java/net/snowflake/client/jdbc/ConnectionIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Enumeration;
Expand All @@ -41,7 +42,6 @@
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 @@ -812,21 +812,15 @@ public void testReadDateAfterSplittingResultSet() throws Exception {

try (ResultSet rs = statement.executeQuery("select * from table_with_date")) {
final SnowflakeResultSet resultSet = rs.unwrap(SnowflakeResultSet.class);
final long arbitrarySizeInBytes = 1024;
final List<SnowflakeResultSetSerializable> serializables =
resultSet.getResultSetSerializables(1 << 20);
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);
}
})
.collect(Collectors.toList());
resultSet.getResultSetSerializables(arbitrarySizeInBytes);
final ArrayList<Date> dates = new ArrayList<>();
for (SnowflakeResultSetSerializable s : serializables) {
ResultSet srs = s.getResultSet();
srs.next();
dates.add(srs.getDate(2));
}
assertEquals(1, dates.size());
assertEquals("2015-10-25", dates.get(0).toString());
}
Expand Down

0 comments on commit 62fe3d7

Please sign in to comment.