Fix TIMESTAMP_DIFF/DATETIME_DIFF transpilation to Presto/Trino [CLAUDE] - #7895
Merged
georgesittas merged 2 commits intoJul 20, 2026
Merged
Conversation
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
left a comment
Collaborator
There was a problem hiding this comment.
This looks ok. I'll do follow-up cleaning up post-merge. Thank you.
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.
Presto and Trino only have
date_diff(unit, ts1, ts2)— they have noTIMESTAMPDIFF,DATETIME_DIFF, orTIMESTAMP_DIFF. sqlglot mapsexp.DateDifftoDATE_DIFFfor Presto, butexp.TimestampDiffandexp.DatetimeDiff(produced by e.g. BigQueryTIMESTAMP_DIFF/DATETIME_DIFF, or MySQL/SnowflakeTIMESTAMPDIFF) had no Presto/Trino handler and fell through to the generic function fallback:Fix
Route all three diff nodes through
DATE_DIFF(unit, expression, this)for Presto (Trino inherits Presto's generator). Sincedate_diff(unit, ts1, ts2) = ts2 - ts1, passing(expression, this)preserves the source'sa - bsemantics — the same outputDATE_DIFFand DuckDB already produce. This mirrors the already-merged fixes for the same bug class in other dialects (#6126 for DuckDB, #4334 for Snowflake).After:
Tests
Added assertions to the existing BigQuery
TIMESTAMP_DIFF/DATETIME_DIFFvalidate_allblocks (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 perAGENTS.md). I reviewed every line, verified the Presto/Trino semantics against their docs, and confirmed the reproduction and tests myself.