Skip to content

Commit 37deeb8

Browse files
vuanhphungIsaac
andcommitted
fix(kernel): fall back to arrow_schema() when schema_key() fails
Review feedback: the schema key only saves work, so a failing schema_key() now logs at debug level and the result set builds its schema from arrow_schema() as before, instead of failing the statement. The NaN test now checks the NaN value on both paths, and the real-kernel parity tests run whenever the kernel has fetch_all_rows (failing if its conversion test hook is missing) instead of skipping on the hook alone. Signed-off-by: Vu Anh Phung <vu.phung@databricks.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 3730c3f commit 37deeb8

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,17 @@ def __init__(self, schema: pyarrow.Schema, key: Any = None):
145145

146146

147147
def _schema_key(kernel_handle: Any) -> Any:
148-
"""The handle's ``SchemaKey``, or None for kernels without them."""
148+
"""The handle's ``SchemaKey``, or None for kernels without them (or if
149+
getting it fails: it only saves work)."""
149150
try:
150151
schema_key = kernel_handle.schema_key
151152
except AttributeError:
152153
return None
153-
return schema_key()
154+
try:
155+
return schema_key()
156+
except Exception:
157+
logger.debug("schema_key() failed; using arrow_schema()", exc_info=True)
158+
return None
154159

155160

156161
class _SchemaCache:

‎tests/unit/test_kernel_result_set.py‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import datetime
88
import inspect
99
import logging
10+
import math
1011
from collections import deque
1112
from decimal import Decimal
1213
from typing import Deque
@@ -810,9 +811,16 @@ def test_kernel_keeps_nan_only_without_pandas(disable_pandas):
810811
schema = pa.schema([("f", pa.float64())])
811812
batch = pa.RecordBatch.from_arrays([pa.array([float("nan"), 1.0])], schema=schema)
812813
handle = _FakeRowsHandle(schema, [batch])
813-
rows = _make_rs(handle, disable_pandas=disable_pandas).fetchall()
814+
rs = _make_rs(handle, disable_pandas=disable_pandas)
815+
rows = rs.fetchall()
814816
assert handle.rows_calls[0][1] is disable_pandas
815-
assert len(rows) == 2 and rows[1][0] == 1.0
817+
if disable_pandas:
818+
# as_py(), as without kernel rows.
819+
assert isinstance(rows[0][0], float) and math.isnan(rows[0][0])
820+
assert rows[1][0] == 1.0
821+
else:
822+
# The kernel declines NaN, so pandas decides it, as before.
823+
_assert_same_rows(rows, ResultSet._convert_arrow_table(rs, pa.table(batch)))
816824

817825

818826
def test_results_the_kernel_declines_come_from_the_batches_it_pulled():
@@ -898,6 +906,17 @@ def schema_key(self):
898906
return _FakeSchemaKey(self._schema)
899907

900908

909+
def test_a_failing_schema_key_falls_back_to_the_arrow_schema(caplog):
910+
handle = _FakeRowsHandle(_ROWS_SCHEMA, [_rows_batch(0, 3)])
911+
handle.schema_key = MagicMock(side_effect=RuntimeError("boom"))
912+
with caplog.at_level(logging.DEBUG, logger=kernel_result_set.__name__):
913+
rs = _make_rs(handle, schema_cache=kernel_result_set._SchemaCache())
914+
assert "schema_key() failed" in caplog.text
915+
assert rs.description == _make_rs(_FakeKernelHandle(_ROWS_SCHEMA, [])).description
916+
_assert_same_rows(rs.fetchall(), _expected_rows(_rows_batch(0, 3)))
917+
assert handle.rows_calls == []
918+
919+
901920
def test_handles_with_schema_keys_but_without_fetch_all_rows_use_the_batches():
902921
rs = _make_rs(_KeyOnlyHandle(_ROWS_SCHEMA, [_rows_batch(0, 3)]))
903922
_assert_same_rows(rs.fetchall(), _expected_rows(_rows_batch(0, 3)))
@@ -935,9 +954,10 @@ class TestKernelRowsMatchThePandasPath:
935954
@staticmethod
936955
def _rows_from_arrow():
937956
kernel = pytest.importorskip("databricks_sql_kernel")
938-
rows_from_arrow = getattr(kernel, "_rows_from_arrow", None)
939-
if rows_from_arrow is None:
957+
if not hasattr(kernel.ExecutedStatement, "fetch_all_rows"):
940958
pytest.skip("kernel has no fetch_all_rows")
959+
rows_from_arrow = getattr(kernel, "_rows_from_arrow", None)
960+
assert rows_from_arrow is not None, "kernel lost its conversion test hook"
941961
return rows_from_arrow
942962

943963
@pytest.mark.parametrize("name", sorted(_columns_skipping_pandas()))

0 commit comments

Comments
 (0)