Skip to content

Commit 9f2b19a

Browse files
authored
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>
1 parent d758163 commit 9f2b19a

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
@@ -518,7 +518,7 @@ tableSource
518518
;
519519

520520
tableFunction
521-
: qualifiedName LT_PRTHS functionArgs RT_PRTHS
521+
: qualifiedName LT_PRTHS namedFunctionArgs RT_PRTHS
522522
;
523523

524524
// fields
@@ -593,10 +593,17 @@ functionArgs
593593
: (functionArg (COMMA functionArg)*)?
594594
;
595595

596+
namedFunctionArgs
597+
: (namedFunctionArg (COMMA namedFunctionArg)*)?
598+
;
599+
596600
functionArg
597-
: (ident EQUAL)? functionArgExpression
601+
: functionArgExpression
598602
;
599603

604+
namedFunctionArg
605+
: (ident EQUAL)? functionArgExpression
606+
;
600607

601608
functionArgExpression
602609
: 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
@@ -551,8 +551,8 @@ public UnresolvedPlan visitTableSourceClause(TableSourceClauseContext ctx) {
551551
@Override
552552
public UnresolvedPlan visitTableFunction(TableFunctionContext ctx) {
553553
ImmutableList.Builder<UnresolvedExpression> builder = ImmutableList.builder();
554-
ctx.functionArgs()
555-
.functionArg()
554+
ctx.namedFunctionArgs()
555+
.namedFunctionArg()
556556
.forEach(
557557
arg -> {
558558
String argName = (arg.ident() != null) ? arg.ident().getText() : null;

0 commit comments

Comments
 (0)