sqlalchemy-bigquery 1.8.0__tar.gz → 1.10.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 (57) hide show
  1. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/PKG-INFO +82 -8
  2. sqlalchemy-bigquery-1.8.0/sqlalchemy_bigquery.egg-info/PKG-INFO → sqlalchemy-bigquery-1.10.0/README.rst +54 -35
  3. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/setup.py +2 -2
  4. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/base.py +254 -35
  5. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/version.py +1 -1
  6. sqlalchemy-bigquery-1.8.0/README.rst → sqlalchemy-bigquery-1.10.0/sqlalchemy_bigquery.egg-info/PKG-INFO +109 -3
  7. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery.egg-info/SOURCES.txt +1 -0
  8. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/sqlalchemy_dialect_compliance/test_dialect_compliance.py +0 -1
  9. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/test_alembic.py +7 -10
  10. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/test_sqlalchemy_bigquery.py +11 -11
  11. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/conftest.py +7 -0
  12. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/fauxdbi.py +0 -1
  13. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_catalog_functions.py +1 -12
  14. sqlalchemy-bigquery-1.10.0/tests/unit/test_compiler.py +280 -0
  15. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_like_reescape.py +0 -2
  16. sqlalchemy-bigquery-1.10.0/tests/unit/test_table_options.py +474 -0
  17. sqlalchemy-bigquery-1.8.0/tests/unit/test_compiler.py +0 -116
  18. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/AUTHORS +0 -0
  19. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/LICENSE +0 -0
  20. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/MANIFEST.in +0 -0
  21. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/setup.cfg +0 -0
  22. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/__init__.py +0 -0
  23. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/_helpers.py +0 -0
  24. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/_struct.py +0 -0
  25. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/_types.py +0 -0
  26. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/geography.py +0 -0
  27. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/parse_url.py +0 -0
  28. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery/requirements.py +0 -0
  29. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery.egg-info/dependency_links.txt +0 -0
  30. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery.egg-info/entry_points.txt +0 -0
  31. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery.egg-info/requires.txt +5 -5
  32. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/sqlalchemy_bigquery.egg-info/top_level.txt +0 -0
  33. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/__init__.py +0 -0
  34. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/conftest.py +0 -0
  35. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/sqlalchemy_dialect_compliance/README.rst +0 -0
  36. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/sqlalchemy_dialect_compliance/conftest.py +0 -0
  37. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/__init__.py +0 -0
  38. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/conftest.py +0 -0
  39. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/data/sample.json +0 -0
  40. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/data/sample_one_row.json +0 -0
  41. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/data/schema.json +0 -0
  42. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/test__struct.py +0 -0
  43. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/test_geography.py +0 -0
  44. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/system/test_helpers.py +0 -0
  45. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/__init__.py +0 -0
  46. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test__struct.py +0 -0
  47. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_comments.py +0 -0
  48. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_compliance.py +0 -0
  49. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_dialect_types.py +0 -0
  50. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_engine.py +0 -0
  51. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_geography.py +0 -0
  52. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_helpers.py +0 -0
  53. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_parse_url.py +0 -0
  54. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_select.py +0 -0
  55. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_sqlalchemy_bigquery.py +0 -0
  56. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_version.py +0 -0
  57. {sqlalchemy-bigquery-1.8.0 → sqlalchemy-bigquery-1.10.0}/tests/unit/test_view.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: sqlalchemy-bigquery
3
- Version: 1.8.0
3
+ Version: 1.10.0
4
4
  Summary: SQLAlchemy dialect for BigQuery
5
5
  Home-page: https://github.com/googleapis/python-bigquery-sqlalchemy
6
6
  Author: The Sqlalchemy-Bigquery Authors
@@ -12,23 +12,46 @@ Classifier: Intended Audience :: Developers
12
12
  Classifier: License :: OSI Approved :: MIT License
13
13
  Classifier: Programming Language :: Python
14
14
  Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.7
16
15
  Classifier: Programming Language :: Python :: 3.8
17
16
  Classifier: Programming Language :: Python :: 3.9
18
17
  Classifier: Programming Language :: Python :: 3.10
19
18
  Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
20
  Classifier: Operating System :: OS Independent
21
21
  Classifier: Topic :: Database :: Front-Ends
22
22
  Obsoletes: pybigquery
23
- Requires-Python: >=3.7, <3.12
23
+ Requires-Python: >=3.8, <3.13
24
24
  Description-Content-Type: text/x-rst
