feat(duckdb): Add transpilation support for TRY_CAST#7672
Closed
fivetran-amrutabhimsenayachit wants to merge 1 commit into
Closed
feat(duckdb): Add transpilation support for TRY_CAST#7672fivetran-amrutabhimsenayachit wants to merge 1 commit into
fivetran-amrutabhimsenayachit wants to merge 1 commit into
Conversation
191284d to
4f86e91
Compare
Contributor
SQLGlot Integration Test ResultsComparing:
By Dialect
Overallmain: 113247 total, 112163 passed (pass rate: 99.0%), sqlglot version: sqlglot:RD-1069322-try-cast: 110938 total, 110147 passed (pass rate: 99.3%), sqlglot version: Transitions: Dialect pair changes: 0 previous results not found, 1 current results not found ✅ 65 test(s) passed |
geooo109
reviewed
May 25, 2026
Comment on lines
+4405
to
+4410
| return self.sql( | ||
| exp.case(exp.func("LOWER", src.copy())) | ||
| .when(exp.Literal.string("on"), exp.true()) | ||
| .when(exp.Literal.string("off"), exp.false()) | ||
| .else_(exp.TryCast(this=src.copy(), to=to)) | ||
| ) |
Collaborator
There was a problem hiding this comment.
Let's reuse _to_boolean_sql here.
| exp.cast( | ||
| exp.func("TRY_STRPTIME", src.copy(), exp.Literal.string(f)), "DATE" | ||
| ) | ||
| for f in self._TRYCAST_DATE_FORMATS |
Collaborator
There was a problem hiding this comment.
These are the only DATE_FORMATS that duckdb supports outside the ISO ?
| this=exp.TryCast(this=src.copy(), to=to), | ||
| expressions=[ | ||
| exp.func("TRY_STRPTIME", src.copy(), exp.Literal.string(f)) | ||
| for f in self._TRYCAST_TIMESTAMP_FORMATS |
Collaborator
There was a problem hiding this comment.
| src, to, to_type = expression.this, expression.to, expression.to.this | ||
|
|
||
| # INTERVAL: TRY_CAST to INT first (preserves NULL for non-numeric input), then call to_*(). | ||
| if isinstance(to_type, exp.Interval): |
Collaborator
There was a problem hiding this comment.
Let's make something like:
if ...:
elif ...:
and lastly:
return super().trycast_sql(expression)
Collaborator
|
@fivetran-amrutabhimsenayachit let's break this PR into smaller ones, tackling the problem incrementally. |
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.
Snowflake's
TRY_CASTis semantically richer than DuckDB's: it usesAUTO date/timeformat detection (acceptingISO-8601, MM/DD/YYYY, and DD-Mon-YYYY), enforces string length constraints onCHAR(n)/VARCHAR(n), acceptsON/OFFas boolean values, and supports interval arithmetic. DuckDB's nativeTRY_CASThandles onlyISO-8601 dates, silently ignores length constraints, rejectsON/OFF, and has nostring-to-interval casting. A naive passthrough transpilation produces silently wrong results — not errors — making the gap invisible to users.This PR fixes it, except for the
NANOSECONDtime unit for which DuckDb has no support at all.