sqlalchemy-bigquery 1.17.3__tar.gz → 1.18.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. {sqlalchemy_bigquery-1.17.3/sqlalchemy_bigquery.egg-info → sqlalchemy_bigquery-1.18.0}/PKG-INFO +27 -10
  2. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/README.rst +20 -4
  3. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/setup.py +1 -0
  4. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/__init__.py +3 -1
  5. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/base.py +38 -0
  6. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/geography.py +27 -0
  7. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/version.py +1 -1
  8. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0/sqlalchemy_bigquery.egg-info}/PKG-INFO +27 -10
  9. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py +18 -0
  10. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_compiler.py +84 -0
  11. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_geography.py +40 -2
  12. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/AUTHORS +0 -0
  13. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/LICENSE +0 -0
  14. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/MANIFEST.in +0 -0
  15. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/pyproject.toml +0 -0
  16. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/setup.cfg +0 -0
  17. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/_helpers.py +0 -0
  18. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/_struct.py +0 -0
  19. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/_types.py +0 -0
  20. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/parse_url.py +0 -0
  21. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/provision.py +0 -0
  22. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery/requirements.py +0 -0
  23. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery.egg-info/SOURCES.txt +0 -0
  24. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery.egg-info/dependency_links.txt +0 -0
  25. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery.egg-info/entry_points.txt +0 -0
  26. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery.egg-info/requires.txt +4 -4
  27. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/sqlalchemy_bigquery.egg-info/top_level.txt +0 -0
  28. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/__init__.py +0 -0
  29. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/conftest.py +0 -0
  30. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/sqlalchemy_dialect_compliance/README.rst +0 -0
  31. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/sqlalchemy_dialect_compliance/conftest.py +0 -0
  32. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/__init__.py +0 -0
  33. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/conftest.py +0 -0
  34. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/data/sample.json +0 -0
  35. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/data/sample_one_row.json +0 -0
  36. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/data/schema.json +0 -0
  37. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test__struct.py +0 -0
  38. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test_alembic.py +0 -0
  39. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test_geography.py +0 -0
  40. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test_helpers.py +0 -0
  41. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test_sqlalchemy_bigquery.py +0 -0
  42. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/system/test_sqlalchemy_bigquery_remote.py +0 -0
  43. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/__init__.py +0 -0
  44. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/conftest.py +0 -0
  45. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/fauxdbi.py +0 -0
  46. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test__struct.py +0 -0
  47. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test__types.py +0 -0
  48. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_alembic.py +0 -0
  49. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_catalog_functions.py +0 -0
  50. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_comments.py +0 -0
  51. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_compliance.py +0 -0
  52. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_dialect_types.py +0 -0
  53. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_engine.py +0 -0
  54. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_helpers.py +0 -0
  55. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_like_reescape.py +0 -0
  56. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_parse_url.py +0 -0
  57. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_provision.py +0 -0
  58. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_select.py +0 -0
  59. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_sqlalchemy_bigquery.py +0 -0
  60. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_table_options.py +0 -0
  61. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_version.py +0 -0
  62. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/tests/unit/test_view.py +0 -0
  63. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/__init__.py +0 -0
  64. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/py.typed +0 -0
  65. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/sqlalchemy/AUTHORS +0 -0
  66. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/sqlalchemy/LICENSE +0 -0
  67. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/sqlalchemy/__init__.py +0 -0
  68. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/sqlalchemy/postgresql/__init__.py +0 -0
  69. {sqlalchemy_bigquery-1.17.3 → sqlalchemy_bigquery-1.18.0}/third_party/sqlalchemy_bigquery_vendored/sqlalchemy/postgresql/base.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlalchemy-bigquery
3
- Version: 1.17.3
3
+ Version: 1.18.0
4
4
  Summary: SQLAlchemy dialect for BigQuery
