Skip to content

[ES|QL] Pushdown string format for BinaryComparison on datetime fields#110669

Merged
fang-xing-esql merged 7 commits intoelastic:mainfrom
fang-xing-esql:equal-datetime
Jul 12, 2024
Merged

[ES|QL] Pushdown string format for BinaryComparison on datetime fields#110669
fang-xing-esql merged 7 commits intoelastic:mainfrom
fang-xing-esql:equal-datetime

Conversation

@fang-xing-esql
Copy link
Copy Markdown
Member

@fang-xing-esql fang-xing-esql commented Jul 9, 2024

Resolves #107900

Equal and IN list predicates on datetime fields may return wrong results. This PR fixes the wrong results by using String representation of the datetime literals/constants in BinaryComparison on datetime fields.

@fang-xing-esql fang-xing-esql added >test Issues or PRs that are addressing/adding tests :Analytics/ES|QL AKA ESQL v8.16.0 labels Jul 9, 2024
@fang-xing-esql fang-xing-esql changed the title [ES|QL] add tests for equal and IN on datetime fields [ES|QL] Use RangeQuery and String in BinaryComparison on datetime fields Jul 10, 2024
@fang-xing-esql fang-xing-esql added >bug and removed >test Issues or PRs that are addressing/adding tests labels Jul 10, 2024
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @fang-xing-esql, I've created a changelog YAML for you.

@fang-xing-esql fang-xing-esql marked this pull request as ready for review July 10, 2024 20:40
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

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

@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Jul 10, 2024
@astefan astefan self-requested a review July 10, 2024 22:08
Copy link
Copy Markdown
Contributor

@alex-spies alex-spies left a comment

Choose a reason for hiding this comment

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

LGTM, since this fixes some csv tests when run in ITs.

No idea if we should use datetime strings or epoch millis in general, though.

Note: I think it's super unfortunate that we need to change both ExpressionTranslators in core and EsqlExpressionTranslators. But we plan to consolidate these (and their tests) in the future.

ImplicitCastingEqual
required_capability: rangequery_for_datetime
from employees
| where birth_date == "1957-05-23T00:00:00Z"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For good measure, can we add a test with a different time zone than Z, too?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We don't currently have any support for non-UTC time zones, so such a test would not be expected to pass.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, we don't support non-UTC time zone yet.

Copy link
Copy Markdown
Member

@nik9000 nik9000 left a comment

Choose a reason for hiding this comment

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

I think this is the right thing to do, but it's kind of a symptom of a problem, one we might should try to fix rather than pushing this mechanism a ton further. If we're on the local node we have the local node we can get the MappedFieldType and talk to it directly. Or we can just make Lucene Query subclasses. Right now we can't do that and we have to go through a few layers - one of which prefers iso8601 for date comparisons. Which is how we got here. I think. But if we didn't loop through all of these layers it'd be more obvious what's going on.

@bpintea bpintea self-requested a review July 11, 2024 14:02
Copy link
Copy Markdown
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.

The RangeQuery accepts a format parameter, that, if specified as "epoch_millis", will match as expected also when provided the millis that we currently provide it.

However, the term query does not. So providing there a string is the right thing (would be great to have it take a format, but not sure why this isn't the case, maybe the matching is done differently).

But I think we shouldn't turn an equality into a RangeQuery, we should rather just provide term with the string representation of the date.
Also, if we can keep the number format and provide a "format" to the inequalities, that'd be great; if not, string also here.

@fang-xing-esql fang-xing-esql changed the title [ES|QL] Use RangeQuery and String in BinaryComparison on datetime fields [ES|QL] Pushdown string format for BinaryComparison on datetime fields Jul 11, 2024
@fang-xing-esql
Copy link
Copy Markdown
Member Author

#109413 moved InComparison from esql-core into EsqlExpressionTranslators, after that the changes in esql-core's ExpressionTranslators are not needed any more, it will be consolidated into EsqlExpressionTranslators.

@fang-xing-esql
Copy link
Copy Markdown
Member Author

The RangeQuery accepts a format parameter, that, if specified as "epoch_millis", will match as expected also when provided the millis that we currently provide it.

However, the term query does not. So providing there a string is the right thing (would be great to have it take a format, but not sure why this isn't the case, maybe the matching is done differently).

But I think we shouldn't turn an equality into a RangeQuery, we should rather just provide term with the string representation of the date. Also, if we can keep the number format and provide a "format" to the inequalities, that'd be great; if not, string also here.

Thanks for reviewing @bpintea ! We'll change datetime literals to string only, and keep the term query.

Copy link
Copy Markdown
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.

Thanks, Fang, good find!

@fang-xing-esql fang-xing-esql merged commit 9f20d47 into elastic:main Jul 12, 2024
@fang-xing-esql fang-xing-esql deleted the equal-datetime branch July 12, 2024 13:18
tvernum pushed a commit that referenced this pull request Feb 25, 2025
#110669)

Use string format in datetime field BinaryComparison.
tvernum pushed a commit that referenced this pull request Feb 25, 2025
#110669)

Use string format in datetime field BinaryComparison.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/ES|QL AKA ESQL >bug Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.16.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ES|QL] Equal predicate on datetime field may return inconsistent results

6 participants