crate / crate-python

Python DB API client library for CrateDB, using HTTP.
https://cratedb.com/docs/python/
Apache License 2.0
79 stars 30 forks source link

SQLAlchemy: Use JSON type adapter for implementing CrateDB's `OBJECT` #561

Closed amotl closed 1 year ago

amotl commented 1 year ago

About

Following up on crate/sqlalchemy-cratedb#93, by using the generalized parts of SQLAlchemy's infrastructure for implementing JSON types for a few databases, the CrateDB dialect will be able to leverage the same interfaces, like data casters, thus improving compatibility.

Example

sa.select(Entity).where(Entity.field['attribute'].as_integer() == 42)

Details

SQLAlchemy's sqltypes.JSON provides a facade for vendor-specific JSON types. Since it supports JSON SQL operations, it only works on backends that have an actual JSON type, which are currently PostgreSQL, MySQL, SQLite, and Microsoft SQL Server.

This patch starts leveraging the same infrastructure, thus bringing corresponding interfaces to the CrateDB dialect. The major difference is that it will not actually do any JSON marshalling, but propagate corresponding data structures 1:1, because within CrateDB's SQL, OBJECTs do not need to be serialized into JSON strings before transfer.

Resources