`no_trycast_sql` silently changes TRY_CAST null-on-failure to CAST error-on-failure with no warning
no_trycast_sql silently changes TRY_CAST null-on-failure to CAST error-on-failure for PostgreSQL, SQLite, MySQL, and Dremio
Verified on
- sqlglot 30.33.0 (latest PyPI, 2026-11-18)
- Python 3.13.9, macOS 15.6.1 arm64
Reproduction
from sqlglot import transpile, ErrorLevel
sql = "SELECT TRY_CAST(x AS INT) FROM t"
for dialect in ["postgres", "sqlite", "mysql", "dremio"]:
print(dialect, transpile(sql, read="snowflake", write=dialect, unsupported_level=ErrorLevel.WARN)[0])
# contrast: a dialect that keeps TRY_CAST
print("duckdb", transpile(sql, read="snowflake", write="duckdb")[0])
Actual output:
postgres SELECT CAST(x AS INT) FROM t
sqlite SELECT CAST(x AS INTEGER) FROM t
mysql SELECT CAST(x AS SIGNED) FROM t
dremio SELECT CAST(x AS INT) FROM t
duckdb SELECT TRY_CAST(x AS INT) FROM t
No warning is emitted at unsupported_level=ErrorLevel.WARN. No entry appears in
generator.unsupported_messages. Passing ErrorLevel.RAISE also produces no
exception.
Behaviour difference
TRY_CAST(x AS INT) returns NULL when x cannot be converted to an integer.
CAST(x AS INT) in PostgreSQL raises ERROR: invalid input syntax for type integer and aborts the query; in MySQL and SQLite it silently returns 0. The
two have completely different error-handling semantics.
Per-dialect CAST behavior on invalid input
| Dialect | CAST('abc' AS INT) |
Same as TRY_CAST? | Evidence |
|---|---|---|---|
| postgres | ERROR | No | Well-documented; raises invalid input syntax |
| sqlite | 0 |
No | Tested locally: SELECT CAST('abc' AS INTEGER) → 0 |
| mysql | 0 with warning |
No | Documented MySQL CAST behavior for non-numeric strings |
| dremio | ERROR | No | Arrow-based engine; sqlglot parses Dremio CAST as exp.Cast (strict) |
| hive | NULL |
Yes | sqlglot parses Hive CAST as exp.TryCast — null-safe by design |
| drill | NULL |
Yes | sqlglot parses Drill CAST as exp.TryCast — null-safe by design |
Hive and Drill are not affected: their native CAST is already null-safe, so
TryCast → CAST is a lossless round-trip. The bug applies only to the four dialects
where CAST changes semantics.
Impact
A query such as
SELECT id, TRY_CAST(raw_value AS INT) AS amount
FROM user_input
WHERE TRY_CAST(raw_value AS INT) IS NOT NULL
is a standard pattern for filtering bad rows in T-SQL or Snowflake. After
transpilation to PostgreSQL the generated CAST aborts on the first bad row
instead of skipping it. Because sqlglot emits no warning at any error level,
callers have no signal that the semantics changed.
Root cause
File: sqlglot/dialects/dialect.py, no_trycast_sql function
def no_trycast_sql(self: Generator, expression: exp.TryCast) -> str:
return self.cast_sql(expression) # ← never calls self.unsupported()
This helper is registered in TRANSFORMS for all six dialects that lack
TRY_CAST. For Hive and Drill it is semantically correct (their CAST is
null-safe). For Postgres, SQLite, MySQL, and Dremio it silently changes
semantics without notifying the caller.
The pattern used throughout the same file for other unsupported constructs is to
call self.unsupported(…) first. The immediately following helper already does
this:
def no_map_from_entries_sql(self: Generator, expression: exp.MapFromEntries) -> str:
self.unsupported("MAP_FROM_ENTRIES unsupported") # ← correct pattern
return ""
no_trycast_sql is the only no_* helper used by lossy dialects that skips
this call.
Duplicate search
Searched with the following terms (all results listed):
repo:tobymao/sqlglot no_trycast_sql— no resultsrepo:tobymao/sqlglot TRY_CAST unsupported warning— #7731 (duckdb interval, closed, different)repo:tobymao/sqlglot TRY_CAST postgres NULL— #7609 (redshift CONVERT, unrelated)repo:tobymao/sqlglot SAFE_CAST postgres— no resultsrepo:tobymao/sqlglot TRY_CAST semantic— #8398 (trino FROM_JSON, unrelated)repo:tobymao/sqlglot trycast silent— #7991, #7681, #7713, #7672 (all duckdb support additions)repo:tobymao/sqlglot TRY_CAST(15 results) — none concern the missing warning inno_trycast_sql
No exact duplicate found.
Suggested fix
Split the single no_trycast_sql into two functions — one for null-safe dialects
(no warning), one for lossy dialects (warning):
def no_trycast_sql(self: Generator, expression: exp.TryCast) -> str:
+ """Use for dialects whose CAST is already null-safe (Hive, Drill)."""
return self.cast_sql(expression)
+
+
+def lossy_trycast_sql(self: Generator, expression: exp.TryCast) -> str:
+ """Use for dialects where CAST raises an error or returns a wrong value
+ on invalid input instead of returning NULL (Postgres, MySQL, SQLite, Dremio)."""
+ self.unsupported(
+ "TRY_CAST is not supported; using CAST instead, "
+ "which raises an error on invalid input rather than returning NULL"
+ )
+ return self.cast_sql(expression)
Then register lossy_trycast_sql in postgres.py, sqlite.py, mysql.py, and
dremio.py (keeping no_trycast_sql in hive.py and drill.py).
This means:
ErrorLevel.RAISE→UnsupportedErrorfor the four affected dialects.ErrorLevel.WARN→ message ingenerator.unsupported_messages.ErrorLevel.IGNORE→ current behaviour preserved exactly.- Hive and Drill: no change at any error level.