Skip to content

ESQL: FIRST/LAST #132603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Aug 12, 2025
Merged

ESQL: FIRST/LAST #132603

merged 41 commits into from
Aug 12, 2025

Conversation

nik9000
Copy link
Member

@nik9000 nik9000 commented Aug 8, 2025

Prototype implementation of FIRST and LAST. FIRST returns the value with the earliest timestamp. LAST returns the values with the latest timestamp.

Looks like:

FROM k8s
| STATS last_bytes_in = LAST(network.bytes_in, @timestamp) BY pod
| SORT pod ASC

SNAPSHOT only for now while we resolve the remaining open questions in #108385.

Important: FIRST/LAST in this PR only return a single value. In the example above, if the value of network.bytes_in with the latest @timestamp is null then we get the value with the next highest timestamp. If the value of network.bytes_in with the latest @timestamp is multivalued we'll get a random value from the top values. Some folks want that behavior, but surely not everyone. We'll figure out what it should do soon. But we can get this in under snapshot and folks can play with it.

In this prototype, if two documents tie in @timestamp then you'll get a value from one of them. Which one is undefined.

@elasticsearchmachine elasticsearchmachine added needs:triage Requires assignment of a team area label v9.2.0 labels Aug 8, 2025
Fixes a bug in the ungrouped FIRST/LAST implementation when it received
timestamps always less than 0 (greater than 0 for LAST). We were always
returning `0` as the value....
Copy link
Contributor

github-actions bot commented Aug 11, 2025

@nik9000 nik9000 changed the title draft 2 ESQL: FIRST/LAST Aug 11, 2025
@nik9000 nik9000 added the :Analytics/ES|QL AKA ESQL label Aug 11, 2025
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Aug 11, 2025
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine elasticsearchmachine removed the needs:triage Requires assignment of a team area label label Aug 11, 2025
@nik9000
Copy link
Member Author

nik9000 commented Aug 11, 2025

>non-issue because it's not being released yet.

Copy link
Member

@dnhatn dnhatn left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks Nik!

Copy link
Contributor

@limotova limotova left a comment

Choose a reason for hiding this comment

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

LGTM! Just 1 small thing

null | two
;

double_by_null
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 this file is missing the null_by_timestamp and has two of the double_by_null tests?

Copy link
Member Author

Choose a reason for hiding this comment

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

👍

nik9000 added a commit to nik9000/elasticsearch that referenced this pull request Aug 12, 2025
Adds `toString` checking for aggregators to the generic aggs test cases
so we can make sure they spit out sensible looking results. We have this
for scalar functions but it isn't plugged in for aggs and I noticed it
while working on elastic#132603 where I stuck `asdf` for the toString thinking
I'd fix it when the test failed. It didn't.

There's to many changes to grab this in one go so I've made a hook that
tests can opt into. We'll drop the hook once everything has opted into
it.
public First(
Source source,
@Param(name = "value", type = { "long", "integer", "double" }, description = "Values to return") Expression field,
@Param(name = "sort", type = { "date", "date_nanos" }, description = "Sort key") Expression sort
Copy link
Contributor

Choose a reason for hiding this comment

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

NIT: should this be order instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking "sort key" or "sort by".

)
public First(
Source source,
@Param(name = "value", type = { "long", "integer", "double" }, description = "Values to return") Expression field,
Copy link
Contributor

Choose a reason for hiding this comment

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

supplier below also declares float. Should it be included here and in returnType above?

Copy link
Member Author

Choose a reason for hiding this comment

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

float can't be even if the function supports it. Because we don't want the docs to say float until we actually have float in the system everywhere as a real type. But that means incrementally adding float to everything. I haven't been doing that normally but the LastOverTime stuff had it so I carried it along.

)
public Last(
Source source,
@Param(name = "value", type = { "long", "integer", "double" }, description = "Values to return") Expression field,
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, is it intentionally skipping float?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah.

@nik9000
Copy link
Member Author

nik9000 commented Aug 12, 2025

Thanks folks!

@idegtiarenko float is in a bit of a state at the moment. I'd hoped we could incrementally add support for it so we could experiment without breaking, but there's a lot of funky little things to handle. In this case I've just cargo-cult-ed float into place here and it shouldn't hurt anything, but it won't really help either.

@nik9000 nik9000 merged commit 3afc254 into elastic:main Aug 12, 2025
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL >non-issue Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants