From 7c0acd36f48e70fa7bc4afa0a24ef591aad161ad Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Tue, 16 Jan 2024 02:59:48 +0100 Subject: [PATCH 1/3] Types: Emulate PostgreSQL's `JSON(B)` types using CrateDB's `OBJECT` --- CHANGES.md | 1 + docs/data-types.rst | 4 ++++ src/sqlalchemy_cratedb/compiler.py | 6 ++++++ tests/compiler_test.py | 13 +++++++++++++ 4 files changed, 24 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 538c99de..d4ef5d72 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,6 +11,7 @@ 'visit_on_conflict_do_update'` by forwarding calls to `PGCompiler.visit_on_conflict_do_update` - Dialect: Added methods concerned with isolation levels as no-ops +- Types: Started emulating PostgreSQL's `JSON(B)` types using CrateDB's `OBJECT` ## 2026/05/28 0.42.0 - Added support for SQL Alchemy 2.1 diff --git a/docs/data-types.rst b/docs/data-types.rst index 3b037525..da05ae4e 100644 --- a/docs/data-types.rst +++ b/docs/data-types.rst @@ -51,6 +51,8 @@ CrateDB SQLAlchemy `string`__ `String`__ `array`__ `ARRAY`__ `object`__ :ref:`object` |nbsp| (extension type) +`object`__ ``JSON`` +`object`__ ``JSONB`` `array(object)`__ :ref:`objectarray` |nbsp| (extension type) `geo_point`__ :ref:`geopoint` |nbsp| (extension type) `geo_shape`__ :ref:`geoshape` |nbsp| (extension type) @@ -79,6 +81,8 @@ __ http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.S __ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#array __ http://docs.sqlalchemy.org/en/latest/core/type_basics.html#sqlalchemy.types.ARRAY __ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#object +__ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#object +__ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#object __ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#array __ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#geo-point __ https://cratedb.com/docs/crate/reference/en/latest/general/ddl/data-types.html#geo-shape diff --git a/src/sqlalchemy_cratedb/compiler.py b/src/sqlalchemy_cratedb/compiler.py index e98dda24..7fffaaff 100644 --- a/src/sqlalchemy_cratedb/compiler.py +++ b/src/sqlalchemy_cratedb/compiler.py @@ -271,6 +271,12 @@ def visit_TIMESTAMP(self, type_, **kw): """ return "TIMESTAMP %s" % ((type_.timezone and "WITH" or "WITHOUT") + " TIME ZONE",) + def visit_JSON(self, type_, **kw): + return "OBJECT" + + def visit_JSONB(self, type_, **kw): + return "OBJECT" + class CrateCompiler(compiler.SQLCompiler): def visit_getitem_binary(self, binary, operator, **kw): diff --git a/tests/compiler_test.py b/tests/compiler_test.py index 853ec47a..1b41e6de 100644 --- a/tests/compiler_test.py +++ b/tests/compiler_test.py @@ -543,3 +543,16 @@ class FooBar(Base): "they will be omitted when generating DDL statements.", str(w[-1].message), ) + + def test_ddl_with_json_columns(self): + mytable = sa.Table("json_table", self.metadata, sa.Column("json", sa.JSON)) + self.metadata.create_all(self.engine, tables=[mytable], checkfirst=False) + self.assertEqual( + self.executed_statement, + dedent(""" + CREATE TABLE testdrive.json_table ( + \tjson OBJECT + ) + + """), + ) # noqa: W291, W293 From 155c965a0106f710a40931c6fe2b3683f382c43e Mon Sep 17 00:00:00 2001 From: Bilal Tonga Date: Fri, 12 Jun 2026 14:00:01 +0200 Subject: [PATCH 2/3] Add test for jsonb columns --- tests/compiler_test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/compiler_test.py b/tests/compiler_test.py index 1b41e6de..cb293554 100644 --- a/tests/compiler_test.py +++ b/tests/compiler_test.py @@ -556,3 +556,18 @@ def test_ddl_with_json_columns(self): """), ) # noqa: W291, W293 + + def test_ddl_with_jsonb_columns(self): + from sqlalchemy.dialects.postgresql import JSONB + + mytable = sa.Table("jsonb_table", self.metadata, sa.Column("data", JSONB)) + self.metadata.create_all(self.engine, tables=[mytable], checkfirst=False) + self.assertEqual( + self.executed_statement, + dedent(""" + CREATE TABLE testdrive.jsonb_table ( + \tdata OBJECT + ) + + """), + ) # noqa: W291, W293 From 6c5a788ecf0de0726b4682b7ba726b2d4d59f011 Mon Sep 17 00:00:00 2001 From: Bilal Tonga Date: Fri, 12 Jun 2026 15:02:14 +0200 Subject: [PATCH 3/3] Remove unnecessary noqa comments --- tests/compiler_test.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/compiler_test.py b/tests/compiler_test.py index cb293554..e7d11589 100644 --- a/tests/compiler_test.py +++ b/tests/compiler_test.py @@ -411,7 +411,7 @@ class ItemStore(Base): ) """), - ) # noqa: W291, W293 + ) # Verify SQL DDL statement. self.metadata.create_all(self.engine, tables=[ItemStore.__table__], checkfirst=False) @@ -426,7 +426,7 @@ class ItemStore(Base): ) """), - ) # noqa: W291, W293 + ) # Verify if corresponding warning is emitted. self.assertEqual(len(w), 1) @@ -468,7 +468,7 @@ class FooBar(Base): ) """), - ) # noqa: W291, W293 + ) # Verify if corresponding warning is emitted. self.assertEqual(len(w), 1) @@ -510,7 +510,7 @@ class FooBar(Base): ) """), - ) # noqa: W291, W293 + ) def test_ddl_with_create_index(self): """ @@ -555,7 +555,7 @@ def test_ddl_with_json_columns(self): ) """), - ) # noqa: W291, W293 + ) def test_ddl_with_jsonb_columns(self): from sqlalchemy.dialects.postgresql import JSONB @@ -570,4 +570,4 @@ def test_ddl_with_jsonb_columns(self): ) """), - ) # noqa: W291, W293 + )