Skip to content

Conversation

treysp
Copy link
Collaborator

@treysp treysp commented Sep 19, 2025

This PR adds support for handling postgres INTERVAL literal spans that omit the span units, both parsing and inferring the correct units for transpilation.

Examples

  • INTERVAL '1 00:01'
  • INTERVAL '1 00:01:01'

Context: INTERVALs

interval literals generally consist of the word INTERVAL , a string literal like '6' and a unit string like DAYS

  • Example: INTERVAL '6' DAYS

in many dialects you may specify compound units, which we parse into exp.IntervalSpan based on the presence of "TO"

  • Example: INTERVAL '163 12:39' DAY TO MINUTE
  • Example: INTERVAL '163 12:39:59.1' DAY TO SECOND

Context: postgres

Postgres lets you omit the unit strings DAY TO MINUTE and DAY TO SECOND if the second element in the literal is a hh:[mm:[ss[.ff]]]

  • Valid examples confirmed to work in postgres 14:
    • INTERVAL '1 01:01:01.01'
    • INTERVAL '1 01:01:01'
    • INTERVAL '1 01:01'
    • INTERVAL '1.5 01:'
    • INTERVAL '-0.25 01:'
  • Invalid example
    • No colon present: INTERVAL '1 01'

This issue

If the span unit is omitted, we incorrectly ingest the token following the literal because we assume it's a unit like DAY

  • Example: a > INTERVAL '1 00:00' AND TRUE
    • We consume AND so don't recognize the compound condition

@treysp treysp requested a review from georgesittas September 19, 2025 21:58
@treysp treysp force-pushed the trey/pg-interval-span branch from 04e2eaa to 38daf9f Compare September 22, 2025 23:27
@treysp treysp force-pushed the trey/pg-interval-span branch from 38daf9f to 265a523 Compare September 23, 2025 15:03
@georgesittas georgesittas merged commit 0dc0015 into main Sep 23, 2025
6 checks passed
@georgesittas georgesittas deleted the trey/pg-interval-span branch September 23, 2025 15:27
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.

2 participants