fix: parse INTERVAL column type with time-unit qualifier - #2456
Open
fudianchn wants to merge 1 commit into
Open
Conversation
Consume the optional <unit> [TO <unit>] qualifier after an INTERVAL column type (e.g. `interval hour to minute`), attaching it to the ColDataType so PostgreSQL/Oracle CREATE TABLE statements parse and round-trip correctly. Fixes JSQLParser#1728 Signed-off-by: 付典 <fudianchn@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Consume the optional time-unit qualifier (
<unit> [TO <unit>]) after anINTERVALcolumn data type, so PostgreSQL/OracleCREATE TABLEstatements such aslen interval hour to minuteparse and round-trip correctly.Why
Per the PostgreSQL docs (sql-createtable),
interval hour to minuteis a valid column type. JSQLParser currently rejects it:Root cause
In
ColDataType,INTERVALis matched as the type name via the reserved-keyword-as-identifier fallback, but the following time-unit qualifier — tokenized asK_DATE_LITERAL(YEAR|MONTH|DAY|HOUR|MINUTE|SECOND), optionally followed byK_TO <K_DATE_LITERAL>— was not consumed, so the next token (hour) triggered a parse error.How
After the type token is matched in
ColDataType, optionally consume[<K_DATE_LITERAL> [<K_TO> <K_DATE_LITERAL>]]and append it to the data type string. The consumption is gated by a semantic-predicateLOOKAHEAD({ tk.kind == K_INTERVAL && getToken(1).kind == K_DATE_LITERAL }), so it only applies to anINTERVALtype immediately followed by a time unit — other types are unaffected, and the existinginterval(...)precision form (parenthesised-arguments branch) keeps working. The qualifier text is read from the matched tokens' images, so deparse preserves the original casing and round-trips.Limitation
The leading/fractional precision form
interval day(2) to second(6)is not covered — only the bare<unit> [TO <unit>]form reported in the issue. This keeps the change surgical; precision-with-qualifier can be addressed in a follow-up.Testing
ColDataTypeTest#testIntervalQualifierIssue1728(round-trip + AST assertion that the qualifier is attached to the column's data type) and#testIntervalQualifierYearToMonthIssue1728— both fail before the grammar change (parse error) and pass after../gradlew test --tests net.sf.jsqlparser.statement.create.table.ColDataTypeTest— green../gradlew test— no regressions introduced by this change. The 33 unrelated failures observed locally are pre-existing environmental issues (Mockito inlineMockMakerdoes not initialise under the local JDK 17 / Windows toolchain, andParserKeywordsUtilsTestresolves its temp file toC:\WINDOWS); both reproduce on unmodifiedmaster.Fixes #1728