Skip to content

Commit ed2203f

Browse files
mdesmetebyhr
authored andcommitted
Cancel current query on cursor.close()
1 parent d19e2cc commit ed2203f

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

tests/integration/test_dbapi_integration.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,18 @@ def test_cancel_query(trino_connection):
775775
assert "Cancel query failed; no running query" in str(cancel_error.value)
776776

777777

778+
def test_close_cursor(trino_connection):
779+
cur = trino_connection.cursor()
780+
cur.execute("SELECT * FROM tpch.sf1.customer")
781+
cur.fetchone()
782+
cur.close() # would raise an exception if cancel fails
783+
784+
cur = trino_connection.cursor()
785+
with pytest.raises(Exception) as cancel_error:
786+
cur.close()
787+
assert "Cancel query failed; no running query" in str(cancel_error.value)
788+
789+
778790
def test_session_properties(run_trino):
779791
_, host, port = run_trino
780792

trino/dbapi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,9 @@ def cancel(self):
567567
self._query.cancel()
568568

569569
def close(self):
570-
self._connection.close()
570+
self.cancel()
571+
# TODO: Cancel not only the last query executed on this cursor
572+
# but also any other outstanding queries executed through this cursor.
571573

572574

573575
Date = datetime.date

0 commit comments

Comments
 (0)