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-1531307: Add variant bind parameters sample #1829

Closed
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 @@ -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");
sfc-gh-pfus marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}
}
Loading