Skip to content

Commit 764ca52

Browse files
committed
feat(sqlalchemy-bigquery): add JSON type support
BigQuery has natively supported the JSON type since 2022, but the SQLAlchemy dialect was missing visit_JSON in BigQueryTypeCompiler and the JSON entry in _type_map. This caused UnsupportedCompilationError when creating tables with sa.JSON() columns via Alembic. Changes: - Add visit_JSON to BigQueryTypeCompiler returning "JSON" - Add "JSON" to _type_map mapping to sqlalchemy.types.JSON - Export JSON from the package __init__.py - Add unit test for JSON column DDL compilation Fixes: #16123
1 parent c6f1e43 commit 764ca52

4 files changed

Lines changed: 20 additions & 0 deletions

File tree

packages/sqlalchemy-bigquery/sqlalchemy_bigquery/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
FLOAT64,
3636
INT64,
3737
INTEGER,
38+
JSON,
3839
NUMERIC,
3940
RECORD,
4041
STRING,
@@ -75,6 +76,7 @@
7576
"FLOAT64",
7677
"INT64",
7778
"INTEGER",
79+
"JSON",
7880
"NUMERIC",
7981
"RECORD",
8082
"STRING",

packages/sqlalchemy-bigquery/sqlalchemy_bigquery/_types.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"STRUCT": STRUCT,
4747
"TIMESTAMP": sqlalchemy.types.TIMESTAMP,
4848
"TIME": sqlalchemy.types.TIME,
49+
"JSON": sqlalchemy.types.JSON,
4950
}
5051

5152
# By convention, dialect-provided types are spelled with all upper case.
@@ -65,6 +66,7 @@
6566
STRING = _type_map["STRING"]
6667
TIMESTAMP = _type_map["TIMESTAMP"]
6768
TIME = _type_map["TIME"]
69+
JSON = _type_map["JSON"]
6870

6971
try:
7072
_type_map["GEOGRAPHY"] = GEOGRAPHY

packages/sqlalchemy-bigquery/sqlalchemy_bigquery/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,9 @@ def visit_NUMERIC(self, type_, **kw):
642642

643643
visit_DECIMAL = visit_NUMERIC
644644

645+
def visit_JSON(self, type_, **kw):
646+
return "JSON"
647+
645648

646649
class BigQueryDDLCompiler(DDLCompiler):
647650
option_datatype_mapping = {

packages/sqlalchemy-bigquery/tests/unit/test_compiler.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ def table(faux_conn, metadata):
4141
table.drop(faux_conn)
4242

4343

44+
def test_compile_json_column(faux_conn, metadata):
45+
sqlalchemy.Table(
46+
"json_table",
47+
metadata,
48+
sqlalchemy.Column("id", sqlalchemy.Integer),
49+
sqlalchemy.Column("data", sqlalchemy.JSON),
50+
)
51+
metadata.create_all(faux_conn.engine)
52+
assert " ".join(faux_conn.test_data["execute"][-1][0].strip().split()) == (
53+
"CREATE TABLE `json_table` ( `id` INT64, `data` JSON )"
54+
)
55+
56+
4457
def test_constraints_are_ignored(faux_conn, metadata):
4558
sqlalchemy.Table(
4659
"ref",

0 commit comments

Comments
 (0)