diff --git a/python/adbc_driver_flightsql/tests/test_errors.py b/python/adbc_driver_flightsql/tests/test_errors.py index 00110c472d2..7ab9699cdaa 100644 --- a/python/adbc_driver_flightsql/tests/test_errors.py +++ b/python/adbc_driver_flightsql/tests/test_errors.py @@ -40,6 +40,11 @@ def assert_detail(e): def test_query_cancel(test_dbapi): with test_dbapi.cursor() as cur: + partitions, _ = cur.adbc_execute_partitions("SELECT 1") + cur.adbc_read_partition(partitions[0]) + assert cur._results._cancel == test_dbapi.adbc_connection.cancel + assert cur.fetchone() == (5,) + cur.execute("forever") cur.adbc_cancel() with pytest.raises( @@ -73,6 +78,34 @@ def _cancel(): cur.fetchone() +def test_read_partition_cancel(test_dbapi): + with test_dbapi.cursor() as cur: + partitions, _ = cur.adbc_execute_partitions("forever") + assert len(partitions) == 1 + + errors = [] + + def _read(): + try: + cur.adbc_read_partition(partitions[0]) + except Exception as e: + errors.append(e) + + t = threading.Thread(target=_read, daemon=True) + t.start() + time.sleep(2) + cur.adbc_cancel() + t.join(timeout=30) + assert not t.is_alive(), "cancel did not interrupt ReadPartition" + + assert len(errors) == 1 + assert isinstance(errors[0], test_dbapi.OperationalError) + assert str(errors[0]) == ( + "CANCELLED: [FlightSQL] context canceled" + " (Canceled; ReadPartition(DoGet)). Vendor code: 1" + ) + + def test_query_error_fetch(test_dbapi): with test_dbapi.cursor() as cur: cur.execute("error_do_get") diff --git a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx index 076def8b5e5..2f645b7c14a 100644 --- a/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx +++ b/python/adbc_driver_manager/adbc_driver_manager/_lib.pyx @@ -1540,7 +1540,7 @@ cdef class AdbcStatement(_AdbcHandle): check_error(status, &c_error) def cancel(self) -> None: - """Attempt to cancel any ongoing operations on the connection.""" + """Attempt to cancel any ongoing operations on the statement.""" cdef CAdbcError c_error = empty_error() cdef CAdbcStatusCode status with nogil: diff --git a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py index e7c51c0c7fb..ff5e585adb9 100644 --- a/python/adbc_driver_manager/adbc_driver_manager/dbapi.py +++ b/python/adbc_driver_manager/adbc_driver_manager/dbapi.py @@ -49,6 +49,7 @@ import weakref from typing import ( Any, + Callable, Dict, List, Literal, @@ -804,6 +805,7 @@ def __init__( self._last_query: Optional[Union[str, bytes]] = None self._results: Optional["_RowIterator"] = None + self._cancel: Callable[[], None] = self._stmt.cancel self._arraysize = 1 self._rowcount = -1 self._bind_by_name = False @@ -815,6 +817,7 @@ def _clear(self) -> None: if self._results is not None: self._results.close() self._results = None + self._cancel = self._stmt.cancel @property def arraysize(self) -> int: @@ -943,7 +946,7 @@ def execute(self, operation: Union[bytes, str], parameters=None) -> "Self": handle, self._rowcount = _blocking_call( self._stmt.execute_query, (), {}, self._stmt.cancel ) - self._results = _RowIterator(self._stmt, handle, self._backend) + self._results = _RowIterator(self._cancel, handle, self._backend) return self def executemany(self, operation: Union[bytes, str], seq_of_parameters) -> None: @@ -1082,13 +1085,13 @@ def __next__(self) -> tuple: def adbc_cancel(self) -> None: """ - Cancel any ongoing operations on this statement. + Cancel any ongoing operations on this cursor. Notes ----- This is an extension and not part of the DBAPI standard. """ - self._stmt.cancel() + self._cancel() def adbc_ingest( self, @@ -1307,12 +1310,12 @@ def adbc_read_partition(self, partition: bytes) -> None: """ _requires_pyarrow() self._clear() - self._results = None + self._cancel = self._conn._conn.cancel handle = _blocking_call( - self._conn._conn.read_partition, (partition,), {}, self._stmt.cancel + self._conn._conn.read_partition, (partition,), {}, self._cancel ) self._rowcount = -1 - self._results = _RowIterator(self._stmt, handle, self._backend) + self._results = _RowIterator(self._cancel, handle, self._backend) @property def adbc_statement(self) -> _lib.AdbcStatement: @@ -1457,11 +1460,11 @@ class _RowIterator(_Closeable): def __init__( self, - stmt: _lib.AdbcStatement, + cancel: Callable[[], None], handle: _lib.ArrowArrayStreamHandle, dbapi_backend: _dbapi_backend.DbapiBackend, ) -> None: - self._stmt = stmt + self._cancel = cancel self._handle: Optional[_lib.ArrowArrayStreamHandle] = handle self._backend = dbapi_backend self._reader: Optional["AdbcRecordBatchReader"] = None @@ -1512,7 +1515,7 @@ def fetchone(self) -> Optional[tuple]: try: while True: self._current_batch = _blocking_call( - self.reader.read_next_batch, (), {}, self._stmt.cancel + self.reader.read_next_batch, (), {}, self._cancel ) if self._current_batch.num_rows > 0: break @@ -1548,10 +1551,10 @@ def fetchall(self) -> List[tuple]: return rows def fetch_arrow_table(self) -> "pyarrow.Table": - return _blocking_call(self.reader.read_all, (), {}, self._stmt.cancel) + return _blocking_call(self.reader.read_all, (), {}, self._cancel) def fetch_df(self) -> "pandas.DataFrame": - return _blocking_call(self.reader.read_pandas, (), {}, self._stmt.cancel) + return _blocking_call(self.reader.read_pandas, (), {}, self._cancel) def fetch_polars(self) -> "polars.DataFrame": import polars @@ -1562,7 +1565,7 @@ def fetch_polars(self) -> "polars.DataFrame": ), (), {}, - self._stmt.cancel, + self._cancel, ) def fetch_arrow(self) -> _lib.ArrowArrayStreamHandle: