Skip to content

Conversation

@fivetran-kwoodbeck
Copy link
Collaborator

@fivetran-kwoodbeck fivetran-kwoodbeck commented Dec 23, 2025

This fixes a variety of issues in transpilation of Snowflake's TO_TIMESTAMP and TRY_TO_TIMESTAMP family of functions to DuckDB, fixing several edge cases and adding support for missing variants.

Snowflake Parser (snowflake.py)

  • Added TRY_TO_TIMESTAMP_LTZ/NTZ/TZ parser mappings for safe timestamp conversion variants
  • Fixed numeric-looking strings with format: TO_TIMESTAMP('20240115103045', 'YYYYMMDDHH24MISS') was incorrectly parsed as unix timestamp instead of using the format string
  • Fixed negative unix timestamps: Added exp.Neg handling so TO_TIMESTAMP(-86400) parses correctly
  • Added missing AM/PM format mapping: AM/PM%p in TIME_MAPPING for 12-hour time parsing
  • Added target_type tracking: UnixToTime and StrToTime expressions now track the target timestamp type (TIMESTAMP, TIMESTAMPLTZ, TIMESTAMPNTZ, TIMESTAMPTZ)

DuckDB Generator (duckdb.py)

  • Fixed NTZ timezone handling: DuckDB's TO_TIMESTAMP(n) returns TIMESTAMPTZ, but Snowflake's returns TIMESTAMP (NTZ interpreted as UTC). Now uses AT TIME ZONE 'UTC' to extract UTC time as naive timestamp
  • Added TIMESTAMPLTZ type mapping: Maps to TIMESTAMPTZ in DuckDB
  • Fixed LTZ/TZ format string handling: strtotime_sql now casts to TIMESTAMPTZ for LTZ/TZ variants (both TRY and non-TRY)

Expressions (expressions.py)

  • Added target_type to UnixToTime.arg_types to track desired output type

Test Queries

Note: See the Jira ticket for the full suite of tests.

Snowflake DuckDB
TO_TIMESTAMP(1704067200) TO_TIMESTAMP(1704067200) AT TIME ZONE 'UTC'
TO_TIMESTAMP('20240115', 'YYYYMMDD') STRPTIME('20240115', '%Y%m%d')
TO_TIMESTAMP('10:30 PM', 'HH12:MI AM') STRPTIME('10:30 PM', '%I:%M %p')
TRY_TO_TIMESTAMP_LTZ('2024-01-15') TRY_CAST('2024-01-15' AS TIMESTAMPTZ)
TO_TIMESTAMP_LTZ('...', 'fmt') CAST(STRPTIME('...', '%fmt') AS TIMESTAMPTZ)

@fivetran-kwoodbeck fivetran-kwoodbeck changed the title feat(snowflake)!: updates / fixes for Snowflake to DuckDB transpilation TO_TIMESTAMP feat(snowflake)!: updates / fixes for Snowflake to DuckDB transpilation of TO_TIMESTAMP family of functions Dec 23, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 23, 2025

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:feature/transpile-to_timestamp, sqlglot version: feature/transpile-to_timestamp)
  • baseline (main, sqlglot version: 28.5.1.dev45)

⚠️ Limited to dialects: duckdb, snowflake

By Dialect

dialect main sqlglot:feature/transpile-to_timestamp difference links
duckdb -> duckdb 4003/4003 passed (100.0%) 4003/4003 passed (100.0%) No change full result / delta
snowflake -> duckdb 626/1085 passed (57.7%) 628/1085 passed (57.9%) ⬆ improved by 0.2% full result / delta
snowflake -> snowflake 981/1085 passed (90.4%) 981/1085 passed (90.4%) No change full result / delta

Overall

main: 6173 total, 5610 passed (pass rate: 90.9%), sqlglot version: 28.5.1.dev45

sqlglot:feature/transpile-to_timestamp: 6173 total, 5612 passed (pass rate: 90.9%), sqlglot version: feature/transpile-to_timestamp

Difference: No change

@fivetran-kwoodbeck fivetran-kwoodbeck changed the title feat(snowflake)!: updates / fixes for Snowflake to DuckDB transpilation of TO_TIMESTAMP family of functions feat(snowflake)!: updates for Snowflake to DuckDB transpilation of TO_TIMESTAMP family of functions Dec 23, 2025
@fivetran-kwoodbeck fivetran-kwoodbeck changed the title feat(snowflake)!: updates for Snowflake to DuckDB transpilation of TO_TIMESTAMP family of functions feat(snowflake)!: updates for Snowflake to DuckDB transpilation of TO_TIMESTAMP functions Dec 23, 2025
Copy link
Collaborator

@VaggelisD VaggelisD left a comment

Choose a reason for hiding this comment

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

Nice work @fivetran-kwoodbeck! Leaving a few comments from my first pass:

@fivetran-kwoodbeck fivetran-kwoodbeck force-pushed the feature/transpile-to_timestamp branch from f1331df to cc79016 Compare December 29, 2025 15:18
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.

3 participants