Skip to content

fix: parse INTERVAL column type with time-unit qualifier - #2456

Open
fudianchn wants to merge 1 commit into
JSQLParser:masterfrom
fudianchn:fix/interval-type-qualifier-1728
Open

fix: parse INTERVAL column type with time-unit qualifier#2456
fudianchn wants to merge 1 commit into
JSQLParser:masterfrom
fudianchn:fix/interval-type-qualifier-1728

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

Consume the optional time-unit qualifier (<unit> [TO <unit>]) after an INTERVAL column data type, so PostgreSQL/Oracle CREATE TABLE statements such as len interval hour to minute parse and round-trip correctly.

Why

Per the PostgreSQL docs (sql-createtable), interval hour to minute is a valid column type. JSQLParser currently rejects it:

net.sf.jsqlparser.JSQLParserException: Encountered unexpected token: "hour" <K_DATE_LITERAL>

Root cause

In ColDataType, INTERVAL is matched as the type name via the reserved-keyword-as-identifier fallback, but the following time-unit qualifier — tokenized as K_DATE_LITERAL (YEAR|MONTH|DAY|HOUR|MINUTE|SECOND), optionally followed by K_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-predicate LOOKAHEAD({ tk.kind == K_INTERVAL && getToken(1).kind == K_DATE_LITERAL }), so it only applies to an INTERVAL type immediately followed by a time unit — other types are unaffected, and the existing interval(...) 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

  • New 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.
  • Full ./gradlew test — no regressions introduced by this change. The 33 unrelated failures observed locally are pre-existing environmental issues (Mockito inline MockMaker does not initialise under the local JDK 17 / Windows toolchain, and ParserKeywordsUtilsTest resolves its temp file to C:\WINDOWS); both reproduce on unmodified master.

Fixes #1728

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] JSQLParser 4.5 : Postgres : fails to parse interval hour to minute

1 participant