Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions docs/data-types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/sqlalchemy_cratedb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
36 changes: 32 additions & 4 deletions tests/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -426,7 +426,7 @@ class ItemStore(Base):
)

"""),
) # noqa: W291, W293
)

# Verify if corresponding warning is emitted.
self.assertEqual(len(w), 1)
Expand Down Expand Up @@ -468,7 +468,7 @@ class FooBar(Base):
)

"""),
) # noqa: W291, W293
)

# Verify if corresponding warning is emitted.
self.assertEqual(len(w), 1)
Expand Down Expand Up @@ -510,7 +510,7 @@ class FooBar(Base):
)

"""),
) # noqa: W291, W293
)

def test_ddl_with_create_index(self):
"""
Expand Down Expand Up @@ -543,3 +543,31 @@ class FooBar(Base):
"they will be omitted when generating DDL statements.",
str(w[-1].message),
)

def test_ddl_with_json_columns(self):
Comment thread
bgunebakan marked this conversation as resolved.
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
Comment thread
amotl marked this conversation as resolved.
)

"""),
)

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
)

"""),
)
Loading