25
+ License-File: LICENSE
26
+ License-File: AUTHORS
27
+ Requires-Dist: google-api-core!=2.0.*,!=2.1.*,!=2.2.*,!=2.3.0,<3.0.0dev,>=1.31.5
28
+ Requires-Dist: google-auth<3.0.0dev,>=1.25.0
29
+ Requires-Dist: google-cloud-bigquery<4.0.0dev,>=2.25.2
30
+ Requires-Dist: packaging
31
+ Requires-Dist: sqlalchemy<2.0.0dev,>=1.2.0
25
32
  Provides-Extra: geography
33
+ Requires-Dist: GeoAlchemy2; extra == "geography"
34
+ Requires-Dist: shapely; extra == "geography"
26
35
  Provides-Extra: alembic
36
+ Requires-Dist: alembic; extra == "alembic"
27
37
  Provides-Extra: tests
38
+ Requires-Dist: packaging; extra == "tests"
39
+ Requires-Dist: pytz; extra == "tests"
28
40
  Provides-Extra: bqstorage
41
+ Requires-Dist: google-cloud-bigquery-storage<3.0.0dev,>=2.0.0; extra == "bqstorage"
42
+ Requires-Dist: grpcio<2.0dev,>=1.47.0; extra == "bqstorage"
43
+ Requires-Dist: grpcio<2.0dev,>=1.49.1; python_version >= "3.11" and extra == "bqstorage"
44
+ Requires-Dist: pyarrow>=3.0.0; extra == "bqstorage"
29
45
  Provides-Extra: all
30
- License-File: LICENSE
31
- License-File: AUTHORS
46
+ Requires-Dist: alembic; extra == "all"
47
+ Requires-Dist: google-cloud-bigquery-storage<3.0.0dev,>=2.0.0; extra == "all"
48
+ Requires-Dist: shapely; extra == "all"
49
+ Requires-Dist: pyarrow>=3.0.0; extra == "all"
50
+ Requires-Dist: grpcio<2.0dev,>=1.49.1; python_version >= "3.11" and extra == "all"
51
+ Requires-Dist: packaging; extra == "all"
52
+ Requires-Dist: pytz; extra == "all"
53
+ Requires-Dist: GeoAlchemy2; extra == "all"
54
+ Requires-Dist: grpcio<2.0dev,>=1.47.0; extra == "all"
32
55
 
33
56
  SQLAlchemy Dialect for BigQuery
34
57
  ===============================
@@ -85,11 +108,11 @@ dependencies.
85
108
 
86
109
  Supported Python Versions
87
110
  ^^^^^^^^^^^^^^^^^^^^^^^^^
88
- Python >= 3.7
111
+ Python >= 3.8
89
112
 
90
113
  Unsupported Python Versions
91
114
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
92
- Python <= 3.6.
115
+ Python <= 3.7.
93
116
 
94
117
 
95
118
  Mac/Linux
@@ -324,7 +347,12 @@ To add metadata to a table:
324
347
 
325
348
  .. code-block:: python
326
349
 
327
- table = Table('mytable', ..., bigquery_description='my table description', bigquery_friendly_name='my table friendly name')
350
+ table = Table('mytable', ...,
351
+ bigquery_description='my table description',
352
+ bigquery_friendly_name='my table friendly name',
353
+ bigquery_default_rounding_mode="ROUND_HALF_EVEN",
354
+ bigquery_expiration_timestamp=datetime.datetime.fromisoformat("2038-01-01T00:00:00+00:00"),
355
+ )
328
356
 
329
357
  To add metadata to a column:
330
358
 
@@ -332,6 +360,52 @@ To add metadata to a column:
332
360
 
333
361
  Column('mycolumn', doc='my column description')
334
362
 
