trinodb / trino-python-client

Python client for Trino
Apache License 2.0
328 stars 163 forks source link

`get_table_names` raises `trino.exceptions.FailedToObtainAddedPrepareHeader` in trino 398 #245

Closed TeddyCr closed 2 years ago

TeddyCr commented 2 years ago

Expected behavior

I was expecting the list of tables to be returned (similar to what we can see with previous versions of trino -- tested up to 394).

Actual behavior

Trino raises the below error:

sqlalchemy.exc.DBAPIError: (trino.exceptions.FailedToObtainAddedPrepareHeader) 

Details In trino[sqlalchemy]==0.316.0 when iterating over results in dbapi.py line 333 and fetching the results in client.py line 723 it seems that self._row_mapper always evaluates to None. This causes lines 334-337 to never evaluate and therefore the method to raise trino.exceptions.FailedToObtainAddedPrepareHeader.

In trino[sqlalchemy]==0.313.0 in client.py line 677 status will never have a row attribute as response line 463 will never have a data key.

Steps To Reproduce

docker run --name trino -d -p 8080:8080 trinodb/trino
from sqlalchemy import create_engine, inspect

url_ = "trino://user:@localhost:8080/tpcds"
engine = create_engine(url_)
inspect(engine).get_table_names("sf1000")

Log output

Traceback (most recent call last):
  File "/Users/teddycrepineau/LocalDocuments/professional/OMD/playground.py", line 7, in <module>
    print(inspect(engine).get_table_names("sf1000"))
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 266, in get_table_names
    return self.dialect.get_table_names(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/trino/sqlalchemy/dialect.py", line 196, in get_table_names
    res = connection.execute(sql.text(query), schema=schema)
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1306, in execute
    return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
    ret = self._execute_context(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1862, in _execute_context
    self._handle_dbapi_exception(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
    util.raise_(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
    raise exception
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
    self.dialect.do_execute(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/trino/sqlalchemy/dialect.py", line 346, in do_execute
    cursor.execute(statement, parameters)
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/trino/dbapi.py", line 460, in execute
    added_prepare_header = self._prepare_statement(
  File "/Users/teddycrepineau/.venv/omd_3_9/lib/python3.9/site-packages/trino/dbapi.py", line 339, in _prepare_statement
    raise trino.exceptions.FailedToObtainAddedPrepareHeader
sqlalchemy.exc.DBAPIError: (trino.exceptions.FailedToObtainAddedPrepareHeader) 

Operating System

macOS Monterey

Trino Python client version

0.313 and 0.316

Trino Server version

398

Python version

3.9.9

Are you willing to submit PR?

hashhar commented 2 years ago

It should be fixed in https://github.com/trinodb/trino-python-client/pull/242. The new release including it will happen later today.

Can you try installing the client from master branch and verify if it solves the get_table_names case as well.

TeddyCr commented 2 years ago

Awesome, thanks @hashhar

hashhar commented 2 years ago

Once you verify we can be sure there's not another related bug.

hashhar commented 2 years ago

Nevermind, thanks for including the simple repro steps, works on current master:

>>> url_ = "trino://user:@localhost:8080/tpcds"
>>> engine = create_engine(url_)
>>> inspect(engine).get_table_names("sf1000")
['call_center', 'catalog_page', 'catalog_returns', 'catalog_sales', 'customer', 'customer_address', 'customer_demographics', 'date_dim', 'household_demographics', 'income_band', 'inventory', 'item', 'promotion', 'reason', 'ship_mode', 'store', 'store_returns', 'store_sales', 'time_dim', 'warehouse', 'web_page', 'web_returns', 'web_sales', 'web_site', 'dbgen_version']
hashhar commented 2 years ago

I'm closing as fixed. It'll be available in 0.317.0 coming out later today.

TeddyCr commented 2 years ago

Perfect. Thanks for the quick response

hashhar commented 2 years ago

Fixed in #242 .