Skip to content

Commit bb7514f

Browse files
fix: add more validation for the async-query plugin (#5853) (#5854)
Co-authored-by: Simeon Widdis <sawiddis@gmail.com> Signed-off-by: Simeon Widdis <sawiddis@amazon.com> Signed-off-by: opensearch-ci-bot <opensearch-infra@amazon.com>
1 parent 93f03f0 commit bb7514f

5 files changed

Lines changed: 125 additions & 3 deletions

File tree

‎async-query-core/src/main/java/org/opensearch/sql/spark/dispatcher/model/IndexQueryDetails.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class IndexQueryDetails {
3232
// table name and mv name are combined.
3333
private String mvName;
3434
private String mvQuery;
35+
private String filterCondition;
3536
private FlintIndexType indexType;
3637

3738
private IndexQueryDetails() {}
@@ -79,6 +80,11 @@ public IndexQueryDetailsBuilder mvQuery(String mvQuery) {
7980
return this;
8081
}
8182

83+
public IndexQueryDetailsBuilder filterCondition(String filterCondition) {
84+
indexQueryDetails.filterCondition = filterCondition;
85+
return this;
86+
}
87+
8288
public IndexQueryDetailsBuilder indexType(FlintIndexType indexType) {
8389
indexQueryDetails.indexType = indexType;
8490
return this;

‎async-query-core/src/main/java/org/opensearch/sql/spark/utils/SQLQueryUtils.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser;
2424
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser.MaterializedViewQueryContext;
2525
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser.RecoverIndexJobStatementContext;
26+
import org.opensearch.sql.spark.antlr.parser.FlintSparkSqlExtensionsParser.WhereClauseContext;
2627
import org.opensearch.sql.spark.antlr.parser.SqlBaseLexer;
2728
import org.opensearch.sql.spark.antlr.parser.SqlBaseParser;
2829
import org.opensearch.sql.spark.antlr.parser.SqlBaseParser.IdentifierReferenceContext;
@@ -368,6 +369,16 @@ public Void visitMaterializedViewQuery(MaterializedViewQueryContext ctx) {
368369
return super.visitMaterializedViewQuery(ctx);
369370
}
370371

372+
@Override
373+
public Void visitWhereClause(WhereClauseContext ctx) {
374+
FlintSparkSqlExtensionsParser.FilterConditionContext filterCtx = ctx.filterCondition();
375+
int a = filterCtx.start.getStartIndex();
376+
int b = filterCtx.stop.getStopIndex();
377+
String condition = filterCtx.start.getInputStream().getText(new Interval(a, b));
378+
indexQueryDetailsBuilder.filterCondition(condition);
379+
return super.visitWhereClause(ctx);
380+
}
381+
371382
@Override
372383
public Void visitRecoverIndexJobStatement(RecoverIndexJobStatementContext ctx) {
373384
indexQueryDetailsBuilder.indexQueryActionType(IndexQueryActionType.RECOVER);

‎async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidationVisitor.java‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -398,9 +398,7 @@ public Void visitJoinRelation(JoinRelationContext ctx) {
398398

399399
@Override
400400
public Void visitTransformClause(TransformClauseContext ctx) {
401-
if (ctx.TRANSFORM() != null) {
402-
validateAllowed(SQLGrammarElement.TRANSFORM);
403-
}
401+
validateAllowed(SQLGrammarElement.TRANSFORM);
404402
return super.visitTransformClause(ctx);
405403
}
406404

‎async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidator.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,9 @@ public void validateFlintExtensionQuery(String sqlQuery, DataSourceType dataSour
5252
if (mvQuery != null) {
5353
validate(mvQuery, dataSourceType);
5454
}
55+
String filterCondition = indexQueryDetails.getFilterCondition();
56+
if (filterCondition != null) {
57+
validate("SELECT * FROM t WHERE " + filterCondition, dataSourceType);
58+
}
5559
}
5660
}

‎async-query-core/src/test/java/org/opensearch/sql/spark/validator/SQLQueryValidatorTest.java‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,109 @@ void testValidateFlintExtensionQuery_coveringIndexPass() {
683683
DataSourceType.S3GLUE));
684684
}
685685

686+
@Test
687+
void testValidateFlintExtensionQuery_blocksMapUsingInMV() {
688+
when(mockedProvider.getValidatorForDatasource(any()))
689+
.thenReturn(new S3GlueSQLGrammarElementValidator());
690+
assertThrows(
691+
IllegalArgumentException.class,
692+
() ->
693+
sqlQueryValidator.validateFlintExtensionQuery(
694+
"CREATE MATERIALIZED VIEW mv AS MAP col USING 'cmd' AS (x) FROM tbl"
695+
+ " WITH (auto_refresh = false)",
696+
DataSourceType.S3GLUE));
697+
}
698+
699+
@Test
700+
void testValidateFlintExtensionQuery_blocksReduceUsingInMV() {
701+
when(mockedProvider.getValidatorForDatasource(any()))
702+
.thenReturn(new S3GlueSQLGrammarElementValidator());
703+
assertThrows(
704+
IllegalArgumentException.class,
705+
() ->
706+
sqlQueryValidator.validateFlintExtensionQuery(
707+
"CREATE MATERIALIZED VIEW mv AS REDUCE col USING 'cmd' AS (x) FROM tbl"
708+
+ " WITH (auto_refresh = false)",
709+
DataSourceType.S3GLUE));
710+
}
711+
712+
@Test
713+
void testValidate_blocksMapUsing() {
714+
when(mockedProvider.getValidatorForDatasource(any()))
715+
.thenReturn(new S3GlueSQLGrammarElementValidator());
716+
VerifyValidator v = new VerifyValidator(sqlQueryValidator, DataSourceType.S3GLUE);
717+
v.ng("MAP col USING 'cmd' AS (x) FROM tbl");
718+
}
719+
720+
@Test
721+
void testValidate_blocksReduceUsing() {
722+
when(mockedProvider.getValidatorForDatasource(any()))
723+
.thenReturn(new S3GlueSQLGrammarElementValidator());
724+
VerifyValidator v = new VerifyValidator(sqlQueryValidator, DataSourceType.S3GLUE);
725+
v.ng("REDUCE col USING 'cmd' AS (x) FROM tbl");
726+
}
727+
728+
@Test
729+
void testValidateFlintExtensionQuery_blocksReflectInCoveringIndexWhere() {
730+
when(mockedProvider.getValidatorForDatasource(any()))
731+
.thenReturn(new S3GlueSQLGrammarElementValidator());
732+
assertThrows(
733+
IllegalArgumentException.class,
734+
() ->
735+
sqlQueryValidator.validateFlintExtensionQuery(
736+
"CREATE INDEX idx ON tbl (col) WHERE reflect('java.lang.System', 'getenv',"
737+
+ " 'PATH') IS NOT NULL",
738+
DataSourceType.S3GLUE));
739+
}
740+
741+
@Test
742+
void testValidateFlintExtensionQuery_blocksJavaMethodInCoveringIndexWhere() {
743+
when(mockedProvider.getValidatorForDatasource(any()))
744+
.thenReturn(new S3GlueSQLGrammarElementValidator());
745+
assertThrows(
746+
IllegalArgumentException.class,
747+
() ->
748+
sqlQueryValidator.validateFlintExtensionQuery(
749+
"CREATE INDEX idx ON tbl (col) WHERE java_method('java.lang.System', 'getenv',"
750+
+ " 'PATH') IS NOT NULL",
751+
DataSourceType.S3GLUE));
752+
}
753+
754+
@Test
755+
void testValidateFlintExtensionQuery_coveringIndexSafeWherePass() {
756+
when(mockedProvider.getValidatorForDatasource(any()))
757+
.thenReturn(new S3GlueSQLGrammarElementValidator());
758+
assertDoesNotThrow(
759+
() ->
760+
sqlQueryValidator.validateFlintExtensionQuery(
761+
"CREATE INDEX idx ON tbl (col) WHERE status > 0 AND age < 100",
762+
DataSourceType.S3GLUE));
763+
}
764+
765+
@Test
766+
void testValidateFlintExtensionQuery_blocksReflectInSkippingIndexWhere() {
767+
when(mockedProvider.getValidatorForDatasource(any()))
768+
.thenReturn(new S3GlueSQLGrammarElementValidator());
769+
assertThrows(
770+
IllegalArgumentException.class,
771+
() ->
772+
sqlQueryValidator.validateFlintExtensionQuery(
773+
"CREATE SKIPPING INDEX ON tbl (col VALUE_SET) WHERE reflect('java.lang.System',"
774+
+ " 'getenv', 'PATH') IS NOT NULL",
775+
DataSourceType.S3GLUE));
776+
}
777+
778+
@Test
779+
void testValidateFlintExtensionQuery_skippingIndexSafeWherePass() {
780+
when(mockedProvider.getValidatorForDatasource(any()))
781+
.thenReturn(new S3GlueSQLGrammarElementValidator());
782+
assertDoesNotThrow(
783+
() ->
784+
sqlQueryValidator.validateFlintExtensionQuery(
785+
"CREATE SKIPPING INDEX ON tbl (col VALUE_SET) WHERE status > 0",
786+
DataSourceType.S3GLUE));
787+
}
788+
686789
@Test
687790
void testInvalidIdentifier() {
688791
when(mockedProvider.getValidatorForDatasource(any())).thenReturn(element -> true);

0 commit comments

Comments
 (0)