python-pinot-dbapi / pinot-dbapi

Python DB-API and SQLAlchemy dialect for Pinot
MIT License
20 stars 33 forks source link

Patch for SQL Alchemy missing JSON deserializer error #68

Closed AZ-AdamDiehl closed 1 year ago

AZ-AdamDiehl commented 1 year ago

The Error Attempting to use PinotDB with SQL Alchemy produced the following error related to a missing _json_deserializer() method:

My relevant versions are pinotdb==0.4.13, and SQLAlchemy==2.0.2.

Traceback (most recent call last):
        File "...sqlalchemy/sql/sqltypes.py", line 2714, in result_processor
        json_deserializer = dialect._json_deserializer or json.loads
        AttributeError: 'PinotDialect' object has no attribute '_json_deserializer'

The Fix In all cases I've encountered, the object is already parsed (into a dict) and could be passed straight through (the file db.py calls json.loads() already). However, for type safety, I added an if/else statement to check to see whether the content is deserializable (is a string, byte array, or bytes). If it is, use the json.loads() method to parse it into a JSON. If the object is not deserializable, then it is simply returned (and presumed to be an already parsed dictionary). The resulting new function is

def _json_deserializer(self, content: any):
        if isinstance(content, str) or isinstance(content, bytearray) or isinstance(content, bytes):
            return json.loads(content)
        else:
            return content