363
+ To create a clustered table:
364
+
365
+ .. code-block:: python
366
+
367
+ table = Table('mytable', ..., bigquery_clustering_fields=["a", "b", "c"])
368
+
369
+ To create a time-unit column-partitioned table:
370
+
371
+ .. code-block:: python
372
+
373
+ from google.cloud import bigquery
374
+
375
+ table = Table('mytable', ...,
376
+ bigquery_time_partitioning=bigquery.TimePartitioning(
377
+ field="mytimestamp",
378
+ type_="MONTH",
379
+ expiration_ms=1000 * 60 * 60 * 24 * 30 * 6, # 6 months
380
+ ),
381
+ bigquery_require_partition_filter=True,
382
+ )
383
+
384
+ To create an ingestion-time partitioned table:
385
+
386
+ .. code-block:: python
387
+
388
+ from google.cloud import bigquery
389
+
390
+ table = Table('mytable', ...,
391
+ bigquery_time_partitioning=bigquery.TimePartitioning(),
392
+ bigquery_require_partition_filter=True,
393
+ )
394
+
395
+ To create an integer-range partitioned table
396
+
397
+ .. code-block:: python
398
+
399
+ from google.cloud import bigquery
400
+
401
+ table = Table('mytable', ...,
402
+ bigquery_range_partitioning=bigquery.RangePartitioning(
403
+ field="zipcode",
404
+ range_=bigquery.PartitionRange(start=0, end=100000, interval=10),
405
+ ),
406
+ bigquery_require_partition_filter=True,
407
+ )
408
+
335
409
 
336
410
  Threading and Multiprocessing
337
411
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -1,35 +1,3 @@
1
- Metadata-Version: 2.1
2
- Name: sqlalchemy-bigquery
3
- Version: 1.8.0
4
- Summary: SQLAlchemy dialect for BigQuery
5
- Home-page: https://github.com/googleapis/python-bigquery-sqlalchemy
6
- Author: The Sqlalchemy-Bigquery Authors
7
- Author-email: googleapis-packages@google.com
8
- Keywords: bigquery,sqlalchemy
9
- Platform: Posix; MacOS X; Windows
10
- Classifier: Development Status :: 5 - Production/Stable
11
- Classifier: Intended Audience :: Developers
12
- Classifier: License :: OSI Approved :: MIT License
13
- Classifier: Programming Language :: Python
14
- Classifier: Programming Language :: Python :: 3
15
- Classifier: Programming Language :: Python :: 3.7
16
- Classifier: Programming Language :: Python :: 3.8
17
- Classifier: Programming Language :: Python :: 3.9
18
- Classifier: Programming Language :: Python :: 3.10
19
- Classifier: Programming Language :: Python :: 3.11
20
- Classifier: Operating System :: OS Independent
21
- Classifier: Topic :: Database :: Front-Ends
22
- Obsoletes: pybigquery
23
- Requires-Python: >=3.7, <3.12
24
- Description-Content-Type: text/x-rst
25
- Provides-Extra: geography
26
- Provides-Extra: alembic
27
- Provides-Extra: tests
28
- Provides-Extra: bqstorage
29
- Provides-Extra: all
30
- License-File: LICENSE
31
- License-File: AUTHORS
32
-
33
1
  SQLAlchemy Dialect for BigQuery
34
2
  ===============================
35
3
 
@@ -85,11 +53,11 @@ dependencies.
85
53
 
86
54
  Supported Python Versions
87
55
  ^^^^^^^^^^^^^^^^^^^^^^^^^
88
- Python >= 3.7
56
+ Python >= 3.8
89
57
 
90
58
  Unsupported Python Versions
91
59
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^
92
- Python <= 3.6.
60
+ Python <= 3.7.
93
61
 
94
62
 
95
63
  Mac/Linux
@@ -324,7 +292,12 @@ To add metadata to a table:
324
292
 
325
293
  .. code-block:: python
326
294
 
327
- table = Table('mytable', ..., bigquery_description='my table description', bigquery_friendly_name='my table friendly name')
295
+ table = Table('mytable', ...,
296
+ bigquery_description='my table description',
297
+ bigquery_friendly_name='my table friendly name',
298
+ bigquery_default_rounding_mode="ROUND_HALF_EVEN",
299
+ bigquery_expiration_timestamp=datetime.datetime.fromisoformat("2038-01-01T00:00:00+00:00"),
300
+ )
328
301
 
329
302
  To add metadata to a column:
330
303
 
@@ -332,6 +305,52 @@ To add metadata to a column:
332
305
 
333
306
  Column('mycolumn', doc='my column description')
334
307
 
