trinodb / trino-python-client

Python client for Trino
Apache License 2.0
311 stars 154 forks source link

DB API raises exceptions which are not derived from the PEP 249 database API specification #199

Closed john-bodley closed 2 years ago

john-bodley commented 2 years ago

The Trino DB API raises exceptions outside the PEP 249 database API specification and thus are not correctly handled by downstream interfaces including SQLAlchemy, Apache Superset, etc.

For example the DB API cursor fetchall method is raising a TrinoUserError which is not derived from a PEP 249 exception.

...
  File "/usr/local/lib/python3.9/dist-packages/trino/dbapi.py", line 560, in fetchall
    return list(self.genall())
  File "/usr/local/lib/python3.9/dist-packages/trino/client.py", line 512, in __iter__
    rows = self._query.fetch()
  File "/usr/local/lib/python3.9/dist-packages/trino/client.py", line 685, in fetch
    status = self._request.process(response)
  File "/usr/local/lib/python3.9/dist-packages/trino/client.py", line 443, in process
    raise self._process_error(response["error"], response.get("id"))
trino.exceptions.TrinoUserError: TrinoUserError(type=USER_ERROR, name=NOT_SUPPORTED, message="line 1:1: Relation 'foo.bar' is a table, not a view", query_id=20220715_233855_30555_sz8ja)

The resolution is that the public DB API methods should only raise PEP 249 exceptions.

hashhar commented 2 years ago

The resolution is that the public DB API methods should only raise PEP 249 exceptions.

Or change existing exceptions to be subclasses of the predefined ones. Thanks for reporting this. Can you elaborate a bit on what you mean by "are not correctly handled by downstream interfaces including SQLAlchemy, Apache Superset, etc."? What do you observe?

john-bodley commented 2 years ago

@hashhar see https://github.com/apache/superset/pull/20729 for details, specifically this comment.