Skip to content

Commit

Permalink
Add uncategorized functions to SQL query validator
Browse files Browse the repository at this point in the history
Signed-off-by: Tomoyuki Morita <[email protected]>
  • Loading branch information
ykmr1224 committed Feb 5, 2025
1 parent 82eebce commit f414ef9
Showing 1 changed file with 17 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.opensearch.sql.spark.validator;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -192,15 +193,14 @@ private enum TestElement {
// Generator Functions
GENERATOR_FUNCTIONS("SELECT explode(array(1, 2, 3));"),

// UNCATEGORIZED functions
// Ucategorized functions
NAMED_STRUCT("SELECT named_struct('a', 1);"),
PARSE_URL("SELECT parse_url(url) FROM my_table;");
PARSE_URL("SELECT parse_url(url) FROM my_table;"),

// UDFs (User-Defined Functions)
SCALAR_USER_DEFINED_FUNCTIONS("SELECT my_udf(name) FROM my_table;"),
USER_DEFINED_AGGREGATE_FUNCTIONS("SELECT my_udaf(age) FROM my_table GROUP BY name;"),
INTEGRATION_WITH_HIVE_UDFS_UDAFS_UDTFS("SELECT my_hive_udf(name) FROM my_table;"),
;
INTEGRATION_WITH_HIVE_UDFS_UDAFS_UDTFS("SELECT my_hive_udf(name) FROM my_table;");

@Getter private final String[] queries;

Expand Down Expand Up @@ -328,14 +328,14 @@ void testDenyAllValidator() {
// Generator Functions
v.ng(TestElement.GENERATOR_FUNCTIONS);

// Uncategorized Functions
v.ng(TestElement.NAMED_STRUCT);
v.ng(TestElement.PARSE_URL);

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

// UNSUPPORTED Functions
v.ng(TestElement.NAMED_STRUCT);
v.ng(TestElement.PARSE_URL);
}

@Test
Expand Down Expand Up @@ -450,8 +450,8 @@ void testS3glueQueries() {
v.ok(TestElement.GENERATOR_FUNCTIONS);

// Uncategorized Functions
v.ng(TestElement.NAMED_STRUCT);
v.ng(TestElement.PARSE_URL);
v.ok(TestElement.NAMED_STRUCT);
v.ok(TestElement.PARSE_URL);

// UDFs
v.ng(TestElement.SCALAR_USER_DEFINED_FUNCTIONS);
Expand Down Expand Up @@ -636,11 +636,10 @@ void testUnsupportedHiveNativeCommand() {

@Test
void testException() {
when(mockedProvider.getValidatorForDatasource(any()))
.thenReturn(new S3GlueSQLGrammarElementValidator());
when(mockedProvider.getValidatorForDatasource(any())).thenReturn(element -> false);
VerifyValidator v = new VerifyValidator(sqlQueryValidator, DataSourceType.S3GLUE);

v.ng("SELECT named_struct('a', 1);", "Uncategorized function (named_struct) is not supported.");
v.ng("SELECT named_struct('a', 1);", "Uncategorized functions (named_struct) is not allowed.");
}

@AllArgsConstructor
Expand Down Expand Up @@ -668,10 +667,11 @@ public void ng(String query) {
}

public void ng(String query, String expectedMessage) {
Exception e = assertThrows(
IllegalArgumentException.class,
() -> runValidate(query),
"The query should throw: query=`" + query.toString() + "`");
Exception e =
assertThrows(
IllegalArgumentException.class,
() -> runValidate(query),
"The query should throw: query=`" + query.toString() + "`");
assertEquals(expectedMessage, e.getMessage());
}

Expand Down

0 comments on commit f414ef9

Please sign in to comment.