-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Strict INTERVAL literal syntax #23157
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
Conversation
|
Thanks for the PR, I'm adding |
|
@hashhar FYI, this syntax is documented in |
|
The second case is anyway rejected at the analysis phase currently, so the only first example (specifying the same unit both for FROM and TO) could be an incompatibility. |
9466171 to
2ee8640
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it took me so long.
The grammar changes seem in agreement with spec. The only caveat being that the spec specifies the restriction in "syntax rules" with language using "SHALL". I don't know what this implies. In general do we implement the checks under "syntax rules" in analyzer or in grammar itself? @martint to answer this.
I've yet to look at tests, will try to do by end of day.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First thing I notice - the grammar in ANSI SQL matches what we had earlier. The restrictions on what combination of from and to are valid is mentioned in the "Syntax Rules" section.
| : INTERVAL sign=(PLUS | MINUS)? string from=YEAR (TO to=MONTH)? | ||
| | INTERVAL sign=(PLUS | MINUS)? string from=MONTH | ||
| | INTERVAL sign=(PLUS | MINUS)? string from=DAY (TO to=(HOUR | MINUTE | SECOND))? | ||
| | INTERVAL sign=(PLUS | MINUS)? string from=HOUR (TO to=(MINUTE | SECOND))? | ||
| | INTERVAL sign=(PLUS | MINUS)? string from=MINUTE (TO to=SECOND)? | ||
| | INTERVAL sign=(PLUS | MINUS)? string from=SECOND |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The rules basically say:
- If
TOis specified thenfromSHALL NOT beMONTH - If
TOis specified thenfromSHALL be more significant thanto. - If
from=YEARthento=MONTHSHALL be the only valid option.
From the above the only valid combinations are:
from=YEAR
from=YEAR TO to=MONTH
from=MONTH
from=DAY
from=DAY TO to=(HOUR | MINUTE | SECOND)
from=HOUR
from=HOUR TO to=(MINUTE | SECOND)
from=MINUTE
from=MINUTE TO to=SECOND
from=SECOND
The changes seem to match this.
And significance is defined in the order YEAR > MONTH > DAY > HOUR > MINUTE > SECOND.
| | INTERVAL from=YEAR (TO to=MONTH)? #yearMonthIntervalDataType | ||
| | INTERVAL from=MONTH #yearMonthIntervalDataType | ||
| | INTERVAL from=DAY (TO to=(HOUR | MINUTE | SECOND))? #dayTimeIntervalDataType | ||
| | INTERVAL from=HOUR (TO to=(MINUTE | SECOND))? #dayTimeIntervalDataType | ||
| | INTERVAL from=MINUTE (TO to=MINUTE)? #dayTimeIntervalDataType | ||
| | INTERVAL from=SECOND #dayTimeIntervalDataType |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why is the sign part missing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also since you've changed the type names here I guess AstBuilder also needs to be updated.
| assertThatThrownBy(assertions.expression("INTERVAL '124' MONTH TO MONTH"):: evaluate) | ||
| .hasMessageContaining("line 1:33: mismatched input 'TO'."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how this is better than the older behaviour?
It depends on each case. Some things are not possible to constrain at the grammar level. For others, it's easier to produce more user-friendly error messages by enforcing the constraints during analysis. This one is in a grey area. I would argue that at the grammar level we just want to support the general shape (to / from field or just a single field) and leave the validation of the specific combinations to the analysis phase. We have to enforce the validity during analysis anyway, since the AST allows for any combination of to/from fields. The format of the interval string also depends on the combination of from/to fields, and that has to be validated during analysis, too. |
|
So it seems #23002 is the better direction to go for then. |
|
As far as I have checked, some major SQL engines seem to check this at the syntax level. However, choice in Trino is ours. I don't have objection as long as invalid literals are rejected with user-friendly error message. |
|
I will close this since we seem to prefer to treat this as a semantic error. Thank you for all comments here. I will continue working on #23002. |
Description
The current INTERVAL literal syntax allows below but it should be rejected:
Semantically invalid literals like below becomes an error at the analysis phase but the error message is not clear (See #23002). Anyway, it's better to reject at the syntax level.
Additional context and related issues
Release notes
( ) This is not user-visible or is docs only, and no release notes are required.
( ) Release notes are required. Please propose a release note for me.
( ) Release notes are required, with the following suggested text: