Skip to content

feat(sqlalchemy-bigquery): support BigQuery SAFE_CAST and SQLAlchemy 2.0 try_cast - #18953

Merged
chalmerlowe merged 1 commit into
mainfrom
feat/sqlalchemy-bigquery-safe-cast
Nov 3, 2026
Merged

chalmerlowe merged 1 commit into
mainfrom
feat/sqlalchemy-bigquery-safe-cast

Conversation

@chalmerlowe

Copy link
Copy Markdown
Contributor

Problem / Context

BigQuery provides SAFE_CAST(expression AS typename), which behaves identically to CAST except that it returns NULL instead of raising a runtime error when a conversion fails (for example, casting a malformed string 'abc' to INT64). Previously, sqlalchemy-bigquery only compiled standard sqlalchemy.cast(...) calls to CAST(...) and had no built-in way to emit SAFE_CAST(...) or compile SQLAlchemy 2.0's sqlalchemy.try_cast(...).

Solution

  • Added SafeCast and the safe_cast(expression, type_) helper function to sqlalchemy_bigquery (and exported both in sqlalchemy_bigquery.__all__) so users on both SQLAlchemy 1.4 and SQLAlchemy 2.x can construct SAFE_CAST(...) expressions that preserve target-type result processors and SQL compilation caching (inherit_cache = True).
  • Implemented BigQueryCompiler.visit_try_cast so that both sqlalchemy_bigquery.safe_cast(...) and SQLAlchemy 2.0's standard sqlalchemy.try_cast(...) compile to SAFE_CAST(<expression> AS <type>).
  • Added unit tests in tests/unit/test_compiler.py covering column expressions across multiple BigQuery types, literals, NULL expressions, nested safe_cast expressions, and sqlalchemy.try_cast.
  • Documented safe_cast and sqlalchemy.try_cast usage in README.rst and docs/README.rst.

Testing

Unit test execution (covering both SQLAlchemy 1.4 and SQLAlchemy 2.x environments) and linting are verified in CI.

Fixes #15779

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request adds support for BigQuery's SAFE_CAST expression by introducing a SafeCast class and a safe_cast helper function, which compiles to SAFE_CAST in BigQuery. It also integrates with SQLAlchemy 2.0's try_cast to compile to SAFE_CAST. The changes include comprehensive unit tests covering various data types, nested casts, and literals, along with updated documentation in the README files. I have no feedback to provide.

@chalmerlowe

Copy link
Copy Markdown
Contributor Author

Waiting on Blocker: #18956

@chalmerlowe
chalmerlowe marked this pull request as ready for review November 2, 2026 17:29
@chalmerlowe
chalmerlowe requested review from a team as code owners November 2, 2026 17:29
@chalmerlowe
chalmerlowe requested review from julianxu-goog and removed request for a team November 2, 2026 17:30
@chalmerlowe
chalmerlowe force-pushed the feat/sqlalchemy-bigquery-safe-cast branch from 3e174f5 to f5b0fdf Compare November 2, 2026 18:50
@chalmerlowe
chalmerlowe merged commit 931968b into main Nov 3, 2026
54 checks passed
@chalmerlowe
chalmerlowe deleted the feat/sqlalchemy-bigquery-safe-cast branch November 3, 2026 08:25
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.

Support calling SAFE_CAST via sqlalchemy

2 participants