Skip to content

fix: compile dates, timestamps and intervals for sql.oracle - #6455

Open
calin2k wants to merge 5 commits into
PRQL:mainfrom
calin2k:oracle-dates
Open

calin2k wants to merge 5 commits into
PRQL:mainfrom
calin2k:oracle-dates

Conversation

@calin2k

@calin2k calin2k commented Oct 18, 2026

Copy link
Copy Markdown

Problem

The date functions emit what Oracle lacks:

  • date.trunc emits DATE_TRUNC;
  • date.to_text is "not yet supported for this dialect";
  • date.diff emits DATEDIFF;
  • TIMESTAMP '2025-06-01T10:30:00' is ORA-01861 (Oracle wants a space);
  • intervals compile to INTERVAL 1 DAY (unquoted, a syntax error) and INTERVAL 2 WEEK (no such field).

Solution

  • date.trunc compiles to TRUNC(col, 'MM'). Units map to Oracle format models: year YYYY, quarter Q, month MM, week IW (ISO, like DATE_TRUNC('week')), day DD, hour HH24, minute MI. Units below a minute are a compile error, with a hint.
  • date.to_text compiles to TO_CHAR with Oracle format models. They're close to Postgres's, but Oracle's FM is a toggle for the rest of the format, so it is switched off after each element (%-d/%-m → FMDDFM/FMMMFM). A double quote can't be produced by an Oracle format model, so it's a compile error.
  • date.diff reports that the operator isn't supported, as on Postgres.
  • TIMESTAMP literals use a space between date and time.
  • Intervals are INTERVAL '3' DAY, with a leading precision above two digits (INTERVAL '100' DAY (3), ORA-01873 without it). Weeks become days; milli- and microseconds become fractional seconds.
  • Time literals are a compile error: Oracle has no TIME type.

Oracle's own interval arithmetic still differs from Postgres in one place: DATE '2024-01-31' + INTERVAL '1' MONTH is ORA-01839 rather than Feb 29. That's how Oracle defines it.

Testing

New inline tests: test_oracle_dates, test_oracle_intervals, test_oracle_date_to_text. On Oracle:

  • Corpus: 11 of the 12 formats of date_to_text match its snapshot on all 20 rows (the 12th contains a ").
  • Differential: date.trunc, date and timestamp literals, intervals of every unit, and date.to_text match DuckDB.

Verified on Oracle Database 19c Enterprise Edition 19.3 (Docker), beyond the inline snapshots:

  • every sql::Dialect::Oracle snapshot in tests/integration/sql.rs was executed on the database;
  • PRQL's integration corpus (tests/integration/queries) was compiled to sql.oracle and run against the Chinook data loaded into Oracle (quoted lower-case names), comparing rows with the results snapshots;
  • 40 extra queries on Chinook were run on DuckDB (sql.duckdb) and Oracle (sql.oracle), comparing rows.

cargo test -p prqlc --all-targets, cargo clippy -p prqlc --all-targets -- -D warnings and cargo fmt --check are green. The harness is outside this repo; happy to share it or contribute an Oracle runner to tests/integration/dbs if that's wanted (the Oracle images need a registry login, which is why I haven't proposed it here).

This is one of a series of independent PRs that make sql.oracle output run on Oracle 19c (the oldest release in support). With all of them applied, 27 of the 31 integration queries return their snapshot results on Oracle (18 on main; the other 4 are read_csv, two s-string queries with unquoted lower-case table names, and the double quote in date_to_text), and 39 of 40 DuckDB-differential queries match (9 on main).

🤖 Generated with Claude Code

calin2k and others added 2 commits October 18, 2026 14:30
- date.trunc compiles to TRUNC(col, 'MM') with Oracle's format models; units
  below a minute are reported as unsupported
- date.to_text compiles to TO_CHAR with Oracle format models; Oracle's FM is a
  toggle, so it's switched off after each element
