-
Notifications
You must be signed in to change notification settings - Fork 25.1k
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: syntax error on date math if date period variable is at the end of the expression #99293
Comments
Pinging @elastic/es-ql (Team:QL) |
Pinging @elastic/elasticsearch-esql (:Query Languages/ES|QL) |
I had a super quick look because I was curious. The exception is triggered in The following fails for the same reason, and does not contain subtractions/negatives:
It does work just fine, though, if we set parentheses:
Resolving this issue may require us to allow adding/subtracting time periods and only disallowing time periods as the final output of an expression instead. |
I made a bit of progress on this one. The problem already occurs when using exclusively date periods, e.g. years:
The problem is that this is equivalent to
and the sum I think this issue can be solved in two steps:
|
@alex-spies an alternative approach might be to canonicalize ADD, having the DATETIME on the left and the temporal amount(s) on the right. This should work with the current folding approach of just chaining evaluators. |
Thanks @bpintea , that's a good point and would work as well. On the other hand, wouldn't we normally want to canonicalize in a way that allows for the most folding possible? Before the evaluator is even triggered, I think our constant folding optimization rule should do as much work as possible, and ideally we should only be left with just one date math ADD or SUB (or two, if we consider both date periods and time durations), right? This requires that we can fold periods/durations in the first place, so I think we'll have to do something about the |
Yes, indeed! We can have the proposed solution as an intermediary one until something more generic is in place. But was thinking that if it's intermediary, a quicker fix might be to just canonicalize. Not having strong feelings about it, though. |
Alright, PR #99432 enables part of this functionality: expressions like What's missing after that are mixed expressions, where both periods (days, months etc.) and durations (hours, minutes etc.) occur and are added to each other before being added to a datetime. This includes the initial expression that started this issue: I'll try to summarize the possible approaches. Approach 1: Add another unrepresentable data typeWe could declare another data type that is comprised of a period and a duration; Pros
Cons
Approach 2: Rely on correct parentheses/canonicalization of expressions@bpintea suggested that canonicalizing expressions would solve this issue. This relates to issue #99502, which suggests canonicalizing arithmetic expressions (though for other reasons). By exploiting (or imposing) associativity and commutativity of More generally, we could canonicalize any date math expression by grouping all period summands and all duration summands, so that e.g.
becomes
Pros
Cons
|
Since both possible approaches are either impactful or (potentially) complex, I think we need to postpone the full solution to this issue for a bit. |
- Enable date math expressions like (1year + 1month) + now() and (1hour + 1second) + now(). - To achieve this, enable folding of + and - for duration/period literals. - Provide user-friendly error messages for date time arithmetic overflows, and for illegal expressions like eval x = 1week + 1month whose type is not representable. Partially solves #99293.
Pinging @elastic/es-analytics-geo (Team:Analytics) |
Pinging @elastic/es-analytical-engine (Team:Analytics) |
Elasticsearch Version
8.11.0
Installed Plugins
No response
Java Version
bundled
OS Version
MacOS
Problem Description
Testing date math in ES|QL in Kibana after #98870 and noticed that the date math feature kicks in only when the date period variable is not at the end of the statement.
Steps to Reproduce
This works:
as in the PR works ✅
but the same expression with a different order errors out:
❌ Errors:
Unexpected error from Elasticsearch: verification_exception - Found 1 problem line 1:29: [-] has arguments with incompatible types [DATE_PERIOD] and [TIME_DURATION]
This one works instead ✅ :
Logs (if relevant)
No response
The text was updated successfully, but these errors were encountered: