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

[Backport 2.x] Bug Fix: Escape query in spark submit parameter (#2587) #2592

Merged
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
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: 'com.squareup.okhttp3', name: 'okhttp', version: '4.9.3'
api group: 'org.apache.commons', name: 'commons-text', version: '1.10.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}"
api group: 'com.amazonaws', name: 'aws-java-sdk-sts', 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 @@ -37,6 +37,7 @@ repositories {
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 @@ -92,6 +92,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,25 @@ 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));
}

@Test
public void testBuildQueryStringSpecialCharacter() {
String rawQuery = "SELECT '{\"test ,:+\\\"inner\\\"/\\|?#><\"}'";
String expectedQueryInParams = "SELECT '{\\\"test ,:+\\\\\\\"inner\\\\\\\"/\\\\|?#><\\\"}'";
String params = SparkSubmitParameters.Builder.builder().query(rawQuery).build().toString();
assertTrue(params.contains(expectedQueryInParams));
}
}
Loading