- date.diff reports that Oracle has no DATEDIFF, instead of emitting it
- TIMESTAMP literals separate date and time with a space (ORA-01861 with T)
- intervals are INTERVAL '3' DAY, with a leading precision above two digits;
  weeks become days, milli- and microseconds fractional seconds
- time literals are reported as unsupported: Oracle has no TIME type

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@prql-bot prql-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small things inline: a panic on a large weeks interval, and an override that never gets called. The rest of the Oracle mapping looks right to me.

Comment thread prqlc/prqlc/src/sql/gen_expr.rs Resolved
Comment thread prqlc/prqlc/src/sql/dialect.rs Resolved
Oracle's TIMESTAMP literal requires the seconds (ORA-01861 for
@2030-06-01T10:30) and an offset written as ±HH:MM (ORA-01874 for -0800,
ORA-01882 for Z).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@calin2k

calin2k commented Oct 18, 2026

Copy link
Copy Markdown
Author

Pushed a follow-up: Oracle's TIMESTAMP literal also requires the seconds (@2030-06-01T10:30 was ORA-01861) and an offset written ±HH:MM (-0800 is ORA-01874, Z ORA-01882), so these are completed now (TIMESTAMP '2030-06-01 10:30:00', '... +00:00', '... -08:00'). Found by running a further query set on Oracle 19.3; covered in test_oracle_dates, executed on the database.

@prql-bot prql-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oracle_timestamp still lets two lexer-accepted forms through: an hour-only time and a fraction written right after the minutes. With target:sql.oracle, @2025-01-01T08 compiles to TIMESTAMP '2025-01-01 08' and @2025-01-01T08:15.5 to TIMESTAMP '2025-01-01 08:15.5', both of which Oracle rejects since it requires HH24:MI:SS. In each case the regex doesn't match, so the input falls back to replacen. The two inline suggestions go together (apply them as one batch). They make minutes, seconds and the fraction separate optional groups, so these become '2025-01-01 08:00:00' and '2025-01-01 08:15:00.5'. I ran test_oracle_dates locally with both applied and it still passes. The lexer always emits the offset as ±HHMM, so the offset arm also gets simpler.

Comment thread prqlc/prqlc/src/sql/gen_expr.rs Resolved
Comment thread prqlc/prqlc/src/sql/gen_expr.rs Resolved
- Oracle TIMESTAMP literals: an hour-only time and a fraction right after
  the minutes are completed too (`08` -> `08:00:00`, `08:15.5` ->
  `08:15:00.5`).
- A `weeks` interval too large for days is an error instead of an
  overflow.
- Drop OracleDialect::interval_quoting_style, which was never called:
  every Oracle interval goes through translate_oracle_interval.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
prql-bot
prql-bot previously approved these changes Oct 18, 2026

@prql-bot prql-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

codecov/patch came back red on this head (91.36% of the diff covered, target 94.39%). It hadn't reported yet when I approved, so I'm withdrawing the approval until it's resolved. The uncovered lines include behavior the PR description promises: the double-quote compile error in date.to_text, and the whole-second output of fraction (2000milliseconds → INTERVAL '2' SECOND). The zero-padded MM/DD and unpadded FMHH24FM arms are uncovered too. The two inline suggestions add tests for all of these. I ran them locally and both test functions pass with the snapshots shown. That still leaves the replacen fallback in oracle_timestamp and the Unsupported interval unit arm uncovered.

Comment thread prqlc/prqlc/tests/integration/sql.rs Resolved
Comment thread prqlc/prqlc/tests/integration/sql.rs Resolved
@prql-bot
prql-bot dismissed their stale review October 18, 2026 18:49

CI failed — codecov/patch below target; see the follow-up review.

`%m`, `%d` and `%-H`; the errors for a double quote in a format and for a
specifier Oracle can't produce; a whole number of seconds from milliseconds;
and the two fallbacks the lexer doesn't reach (unit tests).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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