Skip to content

Commit

Permalink
Address 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 20, 2024
1 parent 11ca4ff commit d31bdc8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 196 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
package org.opensearch.sql.spark.validator;

import lombok.AllArgsConstructor;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
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 static final Logger log = LogManager.getLogger(SQLQueryValidator.class);

private final GrammarElementValidatorProvider grammarElementValidatorProvider;

/**
Expand All @@ -25,6 +29,11 @@ public void validate(String sqlQuery, DataSourceType datasourceType) {
GrammarElementValidator grammarElementValidator =
grammarElementValidatorProvider.getValidatorForDatasource(datasourceType);
SQLQueryValidationVisitor visitor = new SQLQueryValidationVisitor(grammarElementValidator);
visitor.visit(SQLQueryUtils.getBaseParser(sqlQuery).singleStatement());
try {
visitor.visit(SQLQueryUtils.getBaseParser(sqlQuery).singleStatement());
} catch (IllegalArgumentException e) {
log.error("Query validation failed. DataSourceType=" + datasourceType, e);
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -566,124 +566,6 @@ void testSecurityLakeQueries() {
v.ng(TestElement.INTEGRATION_WITH_HIVE_UDFS_UDAFS_UDTFS);
}

@Test
void testCloudWatchLogs() {
when(mockedProvider.getValidatorForDatasource(any()))
.thenReturn(new CloudWatchLogsGrammarElementValidator());
VerifyValidator v =
new VerifyValidator(new SQLQueryValidator(mockedProvider), DataSourceType.SPARK);

// DDL Statements
v.ng(TestElement.ALTER_DATABASE);
v.ng(TestElement.ALTER_TABLE);
v.ng(TestElement.ALTER_VIEW);
v.ng(TestElement.CREATE_DATABASE);
v.ng(TestElement.CREATE_FUNCTION);
v.ng(TestElement.CREATE_TABLE);
v.ng(TestElement.CREATE_VIEW);
v.ng(TestElement.DROP_DATABASE);
v.ng(TestElement.DROP_FUNCTION);
v.ng(TestElement.DROP_TABLE);
v.ng(TestElement.DROP_VIEW);
v.ng(TestElement.REPAIR_TABLE);
v.ng(TestElement.TRUNCATE_TABLE);

// DML Statements
v.ng(TestElement.INSERT_TABLE);
v.ng(TestElement.INSERT_OVERWRITE_DIRECTORY);
v.ng(TestElement.LOAD);

// Data Retrieval
v.ok(TestElement.SELECT);
v.ng(TestElement.EXPLAIN);
v.ng(TestElement.COMMON_TABLE_EXPRESSION);
v.ng(TestElement.CLUSTER_BY_CLAUSE);
v.ng(TestElement.DISTRIBUTE_BY_CLAUSE);
v.ok(TestElement.GROUP_BY_CLAUSE);
v.ok(TestElement.HAVING_CLAUSE);
v.ng(TestElement.HINTS);
v.ng(TestElement.INLINE_TABLE);
v.ng(TestElement.FILE);
v.ok(TestElement.INNER_JOIN);
v.ng(TestElement.CROSS_JOIN);
v.ok(TestElement.LEFT_OUTER_JOIN);
v.ng(TestElement.LEFT_SEMI_JOIN);
v.ng(TestElement.RIGHT_OUTER_JOIN);
v.ng(TestElement.FULL_OUTER_JOIN);
v.ng(TestElement.LEFT_ANTI_JOIN);
v.ok(TestElement.LIKE_PREDICATE);
v.ok(TestElement.LIMIT_CLAUSE);
v.ok(TestElement.OFFSET_CLAUSE);
v.ok(TestElement.ORDER_BY_CLAUSE);
v.ok(TestElement.SET_OPERATORS);
v.ok(TestElement.SORT_BY_CLAUSE);
v.ng(TestElement.TABLESAMPLE);
v.ng(TestElement.TABLE_VALUED_FUNCTION);
v.ok(TestElement.WHERE_CLAUSE);
v.ok(TestElement.AGGREGATE_FUNCTION);
v.ok(TestElement.WINDOW_FUNCTION);
v.ok(TestElement.CASE_CLAUSE);
v.ok(TestElement.PIVOT_CLAUSE);
v.ok(TestElement.UNPIVOT_CLAUSE);
v.ng(TestElement.LATERAL_VIEW_CLAUSE);
v.ng(TestElement.LATERAL_SUBQUERY);
v.ng(TestElement.TRANSFORM_CLAUSE);

// Auxiliary Statements
v.ng(TestElement.ADD_FILE);
v.ng(TestElement.ADD_JAR);
v.ng(TestElement.ANALYZE_TABLE);
v.ng(TestElement.CACHE_TABLE);
v.ng(TestElement.CLEAR_CACHE);
v.ng(TestElement.DESCRIBE_DATABASE);
v.ng(TestElement.DESCRIBE_FUNCTION);
v.ng(TestElement.DESCRIBE_QUERY);
v.ng(TestElement.DESCRIBE_TABLE);
v.ng(TestElement.LIST_FILE);
v.ng(TestElement.LIST_JAR);
v.ng(TestElement.REFRESH);
v.ng(TestElement.REFRESH_TABLE);
v.ng(TestElement.REFRESH_FUNCTION);
v.ng(TestElement.RESET);
v.ng(TestElement.SET);
v.ng(TestElement.SHOW_COLUMNS);
v.ng(TestElement.SHOW_CREATE_TABLE);
v.ng(TestElement.SHOW_DATABASES);
v.ng(TestElement.SHOW_FUNCTIONS);
v.ng(TestElement.SHOW_PARTITIONS);
v.ng(TestElement.SHOW_TABLE_EXTENDED);
v.ng(TestElement.SHOW_TABLES);
v.ng(TestElement.SHOW_TBLPROPERTIES);
v.ng(TestElement.SHOW_VIEWS);
v.ng(TestElement.UNCACHE_TABLE);

// Functions
v.ok(TestElement.ARRAY_FUNCTIONS);
v.ok(TestElement.MAP_FUNCTIONS);
v.ok(TestElement.DATE_AND_TIMESTAMP_FUNCTIONS);
v.ok(TestElement.JSON_FUNCTIONS);
v.ok(TestElement.MATHEMATICAL_FUNCTIONS);
v.ok(TestElement.STRING_FUNCTIONS);
v.ok(TestElement.BITWISE_FUNCTIONS);
v.ok(TestElement.CONVERSION_FUNCTIONS);
v.ok(TestElement.CONDITIONAL_FUNCTIONS);
v.ok(TestElement.PREDICATE_FUNCTIONS);
v.ng(TestElement.CSV_FUNCTIONS);
v.ng(TestElement.MISC_FUNCTIONS);

// Aggregate-like Functions
v.ok(TestElement.AGGREGATE_FUNCTIONS);
v.ok(TestElement.WINDOW_FUNCTIONS);

// Generator Functions
v.ok(TestElement.GENERATOR_FUNCTIONS);

// UDFs
v.ng(TestElement.SCALAR_USER_DEFINED_FUNCTIONS);
v.ng(TestElement.USER_DEFINED_AGGREGATE_FUNCTIONS);
v.ng(TestElement.INTEGRATION_WITH_HIVE_UDFS_UDAFS_UDTFS);
}

@AllArgsConstructor
private static class VerifyValidator {
private final SQLQueryValidator validator;
Expand Down

0 comments on commit d31bdc8

Please sign in to comment.