Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ public void testIfWithLike() throws IOException {
actual, rows(0.0, "John"), rows(0.0, "Jane"), rows(0.0, "Jake"), rows(1.0, "Hello"));
}

@Test
public void testIfWithEquals() throws IOException {
JSONObject actual =
executeQuery(
String.format(
"source=%s | eval jake = if(name='Jake', 1, 0) | fields name, jake",
TEST_INDEX_STATE_COUNTRY));

verifySchema(actual, schema("name", "string"), schema("jake", "int"));

verifyDataRows(actual, rows("Jake", 1), rows("Hello", 0), rows("John", 0), rows("Jane", 0));
}

@Test
public void testIsPresent() throws IOException {
JSONObject actual =
Expand Down
11 changes: 9 additions & 2 deletions ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ tableSource
;

tableFunction
: qualifiedName LT_PRTHS functionArgs RT_PRTHS
: qualifiedName LT_PRTHS namedFunctionArgs RT_PRTHS
;

// fields
Expand Down Expand Up @@ -593,10 +593,17 @@ functionArgs
: (functionArg (COMMA functionArg)*)?
;

namedFunctionArgs
: (namedFunctionArg (COMMA namedFunctionArg)*)?
;

functionArg
: (ident EQUAL)? functionArgExpression
: functionArgExpression
;

namedFunctionArg
: (ident EQUAL)? functionArgExpression
;
Comment on lines +604 to +606
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If users name the argument in the meanwhile, it will be if(isNameJake=name='Jake' ...) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the change, only table functions support named arguments. IF is not one of them. if(isNameJake=name='Jake' ...) will not be a valid expression. For table functions, based on the following rules:

tableFunction
   : qualifiedName LT_PRTHS namedFunctionArgs RT_PRTHS
   ;

namedFunctionArg
   : (ident EQUAL)? functionArgExpression
   ;

functionArgExpression
   : lambda
   | logicalExpression
   ;

logicalExpression
   : NOT logicalExpression                                      # logicalNot
   | left = logicalExpression AND right = logicalExpression     # logicalAnd
   | left = logicalExpression XOR right = logicalExpression     # logicalXor
   | left = logicalExpression OR right = logicalExpression      # logicalOr
   | expression                                                 # logicalExpr
   ;

I think you are right.

BTW, I haven't found any official definitions for table functions in OpenSearch, although they present in the grammar file. Do you know some examples of them?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Could you create follow up issue if so? To support both, we may need breaking change right?
I recall table function was related to PromQL support. Ref: https://github.com/opensearch-project/sql/blob/main/docs/user/ppl/admin/connectors/prometheus_connector.rst#query_range-table-function

Copy link
Collaborator Author

@yuancu yuancu Aug 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the source! Do you mean to support named arguments for all functions by to support both? I am a little confused in why should we do so. Afaik, named arguments isn't mentioned anywhere in PPL specifications. And we had no plan to support out-of-order arguments even for functions with named arguments. Moreover, it's hard to tell whether if(a=bool_column, ...) is a named argument or a logical expression if we allow named arguments for all functions.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for confusion. I asked because I saw if(predicate:code=200 in SPL doc: https://help.splunk.com/en/splunk-cloud-platform/search/spl2-search-manual/functions/naming-function-arguments. Never mind if this is not planned.

Copy link
Collaborator Author

@yuancu yuancu Aug 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation. That's a specific grammar. They use : to avoid the dilemma of telling apart logical comparison and assignment,


functionArgExpression
: lambda
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ public UnresolvedPlan visitTableSourceClause(TableSourceClauseContext ctx) {
@Override
public UnresolvedPlan visitTableFunction(TableFunctionContext ctx) {
ImmutableList.Builder<UnresolvedExpression> builder = ImmutableList.builder();
ctx.functionArgs()
.functionArg()
ctx.namedFunctionArgs()
.namedFunctionArg()
.forEach(
arg -> {
String argName = (arg.ident() != null) ? arg.ident().getText() : null;
Expand Down
Loading