Skip to content

Commit

Permalink
Use SyntaxCheckException from RelevanceQuery.build
Browse files Browse the repository at this point in the history
Also, use getWriteableName() to get the name of the query
 in the error message.

Signed-off-by: MaxKsyunz <[email protected]>
  • Loading branch information
MaxKsyunz authored and MaxKsyunz committed May 21, 2022
1 parent d3667ce commit 7101d07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,7 +34,7 @@ public QueryBuilder build(FunctionExpression func) {
List<Expression> 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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Expression> 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<Expression> 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<Expression> arguments = List.of(
namedArgument("field", "test"),
namedArgument("query", "test2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,8 +81,8 @@ void calls_action_when_correct_argument_name() {

@ParameterizedTest
@MethodSource("insufficientArguments")
public void throws_SemanticCheckException_when_no_required_arguments(List<Expression> arguments) {
assertThrows(SemanticCheckException.class, () -> query.build(createCall(arguments)));
public void throws_SyntaxCheckException_when_no_required_arguments(List<Expression> arguments) {
assertThrows(SyntaxCheckException.class, () -> query.build(createCall(arguments)));
}

public static Stream<List<Expression>> insufficientArguments() {
Expand Down

0 comments on commit 7101d07

Please sign in to comment.