Skip to content

Commit

Permalink
Code cleanup, adding parsing failure tests, and adding tests for high…
Browse files Browse the repository at this point in the history
…light acceptance as a string literal as well as qualified name.

Signed-off-by: forestmvey <forestv@bitquilltech.com>
  • Loading branch information
forestmvey committed Jul 29, 2022
1 parent 3db8788 commit 3ee3491
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;
import org.opensearch.sql.ast.dsl.AstDSL;
import org.opensearch.sql.data.model.ExprValue;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
public class HighlightOperatorTest extends PhysicalPlanTestBase {

@Test
public void highlight_all_test() {
public void valid_highlight_operator_test() {
PhysicalPlan plan = new HighlightOperator(new TestScan(), DSL.ref("*", STRING));
List<ExprValue> result = execute(plan);
assertEquals(5, result.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@ protected void init() throws Exception {
public void single_highlight_test() {
String query = "SELECT Tags, highlight('Tags') FROM %s WHERE match(Tags, 'yeast') LIMIT 1";
JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_BEER));
verifySchema(response, schema("Tags", null, "text"), schema("highlight('Tags')", null, "keyword"));
verifySchema(response, schema("Tags", null, "text"),
schema("highlight('Tags')", null, "keyword"));
assertEquals(1, response.getInt("total"));
}

@Test
public void accepts_unquoted_test() {
String query = "SELECT Tags, highlight(Tags) FROM %s WHERE match(Tags, 'yeast') LIMIT 1";
JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_BEER));
verifySchema(response, schema("Tags", null, "text"), schema("highlight(Tags)", null, "keyword"));
verifySchema(response, schema("Tags", null, "text"),
schema("highlight(Tags)", null, "keyword"));
assertEquals(1, response.getInt("total"));
}

@Test
public void multiple_highlight_test() {
String query = "SELECT highlight(Title), highlight(Body) FROM %s WHERE MULTI_MATCH([Title, Body], 'hops') LIMIT 1";
JSONObject response = executeJdbcRequest(String.format(query, TestsConstants.TEST_INDEX_BEER));
verifySchema(response, schema("highlight(Title)", null, "keyword"), schema("highlight(Body)", null, "keyword"));
verifySchema(response, schema("highlight(Title)", null, "keyword"),
schema("highlight(Body)", null, "keyword"));
assertEquals(1, response.getInt("total"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,4 @@ void highlight_iterator() {
assertTrue(expected.equals(result));
}
}
}
}
13 changes: 13 additions & 0 deletions sql/src/test/java/org/opensearch/sql/sql/antlr/HighlightTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ void wildcard_test() {

@Test
void highlight_all_test() {

acceptQuery("SELECT HIGHLIGHT('*') FROM Index WHERE MULTI_MATCH([Tags, Body], 'Time')");
}

@Test
void multiple_parameters_failure_test() {
rejectQuery("SELECT HIGHLIGHT(Tags1, Tags2) FROM Index "
+ "WHERE MULTI_MATCH([Tags, Body], 'Time')");
}

@Test
void no_parameters_failure_test() {
rejectQuery("SELECT HIGHLIGHT() FROM Index "
+ "WHERE MULTI_MATCH([Tags, Body], 'Time')");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,14 @@ public void describe_and_column_compatible_with_old_engine_syntax() {

@Test
public void can_build_alias_by_keywords() {
var expected = project(
relation("test"),
alias("avg_age", qualifiedName("avg_age"), "avg")
);
var comp = buildAST("SELECT avg_age AS avg FROM test");
assertEquals(
project(
relation("test"),
alias("avg_age", qualifiedName("avg_age"), "avg")
),
buildAST("SELECT avg_age AS avg FROM test")
expected,
comp
);
}

Expand Down Expand Up @@ -670,14 +672,23 @@ public void can_build_limit_clause_with_offset() {
}

@Test
public void can_build_highlight() {
public void can_build_qualified_name_highlight() {
assertEquals(
project(relation("test"),
alias("highlight(fieldA)", highlight(AstDSL.stringLiteral("fieldA")))),
alias("highlight(fieldA)", highlight(AstDSL.qualifiedName("fieldA")))),
buildAST("SELECT highlight(fieldA) FROM test")
);
}

@Test
public void can_build_string_literal_highlight() {
assertEquals(
project(relation("test"),
alias("highlight(\"fieldA\")", highlight(AstDSL.stringLiteral("fieldA")))),
buildAST("SELECT highlight(\"fieldA\") FROM test")
);
}

private UnresolvedPlan buildAST(String query) {
ParseTree parseTree = parser.parse(query);
return parseTree.accept(new AstBuilder(query));
Expand Down

0 comments on commit 3ee3491

Please sign in to comment.