Skip to content
Open
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
13 changes: 13 additions & 0 deletions tests/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the column policy is strict? Do we need a test of that case?

Copy link
Copy Markdown
Contributor Author

@amotl amotl Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

column_policy = 'strict' is a table option. I don't think the dialect currently implements it, and I don't know if it has any impact on columns that are defined as OBJECT without qualifier.

The dialect currently provides the options crate_number_of_shards, crate_clustered_by, crate_number_of_replicas, crate_partitioned_by, crate_index, crate_columnstore, no column policy option in sight.

Other than this, the object type does not seem to implement any specials wrt. DDL generation.

class ObjectTypeImpl(sqltypes.UserDefinedType, sqltypes.JSON):
__visit_name__ = "OBJECT"
cache_ok = False
none_as_null = False
# Designated name to refer to. `Object` is too ambiguous.
ObjectType = MutableDict.as_mutable(ObjectTypeImpl)
# Backward-compatibility aliases.
_deprecated_Craty = ObjectType
_deprecated_Object = ObjectType
# https://www.lesinskis.com/deprecating-module-scope-variables.html
deprecated_names = ["Craty", "Object"]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@coderabbitai: Please converge this conversation into a dedicated issue.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
gh issue create \
  --repo crate/sqlalchemy-cratedb \
  --title "feat: Support \`column_policy\` table option for CrateDB OBJECT type" \
  --assignee amotl \
  --body "## Overview

During the review of PR `#27` (adding \`test_ddl_with_json_columns\`), the question was raised whether the dialect should also support and test CrateDB's \`column_policy\` table option (e.g. \`STRICT\`, \`DYNAMIC\`, \`IGNORED\`) in DDL generation.

## Background

\`column_policy\` is a CrateDB table-level option that controls how columns are handled when inserting data that does not match the defined schema. It is not currently implemented in the dialect.

Currently implemented CrateDB-specific dialect options are:
- \`crate_number_of_shards\`
- \`crate_clustered_by\`
- \`crate_number_of_replicas\`
- \`crate_partitioned_by\`
- \`crate_index\`
- \`crate_columnstore\`

The \`ObjectType\` implementation does not currently handle any DDL-specific extras beyond what is defined in [\`src/sqlalchemy_cratedb/type/object.py\` (lines 64–79)](https://github.com/crate/sqlalchemy-cratedb/blob/889c7670960d28124746ee20a7eacbb2e538af45/src/sqlalchemy_cratedb/type/object.py#L64-L79).

## Tasks

- [ ] Investigate whether \`column_policy\` should be added as a supported table option in the CrateDB dialect
- [ ] If implemented, add corresponding DDL compiler tests (e.g. \`column_policy = 'strict'\`, \`column_policy = 'dynamic'\`, \`column_policy = 'ignored'\`)

## References

- PR: `#27`
- Discussion comment: https://github.com/crate/sqlalchemy-cratedb/pull/27#discussion_r3355672545"

Length of output: 1675



@amotl, I've created a dedicated issue to track this: #269 – feat: Support column_policy table option for CrateDB OBJECT type.

It summarises the conversation, lists the currently supported dialect options, and includes tasks to investigate and potentially implement column_policy support along with corresponding DDL compiler tests.

)

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