5
5
  Home-page: https://github.com/googleapis/google-cloud-python/tree/main/packages/sqlalchemy-bigquery
6
6
  Author: The Sqlalchemy-Bigquery Authors
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3.15
20
21
  Classifier: Operating System :: OS Independent
21
22
  Classifier: Topic :: Database :: Front-Ends
22
23
  Obsoletes: pybigquery
@@ -43,15 +44,15 @@ Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "bqstorage"
43
44
  Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "bqstorage"
44
45
  Requires-Dist: pyarrow>=6.0.0; extra == "bqstorage"
45
46
  Provides-Extra: all
46
- Requires-Dist: packaging; extra == "all"
47
- Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "all"
48
47
  Requires-Dist: pyarrow>=6.0.0; extra == "all"
49
- Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "all"
48
+ Requires-Dist: GeoAlchemy2; extra == "all"
50
49
  Requires-Dist: alembic; extra == "all"
51
- Requires-Dist: pytz; extra == "all"
52
50
  Requires-Dist: google-cloud-bigquery-storage<3.0.0,>=2.29.0; extra == "all"
53
- Requires-Dist: GeoAlchemy2; extra == "all"
51
+ Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "all"
52
+ Requires-Dist: packaging; extra == "all"
53
+ Requires-Dist: pytz; extra == "all"
54
54
  Requires-Dist: shapely; extra == "all"
55
+ Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "all"
55
56
  Dynamic: author
56
57
  Dynamic: author-email
57
58
  Dynamic: classifier
@@ -153,7 +154,7 @@ Installations when processing large datasets
153
154
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154
155
 
155
156
  When handling large datasets, you may see speed increases by also installing the
156
- `bqstorage` dependencies. See the instructions above about creating a virtual
157
+ `bqstorage` dependencies. See the instructions above about creating a virtual
157
158
  environment and then install `sqlalchemy-bigquery` using the `bqstorage` extras:
158
159
 
159
160
  .. code-block:: console
@@ -186,7 +187,7 @@ Project
186
187
  Authentication
187
188
  ^^^^^^^^^^^^^^
188
189
 
189
- Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
190
+ Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
190
191
 
191
192
  Alternatively, you can choose either of the following approaches:
192
193
 
@@ -200,10 +201,10 @@ Alternatively, you can choose either of the following approaches:
200
201
  * pass the credentials in ``create_engine()`` as a Python dictionary using the ``credentials_info`` parameter:
201
202
 
202
203
  .. code-block:: python
203
-
204
+
204
205
  # provide credentials as a Python dictionary
205
206
  credentials_info = {
206
- "type": "service_account",
207
+ "type": "service_account",
207
208
  "project_id": "your-service-account-project-id"
208
209
  }
209
210
  engine = create_engine('bigquery://', credentials_info=credentials_info)
@@ -420,6 +421,22 @@ To create an integer-range partitioned table
420
421
  )
421
422
 
422
423
 
424
+ Casting with ``SAFE_CAST``
425
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
426
+
427
+ To generate a BigQuery ``SAFE_CAST`` expression (which returns ``NULL`` instead
428
+ of raising an error when a cast fails), use ``safe_cast`` from
429
+ ``sqlalchemy_bigquery`` (or ``sqlalchemy.try_cast`` on SQLAlchemy 2.0+):
430
+
431
+ .. code-block:: python
432
+
433
+ from sqlalchemy import Integer, select
434
+ from sqlalchemy_bigquery import safe_cast
435
+
436
+ stmt = select(safe_cast(table.c.string_col, Integer))
437
+
438
+
439
+
423
440
  Threading and Multiprocessing
424
441
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
425
442
 
@@ -84,7 +84,7 @@ Installations when processing large datasets
84
84
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85
85
 
86
86
  When handling large datasets, you may see speed increases by also installing the
87
- `bqstorage` dependencies. See the instructions above about creating a virtual
87
+ `bqstorage` dependencies. See the instructions above about creating a virtual
88
88
  environment and then install `sqlalchemy-bigquery` using the `bqstorage` extras:
