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

Esql - Support date nanos in date extract function #120727

Merged
merged 10 commits into from
Jan 27, 2025

Conversation

not-napoleon
Copy link
Member

Resolves #110000

Add support for running the date extract function on nanosecond dates.

Copy link
Contributor

Documentation preview:

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jan 23, 2025
@elasticsearchmachine
Copy link
Collaborator

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

@elasticsearchmachine
Copy link
Collaborator

Hi @not-napoleon, I've created a changelog YAML for you.

@not-napoleon not-napoleon added the auto-backport Automatically create backport pull requests when merged label Jan 23, 2025
Copy link
Member

@fang-xing-esql fang-xing-esql left a comment

Choose a reason for hiding this comment

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

Just a minor comment, the rest looks good to me!

Comment on lines 119 to 123
ChronoField chrono = chronoField(toEvaluator.foldCtx());
if (chrono == null) {
BytesRef field = (BytesRef) children().get(0).fold(toEvaluator.foldCtx());
throw new InvalidArgumentException("invalid date field for [{}]: {}", sourceText(), field.utf8ToString());
}
Copy link
Member

Choose a reason for hiding this comment

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

A minor comment: these few lines seem to be duplicated with those under date_nanos field a few lines below, they can be refactored a bit further.

Copy link
Contributor

Choose a reason for hiding this comment

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

I made a little refactor, and added a comment there. I feel like it's a bit simpler now. I have my doubts, but let's see what you think 👀

return isStringAndExact(children().get(0), sourceText(), TypeResolutions.ParamOrdinal.FIRST).and(
isDate(children().get(1), sourceText(), TypeResolutions.ParamOrdinal.SECOND)
TypeResolutions.isType(children().get(1), DataType::isDate, operationName, TypeResolutions.ParamOrdinal.SECOND, "datetime")
Copy link
Contributor

Choose a reason for hiding this comment

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

In DateFormat we use "datetime or date_nanos" as the message

List.of(DataType.TEXT, DataType.DATE_NANOS),
() -> new TestCaseSupplier.TestCase(
List.of(
new TestCaseSupplier.TypedData(new BytesRef("YeAr"), DataType.TEXT, "chrono"),
Copy link
Contributor

Choose a reason for hiding this comment

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

I would refactor those text/keyword tests with a for+DataType.stringTypes(), and add a case asking for nanos as the chrono field

Copy link
Contributor

@bpintea bpintea left a comment

Choose a reason for hiding this comment

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

Minor optional notes, LG otherwise.

* @param zone - Timezone for the given date
* @return - long representing the given ChronoField value
*/
public static long chronoToLongNanos(long dateNanos, BytesRef chronoField, ZoneId zone) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Optional: this method seems very specific, the BytesRef conversion could also be done in the processor. But even if kept here, once having a ChronoField, it would be good to reuse the chronoToLongNanos(long, ChronoField, ZoneId) below.

return chronoToLong(value, chronoField, zone);
}

@Evaluator(extraName = "Nanos", warnExceptions = { IllegalArgumentException.class })
static long processNanos(long value, BytesRef chronoField, @Fixed ZoneId zone) {
return chronoToLongNanos(value, chronoField, zone);
Copy link
Contributor

@bpintea bpintea Jan 27, 2025

Choose a reason for hiding this comment

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

As mentioned before, an alternative to having the util that takes a BytesRef would be to convert that here (which would also make it clearer where the IllegalArgumentException could stem from) and reuse the processNanos(long, @Fixed ChronoField, @Fixed ZoneId) below.

Copy link
Contributor

Choose a reason for hiding this comment

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

(Answering both comments)
Just pushed the change, reusing one chronoToLongNanos from the other. I didn't remove the second helper though, as both are really oneliners abstracting some logic (One the Instant.ofEpochMilli and the other the ChronoField.valueOf). So I feel like if we have one, the other makes sense too.
Also, many converters take BytesRef or Strings in that class, so also to follow the general ""standard"" there

@ivancea ivancea self-assigned this Jan 27, 2025
Copy link
Member

@fang-xing-esql fang-xing-esql left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@ivancea ivancea enabled auto-merge (squash) January 27, 2025 13:54
@ivancea ivancea merged commit 5b3436d into elastic:main Jan 27, 2025
15 of 16 checks passed
not-napoleon added a commit to not-napoleon/elasticsearch that referenced this pull request Jan 27, 2025
Resolves elastic#110000

Add support for running the date extract function on nanosecond dates.
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

elasticsearchmachine pushed a commit that referenced this pull request Jan 28, 2025
…120908)

* Esql - Support date nanos in date extract function (#120727)

Resolves #110000

Add support for running the date extract function on nanosecond dates.

* Fix switch error

* ESQL: Fix DateExtract with nanos tests

---------

Co-authored-by: Iván Cea Fontenla <[email protected]>
Co-authored-by: Iván Cea Fontenla <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL auto-backport Automatically create backport pull requests when merged >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.18.0 v9.0.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ESQL] Support Date Extract function on date nanos
5 participants