Skip to content

Fix TIMESTAMP_DIFF/DATETIME_DIFF transpilation to Presto/Trino [CLAUDE] - #7895

Merged
georgesittas merged 2 commits into
tobymao:mainfrom
winklemad:fix-presto-timestamp-datetime-diff
Jul 20, 2026
Merged

Fix TIMESTAMP_DIFF/DATETIME_DIFF transpilation to Presto/Trino [CLAUDE]#7895
georgesittas merged 2 commits into
tobymao:mainfrom
winklemad:fix-presto-timestamp-datetime-diff

Conversation

@winklemad

Copy link
Copy Markdown
Contributor

Presto and Trino only have date_diff(unit, ts1, ts2) — they have no TIMESTAMPDIFF, DATETIME_DIFF, or TIMESTAMP_DIFF. sqlglot maps exp.DateDiff to DATE_DIFF for Presto, but exp.TimestampDiff and exp.DatetimeDiff (produced by e.g. BigQuery TIMESTAMP_DIFF/DATETIME_DIFF, or MySQL/Snowflake TIMESTAMPDIFF) had no Presto/Trino handler and fell through to the generic function fallback:

>>> import sqlglot
>>> sqlglot.transpile("SELECT TIMESTAMP_DIFF(a, b, DAY)", read="bigquery", write="presto")
['SELECT TIMESTAMPDIFF(a, b, DAY)']   # invalid: no such function, and unit-last arg order
>>> sqlglot.transpile("SELECT DATETIME_DIFF(a, b, MONTH)", read="bigquery", write="trino")
['SELECT DATETIME_DIFF(a, b, MONTH)'] # invalid

Fix

Route all three diff nodes through DATE_DIFF(unit, expression, this) for Presto (Trino inherits Presto's generator). Since date_diff(unit, ts1, ts2) = ts2 - ts1, passing (expression, this) preserves the source's a - b semantics — the same output DATE_DIFF and DuckDB already produce. This mirrors the already-merged fixes for the same bug class in other dialects (#6126 for DuckDB, #4334 for Snowflake).

After:

['SELECT DATE_DIFF(\'DAY\', b, a)']    # presto
['SELECT DATE_DIFF(\'MONTH\', b, a)']  # trino

Tests

Added assertions to the existing BigQuery TIMESTAMP_DIFF/DATETIME_DIFF validate_all blocks (presto + trino outputs). They fail on the current code and pass with the fix. tests/dialects/test_bigquery.py → 58 passed / 1073 subtests.


Disclosure: this fix was prepared with AI assistance (hence the [CLAUDE] tag per AGENTS.md). I reviewed every line, verified the Presto/Trino semantics against their docs, and confirmed the reproduction and tests myself.

Presto and Trino only have date_diff(unit, ts1, ts2); they have no
TIMESTAMPDIFF, DATETIME_DIFF or TIMESTAMP_DIFF. sqlglot maps exp.DateDiff
to DATE_DIFF for Presto, but exp.TimestampDiff and exp.DatetimeDiff
(produced by e.g. BigQuery TIMESTAMP_DIFF/DATETIME_DIFF) had no
Presto/Trino handler and fell through to the generic function fallback,
emitting the node name verbatim with the unit-last arg order - invalid,
unparseable SQL.

Route all three diff nodes through DATE_DIFF(unit, expression, this) for
Presto (Trino inherits it). date_diff(unit, ts1, ts2) = ts2 - ts1, so
(expression, this) preserves the source's a - b semantics, matching what
DATE_DIFF and DuckDB already produce (cf. merged tobymao#6126, tobymao#4334).
@georgesittas georgesittas self-assigned this Jul 20, 2026

@georgesittas georgesittas 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.

This looks ok. I'll do follow-up cleaning up post-merge. Thank you.

Comment thread tests/dialects/test_bigquery.py Outdated
@georgesittas
georgesittas merged commit e4d2ea5 into tobymao:main Jul 20, 2026
7 checks passed
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