|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.sql.ppl.parser; |
| 7 | + |
| 8 | +import static org.opensearch.sql.ast.dsl.AstDSL.*; |
| 9 | + |
| 10 | +import com.google.common.collect.ImmutableMap; |
| 11 | +import org.junit.Test; |
| 12 | +import org.opensearch.sql.ast.expression.RelevanceFieldList; |
| 13 | + |
| 14 | +public class AstWhereClauseTest extends AstBuilderTest { |
| 15 | + @Test |
| 16 | + public void testBinaryOperationExprWithParentheses() { |
| 17 | + assertEqual( |
| 18 | + "source = t | where a = (1 + 2) * 3", |
| 19 | + filter( |
| 20 | + relation("t"), |
| 21 | + compare( |
| 22 | + "=", |
| 23 | + field("a"), |
| 24 | + function("*", function("+", intLiteral(1), intLiteral(2)), intLiteral(3))))); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testBinaryOperationExprPrecedence() { |
| 29 | + assertEqual( |
| 30 | + "source = t | where a = 1 + 2 * 3", |
| 31 | + filter( |
| 32 | + relation("t"), |
| 33 | + compare( |
| 34 | + "=", |
| 35 | + field("a"), |
| 36 | + function("+", intLiteral(1), function("*", intLiteral(2), intLiteral(3)))))); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void canBuildMatchRelevanceFunctionWithArguments() { |
| 41 | + assertEqual( |
| 42 | + "source=test | where match('message', 'test query', analyzer='keyword')", |
| 43 | + filter( |
| 44 | + relation("test"), |
| 45 | + function( |
| 46 | + "match", |
| 47 | + unresolvedArg("field", qualifiedName("message")), |
| 48 | + unresolvedArg("query", stringLiteral("test query")), |
| 49 | + unresolvedArg("analyzer", stringLiteral("keyword"))))); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void canBuildMulti_matchRelevanceFunctionWithArguments() { |
| 54 | + assertEqual( |
| 55 | + "source=test | where multi_match(['field1', 'field2' ^ 3.2]," |
| 56 | + + "'test query', analyzer='keyword')", |
| 57 | + filter( |
| 58 | + relation("test"), |
| 59 | + function( |
| 60 | + "multi_match", |
| 61 | + unresolvedArg( |
| 62 | + "fields", |
| 63 | + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), |
| 64 | + unresolvedArg("query", stringLiteral("test query")), |
| 65 | + unresolvedArg("analyzer", stringLiteral("keyword"))))); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void canBuildSimple_query_stringRelevanceFunctionWithArguments() { |
| 70 | + assertEqual( |
| 71 | + "source=test | where simple_query_string(['field1', 'field2' ^ 3.2]," |
| 72 | + + "'test query', analyzer='keyword')", |
| 73 | + filter( |
| 74 | + relation("test"), |
| 75 | + function( |
| 76 | + "simple_query_string", |
| 77 | + unresolvedArg( |
| 78 | + "fields", |
| 79 | + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), |
| 80 | + unresolvedArg("query", stringLiteral("test query")), |
| 81 | + unresolvedArg("analyzer", stringLiteral("keyword"))))); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void canBuildQuery_stringRelevanceFunctionWithArguments() { |
| 86 | + assertEqual( |
| 87 | + "source=test | where query_string(['field1', 'field2' ^ 3.2]," |
| 88 | + + "'test query', analyzer='keyword')", |
| 89 | + filter( |
| 90 | + relation("test"), |
| 91 | + function( |
| 92 | + "query_string", |
| 93 | + unresolvedArg( |
| 94 | + "fields", |
| 95 | + new RelevanceFieldList(ImmutableMap.of("field1", 1.F, "field2", 3.2F))), |
| 96 | + unresolvedArg("query", stringLiteral("test query")), |
| 97 | + unresolvedArg("analyzer", stringLiteral("keyword"))))); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void canBuildSingleVariableExpression() { |
| 102 | + assertEqual("source = test | where x", filter(relation("test"), field("x"))); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void canBuildPureBooleanExpression() { |
| 107 | + assertEqual("source = test | where TRUE", filter(relation("test"), booleanLiteral(true))); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void canBuildVariableBooleanExpression() { |
| 112 | + assertEqual( |
| 113 | + "source = test | where x OR y", filter(relation("test"), or(field("x"), field("y")))); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void canBuildPlainParenthesizedExpression() { |
| 118 | + assertEqual( |
| 119 | + "source = test | where (1 >= 0)", |
| 120 | + filter(relation("test"), compare(">=", intLiteral(1), intLiteral(0)))); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void canBuildMultiPartParenthesizedExpression() { |
| 125 | + assertEqual( |
| 126 | + "source = test | where (day_of_week_i < 2) OR (day_of_week_i > 5)", |
| 127 | + filter( |
| 128 | + relation("test"), |
| 129 | + or( |
| 130 | + compare("<", field("day_of_week_i"), intLiteral(2)), |
| 131 | + compare(">", field("day_of_week_i"), intLiteral(5))))); |
| 132 | + } |
| 133 | +} |
0 commit comments