Skip to content

Commit

Permalink
Esql compare nanos and millis (elastic#118027)
Browse files Browse the repository at this point in the history
Resolves elastic#116281

Introduces support for comparing millisecond dates with nanosecond dates, without the need for casting. Millisecond dates outside of the nanosecond date range are handled correctly.
  • Loading branch information
not-napoleon committed Dec 6, 2024
1 parent d08c26b commit fdb36aa
Show file tree
Hide file tree
Showing 45 changed files with 2,560 additions and 24 deletions.
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

0 comments on commit fdb36aa

Please sign in to comment.