From 7101d072dca42ce4c620210dda7ab8544ec32e4e Mon Sep 17 00:00:00 2001 From: MaxKsyunz Date: Fri, 20 May 2022 02:20:09 -0700 Subject: [PATCH] Use SyntaxCheckException from RelevanceQuery.build Also, use getWriteableName() to get the name of the query in the error message. Signed-off-by: MaxKsyunz --- .../filter/lucene/relevance/RelevanceQuery.java | 10 +++++----- .../script/filter/lucene/MatchPhraseQueryTest.java | 12 ++++++------ .../lucene/relevance/RelevanceQueryBuildTest.java | 5 +++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQuery.java b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQuery.java index d6883144ec..0a90370f62 100644 --- a/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQuery.java +++ b/opensearch/src/main/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQuery.java @@ -10,9 +10,8 @@ import java.util.Map; import java.util.Objects; import java.util.function.BiFunction; -import org.opensearch.index.query.MatchPhraseQueryBuilder; import org.opensearch.index.query.QueryBuilder; -import org.opensearch.index.query.QueryBuilders; +import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.exception.SemanticCheckException; import org.opensearch.sql.expression.Expression; @@ -35,7 +34,7 @@ public QueryBuilder build(FunctionExpression func) { List arguments = func.getArguments(); if (arguments.size() < 2) { String queryName = createQueryBuilder("", "").getWriteableName(); - throw new SemanticCheckException( + throw new SyntaxCheckException( String.format("%s requires at least two parameters", queryName)); } NamedArgumentExpression field = (NamedArgumentExpression) arguments.get(0); @@ -48,8 +47,9 @@ public QueryBuilder build(FunctionExpression func) { while (iterator.hasNext()) { NamedArgumentExpression arg = (NamedArgumentExpression) iterator.next(); if (!queryBuildActions.containsKey(arg.getArgName())) { - throw new SemanticCheckException(String - .format("Parameter %s is invalid for %s function.", arg.getArgName(), queryBuilder.getWriteableName())); + throw new SemanticCheckException( + String.format("Parameter %s is invalid for %s function.", + arg.getArgName(), queryBuilder.getWriteableName())); } (Objects.requireNonNull( queryBuildActions diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MatchPhraseQueryTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MatchPhraseQueryTest.java index 599834f89c..fef3d64f95 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MatchPhraseQueryTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/MatchPhraseQueryTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.DisplayNameGeneration; import org.junit.jupiter.api.DisplayNameGenerator; import org.junit.jupiter.api.Test; +import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprType; import org.opensearch.sql.exception.SemanticCheckException; @@ -37,22 +38,21 @@ private NamedArgumentExpression namedArgument(String name, String value) { } @Test - public void test_SemanticCheckException_when_no_arguments() { - + public void test_SyntaxCheckException_when_no_arguments() { List arguments = List.of(); - assertThrows(SemanticCheckException.class, + assertThrows(SyntaxCheckException.class, () -> matchPhraseQuery.build(new MatchPhraseExpression(arguments))); } @Test - public void test_SemanticCheckException_when_one_argument() { + public void test_SyntaxCheckException_when_one_argument() { List arguments = List.of(namedArgument("field", "test")); - assertThrows(SemanticCheckException.class, + assertThrows(SyntaxCheckException.class, () -> matchPhraseQuery.build(new MatchPhraseExpression(arguments))); } @Test - public void test_SemanticCheckException_when_invalid_parameter() { + public void test_SyntaxCheckException_when_invalid_parameter() { List arguments = List.of( namedArgument("field", "test"), namedArgument("query", "test2"), diff --git a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQueryBuildTest.java b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQueryBuildTest.java index 97e956affd..8d0cc9fa7d 100644 --- a/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQueryBuildTest.java +++ b/opensearch/src/test/java/org/opensearch/sql/opensearch/storage/script/filter/lucene/relevance/RelevanceQueryBuildTest.java @@ -25,6 +25,7 @@ import org.junit.jupiter.params.provider.MethodSource; import org.mockito.Mockito; import org.opensearch.index.query.QueryBuilder; +import org.opensearch.sql.common.antlr.SyntaxCheckException; import org.opensearch.sql.data.model.ExprStringValue; import org.opensearch.sql.data.model.ExprValue; import org.opensearch.sql.data.type.ExprType; @@ -80,8 +81,8 @@ void calls_action_when_correct_argument_name() { @ParameterizedTest @MethodSource("insufficientArguments") - public void throws_SemanticCheckException_when_no_required_arguments(List arguments) { - assertThrows(SemanticCheckException.class, () -> query.build(createCall(arguments))); + public void throws_SyntaxCheckException_when_no_required_arguments(List arguments) { + assertThrows(SyntaxCheckException.class, () -> query.build(createCall(arguments))); } public static Stream> insufficientArguments() {