Skip to content

Esql Support date nanos on date diff function #120645

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 14 commits into from
Jan 23, 2025

Conversation

not-napoleon
Copy link
Member

Resolves #109999

This adds support for date nanos in the date diff function, as well as mixed nanos/millis use cases.

@not-napoleon not-napoleon added >enhancement auto-backport Automatically create backport pull requests when merged :Analytics/ES|QL AKA ESQL v9.0.0 v8.18.0 labels Jan 22, 2025
Copy link
Contributor

Documentation preview:

@elasticsearchmachine
Copy link
Collaborator

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

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

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

Copy link
Contributor

@ivancea ivancea left a comment

Choose a reason for hiding this comment

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

LGTM! Commented some nits and some improvements on testing


FROM date_nanos
| EVAL n = MV_MAX(nanos)
| EVAL diff_sec = DATE_DIFF("seconds", TO_DATE_NANOS("2023-10-23T12:15:03.360103847Z"), n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we add two extra cases for the combinations of millis and nanos? Maybe in this same test, as extra columns

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've added one such.

)
),
new TestCaseSupplier(
"DateDiff(" + unit + "<TEXT>, " + startTimestamp + "<NANOS>, " + endTimestamp + "<NANOS>) == " + expected,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: We can merge the test andkeyword in a for over DataType.stringTypes(). Not related with this PR, but now that there are that many cases, would be a quick improvement

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, there's a lot of test refactoring we could do. I just don't have time to do it right now.

}

@Evaluator(extraName = "ConstantNanosMillis", warnExceptions = { IllegalArgumentException.class, InvalidArgumentException.class })
static int processNanosMillis(@Fixed Part datePartFieldUnit, long startTimestamp, long endTimestamp) throws IllegalArgumentException {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I would suffix the parameters like "startTimestampNanos", to clearly see which one is each. For future readability mostly, as the only way to know it here is the method name

Comment on lines 293 to 302
if (startTimestamp.dataType() == DATETIME && endTimestamp.dataType() == DATETIME) {
return toEvaluator(toEvaluator, DateDiffConstantMillisEvaluator.Factory::new, DateDiffMillisEvaluator.Factory::new);
} else if (startTimestamp.dataType() == DATE_NANOS && endTimestamp.dataType() == DATE_NANOS) {
return toEvaluator(toEvaluator, DateDiffConstantNanosEvaluator.Factory::new, DateDiffNanosEvaluator.Factory::new);
} else if (startTimestamp.dataType() == DATE_NANOS && endTimestamp.dataType() == DATETIME) {
return toEvaluator(toEvaluator, DateDiffConstantNanosMillisEvaluator.Factory::new, DateDiffNanosMillisEvaluator.Factory::new);
} else if (startTimestamp.dataType() == DATETIME && endTimestamp.dataType() == DATE_NANOS) {
return toEvaluator(toEvaluator, DateDiffConstantMillisNanosEvaluator.Factory::new, DateDiffMillisNanosEvaluator.Factory::new);
}
throw new UnsupportedOperationException("How'd we get here?");
Copy link
Contributor

Choose a reason for hiding this comment

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

This is technically impossible as long as the resolveType and the rules/ESQL is right, but I would show the startTimestamp and endTimestamp types in the error, just in case

TypeResolution resolution = isString(unit, sourceText(), FIRST).and(isDate(startTimestamp, sourceText(), SECOND))
.and(isDate(endTimestamp, sourceText(), THIRD));
String operationName = sourceText();
String operationName1 = sourceText();
Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose this isn't supposed to be here


FROM date_nanos
| EVAL n = MV_MAX(nanos)
| EVAL diff_sec = DATE_DIFF("seconds", TO_DATE_NANOS("2023-10-23T12:15:03.360103847Z"), n)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, maybe another test with the first param coming from an index, as we don't test it here. From an index, or from a CASE(). Whatever, as long as it's not a constant

@not-napoleon not-napoleon enabled auto-merge (squash) January 23, 2025 15:48
@not-napoleon not-napoleon merged commit 7e43605 into elastic:main Jan 23, 2025
16 checks passed
not-napoleon added a commit to not-napoleon/elasticsearch that referenced this pull request Jan 23, 2025
Resolves elastic#109999

This adds support for date nanos in the date diff function, as well as mixed nanos/millis use cases.

---------

Co-authored-by: elasticsearchmachine <[email protected]>
@elasticsearchmachine
Copy link
Collaborator

💚 Backport successful

Status Branch Result
8.x

elasticsearchmachine added a commit that referenced this pull request Jan 23, 2025
Resolves #109999

This adds support for date nanos in the date diff function, as well as mixed nanos/millis use cases.

---------

Co-authored-by: elasticsearchmachine <[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 Diff function on date nanos
3 participants