Repository navigation
feat(sqlalchemy-bigquery): support BigQuery SAFE_CAST and SQLAlchemy 2.0 try_cast - #18953
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
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.
Contributor
Author
|
Waiting on Blocker: #18956 |
chalmerlowe
marked this pull request as ready for review
November 2, 2026 17:29
chalmerlowe
requested review from
julianxu-goog
and removed request for
a team
November 2, 2026 17:30
chalmerlowe
force-pushed
the
feat/sqlalchemy-bigquery-safe-cast
branch
from
November 2, 2026 18:50
3e174f5 to
f5b0fdf
Compare
daniel-sanche
approved these changes
Nov 2, 2026
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.
Problem / Context
BigQuery provides
SAFE_CAST(expression AS typename), which behaves identically toCASTexcept that it returnsNULLinstead of raising a runtime error when a conversion fails (for example, casting a malformed string'abc'toINT64). Previously,sqlalchemy-bigqueryonly compiled standardsqlalchemy.cast(...)calls toCAST(...)and had no built-in way to emitSAFE_CAST(...)or compile SQLAlchemy 2.0'ssqlalchemy.try_cast(...).Solution
SafeCastand thesafe_cast(expression, type_)helper function tosqlalchemy_bigquery(and exported both insqlalchemy_bigquery.__all__) so users on both SQLAlchemy 1.4 and SQLAlchemy 2.x can constructSAFE_CAST(...)expressions that preserve target-type result processors and SQL compilation caching (inherit_cache = True).BigQueryCompiler.visit_try_castso that bothsqlalchemy_bigquery.safe_cast(...)and SQLAlchemy 2.0's standardsqlalchemy.try_cast(...)compile toSAFE_CAST(<expression> AS <type>).tests/unit/test_compiler.pycovering column expressions across multiple BigQuery types, literals,NULLexpressions, nestedsafe_castexpressions, andsqlalchemy.try_cast.safe_castandsqlalchemy.try_castusage inREADME.rstanddocs/README.rst.Testing
Unit test execution (covering both SQLAlchemy 1.4 and SQLAlchemy 2.x environments) and linting are verified in CI.
Fixes #15779