308
+ To create a clustered table:
309
+
310
+ .. code-block:: python
311
+
312
+ table = Table('mytable', ..., bigquery_clustering_fields=["a", "b", "c"])
313
+
314
+ To create a time-unit column-partitioned table:
315
+
316
+ .. code-block:: python
317
+
318
+ from google.cloud import bigquery
319
+
320
+ table = Table('mytable', ...,
321
+ bigquery_time_partitioning=bigquery.TimePartitioning(
322
+ field="mytimestamp",
323
+ type_="MONTH",
324
+ expiration_ms=1000 * 60 * 60 * 24 * 30 * 6, # 6 months
325
+ ),
326
+ bigquery_require_partition_filter=True,
327
+ )
328
+
329
+ To create an ingestion-time partitioned table:
330
+
331
+ .. code-block:: python
332
+
333
+ from google.cloud import bigquery
334
+
335
+ table = Table('mytable', ...,
336
+ bigquery_time_partitioning=bigquery.TimePartitioning(),
337
+ bigquery_require_partition_filter=True,
338
+ )
339
+
340
+ To create an integer-range partitioned table
341
+
342
+ .. code-block:: python
343
+
344
+ from google.cloud import bigquery
345
+
346
+ table = Table('mytable', ...,
347
+ bigquery_range_partitioning=bigquery.RangePartitioning(
348
+ field="zipcode",
349
+ range_=bigquery.PartitionRange(start=0, end=100000, interval=10),
350
+ ),
351
+ bigquery_require_partition_filter=True,
352
+ )
353
+
335
354
 
336
355
  Threading and Multiprocessing
337
356
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -84,11 +84,11 @@ setup(
84
84
  "License :: OSI Approved :: MIT License",
85
85
  "Programming Language :: Python",
86
86
  "Programming Language :: Python :: 3",
87
- "Programming Language :: Python :: 3.7",
88
87
  "Programming Language :: Python :: 3.8",
89
88
  "Programming Language :: Python :: 3.9",
90
89
  "Programming Language :: Python :: 3.10",
91
90
  "Programming Language :: Python :: 3.11",
91
+ "Programming Language :: Python :: 3.12",
92
92
  "Operating System :: OS Independent",
93
93
  "Topic :: Database :: Front-Ends",
94
94
  ],
@@ -104,7 +104,7 @@ setup(
104
104
  "sqlalchemy>=1.2.0,<2.0.0dev",
105
105
  ],
106
106
  extras_require=extras,
107
- python_requires=">=3.7, <3.12",
107
+ python_requires=">=3.8, <3.13",
108
108
  tests_require=["packaging", "pytz"],
