Skip to content
Closed
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
3 changes: 2 additions & 1 deletion x-pack/plugin/esql/src/main/antlr/parser/Expression.g4
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ primaryExpression
;

functionExpression
: functionName LP (ASTERISK | (booleanExpression (COMMA booleanExpression)* (COMMA mapExpression)?))? RP
: functionName LP (ASTERISK | (booleanExpression (COMMA booleanExpression)* (COMMA mapExpression)?))? RP #functionStandard
| (FIRST | LAST) LP value=booleanExpression BY by=booleanExpression RP #functionFirstLast
Copy link
Contributor

@idegtiarenko idegtiarenko Aug 6, 2025

Choose a reason for hiding this comment

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

Why value and by are boolean expressions? I would expect something about column identifier.

Also I am a bit worried about making a custom syntax for each function.
I understand the workaround for FIRST and LAST, however I am not sure if we should also have BY in it opposed to a simpler coma separated list of parameters.
I am imagining something like:

functionExpression
    : name=(functionName | FIRST | LAST) LP (ASTERISK | (booleanExpression (COMMA booleanExpression)* (COMMA mapExpression)?))? RP

I feel like #132469 could be a safer option

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should work that we specifically allow (FIRST | LAST) in place of the function name.

I think we shouldn't introduce the BY part as it would change how our functions work; we normally don't have special keywords inside function arguments - normally that is reserved for operators.

I'd start with FIRST(v, @timestamp) to implement the function, and would put syntactic sugar (FIRST(v BY @timestamp)) into a separate PR, as we'd need to see how this fits into the language as a whole.

I think a straight-forward way would be to adjust functionName below and keep everything else the same:

functionName
    : identifierOrParameter
    | keywordsAllowedAsFunctionNames
    ;

keywordsAllowedAsFunctionNames:
    FIRST
    | LAST
    ;

Copy link
Contributor

Choose a reason for hiding this comment

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

Once FIRST and LAST get enabled (in SNAPSHOT), we also best test that they can be properly used as a query parameter (although keywords do not clash with parameters):

curl -u elastic:password -H "Content-Type: application/json" "127.0.0.1:9200/_query?format=txt" -d '
{
  "query": "row x = 1 | stats y = ?foo(x, @timestamp)", "params": [{"foo": {"identifier": "first"}}]
}'

;

functionName
Expand Down

Large diffs are not rendered by default.

Loading