Skip to content

fix(sqlalchemy-bigquery): restore String bindparam type for GeoAlchemy2 0.21.0 - #18985

Merged
parthea merged 2 commits into
mainfrom
fix/sqlalchemy-bigquery-geoalchemy2-0.21
Nov 4, 2026
Merged

parthea merged 2 commits into
mainfrom
fix/sqlalchemy-bigquery-geoalchemy2-0.21

Conversation

@chalmerlowe

@chalmerlowe chalmerlowe commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Context

In GeoAlchemy2 >= 0.21.0, GenericFunction.__init__ wraps raw string arguments (isinstance(elem, str)) in an _UntypedString(NullType) bind parameter so PostgreSQL drivers send raw strings untyped.

In sqlalchemy-bigquery:

  1. _fixup_st_arguments only overrides BindParameter.type for functions registered in _argument_types (which maps functions expecting GEOGRAPHY parameters). For geography constructor functions that accept a STRING argument (such as ST_GeogFromText), the BindParameter.type remains _UntypedString(NullType).
  2. Furthermore, GeoAlchemy2 registers dialect-specific @compiles handlers (e.g., for "mssql") on GenericFunction subclasses such as ST_GeogFromText, giving those subclasses their own _compiler_dispatcher that shadows GenericFunction._compiler_dispatcher.

When BigQueryCompiler.visit_bindparam inspects a NullType bind parameter, it returns %(ST_GeogFromText_1)s without the :STRING type suffix, breaking sqlalchemy-bigquery's typed bind parameter contract and unit tests (test_geoalchemy2_core and test_calling_st_functions_that_dont_take_geographies).

Solution

  • packages/sqlalchemy-bigquery/sqlalchemy_bigquery/geography.py:
    • In _fixup_st_arguments, restore argument.type = sqlalchemy.types.String() for any BindParameter whose type is NullType and whose value is a str prior to applying _argument_types overrides.
    • Recursively register _fixup_st_arguments across _get_all_subclasses(geoalchemy2.functions.GenericFunction) for the "bigquery" dialect so subclasses with their own _compiler_dispatcher invoke _fixup_st_arguments.
  • packages/sqlalchemy-bigquery/tests/unit/test_geography.py:
    • Expanded test_fixup_st_arguments to verify that NullType bind parameters with str values are coerced to String(), while NullType bind parameters with non-string values (None) remain NullType.
    • Added test_nested_generic_function_subclasses_register_compiler to verify recursive subclass discovery and compiler dispatcher registration.
    • See CI results for test verification.

@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 updates the BigQuery geography compiler to support GeoAlchemy2 >= 0.21.0 by restoring the String type for string bind parameters wrapped in NullType, and registers the fixup hook on all GenericFunction subclasses. The review feedback recommends wrapping the subclass registration in a try-except block to prevent import errors in environments where geoalchemy2 is not installed.

@chalmerlowe
chalmerlowe marked this pull request as ready for review November 4, 2026 16:15
@chalmerlowe
chalmerlowe requested review from a team as code owners November 4, 2026 16:15
@chalmerlowe
chalmerlowe requested review from sycai and removed request for a team November 4, 2026 16:15
@parthea parthea self-assigned this Nov 4, 2026
@parthea
parthea merged commit edc4372 into main Nov 4, 2026
54 checks passed
@parthea
parthea deleted the fix/sqlalchemy-bigquery-geoalchemy2-0.21 branch November 4, 2026 17:32
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.

2 participants