Skip to content

Commit 4f5a1c0

Browse files
hovaescohashhar
authored andcommitted
Fix handling transaction requests in auto-commit mode
When explicit transaction was started in auto-commit mode, transaction request was not correctly attached to cursor. That caused query and transaction were executed separately.
1 parent 69946fe commit 4f5a1c0

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

tests/integration/conftest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,8 @@ def run_trino():
153153
stop_trino(container_id, proc)
154154

155155

156-
@pytest.fixture(scope="module")
157156
def trino_version():
158-
yield TRINO_VERSION
157+
return TRINO_VERSION
159158

160159

161160
@click.group()

tests/integration/test_dbapi_integration.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import pytz
1717

1818
import trino
19-
from trino.exceptions import TrinoQueryError
19+
from tests.integration.conftest import trino_version
20+
from trino.exceptions import TrinoQueryError, TrinoUserError
2021
from trino.transaction import IsolationLevel
2122

2223

@@ -43,16 +44,30 @@ def trino_connection_with_transaction(run_trino):
4344
)
4445

4546

46-
def test_select_query(trino_connection, trino_version):
47+
@pytest.fixture
48+
def trino_connection_in_autocommit(run_trino):
49+
_, host, port = run_trino
50+
51+
yield trino.dbapi.Connection(
52+
host=host,
53+
port=port,
54+
user="test",
55+
source="test",
56+
max_attempts=1,
57+
isolation_level=IsolationLevel.AUTOCOMMIT,
58+
)
59+
60+
61+
def test_select_query(trino_connection):
4762
cur = trino_connection.cursor()
4863
cur.execute("SELECT * FROM system.runtime.nodes")
4964
rows = cur.fetchall()
5065
assert len(rows) > 0
5166
row = rows[0]
52-
if trino_version == "latest":
67+
if trino_version() == "latest":
5368
assert row[2] is not None
5469
else:
55-
assert row[2] == trino_version
70+
assert row[2] == trino_version()
5671
columns = dict([desc[:2] for desc in cur.description])
5772
assert columns["node_id"] == "varchar"
5873
assert columns["http_uri"] == "varchar"
@@ -362,6 +377,24 @@ def test_transaction_multiple(trino_connection_with_transaction):
362377
assert len(rows2) == 1000
363378

364379

380+
@pytest.mark.skipif(trino_version() == '351', reason="Autocommit behaves "
381+
"differently in older Trino versions")
382+
def test_transaction_autocommit(trino_connection_in_autocommit):
383+
with trino_connection_in_autocommit as connection:
384+
connection.start_transaction()
385+
cur = connection.cursor()
386+
cur.execute(
387+
"""
388+
CREATE TABLE memory.default.nation
389+
AS SELECT * from tpch.tiny.nation
390+
""")
391+
392+
with pytest.raises(TrinoUserError) as transaction_error:
393+
cur.fetchall()
394+
assert "Catalog only supports writes using autocommit: memory" \
395+
in str(transaction_error.value)
396+
397+
365398
def test_invalid_query_throws_correct_error(trino_connection):
366399
"""Tests that an invalid query raises the correct exception
367400
"""

trino/dbapi.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ def cursor(self):
202202
if self.transaction is None:
203203
self.start_transaction()
204204
request = self.transaction._request
205+
elif self.transaction is not None:
206+
request = self.transaction._request
205207
else:
206208
request = self._create_request()
207209
return Cursor(self, request)

0 commit comments

Comments
 (0)