89
89
 
90
90
  .. code-block:: console
@@ -117,7 +117,7 @@ Project
117
117
  Authentication
118
118
  ^^^^^^^^^^^^^^
119
119
 
120
- Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
120
+ Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
121
121
 
122
122
  Alternatively, you can choose either of the following approaches:
123
123
 
@@ -131,10 +131,10 @@ Alternatively, you can choose either of the following approaches:
131
131
  * pass the credentials in ``create_engine()`` as a Python dictionary using the ``credentials_info`` parameter:
132
132
 
133
133
  .. code-block:: python
134
-
134
+
135
135
  # provide credentials as a Python dictionary
136
136
  credentials_info = {
137
- "type": "service_account",
137
+ "type": "service_account",
138
138
  "project_id": "your-service-account-project-id"
139
139
  }
140
140
  engine = create_engine('bigquery://', credentials_info=credentials_info)
@@ -351,6 +351,22 @@ To create an integer-range partitioned table
351
351
  )
352
352
 
353
353
 
354
+ Casting with ``SAFE_CAST``
355
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
356
+
357
+ To generate a BigQuery ``SAFE_CAST`` expression (which returns ``NULL`` instead
358
+ of raising an error when a cast fails), use ``safe_cast`` from
359
+ ``sqlalchemy_bigquery`` (or ``sqlalchemy.try_cast`` on SQLAlchemy 2.0+):
360
+
361
+ .. code-block:: python
362
+
363
+ from sqlalchemy import Integer, select
364
+ from sqlalchemy_bigquery import safe_cast
365
+
366
+ stmt = select(safe_cast(table.c.string_col, Integer))
367
+
368
+
369
+
354
370
  Threading and Multiprocessing
355
371
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356
372
 
@@ -105,6 +105,7 @@ setup(
105
105
  "Programming Language :: Python :: 3.12",
106
106
  "Programming Language :: Python :: 3.13",
107
107
  "Programming Language :: Python :: 3.14",
108
+ "Programming Language :: Python :: 3.15",
108
109
  "Operating System :: OS Independent",
109
110
  "Topic :: Database :: Front-Ends",
110
111
  ],
@@ -42,7 +42,7 @@ from ._types import (
42
42
  TIME,
43
43
  TIMESTAMP,
44
44
  )
45
- from .base import BigQueryDialect, dialect
45
+ from .base import BigQueryDialect, SafeCast, dialect, safe_cast
46
46
  from .version import __version__
47
47
 
48
48
  # Now that support for Python 3.7, 3.8 and 3.9 has been removed, we don't expect the
@@ -77,8 +77,10 @@ __all__ = [
77
77
  "RECORD",
78
78
  "STRING",
79
79
  "STRUCT",
80
+ "SafeCast",
80
81
  "TIME",
81
82
  "TIMESTAMP",
83
+ "safe_cast",
82
84
  ]
83
85
 
84
86
  try:
@@ -25,6 +25,7 @@ import random
25
25
  import re
26
26
  import uuid
27
27
  from decimal import Decimal
28
+ from typing import Any
28
29
 
29
30
  import google.api_core.exceptions
30
31
  import packaging.version
@@ -587,6 +588,17 @@ class BigQueryCompiler(vendored_postgresql.PGCompiler, SQLCompiler):
587
588
  def visit_mod_binary(self, binary, operator, **kw):
588
589
  return f"MOD({self.process(binary.left, **kw)}, {self.process(binary.right, **kw)})"
589
590
 
591
+ def visit_now_func(self, fn, **kw):
592
+ # BigQuery has no NOW() function, use CURRENT_TIMESTAMP instead.
593
+ return "CURRENT_TIMESTAMP"
594
+
595
+ def visit_try_cast(self, cast: elements.Cast, **kwargs: Any) -> str:
596
+ """Compile a ``SAFE_CAST`` / ``try_cast`` expression for BigQuery."""
597
+ return "SAFE_CAST(%s AS %s)" % (
598
+ self.process(cast.clause, **kwargs),
599
+ self.process(cast.typeclause, **kwargs),
600
+ )
601
+
590
602
 
