Skip to content

feat(snowflake)!: Transpilation support for TRY_CAST#7355

Closed
fivetran-ashashankar wants to merge 1 commit intomainfrom
RD-1069322-transpile_try_cast
Closed

feat(snowflake)!: Transpilation support for TRY_CAST#7355
fivetran-ashashankar wants to merge 1 commit intomainfrom
RD-1069322-transpile_try_cast

Conversation

@fivetran-ashashankar
Copy link
Copy Markdown
Collaborator

Transpilation support for try_cast snowflake function

@github-actions
Copy link
Copy Markdown
Contributor

Benchmark Results

Legend: 🟢🟢 = 5%+ faster | 🟢 = 3-5% faster | 🟩 = 1-3% faster | ⚪ = unchanged | 🟧 = 1-3% slower | 🔴 = 3-5% slower | 🔴🔴 = 5%+ slower

sqlglot

Query main PR diff
tpch 2.6ms 2.5ms 0.9% faster
short 202us 200us 1.0% faster
deep_arithmetic 7.4ms 7.5ms 1.3% slower 🟧
large_in 387.9ms 389.9ms 0.5% slower
values 431.0ms 429.8ms 0.3% faster
many_joins 10.8ms 10.8ms 0.0%
many_unions 41.2ms 41.0ms 0.6% faster
nested_subqueries 1.3ms 1.2ms 5.5% faster 🟢🟢
many_columns 11.8ms 11.6ms 1.3% faster 🟩
large_case 34.2ms 33.6ms 1.7% faster 🟩
complex_where 28.1ms 27.3ms 2.9% faster 🟩
many_ctes 17.1ms 17.0ms 1.0% faster
many_windows 20.8ms 20.5ms 1.5% faster 🟩
nested_functions 696us 687us 1.3% faster 🟩
large_strings 5.3ms 5.4ms 0.6% slower
many_numbers 99.7ms 98.8ms 0.8% faster

sqlglot[c]

Query main PR diff
tpch 722us 696us 3.6% faster 🟢
short 61us 61us 0.6% faster
deep_arithmetic 2.8ms 2.8ms 0.2% faster
large_in 128.2ms 127.0ms 0.9% faster
values 131.1ms 132.7ms 1.2% slower 🟧
many_joins 2.8ms 2.6ms 7.0% faster 🟢🟢
many_unions 9.4ms 9.0ms 4.1% faster 🟢
nested_subqueries 253us 250us 1.5% faster 🟩
many_columns 3.4ms 3.2ms 5.1% faster 🟢🟢
large_case 10.4ms 10.1ms 3.4% faster 🟢
complex_where 7.7ms 7.4ms 4.6% faster 🟢
many_ctes 3.8ms 3.7ms 1.9% faster 🟩
many_windows 5.3ms 5.2ms 3.0% faster 🟢
nested_functions 171us 166us 3.1% faster 🟢
large_strings 1.5ms 1.5ms 0.7% slower
many_numbers 28.0ms 28.5ms 1.7% slower 🟧

Comment /benchmark to re-run.

@fivetran-ashashankar fivetran-ashashankar force-pushed the RD-1069322-transpile_try_cast branch from 0171043 to a39f32d Compare March 23, 2026 22:35
@github-actions
Copy link
Copy Markdown
Contributor

SQLGlot Integration Test Results

Comparing:

  • this branch (sqlglot:RD-1069322-transpile_try_cast, sqlglot version: RD-1069322-transpile_try_cast)
  • baseline (main, sqlglot version: 0.0.1.dev1)

⚠️ Limited to dialects: duckdb

By Dialect

dialect main sqlglot:RD-1069322-transpile_try_cast transitions links
duckdb -> duckdb 4003/4004 passed (100.0%) 4003/4004 passed (100.0%) No change full result / delta

Overall

main: 4004 total, 4003 passed (pass rate: 100.0%), sqlglot version: 0.0.1.dev1

sqlglot:RD-1069322-transpile_try_cast: 4004 total, 4003 passed (pass rate: 100.0%), sqlglot version: RD-1069322-transpile_try_cast

Transitions:
No change

Comment on lines +96 to +103
# Common date format patterns for TRY_CAST transpilation to TRY_STRPTIME
# Maps compiled regex patterns to DuckDB strptime format strings
_DATE_FORMAT_PATTERNS = [
(re.compile(r"^\d{2}-[A-Za-z]{3}-\d{4}$"), "%d-%b-%Y"), # DD-Mon-YYYY
(re.compile(r"^[A-Za-z]{3}-\d{2}-\d{4}$"), "%b-%d-%Y"), # Mon-DD-YYYY
(re.compile(r"^\d{1,2}/\d{1,2}/\d{4}$"), "%m/%d/%Y"), # MM/DD/YYYY
(re.compile(r"^[A-Za-z]+\s+\d{1,2},\s+\d{4}$"), "%B %d, %Y"), # Month DD, YYYY
]
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.

Why are we doing this? I don't think this is an established pattern. Can you please elaborate on the difficulties you encountered while trying to transpile this function?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

on duckdb - SELECT TRY_CAST('05-Mar-2016' AS DATE)
on snowflake - TRY_CAST('05-Mar-2016' AS DATE) → returns DATE type (2016-03-05)

For DATE target
SELECT CAST(TRY_STRPTIME('05-Mar-2016', '%d-%b-%Y') AS DATE)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

so my thought was - DuckDB's TRY_STRPTIME needs to know the format string for parsing:

-- Different formats need different format strings
TRY_STRPTIME('05-Mar-2016', '%d-%b-%Y') -- DD-Mon-YYYY
TRY_STRPTIME('Mar-05-2016', '%b-%d-%Y') -- Mon-DD-YYYY
TRY_STRPTIME('03/05/2016', '%m/%d/%Y') -- MM/DD/YYYY
TRY_STRPTIME('March 5, 2016', '%B %d, %Y') -- Month DD, YYYY

_DATE_FORMAT_PATTERNS maps each format to its format string.

The pattern matching detects which format the literal string is using, so you know which format string to pass to TRY_STRPTIME.

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.

There's an existing pattern in the codebase for this, though: calling build_formatted_time. Can't it be applied in this case as well? Doing manual regex matching doesn't seem like the right approach.

@geooo109 geooo109 closed this Mar 26, 2026
@geooo109
Copy link
Copy Markdown
Collaborator

I will close this for now, it's complicated and the solution isn't trivial.

@georgesittas georgesittas deleted the RD-1069322-transpile_try_cast branch March 26, 2026 18:55
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