Skip to content

feat(snowflake): Transpile exp.DatetimeDiff - #4334

Merged
georgesittas merged 1 commit into
mainfrom
vaggelisd/sf_datetimediff
Nov 4, 2024
Merged

georgesittas merged 1 commit into
mainfrom
vaggelisd/sf_datetimediff

Conversation

@VaggelisD

Copy link
Copy Markdown
Collaborator

Transpile BQ's DATETIME_DIFF into Snowflake's TIMESTAMPDIFF. Both functions can process different date/time types, so the solution here should be a catch-all, e.g:

  • DATETIME -> TIMESTAMP diff
bigquery> SELECT DATETIME_DIFF(DATETIME "2010-07-07 10:20:00", DATETIME "2008-12-25 15:30:00", DAY);
559

snowflake> SELECT TIMESTAMPDIFF(DAY, CAST('2008-12-25 15:30:00' AS TIMESTAMP), CAST('2010-07-07 10:20:00' AS TIMESTAMP));
559
  • DATE -> DATE diff
bigquery> SELECT DATETIME_DIFF(DATE "2010-07-07", DATE "2008-12-25", MONTH);
19

snowflake> SELECT TIMESTAMPDIFF(MONTH, CAST('2008-12-25' AS DATE), CAST('2010-07-07' AS DATE));
19

Docs

BQ DATETIME_DIFF | SF TIMESTAMPDIFF

@georgesittas
georgesittas merged commit f8fec0a into main Nov 4, 2024
@georgesittas
georgesittas deleted the vaggelisd/sf_datetimediff branch November 4, 2024 12:03
fresioAS pushed a commit to fresioAS/sqlglot that referenced this pull request May 3, 2026
georgesittas added a commit that referenced this pull request Jul 20, 2026
…no [CLAUDE] (#7895)

* Fix TIMESTAMP_DIFF/DATETIME_DIFF transpilation to Presto/Trino [CLAUDE]

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 #6126, #4334).

* Update tests/dialects/test_bigquery.py

---------

Co-authored-by: Jo <[email protected]>
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