109
109
  entry_points={
110
110
  "sqlalchemy.dialects": ["bigquery = sqlalchemy_bigquery:BigQueryDialect"]
@@ -19,6 +19,7 @@
19
19
 
20
20
  """Integration between SQLAlchemy and BigQuery."""
21
21
 
22
+ import datetime
22
23
  from decimal import Decimal
23
24
  import random
24
25
  import operator
@@ -27,7 +28,11 @@ import uuid
27
28
  from google import auth
28
29
  import google.api_core.exceptions
29
30
  from google.cloud.bigquery import dbapi
30
- from google.cloud.bigquery.table import TableReference
31
+ from google.cloud.bigquery.table import (
32
+ RangePartitioning,
33
+ TableReference,
34
+ TimePartitioning,
35
+ )
31
36
  from google.api_core.exceptions import NotFound
32
37
  import packaging.version
33
38
  import sqlalchemy
@@ -35,7 +40,7 @@ import sqlalchemy.sql.expression
35
40
  import sqlalchemy.sql.functions
36
41
  import sqlalchemy.sql.sqltypes
37
42
  import sqlalchemy.sql.type_api
38
- from sqlalchemy.exc import NoSuchTableError
43
+ from sqlalchemy.exc import NoSuchTableError, NoSuchColumnError
39
44
  from sqlalchemy import util
40
45
  from sqlalchemy.ext.compiler import compiles
41
46
  from sqlalchemy.sql.compiler import (
@@ -183,7 +188,6 @@ class BigQueryExecutionContext(DefaultExecutionContext):
183
188
 
184
189
 
185
190
  class BigQueryCompiler(_struct.SQLCompiler, SQLCompiler):
186
-
187
191
  compound_keywords = SQLCompiler.compound_keywords.copy()
188
192
  compound_keywords[selectable.CompoundSelect.UNION] = "UNION DISTINCT"
189
193
  compound_keywords[selectable.CompoundSelect.UNION_ALL] = "UNION ALL"
@@ -270,6 +274,14 @@ class BigQueryCompiler(_struct.SQLCompiler, SQLCompiler):
270
274
  if table is not None:
271
275
  known_tables.add(table.name)
272
276
 
277
+ # If we have the table in the `from` of our parent, do not add the alias
278
+ # as this will add the table twice and cause an implicit JOIN for that
279
+ # table on itself
280
+ asfrom_froms = self.stack[-1].get("asfrom_froms", [])
281
+ for from_ in asfrom_froms:
282
+ if isinstance(from_, Table):
283
+ known_tables.add(from_.name)
284
+
273
285
  return known_tables
274
286
 
275
287
  def visit_column(
@@ -624,6 +636,12 @@ class BigQueryTypeCompiler(GenericTypeCompiler):
624
636
 
625
637
 
626
638
  class BigQueryDDLCompiler(DDLCompiler):
639
+ option_datatype_mapping = {
640
+ "friendly_name": str,
641
+ "expiration_timestamp": datetime.datetime,
642
+ "require_partition_filter": bool,
643
+ "default_rounding_mode": str,
644
+ }
627
645
 
628
646
  # BigQuery has no support for foreign keys.
629
647
  def visit_foreign_key_constraint(self, constraint):
@@ -648,26 +666,99 @@ class BigQueryDDLCompiler(DDLCompiler):
648
666
  return colspec
649
667
 
650
668
  def post_create_table(self, table):
669
+ """
670
+ Constructs additional SQL clauses for table creation in BigQuery.
671
+
672
+ This function processes the BigQuery dialect-specific options and generates SQL clauses for partitioning,
673
+ clustering, and other table options.
674
+
675
+ Args:
676
+ table (Table): The SQLAlchemy Table object for which the SQL is being generated.
677
+
678
+ Returns:
679
+ str: A string composed of SQL clauses for time partitioning, clustering, and other BigQuery specific
680
+ options, each separated by a newline. Returns an empty string if no such options are specified.
681
+
682
+ Raises:
683
+ TypeError: If the time_partitioning option is not a `TimePartitioning` object or if the clustering_fields option is not a list.
684
+ NoSuchColumnError: If any field specified in clustering_fields does not exist in the table.
685
+ """
686
+
651
687
  bq_opts = table.dialect_options["bigquery"]
652
- opts = []
653
688
 
654
- if ("description" in bq_opts) or table.comment:
655
- description = process_string_literal(
656
- bq_opts.get("description", table.comment)
689
+ options = {}
690
+ clauses = []
691
+
692
+ if (
693
+ bq_opts.get("time_partitioning") is not None
694
+ and bq_opts.get("range_partitioning") is not None
695
+ ):
696
+ raise ValueError(
697
+ "biquery_time_partitioning and bigquery_range_partitioning"
698
+ " dialect options are mutually exclusive."
699
+ )
700
+
701
+ if (time_partitioning := bq_opts.get("time_partitioning")) is not None:
702
+ self._raise_for_type(
703
+ "time_partitioning",
704
+ time_partitioning,
705
+ TimePartitioning,
657
706
  )
658
- opts.append(f"description={description}")
659
707
 
660
- if "friendly_name" in bq_opts:
661
- opts.append(
662
- "friendly_name={}".format(
663
- process_string_literal(bq_opts["friendly_name"])
708
+ if time_partitioning.expiration_ms:
709
+ _24hours = 1000 * 60 * 60 * 24
710
+ options["partition_expiration_days"] = (
711
+ time_partitioning.expiration_ms / _24hours
664
712
  )
713
+
714
+ partition_by_clause = self._process_time_partitioning(
715
+ table,
716
+ time_partitioning,
665
717
  )
666
718
 
667
- if opts:
668
- return "\nOPTIONS({})".format(", ".join(opts))
719
+ clauses.append(partition_by_clause)
669
720
 
670
- return ""
721
+ if (range_partitioning := bq_opts.get("range_partitioning")) is not None:
722
+ self._raise_for_type(
723
+ "range_partitioning",
724
+ range_partitioning,
725
+ RangePartitioning,
726
+ )
727
+
728
+ partition_by_clause = self._process_range_partitioning(
729
+ table,
730
+ range_partitioning,
731
+ )
732
+
733
+ clauses.append(partition_by_clause)
734
+
735
+ if (clustering_fields := bq_opts.get("clustering_fields")) is not None:
736
+ self._raise_for_type("clustering_fields", clustering_fields, list)
737
+
738
+ for field in clustering_fields:
739
+ if field not in table.c:
740
+ raise NoSuchColumnError(field)
741
+
742
+ clauses.append(f"CLUSTER BY {', '.join(clustering_fields)}")
743
+
744
+ if ("description" in bq_opts) or table.comment:
745
+ description = bq_opts.get("description", table.comment)
746
+ self._validate_option_value_type("description", description)
747
+ options["description"] = description
748
+
749
+ for option in self.option_datatype_mapping:
750
+ if option in bq_opts:
751
+ options[option] = bq_opts.get(option)
752
+
753
+ if options:
754
+ individual_option_statements = [
755
+ "{}={}".format(k, self._process_option_value(v))
756
+ for (k, v) in options.items()
757
+ if self._validate_option_value_type(k, v)
758
+ ]
759
+ clauses.append(f"OPTIONS({', '.join(individual_option_statements)})")
760
+
761
+ return " " + "\n".join(clauses)
671
762
 
672
763
  def visit_set_table_comment(self, create):
673
764
  table_name = self.preparer.format_table(create.element)
@@ -680,6 +771,152 @@ class BigQueryDDLCompiler(DDLCompiler):
680
771
  table_name = self.preparer.format_table(drop.element)
681
772
  return f"ALTER TABLE {table_name} SET OPTIONS(description=null)"
682
773
 
774
+ def _validate_option_value_type(self, option: str, value):
775
+ """
776
+ Validates the type of the given option value against the expected data type.
777
+
778
+ Args:
779
+ option (str): The name of the option to be validated.
780
+ value: The value of the dialect option whose type is to be checked. The type of this parameter
781
+ is dynamic and is verified against the expected type in `self.option_datatype_mapping`.
782
+
783
+ Returns:
784
+ bool: True if the type of the value matches the expected type, or if the option is not found in
785
+ `self.option_datatype_mapping`.
786
+
787
+ Raises:
788
+ TypeError: If the type of the provided value does not match the expected type as defined in
789
+ `self.option_datatype_mapping`.
790
+ """
791
+ if option in self.option_datatype_mapping:
792
+ self._raise_for_type(
793
+ option,
794
+ value,
795
+ self.option_datatype_mapping[option],
796
+ )
797
+
798
+ return True
799
+
800
+ def _raise_for_type(self, option, value, expected_type):
801
+ if type(value) is not expected_type:
802
+ raise TypeError(
803
+ f"bigquery_{option} dialect option accepts only {expected_type},"
804
+ f" provided {repr(value)}"
805
+ )
806
+
807
+ def _process_time_partitioning(
808
+ self, table: Table, time_partitioning: TimePartitioning
809
+ ):
810
+ """
811
+ Generates a SQL 'PARTITION BY' clause for partitioning a table by a date or timestamp.
812
+
813
+ Args:
814
+ - table (Table): The SQLAlchemy table object representing the BigQuery table to be partitioned.
815
+ - time_partitioning (TimePartitioning): The time partitioning details,
816
+ including the field to be used for partitioning.
817
+
818
+ Returns:
819
+ - str: A SQL 'PARTITION BY' clause that uses either TIMESTAMP_TRUNC or DATE_TRUNC to
820
+ partition data on the specified field.
821
+
822
+ Example:
823
+ - Given a table with a TIMESTAMP type column 'event_timestamp' and setting
824
+ 'time_partitioning.field' to 'event_timestamp', the function returns
825
+ "PARTITION BY TIMESTAMP_TRUNC(event_timestamp, DAY)".
826
+ """
827
+ field = "_PARTITIONDATE"
828
+ trunc_fn = "DATE_TRUNC"
829
+
830
+ if time_partitioning.field is not None:
831
+ field = time_partitioning.field
832
+ if isinstance(
833
+ table.columns[time_partitioning.field].type,
834
+ sqlalchemy.sql.sqltypes.TIMESTAMP,
835
+ ):
836
+ trunc_fn = "TIMESTAMP_TRUNC"
837
+
838
+ return f"PARTITION BY {trunc_fn}({field}, {time_partitioning.type_})"
839
+
840
+ def _process_range_partitioning(
841
+ self, table: Table, range_partitioning: RangePartitioning
842
+ ):
843
+ """
844
+ Generates a SQL 'PARTITION BY' clause for partitioning a table by a range of integers.
845
+
846
+ Args:
847
+ - table (Table): The SQLAlchemy table object representing the BigQuery table to be partitioned.
848
+ - range_partitioning (RangePartitioning): The RangePartitioning object containing the
849
+ partitioning field, range start, range end, and interval.
850
+
851
+ Returns:
852
+ - str: A SQL string for range partitioning using RANGE_BUCKET and GENERATE_ARRAY functions.
853
+
854
+ Raises:
855
+ - AttributeError: If the partitioning field is not defined.
856
+ - ValueError: If the partitioning field (i.e. column) data type is not an integer.
857
+ - TypeError: If the partitioning range start/end values are not integers.
858
+
859
+ Example:
860
+ "PARTITION BY RANGE_BUCKET(zipcode, GENERATE_ARRAY(0, 100000, 10))"
861
+ """
862
+ if range_partitioning.field is None:
863
+ raise AttributeError(
864
+ "bigquery_range_partitioning expects field to be defined"
865
+ )
866
+
867
+ if not isinstance(
868
+ table.columns[range_partitioning.field].type,
869
+ sqlalchemy.sql.sqltypes.INT,
870
+ ):
871
+ raise ValueError(
872
+ "bigquery_range_partitioning expects field (i.e. column) data type to be INTEGER"
873
+ )
874
+
875
+ range_ = range_partitioning.range_
876
+
877
+ if not isinstance(range_.start, int):
878
+ raise TypeError(
879
+ "bigquery_range_partitioning expects range_.start to be an int,"
880
+ f" provided {repr(range_.start)}"
881
+ )
882
+
883
+ if not isinstance(range_.end, int):
884
+ raise TypeError(
885
+ "bigquery_range_partitioning expects range_.end to be an int,"
886
+ f" provided {repr(range_.end)}"
887
+ )
888
+
889
+ default_interval = 1
890
+
891
+ return f"PARTITION BY RANGE_BUCKET({range_partitioning.field}, GENERATE_ARRAY({range_.start}, {range_.end}, {range_.interval or default_interval}))"
892
+
893
+ def _process_option_value(self, value):
894
+ """
895
+ Transforms the given option value into a literal representation suitable for SQL queries in BigQuery.
896
+
897
+ Args:
898
+ value: The value to be transformed.
899
+
900
+ Returns:
901
+ The processed value in a format suitable for inclusion in a SQL query.
902
+
903
+ Raises:
904
+ NotImplementedError: When there is no transformation registered for a data type.
905
+ """
906
+ option_casting = {
907
+ # Mapping from option type to its casting method
908
+ str: lambda x: process_string_literal(x),
909
+ int: lambda x: x,
910
+ float: lambda x: x,
911
+ bool: lambda x: "true" if x else "false",
912
+ datetime.datetime: lambda x: BQTimestamp.process_timestamp_literal(x),
913
+ }
914
+
915
+ if (option_cast := option_casting.get(type(value))) is not None:
916
+ return option_cast(value)
917
+
918
+ raise NotImplementedError(f"No transformation registered for {repr(value)}")
919
+
683
920
 
684
921
  def process_string_literal(value):
685
922
  return repr(value.replace("%", "%%"))
@@ -723,7 +960,6 @@ class BQTimestamp(sqlalchemy.sql.type_api.TypeEngine):
723
960
 
724
961
  class BQArray(sqlalchemy.sql.sqltypes.ARRAY):
725
962
  def literal_processor(self, dialect):
726
-
727
963
  item_processor = self.item_type._cached_literal_processor(dialect)
728
964
  if not item_processor:
729
965
  raise NotImplementedError(
@@ -992,25 +1228,8 @@ class BigQueryDialect(DefaultDialect):
992
1228
  return {"constrained_columns": []}
993
1229
 
994
1230
  def get_indexes(self, connection, table_name, schema=None, **kw):
995
- table = self._get_table(connection, table_name, schema)
996
- indexes = []
997
- if table.time_partitioning:
998
- indexes.append(
999
- {
1000
- "name": "partition",
1001
- "column_names": [table.time_partitioning.field],
1002
- "unique": False,
1003
- }
1004
- )
1005
- if table.clustering_fields:
1006
- indexes.append(
1007
- {
1008
- "name": "clustering",
1009
- "column_names": table.clustering_fields,
1010
- "unique": False,
1011
- }
1012
- )
1013
- return indexes
1231
+ # BigQuery has no support for indexes.
1232
+ return []
1014
1233
 
1015
1234
  def get_schema_names(self, connection, **kw):
1016
1235
  if isinstance(connection, Engine):
@@ -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.8.0"
20
+ __version__ = "1.10.0"