Skip to content

Commit edd7608

Browse files
Fetch completed async results without a redundant status poll
Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>
1 parent 0c1b9e8 commit edd7608

3 files changed

Lines changed: 32 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Release History
22

33
# Unreleased
4+
- Avoid an extra status poll when retrieving a completed asynchronous query result with the kernel backend.
45
- Fix the kernel backend ignoring `use_cloud_fetch=False`, so complete results can be fetched without cloud-storage downloads.
56
- Fix inline parameters (`use_inline_params=True`) so a sequence directly after `IN` renders as a list again: `x IN %(ids)s` renders as `x IN (1,2,3)` instead of `x IN ARRAY(1,2,3)`, which Spark rejects. Inside the list, tuples render as rows for a multi-column `IN`, and a `set` or `frozenset` is accepted again. Sequences anywhere else still render as `ARRAY(...)` ([#629](https://github.com/databricks/databricks-sql-python/issues/629))
67
- Speed up Thrift response handling: read replies in 1 MiB chunks and decode them with Thrift's C-accelerated protocol, and stop stringifying every request/response for the default no-op debug logger. Wide result schemas, metadata calls with many rows, and inline Arrow results benefit most.

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ def get_execution_result(
966966
# derive a reader for re-fetch. This preserves the Thrift-parity
967967
# behavior where results remain re-callable until explicit close.
968968
#
969-
# Concurrency: the owning handle is shared, and ``await_result()``
969+
# Concurrency: the owning handle is shared, and ``fetch_result()``
970970
# below runs OUTSIDE the lock, so it must not run on the same
971971
# handle a concurrent ``get_query_state`` poll is already using
972972
# for ``status()``. Mirror that method's guard here — if a status
@@ -1004,7 +1004,8 @@ def get_execution_result(
10041004
if owner is not None
10051005
else self._kernel_session.attach_async_statement(command_id.guid)
10061006
)
1007-
stream = handle.await_result()
1007+
# Cursor.get_async_execution_result has already confirmed success.
1008+
stream = handle.fetch_result()
10081009
except Exception as exc:
10091010
if uses_owning_handle:
10101011
with self._async_handles_lock:
@@ -1013,16 +1014,16 @@ def get_execution_result(
10131014
# ``KernelResultSet.__init__`` calls ``arrow_schema()`` which
10141015
# can raise — map that to PEP 249 too.
10151016
#
1016-
# Unlike the ``await_result()`` failure above, we deliberately do
1017+
# Unlike the ``fetch_result()`` failure above, we deliberately do
10171018
# NOT discard the ``_async_result_stream_started`` marker here.
1018-
# By this point ``await_result()`` has already succeeded, so the
1019+
# By this point ``fetch_result()`` has already succeeded, so the
10191020
# owning handle's result stream has been started (and may be
1020-
# partially consumed); re-awaiting that same handle on a retry is
1021+
# partially consumed); re-fetching that same handle on a retry is
10211022
# not safe. Leaving the marker set routes any retry through the
10221023
# reader fallback, which re-materialises a fresh stream.
10231024
# The trade-off is that such a retry loses the async-statement
10241025
# telemetry — an accepted, narrow gap limited to the case where
1025-
# result-set construction fails after a successful await.
1026+
# result-set construction fails after a successful fetch.
10261027
try:
10271028
return self._make_result_set(stream, cursor, command_id)
10281029
except Exception as exc:

‎tests/unit/test_kernel_client.py‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,20 +1285,20 @@ def test_close_command_tolerant_when_handle_missing():
12851285
c.close_command(fake_command_id) # must not raise
12861286

12871287

1288-
def _attach_returns(c, *, status=None, status_error=None, await_result=None):
1288+
def _attach_returns(c, *, status=None, status_error=None, fetch_result=None):
12891289
"""Wire ``_kernel_session.attach_async_statement`` to return a fake
12901290
handle. ``status`` is a ``(state, failure)`` tuple for ``handle.status()``;
12911291
``status_error`` makes ``attach`` itself raise (e.g. NotFound);
1292-
``await_result`` sets ``handle.await_result()`` return value."""
1292+
``fetch_result`` sets ``handle.fetch_result()`` return value."""
12931293
c._kernel_session = MagicMock()
12941294
if status_error is not None:
12951295
c._kernel_session.attach_async_statement.side_effect = status_error
12961296
return None
12971297
handle = MagicMock()
12981298
if status is not None:
12991299
handle.status.return_value = status
1300-
if await_result is not None:
1301-
handle.await_result.return_value = await_result
1300+
if fetch_result is not None:
1301+
handle.fetch_result.return_value = fetch_result
13021302
c._kernel_session.attach_async_statement.return_value = handle
13031303
return handle
13041304

@@ -1319,7 +1319,7 @@ def test_get_query_state_returns_succeeded_when_server_404s():
13191319
def test_independent_async_read_requires_reader_api(read_result):
13201320
c = _make_client()
13211321
c._kernel_session = MagicMock()
1322-
owner = MagicMock(spec=["status", "await_result"])
1322+
owner = MagicMock(spec=["status", "fetch_result"])
13231323
cid = CommandId.from_sea_statement_id("tracked-async")
13241324
c._async_handles[cid.guid] = owner
13251325
c._async_result_stream_started.add(cid.guid)
@@ -1440,7 +1440,7 @@ def test_get_execution_result_uses_retained_owning_handle_first():
14401440
fake_stream = MagicMock()
14411441
fake_stream.arrow_schema.return_value = pa.schema([("n", pa.int64())])
14421442
handle = MagicMock()
1443-
handle.await_result.return_value = fake_stream
1443+
handle.fetch_result.return_value = fake_stream
14441444
cursor = MagicMock()
14451445
cursor.arraysize = 100
14461446
cursor.buffer_size_bytes = 1024
@@ -1452,7 +1452,7 @@ def test_get_execution_result_uses_retained_owning_handle_first():
14521452

14531453
assert rs is not None
14541454
c._kernel_session.attach_async_statement.assert_not_called()
1455-
handle.await_result.assert_called_once_with()
1455+
handle.fetch_result.assert_called_once_with()
14561456
assert cid.guid in c._async_result_stream_started
14571457

14581458

@@ -1462,7 +1462,7 @@ def test_get_execution_result_attaches_by_id_when_no_retained_handle():
14621462
c = _make_client()
14631463
fake_stream = MagicMock()
14641464
fake_stream.arrow_schema.return_value = pa.schema([("n", pa.int64())])
1465-
handle = _attach_returns(c, await_result=fake_stream)
1465+
handle = _attach_returns(c, fetch_result=fake_stream)
14661466
cursor = MagicMock()
14671467
cursor.arraysize = 100
14681468
cursor.buffer_size_bytes = 1024
@@ -1472,7 +1472,7 @@ def test_get_execution_result_attaches_by_id_when_no_retained_handle():
14721472

14731473
assert rs is not None
14741474
c._kernel_session.attach_async_statement.assert_called_once_with("async-1")
1475-
handle.await_result.assert_called_once_with()
1475+
handle.fetch_result.assert_called_once_with()
14761476

14771477

14781478
def test_get_execution_result_owning_handle_failure_can_retry_owning_handle():
@@ -1482,7 +1482,7 @@ def test_get_execution_result_owning_handle_failure_can_retry_owning_handle():
14821482
c = _make_client()
14831483
c._kernel_session = MagicMock()
14841484
handle = MagicMock()
1485-
handle.await_result.side_effect = _FakeKernelError(code="Unavailable")
1485+
handle.fetch_result.side_effect = _FakeKernelError(code="Unavailable")
14861486
cid = CommandId.from_sea_statement_id("async-retry-owning")
14871487
c._async_handles[cid.guid] = handle
14881488

@@ -1494,11 +1494,11 @@ def test_get_execution_result_owning_handle_failure_can_retry_owning_handle():
14941494

14951495

14961496
def test_get_execution_result_construction_failure_retains_marker_and_uses_reader():
1497-
"""If the owning handle's ``await_result()`` succeeds but result-set
1497+
"""If the owning handle's ``fetch_result()`` succeeds but result-set
14981498
construction then raises, the ``_async_result_stream_started`` marker
14991499
is deliberately left set (the owning stream may be partially
1500-
consumed, so re-awaiting it is unsafe). A subsequent call must route
1501-
through the derived reader fallback rather than re-awaiting the owning
1500+
consumed, so re-fetching it is unsafe). A subsequent call must route
1501+
through the derived reader fallback rather than re-fetching the owning
15021502
handle."""
15031503
c = _make_client()
15041504
c._kernel_session = MagicMock()
@@ -1507,7 +1507,7 @@ def test_get_execution_result_construction_failure_retains_marker_and_uses_reade
15071507
# raise so ``_make_result_set`` fails after a successful await.
15081508
owning_stream.arrow_schema.side_effect = _FakeKernelError(code="Internal")
15091509
owning_handle = MagicMock()
1510-
owning_handle.await_result.return_value = owning_stream
1510+
owning_handle.fetch_result.return_value = owning_stream
15111511
cid = CommandId.from_sea_statement_id("async-construct-fail")
15121512
c._async_handles[cid.guid] = owning_handle
15131513

@@ -1516,15 +1516,15 @@ def test_get_execution_result_construction_failure_retains_marker_and_uses_reade
15161516

15171517
# Marker stays set even though construction failed.
15181518
assert cid.guid in c._async_result_stream_started
1519-
owning_handle.await_result.assert_called_once_with()
1519+
owning_handle.fetch_result.assert_called_once_with()
15201520
c._kernel_session.attach_async_statement.assert_not_called()
15211521

1522-
# A retry derives a reader (fresh stream) instead of re-awaiting
1522+
# A retry derives a reader (fresh stream) instead of re-fetching
15231523
# the partially-consumed owning handle.
15241524
retry_stream = MagicMock()
15251525
retry_stream.arrow_schema.return_value = pa.schema([("n", pa.int64())])
15261526
reader = MagicMock()
1527-
reader.await_result.return_value = retry_stream
1527+
reader.fetch_result.return_value = retry_stream
15281528
owning_handle.reader.return_value = reader
15291529
cursor = MagicMock()
15301530
cursor.arraysize = 100
@@ -1535,9 +1535,9 @@ def test_get_execution_result_construction_failure_retains_marker_and_uses_reade
15351535
assert rs is not None
15361536
owning_handle.reader.assert_called_once_with()
15371537
c._kernel_session.attach_async_statement.assert_not_called()
1538-
reader.await_result.assert_called_once_with()
1539-
# The owning handle was not re-awaited on the retry.
1540-
owning_handle.await_result.assert_called_once_with()
1538+
reader.fetch_result.assert_called_once_with()
1539+
# The owning handle was not re-fetched on the retry.
1540+
owning_handle.fetch_result.assert_called_once_with()
15411541

15421542

15431543
def test_get_execution_result_maps_not_found_to_programming_error():
@@ -1779,9 +1779,9 @@ def test_get_execution_result_is_re_callable():
17791779
fake_stream = MagicMock()
17801780
fake_stream.arrow_schema.return_value = pa.schema([("n", pa.int64())])
17811781
owning_handle = MagicMock()
1782-
owning_handle.await_result.return_value = fake_stream
1782+
owning_handle.fetch_result.return_value = fake_stream
17831783
reader = MagicMock()
1784-
reader.await_result.return_value = fake_stream
1784+
reader.fetch_result.return_value = fake_stream
17851785
owning_handle.reader.return_value = reader
17861786
cid = CommandId.from_sea_statement_id("async-recall-twice")
17871787
c._async_handles[cid.guid] = owning_handle
@@ -1793,10 +1793,10 @@ def test_get_execution_result_is_re_callable():
17931793
rs2 = c.get_execution_result(cid, cursor=cursor)
17941794

17951795
assert rs1 is not None and rs2 is not None
1796-
owning_handle.await_result.assert_called_once_with()
1796+
owning_handle.fetch_result.assert_called_once_with()
17971797
owning_handle.reader.assert_called_once_with()
17981798
c._kernel_session.attach_async_statement.assert_not_called()
1799-
reader.await_result.assert_called_once_with()
1799+
reader.fetch_result.assert_called_once_with()
18001800

18011801

18021802
# ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)