From 11ca4ff6e1b5e6dd8325006dde2c144323643e00 Mon Sep 17 00:00:00 2001 From: Tomoyuki Morita Date: Thu, 19 Sep 2024 11:14:25 -0700 Subject: [PATCH] Add comments Signed-off-by: Tomoyuki Morita --- .../org/opensearch/sql/spark/validator/FunctionType.java | 4 ++++ .../sql/spark/validator/GrammarElementValidator.java | 5 +++++ .../spark/validator/GrammarElementValidatorProvider.java | 1 + .../sql/spark/validator/SQLQueryValidationVisitor.java | 1 + .../opensearch/sql/spark/validator/SQLQueryValidator.java | 8 ++++++++ .../sql/spark/validator/SQLQueryValidatorTest.java | 6 ++---- 6 files changed, 21 insertions(+), 4 deletions(-) diff --git a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/FunctionType.java b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/FunctionType.java index a17f2f8b21..da3760efd6 100644 --- a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/FunctionType.java +++ b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/FunctionType.java @@ -11,6 +11,10 @@ import java.util.stream.Collectors; import lombok.AllArgsConstructor; +/** + * Enum for defining and looking up SQL function type based on its name. Unknown one will be + * considered as UDF (User Defined Function) + */ @AllArgsConstructor public enum FunctionType { AGGREGATE("Aggregate"), diff --git a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidator.java b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidator.java index b11999b5d1..cc49643772 100644 --- a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidator.java +++ b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidator.java @@ -5,6 +5,11 @@ package org.opensearch.sql.spark.validator; +/** Interface for validator to decide if each GrammarElement is valid or not. */ public interface GrammarElementValidator { + + /** + * @return true if element is valid (accepted) + */ boolean isValid(GrammarElement element); } diff --git a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidatorProvider.java b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidatorProvider.java index 7c715a5a7d..9755a1c0b6 100644 --- a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidatorProvider.java +++ b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/GrammarElementValidatorProvider.java @@ -9,6 +9,7 @@ import lombok.AllArgsConstructor; import org.opensearch.sql.datasource.model.DataSourceType; +/** Provides GrammarElementValidator based on DataSourceType. */ @AllArgsConstructor public class GrammarElementValidatorProvider { diff --git a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidationVisitor.java b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidationVisitor.java index 13a3740c8a..9ec0fb0109 100644 --- a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidationVisitor.java +++ b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidationVisitor.java @@ -84,6 +84,7 @@ import org.opensearch.sql.spark.antlr.parser.SqlBaseParser.UnsetNamespacePropertiesContext; import org.opensearch.sql.spark.antlr.parser.SqlBaseParserBaseVisitor; +/** This visitor validate grammar using GrammarElementValidator */ @AllArgsConstructor public class SQLQueryValidationVisitor extends SqlBaseParserBaseVisitor { private final GrammarElementValidator grammarElementValidator; diff --git a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidator.java b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidator.java index 23bbb933ab..b0f93c7e14 100644 --- a/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidator.java +++ b/async-query-core/src/main/java/org/opensearch/sql/spark/validator/SQLQueryValidator.java @@ -9,10 +9,18 @@ import org.opensearch.sql.datasource.model.DataSourceType; import org.opensearch.sql.spark.utils.SQLQueryUtils; +/** Validate input SQL query based on the DataSourceType. */ @AllArgsConstructor public class SQLQueryValidator { private final GrammarElementValidatorProvider grammarElementValidatorProvider; + /** + * It will look up validator associated with the DataSourceType, and throw + * IllegalArgumentException if invalid grammar element is found. + * + * @param sqlQuery The query to be validated + * @param datasourceType + */ public void validate(String sqlQuery, DataSourceType datasourceType) { GrammarElementValidator grammarElementValidator = grammarElementValidatorProvider.getValidatorForDatasource(datasourceType); diff --git a/async-query-core/src/test/java/org/opensearch/sql/spark/validator/SQLQueryValidatorTest.java b/async-query-core/src/test/java/org/opensearch/sql/spark/validator/SQLQueryValidatorTest.java index 725d5362aa..a939e9411c 100644 --- a/async-query-core/src/test/java/org/opensearch/sql/spark/validator/SQLQueryValidatorTest.java +++ b/async-query-core/src/test/java/org/opensearch/sql/spark/validator/SQLQueryValidatorTest.java @@ -211,16 +211,14 @@ private enum TestElement { void testAllowAllByDefault() { when(mockedProvider.getValidatorForDatasource(any())) .thenReturn(new DefaultGrammarElementValidator()); - VerifyValidator v = - new VerifyValidator(sqlQueryValidator, DataSourceType.SPARK); + VerifyValidator v = new VerifyValidator(sqlQueryValidator, DataSourceType.SPARK); Arrays.stream(TestElement.values()).forEach(v::ok); } @Test void testDenyAllValidator() { when(mockedProvider.getValidatorForDatasource(any())).thenReturn(element -> false); - VerifyValidator v = - new VerifyValidator(sqlQueryValidator, DataSourceType.SPARK); + VerifyValidator v = new VerifyValidator(sqlQueryValidator, DataSourceType.SPARK); // The elements which doesn't have validation will be accepted. // That's why there are some ok case