Skip to content

Commit

Permalink
SNOW-1531307: Add variant bind parameters sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dprzybysz committed Jul 17, 2024
1 parent b9dcc47 commit 9e6f7c0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
71 changes: 71 additions & 0 deletions src/test/java/net/snowflake/client/jdbc/PreparedStatement1IT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.sql.Types;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import net.snowflake.client.ConditionalIgnoreRule;
import net.snowflake.client.RunningOnGithubAction;
import net.snowflake.client.category.TestCategoryStatement;
Expand Down Expand Up @@ -790,4 +791,74 @@ public void manualTestForPreparedStatementLogging() throws SQLException {
}
}
}

/**
* This test shows how to use batch requests with variant columns. Syntax <code>
* insert into ... Select column1, ... from values (?,...)</code> is crucial there.
*/
@Test
public void shouldUseVariantBindParametersSNOW1531307() throws SQLException {
try (Connection con = getConnection();
Statement stmt = con.createStatement()) {
String tableName = "SNOW1531307";
createTableForVariantBindParametersSNOW1531307(stmt, tableName);
executePreparedStatementWithBatchesSNOW1531307(con, tableName, 20);
}
}

/**
* This test shows how to use batch requests with variant columns. Syntax <code>
* insert into ... Select column1, ... from values (?,...)</code> is crucial there.
* CLIENT_STAGE_ARRAY_BINDING_THRESHOLD parameter is used on session to set the minimal number of
* bind parameters to create stage file with provided parameters - it may be set only by
* administrator.
*/
@Test
@ConditionalIgnoreRule.ConditionalIgnore(condition = RunningOnGithubAction.class)
public void shouldUseVariantBindParametersWithStageSNOW1531307() throws SQLException {
try (Connection con = getConnection();
Statement stmt = con.createStatement()) {
String tableName = "SNOW1531307";
createTableForVariantBindParametersSNOW1531307(stmt, tableName);

try {
stmt.execute("ALTER SESSION SET CLIENT_STAGE_ARRAY_BINDING_THRESHOLD = 10");
executePreparedStatementWithBatchesSNOW1531307(con, tableName, 20);
} finally {
// we are planning to reuse connection so let's cleanup
stmt.execute("ALTER SESSION UNSET CLIENT_STAGE_ARRAY_BINDING_THRESHOLD");
}
}
}

private static void createTableForVariantBindParametersSNOW1531307(
Statement stmt, String tableName) throws SQLException {
stmt.execute(
"CREATE OR REPLACE TABLE "
+ tableName
+ "(id text, array variant, json1 variant, json2 variant, str variant, num variant)");
}

private static void executePreparedStatementWithBatchesSNOW1531307(
Connection con, String tableName, int numberOfBatches) throws SQLException {
try (PreparedStatement ps =
con.prepareStatement(
"insert into "
+ tableName
+ "(id, array, json1, json2, str, num) "
+ "Select column1, parse_json(column2), parse_json(column3), parse_json(column4), to_variant(column5), to_variant(column6) "
+ "from values(?, ?, ?, ?, ?, ?)")) {
for (int i = 0; i < numberOfBatches; i++) {
int parameterIndex = 1;
ps.setString(parameterIndex++, UUID.randomUUID().toString());
ps.setObject(parameterIndex++, "[1,2]");
ps.setObject(parameterIndex++, null);
ps.setObject(parameterIndex++, "{\"a\": 5}");
ps.setObject(parameterIndex++, "bla");
ps.setObject(parameterIndex++, 5);
ps.addBatch();
}
assertEquals(numberOfBatches, ps.executeBatch().length);
}
}
}
2 changes: 1 addition & 1 deletion src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</encoder>
</appender>

<root level="OFF">
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>

0 comments on commit 9e6f7c0

Please sign in to comment.