591
603
  class BigQueryTypeCompiler(GenericTypeCompiler):
592
604
  def visit_INTEGER(self, type_, **kw):
@@ -1382,6 +1394,32 @@ class BigQueryDialect(DefaultDialect):
1382
1394
  return view.view_query
1383
1395
 
1384
1396
 
1397
+ _BaseTryCast = getattr(elements, "TryCast", elements.Cast)
1398
+
1399
+
1400
+ class SafeCast(_BaseTryCast):
1401
+ """Represent a BigQuery ``SAFE_CAST`` expression."""
1402
+
1403
+ __visit_name__ = "try_cast"
1404
+ inherit_cache = True
1405
+
1406
+
1407
+ def safe_cast(expression: Any, type_: Any) -> SafeCast:
1408
+ """Produce a BigQuery ``SAFE_CAST`` expression.
1409
+
1410
+ ``SAFE_CAST`` is identical to ``CAST``, except that it returns ``NULL``
1411
+ instead of raising an error when a cast fails.
1412
+
1413
+ Args:
1414
+ expression: A SQL expression, column, or literal to be cast.
1415
+ type_: The target SQLAlchemy ``TypeEngine`` class or instance.
1416
+
1417
+ Returns:
1418
+ A ``SafeCast`` clause element that compiles to ``SAFE_CAST(...)``.
1419
+ """
1420
+ return SafeCast(expression, type_)
1421
+
1422
+
1385
1423
  class unnest(sqlalchemy.sql.functions.GenericFunction):
1386
1424
  def __init__(self, *args, **kwargs):
1387
1425
  expr = kwargs.pop("expr", None)
@@ -180,6 +180,17 @@ def _fixup_st_arguments(element, compiler, **kw):
180
180
 
181
181
  This is not a user-facing function.
