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

[SEDONA-541] Allow concurrent snowflake testers #1357

Merged
merged 1 commit into from
Apr 23, 2024
Merged
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 @@ -32,11 +32,13 @@

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.assertArrayEquals;

Expand All @@ -52,6 +54,8 @@ public class TestBase extends TestCase {

private static boolean jarUploaded = false;

String snowflake_db_name = generateRandomString(8);

public void registerUDF(String functionName, Class<?> ... paramTypes) {
try {
String ddl = UDFDDLGenerator.buildUDFDDL(UDFs.class.getMethod(
Expand Down Expand Up @@ -102,12 +106,13 @@ public void init() throws SQLException {
buildDDLConfigs = new HashMap<>();
buildDDLConfigs.put(Constants.SEDONA_VERSION, sedonaVersion);
buildDDLConfigs.put(Constants.GEOTOOLS_VERSION, geotoolsVersion);
System.out.println("Using Snowflake DB: " + snowflake_db_name);
// upload libraries
if (!jarUploaded) {
// drop then create db to make sure test env fresh
snowClient.executeQuery("drop database if exists " + System.getenv("SNOWFLAKE_DB"));
snowClient.executeQuery("create database " + System.getenv("SNOWFLAKE_DB"));
snowClient.executeQuery("use database " + System.getenv("SNOWFLAKE_DB"));
snowClient.executeQuery("drop database if exists " + snowflake_db_name);
snowClient.executeQuery("create database " + snowflake_db_name);
snowClient.executeQuery("use database " + snowflake_db_name);
snowClient.executeQuery("create schema " + System.getenv("SNOWFLAKE_SCHEMA"));
snowClient.executeQuery("use schema " + System.getenv("SNOWFLAKE_SCHEMA"));
snowClient.executeQuery("CREATE STAGE ApacheSedona FILE_FORMAT = (COMPRESSION = NONE)");
Expand All @@ -118,7 +123,15 @@ public void init() throws SQLException {
registerDependantUDFs();
}

public void tearDown() {
public void tearDown()
{
try {
System.out.println("Dropping Snowflake DB: " + snowflake_db_name);
snowClient.executeQuery("drop database if exists " + snowflake_db_name);
}
catch (SQLException e) {
throw new RuntimeException(e);
}
if (snowClient != null) {
try {
snowClient.close();
Expand Down Expand Up @@ -174,4 +187,24 @@ public ResultSet sqlSingleRes(String sql) {
throw new RuntimeException(e);
}
}

/**
* Generates a random string of a specified length.
*
* @param length The length of the random string.
* @return A random string consisting of alphabet letters only.
*/
private static String generateRandomString(int length) {
// Generate a string of all letters
String chars = IntStream.concat(IntStream.rangeClosed('A', 'Z'), IntStream.rangeClosed('a', 'z'))
.mapToObj(c -> "" + (char) c)
.collect(Collectors.joining());

Random random = new Random();

// Build a random string from the full set of characters
return random.ints(length, 0, chars.length())
.mapToObj(i -> "" + chars.charAt(i)) // Correctly refer to character position
.collect(Collectors.joining());
}
}
Loading