Skip to content

Commit c23e033

Browse files
vuanhphungIsaac
andcommitted
fix(kernel): keep a result set read with Arrow fetches on the Arrow path
Review feedback: whether a row fetch after an Arrow fetch asked the kernel for rows depended on where the Arrow fetch stopped; an Arrow fetch now turns kernel rows off for the result set. Also pins that a fetch retried after a fetch_all_rows error returns every row once, in order, from the batches the kernel kept. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 37deeb8 commit c23e033

2 files changed

Lines changed: 27 additions & 4 deletions

File tree

‎src/databricks/sql/backend/kernel/result_set.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ def __init__(
276276
self._schema: pyarrow.Schema = result_schema.schema
277277
# Kernels with schema keys can also return small results as rows of
278278
# Python values (``fetch_all_rows``), skipping pyarrow. Tried once,
279-
# on the first row fetch, for types the pandas-free path converts.
279+
# on the first row fetch unless an Arrow fetch came first, for types
280+
# the pandas-free path converts.
280281
self._try_kernel_rows = (
281282
key is not None and result_schema.max_rows_without_pandas >= 0
282283
)
@@ -448,6 +449,7 @@ def _make_rows(self, values: List[tuple]) -> List[Row]:
448449
def fetchall_arrow(self) -> pyarrow.Table:
449450
if self._kernel_rows is not None:
450451
return self._kernel_rows_to_arrow(None)
452+
self._try_kernel_rows = False
451453
return self._drain()
452454

453455
def fetchmany_arrow(self, size: int) -> pyarrow.Table:
@@ -457,6 +459,7 @@ def fetchmany_arrow(self, size: int) -> pyarrow.Table:
457459
return pyarrow.Table.from_batches([], schema=self._schema)
458460
if self._kernel_rows is not None:
459461
return self._kernel_rows_to_arrow(size)
462+
self._try_kernel_rows = False
460463
self._ensure_buffered(size)
461464
return self._take_buffered(size)
462465

‎tests/unit/test_kernel_result_set.py‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,13 +922,14 @@ def test_handles_with_schema_keys_but_without_fetch_all_rows_use_the_batches():
922922
_assert_same_rows(rs.fetchall(), _expected_rows(_rows_batch(0, 3)))
923923

924924

925-
def test_row_fetches_after_an_arrow_fetch_use_the_buffered_batches():
925+
@pytest.mark.parametrize("taken", [1, 3], ids=["mid_batch", "batch_boundary"])
926+
def test_row_fetches_after_an_arrow_fetch_stay_on_the_arrow_path(taken):
926927
handle = _FakeRowsHandle(_ROWS_SCHEMA, [_rows_batch(0, 3), _rows_batch(3, 4)])
927928
rs = _make_rs(handle)
928-
assert rs.fetchmany_arrow(1).num_rows == 1
929+
assert rs.fetchmany_arrow(taken).num_rows == taken
929930
rows = rs.fetchall()
930931
assert handle.rows_calls == []
931-
_assert_same_rows(rows, _expected_rows(_rows_batch(1, 3), _rows_batch(3, 4)))
932+
_assert_same_rows(rows, _expected_rows(_rows_batch(taken, 4)))
932933

933934

934935
def test_kernel_row_errors_are_mapped():
@@ -938,6 +939,25 @@ def test_kernel_row_errors_are_mapped():
938939
_make_rs(handle).fetchall()
939940

940941

942+
def test_a_fetch_after_a_kernel_row_error_returns_every_row_once():
943+
batches = [_rows_batch(0, 2), _rows_batch(2, 4), _rows_batch(4, 5)]
944+
handle = _FakeRowsHandle(_ROWS_SCHEMA, batches)
945+
946+
def pull_two_then_fail(max_rows, nan_ok=False):
947+
# As the kernel does when reading fails mid-way: the batches read so
948+
# far stay queued for fetch_next_batch.
949+
handle._pending.extend([handle._batches.popleft() for _ in range(2)])
950+
raise RuntimeError("connection reset")
951+
952+
handle.fetch_all_rows = pull_two_then_fail
953+
rs = _make_rs(handle)
954+
with pytest.raises(OperationalError):
955+
rs.fetchone()
956+
rows = [rs.fetchone()] + rs.fetchall()
957+
_assert_same_rows(rows, _expected_rows(*batches))
958+
assert rs.rownumber == 5
959+
960+
941961
def test_close_drops_kernel_rows():
942962
rs = _make_rs(_FakeRowsHandle(_ROWS_SCHEMA, [_rows_batch(0, 3)]))
943963
assert rs.fetchone()[0] == 0

0 commit comments

Comments
 (0)