182
182
  """
183
+ # GeoAlchemy2 >= 0.21.0 wraps raw string arguments in an _UntypedString(NullType)
184
+ # bindparam for PostgreSQL compatibility. Restore String() for string bindparams
185
+ # so BigQueryCompiler.visit_bindparam emits typed %(...:STRING)s placeholders.
186
+ for argument in element.clauses.clauses:
187
+ if (
188
+ isinstance(argument, BindParameter)
189
+ and isinstance(argument.type, sqlalchemy.types.NullType)
190
+ and isinstance(argument.value, str)
191
+ ):
192
+ argument.type = sqlalchemy.types.String()
193
+
183
194
  argument_types = _argument_types.get(element.name.lower())
184
195
  if argument_types:
185
196
  for argument_type, argument in zip(argument_types, element.clauses.clauses):
@@ -191,6 +202,22 @@ def _fixup_st_arguments(element, compiler, **kw):
191
202
  return compiler.visit_function(element, **kw)
192
203
 
193
204
 
205
+ def _get_all_subclasses(cls):
206
+ subclasses = set(cls.__subclasses__())
207
+ for sub in list(subclasses):
208
+ subclasses.update(_get_all_subclasses(sub))
209
+ return subclasses
210
+
211
+
212
+ # GeoAlchemy2 registers dialect-specific @compiles handlers (e.g. for "mssql")
213
+ # on several GenericFunction subclasses such as ST_GeogFromText, which gives
214
+ # those subclasses their own _compiler_dispatcher that shadows GenericFunction's.
215
+ # Register _fixup_st_arguments on each subclass so the "bigquery" compiler
216
+ # hook is invoked for all spatial functions.
217
+ for _cls in _get_all_subclasses(geoalchemy2.functions.GenericFunction):
218
+ sqlalchemy.ext.compiler.compiles(_cls, "bigquery")(_fixup_st_arguments)
219
+
220
+
194
221
  _argument_types = dict(
195
222
  st_area=(GEOGRAPHY,),
196
223
  st_asbinary=(GEOGRAPHY,),
@@ -17,4 +17,4 @@
17
17
  # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
18
  # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
19
 
20
- __version__ = "1.17.3"
20
+ __version__ = "1.18.0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: sqlalchemy-bigquery
3
- Version: 1.17.3
3
+ Version: 1.18.0
4
4
  Summary: SQLAlchemy dialect for BigQuery
5
5
  Home-page: https://github.com/googleapis/google-cloud-python/tree/main/packages/sqlalchemy-bigquery
6
6
  Author: The Sqlalchemy-Bigquery Authors
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Classifier: Programming Language :: Python :: 3.14
20
+ Classifier: Programming Language :: Python :: 3.15
20
21
  Classifier: Operating System :: OS Independent
21
22
  Classifier: Topic :: Database :: Front-Ends
22
23
  Obsoletes: pybigquery
@@ -43,15 +44,15 @@ Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "bqstorage"
43
44
  Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "bqstorage"
44
45
  Requires-Dist: pyarrow>=6.0.0; extra == "bqstorage"
45
46
  Provides-Extra: all
46
- Requires-Dist: packaging; extra == "all"
47
- Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "all"
48
47
  Requires-Dist: pyarrow>=6.0.0; extra == "all"
49
- Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "all"
48
+ Requires-Dist: GeoAlchemy2; extra == "all"
50
49
  Requires-Dist: alembic; extra == "all"
51
- Requires-Dist: pytz; extra == "all"
52
50
  Requires-Dist: google-cloud-bigquery-storage<3.0.0,>=2.29.0; extra == "all"
53
- Requires-Dist: GeoAlchemy2; extra == "all"
51
+ Requires-Dist: grpcio<2.0.0,>=1.59.0; extra == "all"
52
+ Requires-Dist: packaging; extra == "all"
53
+ Requires-Dist: pytz; extra == "all"
54
54
  Requires-Dist: shapely; extra == "all"
55
+ Requires-Dist: grpcio<2.0.0,>=1.75.1; python_version >= "3.14" and extra == "all"
55
56
  Dynamic: author
56
57
  Dynamic: author-email
57
58
  Dynamic: classifier
@@ -153,7 +154,7 @@ Installations when processing large datasets
153
154
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
154
155
 
155
156
  When handling large datasets, you may see speed increases by also installing the
156
- `bqstorage` dependencies. See the instructions above about creating a virtual
157
+ `bqstorage` dependencies. See the instructions above about creating a virtual
157
158
  environment and then install `sqlalchemy-bigquery` using the `bqstorage` extras:
158
159
 
159
160
  .. code-block:: console
@@ -186,7 +187,7 @@ Project
186
187
  Authentication
187
188
  ^^^^^^^^^^^^^^
188
189
 
189
- Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
190
+ Follow the `Google Cloud library guide <https://google-cloud-python.readthedocs.io/en/latest/core/auth.html>`_ for authentication.
190
191
 
191
192
  Alternatively, you can choose either of the following approaches:
192
193
 
@@ -200,10 +201,10 @@ Alternatively, you can choose either of the following approaches:
200
201
  * pass the credentials in ``create_engine()`` as a Python dictionary using the ``credentials_info`` parameter:
201
202
 
202
203
  .. code-block:: python
203
-
204
+
204
205
  # provide credentials as a Python dictionary
205
206
  credentials_info = {
206
- "type": "service_account",
207
+ "type": "service_account",
207
208
  "project_id": "your-service-account-project-id"
208
209
  }
209
210
  engine = create_engine('bigquery://', credentials_info=credentials_info)
@@ -420,6 +421,22 @@ To create an integer-range partitioned table
420
421
  )
421
422
 
422
423
 
424
+ Casting with ``SAFE_CAST``
425
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^
426
+
427
+ To generate a BigQuery ``SAFE_CAST`` expression (which returns ``NULL`` instead
428
+ of raising an error when a cast fails), use ``safe_cast`` from
429
+ ``sqlalchemy_bigquery`` (or ``sqlalchemy.try_cast`` on SQLAlchemy 2.0+):
430
+
431
+ .. code-block:: python
432
+
433
+ from sqlalchemy import Integer, select
434
+ from sqlalchemy_bigquery import safe_cast
435
+
436
+ stmt = select(safe_cast(table.c.string_col, Integer))
437
+
438
+
439
+
423
440
  Threading and Multiprocessing
424
441
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
425
442
 
@@ -271,6 +271,24 @@ if packaging.version.parse(sqlalchemy.__version__) >= packaging.version.parse("2
271
271
  def test_floordiv_integer_bound(self):
272
272
  pass
273
273
 
274
+ @pytest.mark.skip(
275
+ "Skipped due to BigQuery Standard SQL limitations with extreme decimal/numeric inputs:\n"
276
+ "1. 38-Digit Integer Literal Overflows: BigQuery's SQL parser lexically evaluates "
277
+ "unquoted digit sequences without a decimal point as INT64 *before* evaluating "
278
+ "CAST() wrappers. Values like 30000000000000000000000000000000000000 overflow "
279
+ "signed 64-bit integer limits (max: 9,223,372,036,854,775,807) and raise a 400 "
280
+ "'Invalid integer literal' error unless quoted as strings.\n"
281
+ "2. Untyped Tiny Divisor Underflow: Standard NUMERIC in BigQuery is capped at 9 "
282
+ "decimal places (whereas BIGNUMERIC supports 38). When untyped functions (like "
283
+ "nullif) cast a 13-decimal divisor (0.00000000000012345679) to NUMERIC, it "
284
+ "rounds down to 0 and triggers a 400 division-by-zero error.\n"
285
+ "3. Untyped Precision Loss: Coercing high-precision operands to standard NUMERIC "
286
+ "during untyped division causes trailing precision mismatches "
287
+ "(e.g., returning 7.00000000447361540043026119825490002512 instead of 7)."
288
+ )
289
+ def test_truediv_divisor(self):
290
+ pass
291
+
274
292
  class SimpleUpdateDeleteTest(_SimpleUpdateDeleteTest):
275
293
  """The base tests fail if operations return rows for some reason."""
276
294
 
@@ -21,6 +21,8 @@ import pytest
21
21
  import sqlalchemy.exc
22
22
  from sqlalchemy.sql.functions import cube, grouping_sets, rollup
23
23
 
24
+ from sqlalchemy_bigquery import SafeCast, safe_cast
25
+
24
26
  from .conftest import setup_table, sqlalchemy_2_0_or_higher, sqlalchemy_before_2_0
25
27
 
26
28
 
@@ -487,3 +489,85 @@ def test_float_literal_pyformat_bindparam():
487
489
  stmt = sqlalchemy.select(sqlalchemy.literal(15.7563))
488
490
  compiled = stmt.compile(dialect=dialect)
489
491
  assert "%(param_1:FLOAT64)s" in str(compiled)
492
+
493
+
494
+ def test_now_compiles_to_current_timestamp(faux_conn):
495
+ # BigQuery has no NOW() function.
496
+ stmt = sqlalchemy.select(sqlalchemy.func.now().label("ts"))
497
+ found_sql = stmt.compile(faux_conn).string
498
+ assert found_sql == "SELECT CURRENT_TIMESTAMP AS `ts`"
499
+
500
+
501
+ SAFE_CAST_TYPE_CASES = [
502
+ pytest.param(sqlalchemy.Integer, "INT64", id="integer_class"),
503
+ pytest.param(sqlalchemy.Integer(), "INT64", id="integer_instance"),
504
+ pytest.param(sqlalchemy.String, "STRING", id="string"),
505
+ pytest.param(sqlalchemy.Float, "FLOAT64", id="float"),
506
+ pytest.param(sqlalchemy.Boolean, "BOOL", id="boolean"),
507
+ pytest.param(sqlalchemy.Date, "DATE", id="date"),
508
+ pytest.param(sqlalchemy.Numeric, "NUMERIC", id="numeric"),
509
+ ]
510
+
511
+
512
+ @pytest.mark.parametrize("target_type,expected_bq_type", SAFE_CAST_TYPE_CASES)
513
+ def test_safe_cast_column(faux_conn, table, target_type, expected_bq_type):
514
+ expr = safe_cast(table.c.foo, target_type)
515
+ assert isinstance(expr, SafeCast)
516
+ assert expr.inherit_cache is True
517
+ expected_type_cls = (
518
+ target_type if isinstance(target_type, type) else type(target_type)
519
+ )
520
+ assert isinstance(expr.type, expected_type_cls)
521
+
522
+ stmt = sqlalchemy.select(expr.label("casted"))
523
+ found_sql = stmt.compile(faux_conn).string
524
+ expected_sql = (
525
+ f"SELECT SAFE_CAST(`table1`.`foo` AS {expected_bq_type}) AS `casted` \n"
526
+ "FROM `table1`"
527
+ )
528
+ assert found_sql == expected_sql
529
+
530
+
531
+ def test_safe_cast_literal(faux_conn):
532
+ stmt = sqlalchemy.select(safe_cast(sqlalchemy.literal("123"), sqlalchemy.Integer))
533
+ found_sql = stmt.compile(faux_conn, compile_kwargs={"literal_binds": True}).string
534
+ assert found_sql == "SELECT SAFE_CAST('123' AS INT64) AS `anon_1`"
535
+
536
+
537
+ def test_safe_cast_null(faux_conn):
538
+ stmt = sqlalchemy.select(
539
+ safe_cast(sqlalchemy.null(), sqlalchemy.Integer).label("val")
540
+ )
541
+ found_sql = stmt.compile(faux_conn).string
542
+ assert found_sql == "SELECT SAFE_CAST(NULL AS INT64) AS `val`"
543
+
544
+
545
+ def test_safe_cast_nested(faux_conn, table):
546
+ inner = safe_cast(table.c.foo, sqlalchemy.String)
547
+ outer = safe_cast(inner, sqlalchemy.Integer).label("casted")
548
+ stmt = sqlalchemy.select(outer)
549
+ found_sql = stmt.compile(faux_conn).string
550
+ expected_sql = (
551
+ "SELECT SAFE_CAST(SAFE_CAST(`table1`.`foo` AS STRING) AS INT64) AS `casted` \n"
552
+ "FROM `table1`"
553
+ )
554
+ assert found_sql == expected_sql
555
+
556
+
557
+ @pytest.mark.skipif(
558
+ not hasattr(sqlalchemy, "try_cast"),
559
+ reason="sqlalchemy.try_cast requires SQLAlchemy 2.0.14 or higher",
560
+ )
561
+ @pytest.mark.parametrize("target_type,expected_bq_type", SAFE_CAST_TYPE_CASES)
562
+ def test_sqlalchemy_try_cast_compiles_to_safe_cast(
563
+ faux_conn, table, target_type, expected_bq_type
564
+ ):
565
+ stmt = sqlalchemy.select(
566
+ sqlalchemy.try_cast(table.c.foo, target_type).label("casted")
567
+ )
568
+ found_sql = stmt.compile(faux_conn).string
569
+ expected_sql = (
570
+ f"SELECT SAFE_CAST(`table1`.`foo` AS {expected_bq_type}) AS `casted` \n"
571
+ "FROM `table1`"
572
+ )
573
+ assert found_sql == expected_sql
@@ -184,6 +184,7 @@ def test_calling_st_functions_that_dont_take_geographies(faux_conn, last_query):
184
184
 
185
185
 
186
186
  def test_fixup_st_arguments():
187
+ import sqlalchemy.types
187
188
  from geoalchemy2.functions import GenericFunction, ST_Area
188
189
  from sqlalchemy.sql.elements import BindParameter
189
190
 
@@ -203,9 +204,46 @@ def test_fixup_st_arguments():
203
204
  func_element2 = ST_Area(BindParameter("param", "point(0 0)", type_=GEOGRAPHY()))
204
205
  _fixup_st_arguments(func_element2, DummyCompiler())
205
206
 
206
- # Case 3: function without specified argument types
207
+ # Case 3: function without specified argument types coerces NullType str to String
207
208
  class ST_Unknown(GenericFunction):
208
209
  name = "ST_Unknown"
209
210
 
210
- func_element3 = ST_Unknown(BindParameter("param", "point(0 0)"))
211
+ func_element3 = ST_Unknown(
212
+ BindParameter("param", "point(0 0)", type_=sqlalchemy.types.NullType())
213
+ )
211
214
  _fixup_st_arguments(func_element3, DummyCompiler())
215
+ assert isinstance(func_element3.clauses.clauses[0].type, sqlalchemy.types.String)
216
+
217
+ # Case 4: NullType with non-str value (e.g. None) remains NullType
218
+ func_element4 = ST_Unknown(
219
+ BindParameter("param", None, type_=sqlalchemy.types.NullType())
220
+ )
221
+ _fixup_st_arguments(func_element4, DummyCompiler())
222
+ assert isinstance(func_element4.clauses.clauses[0].type, sqlalchemy.types.NullType)
223
+
224
+
225
+ def test_nested_generic_function_subclasses_register_compiler():
226
+ """Verify that multi-tier GenericFunction subclasses register the bigquery compiler handler."""
227
+ from geoalchemy2.functions import GenericFunction
228
+
229
+ from sqlalchemy_bigquery.geography import _get_all_subclasses
230
+
231
+ # Intermediate base class inheriting directly from GenericFunction
232
+ class ST_BaseFunction(GenericFunction):
233
+ name = "ST_BaseFunction"
234
+
235
+ # Multi-tier / nested subclass inheriting through the intermediate base
236
+ class ST_NestedFunction(ST_BaseFunction):
237
+ name = "ST_NestedFunction"
238
+
239
+ # Step 1: Ensure the recursive discovery function resolves transitive descendants
240
+ discovered_classes = _get_all_subclasses(GenericFunction)
241
+ assert ST_NestedFunction in discovered_classes, (
242
+ "ST_NestedFunction must be discovered by recursive subclass collection."
243
+ )
244
+
245
+ # Step 2: Ensure the BigQuery compiler dispatcher hook is registered on the nested class
246
+ dispatcher = getattr(ST_NestedFunction, "_compiler_dispatcher", None)
247
+ assert dispatcher is not None and "bigquery" in dispatcher.specs, (
248
+ "Nested GenericFunction subclasses must have the 'bigquery' compiler handler registered."
249
+ )
@@ -8,13 +8,13 @@ sqlalchemy<3.0.0,>=1.4.16
8
8
  alembic
9
9
 
10
10
  [all]
11
- packaging
12
11
  pyarrow>=6.0.0
13
- grpcio<2.0.0,>=1.59.0
12
+ GeoAlchemy2
14
13
  alembic
15
- pytz
16
14
  google-cloud-bigquery-storage<3.0.0,>=2.29.0
17
- GeoAlchemy2
15
+ grpcio<2.0.0,>=1.59.0
16
+ packaging
17
+ pytz
18
18
  shapely
19
19
 
20
20
  [all:python_version >= "3.14"]