Skip to content

Commit

Permalink
escape query in spark submit parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Sean Kao <[email protected]>
  • Loading branch information
seankao-az committed Mar 22, 2024
1 parent a67de21 commit 24a23fe
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ dependencies {
api group: 'com.google.guava', name: 'guava', version: '32.0.1-jre'
api group: 'org.apache.logging.log4j', name: 'log4j-core', version:"${versions.log4j}"
api group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
api group: 'org.apache.commons', name: 'commons-text', version: '1.10.0'
api group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.12.0'
implementation 'com.github.babbel:okhttp-aws-signer:1.0.2'
api group: 'com.amazonaws', name: 'aws-java-sdk-core', version: "${aws_java_sdk_version}"
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pitest {
dependencies {
api group: 'com.google.guava', name: 'guava', version: '32.0.1-jre'
api group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
api group: 'org.apache.commons', name: 'commons-text', version: '1.10.0'
api group: 'com.facebook.presto', name: 'presto-matching', version: '0.240'
api group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
api "com.fasterxml.jackson.core:jackson-core:${versions.jackson}"
Expand Down
1 change: 1 addition & 0 deletions legacy/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ dependencies {
implementation group: 'com.google.guava', name: 'guava', version: '32.0.1-jre'
implementation group: 'org.json', name: 'json', version:'20231013'
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.12.0'
implementation group: 'org.apache.commons', name: 'commons-text', version: '1.10.0'
implementation group: 'org.opensearch', name: 'opensearch', version: "${opensearch_version}"
// add geo module as dependency. https://github.com/opensearch-project/OpenSearch/pull/4180/.
implementation group: 'org.opensearch.plugin', name: 'geo', version: "${opensearch_version}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.function.Supplier;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.apache.commons.text.StringEscapeUtils;
import org.opensearch.sql.datasource.model.DataSourceMetadata;
import org.opensearch.sql.datasource.model.DataSourceType;
import org.opensearch.sql.datasources.auth.AuthenticationType;
Expand Down Expand Up @@ -85,8 +86,13 @@ public Builder clusterName(String clusterName) {
return this;
}

/**
* For query in spark submit parameters to be parsed correctly, escape
* the characters in the query, then wrap the query with double quotes.
*/
public Builder query(String query) {
String wrappedQuery = "\"" + query + "\""; // Wrap the query with double quotes
String escapedQuery = StringEscapeUtils.escapeJava(query);
String wrappedQuery = "\"" + escapedQuery + "\"";
config.put(FLINT_JOB_QUERY, wrappedQuery);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ public void testBuildWithExtraParameters() {

@Test
public void testBuildQueryString() {
String query = "SHOW tables LIKE \"%\";";
String params = SparkSubmitParameters.Builder.builder().query(query).build().toString();
assertTrue(params.contains(query));
String rawQuery = "SHOW tables LIKE \"%\";";
String expectedQueryInParams = "\"SHOW tables LIKE \\\"%\\\";\"";
String params = SparkSubmitParameters.Builder.builder().query(rawQuery).build().toString();
assertTrue(params.contains(expectedQueryInParams));
}

@Test
public void testBuildQueryStringNestedQuote() {
String rawQuery = "SELECT '\"1\"'";
String expectedQueryInParams = "\"SELECT '\\\"1\\\"'\"";
String params = SparkSubmitParameters.Builder.builder().query(rawQuery).build().toString();
assertTrue(params.contains(expectedQueryInParams));
}
}

0 comments on commit 24a23fe

Please sign in to comment.