Skip to content
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

[8.x] Esql compare nanos and millis (#118027) #118159

Merged
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
6 changes: 6 additions & 0 deletions docs/changelog/118027.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 118027
summary: Esql compare nanos and millis
area: ES|QL
type: enhancement
issues:
- 116281
36 changes: 36 additions & 0 deletions docs/reference/esql/functions/kibana/definition/equals.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions docs/reference/esql/functions/kibana/definition/greater_than.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions docs/reference/esql/functions/kibana/definition/less_than.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions docs/reference/esql/functions/kibana/definition/not_equals.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/reference/esql/functions/types/equals.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/reference/esql/functions/types/greater_than.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/reference/esql/functions/types/less_than.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/reference/esql/functions/types/not_equals.asciidoc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions server/src/main/java/org/elasticsearch/common/time/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,37 @@ public static long toMilliSeconds(long nanoSecondsSinceEpoch) {
return nanoSecondsSinceEpoch / 1_000_000;
}

/**
* Compare an epoch nanosecond date (such as returned by {@link DateUtils#toLong}
* to an epoch millisecond date (such as returned by {@link Instant#toEpochMilli()}}.
* <p>
* NB: This function does not implement {@link java.util.Comparator} in
* order to avoid performance costs of autoboxing the input longs.
*
* @param nanos Epoch date represented as a long number of nanoseconds.
* Note that Elasticsearch does not support nanosecond dates
* before Epoch, so this number should never be negative.
* @param millis Epoch date represented as a long number of milliseconds.
* This parameter does not have to be constrained to the
* range of long nanosecond dates.
* @return -1 if the nanosecond date is before the millisecond date,
* 0 if the two dates represent the same instant,
* 1 if the nanosecond date is after the millisecond date
*/
public static int compareNanosToMillis(long nanos, long millis) {
assert nanos >= 0;
if (millis < 0) {
return 1;
}
if (millis > MAX_NANOSECOND_IN_MILLIS) {
return -1;
}
// This can't overflow, because we know millis is between 0 and MAX_NANOSECOND_IN_MILLIS,
// and MAX_NANOSECOND_IN_MILLIS * 1_000_000 doesn't overflow.
long diff = nanos - (millis * 1_000_000);
return diff == 0 ? 0 : diff < 0 ? -1 : 1;
}

/**
* Rounds the given utc milliseconds sicne the epoch down to the next unit millis
*
Expand Down
Loading
Loading