Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoyuki Morita <[email protected]>
  • Loading branch information
ykmr1224 committed Sep 19, 2024
1 parent 57a5289 commit 11ca4ff
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.AllArgsConstructor;
import org.opensearch.sql.datasource.model.DataSourceType;

/** Provides GrammarElementValidator based on DataSourceType. */
@AllArgsConstructor
public class GrammarElementValidatorProvider {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void> {
private final GrammarElementValidator grammarElementValidator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 11ca4ff

Please sign in to comment.