crate / crate-python

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

SQLAlchemy: Fix SQL statement caching for CrateDB's `OBJECT` type #559

Closed amotl closed 1 year ago

amotl commented 1 year ago

About

The SQLAlchemy implementation of CrateDB's OBJECT type offers indexed access to the instance's content in form of a dictionary. Thus, it must not use cache_ok = True on its implementation, i.e. to signal this part of the compiled SQL clause must not be cached.

Problem

Currently, when using those statements subsequently, based on a corresponding ORM schema definition, the second one will yield the wrong results, because it will use the compiled SQL statement of the first one, as it has been cached wrongly. This flaw has been reported by @faymarie, thank you very much.

class Character(Base):
    __tablename__ = 'characters'
    name = sa.Column(sa.String, primary_key=True)
    age = sa.Column(sa.Integer)
    data = sa.Column(Object)
sa.select(Character).where(Character.data['x'] == 1)
sa.select(Character).where(Character.data['y'] == 2)

Details

The accompanying test case verifies that two subsequent SELECT statements are translated well, and don't trip on incorrect SQL compiled statement caching. Because I have not been able to catch that error by unit tests based on mocking, this test case has now been introduced as a first integration test to the SQLAlchemy dialect subsystem.