Skip to content

Commit dc6761e

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 dc6761e

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

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
@@ -40,6 +40,7 @@
4040
"FLOAT": sqlalchemy.types.Float,
4141
"INT64": sqlalchemy.types.Integer,
4242
"INTEGER": sqlalchemy.types.Integer,
43+
"JSON": sqlalchemy.types.JSON,
4344
"NUMERIC": sqlalchemy.types.Numeric,
4445
"RECORD": STRUCT,
4546
"STRING": sqlalchemy.types.String,
@@ -60,6 +61,7 @@
6061
FLOAT = _type_map["FLOAT"]
6162
INT64 = _type_map["INT64"]
6263
INTEGER = _type_map["INTEGER"]
64+
JSON = _type_map["JSON"]
6365
NUMERIC = _type_map["NUMERIC"]
6466
RECORD = _type_map["RECORD"]
6567
STRING = _type_map["STRING"]

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)