Skip to content

Commit 215ea70

Browse files
committed
Allow equal expression as a function argument (opensearch-project#4001)
* Remove named function arg from functions other than table functions Signed-off-by: Yuanchun Shen <yuanchu@amazon.com> * Test eval if function with equal as condition Signed-off-by: Yuanchun Shen <yuanchu@amazon.com> --------- Signed-off-by: Yuanchun Shen <yuanchu@amazon.com> (cherry picked from commit 9f2b19a)
1 parent ab211df commit 215ea70

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

integ-test/src/test/java/org/opensearch/sql/calcite/remote/CalcitePPLConditionBuiltinFunctionIT.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,19 @@ public void testIfWithLike() throws IOException {
194194
actual, rows(0.0, "John"), rows(0.0, "Jane"), rows(0.0, "Jake"), rows(1.0, "Hello"));
195195
}
196196

197+
@Test
198+
public void testIfWithEquals() throws IOException {
199+
JSONObject actual =
200+
executeQuery(
201+
String.format(
202+
"source=%s | eval jake = if(name='Jake', 1, 0) | fields name, jake",
203+
TEST_INDEX_STATE_COUNTRY));
204+
205+
verifySchema(actual, schema("name", "string"), schema("jake", "int"));
206+
207+
verifyDataRows(actual, rows("Jake", 1), rows("Hello", 0), rows("John", 0), rows("Jane", 0));
208+
}
209+
197210
@Test
198211
public void testIsPresent() throws IOException {
199212
JSONObject actual =

ppl/src/main/antlr/OpenSearchPPLParser.g4

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ tableSource
522522
;
523523

524524
tableFunction
525-
: qualifiedName LT_PRTHS functionArgs RT_PRTHS
525+
: qualifiedName LT_PRTHS namedFunctionArgs RT_PRTHS
526526
;
527527

528528
// fields
@@ -597,10 +597,17 @@ functionArgs
597597
: (functionArg (COMMA functionArg)*)?
598598
;
599599

600+
namedFunctionArgs
601+
: (namedFunctionArg (COMMA namedFunctionArg)*)?
602+
;
603+
600604
functionArg
601-
: (ident EQUAL)? functionArgExpression
605+
: functionArgExpression
602606
;
603607

608+
namedFunctionArg
609+
: (ident EQUAL)? functionArgExpression
610+
;
604611

605612
functionArgExpression
606613
: lambda

ppl/src/main/java/org/opensearch/sql/ppl/parser/AstBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,8 @@ public UnresolvedPlan visitTableSourceClause(TableSourceClauseContext ctx) {
550550
@Override
551551
public UnresolvedPlan visitTableFunction(TableFunctionContext ctx) {
552552
ImmutableList.Builder<UnresolvedExpression> builder = ImmutableList.builder();
553-
ctx.functionArgs()
554-
.functionArg()
553+
ctx.namedFunctionArgs()
554+
.namedFunctionArg()
555555
.forEach(
556556
arg -> {
557557
String argName = (arg.ident() != null) ? arg.ident().getText() : null;

0 commit comments

Comments
 (0)