Skip to content

Commit 6a7ce78

Browse files
aakash-saravanan-dbIsaac
andcommitted
fix(kernel): accept path objects in staging_allowed_local_path
The kernel backend sets the staging paths on every statement, so a pathlib.Path value failed every query with a TypeError, while Thrift only rejects it for a PUT or GET. A path or a list of paths is now converted with os.fspath. Any other value allows no local paths, so a PUT or GET reports the same error as Thrift and other statements still run. Also tests the Volume error mapping on the async result path. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>
1 parent 4e58b27 commit 6a7ce78

2 files changed

Lines changed: 43 additions & 7 deletions

File tree

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@
8585
_KERNEL_MANAGED_HEADERS = frozenset({"authorization", "x-databricks-org-id"})
8686

8787

88-
def _staging_paths(paths: Union[None, str, List[str]]) -> List[str]:
89-
"""``staging_allowed_local_path`` as a list. With none, a REMOVE still
88+
def _staging_paths(paths: object) -> List[str]:
89+
"""``staging_allowed_local_path`` as a list. A value that is not a path or
90+
a list of paths allows none, so a PUT or GET fails while a REMOVE still
9091
runs, as on Thrift."""
91-
if paths is None:
92-
return []
93-
return [paths] if isinstance(paths, str) else list(paths)
92+
if isinstance(paths, (str, os.PathLike)):
93+
paths = [paths]
94+
if isinstance(paths, (list, tuple)) and all(
95+
isinstance(path, (str, os.PathLike)) for path in paths
96+
):
97+
return [os.fspath(path) for path in paths]
98+
return []
9499

95100

96101
def _read_stream(stream: Optional[BinaryIO]) -> Optional[bytes]:
@@ -101,12 +106,12 @@ def _read_stream(stream: Optional[BinaryIO]) -> Optional[bytes]:
101106

102107

103108
def _volume_error(
104-
exc: BaseException, staging_allowed_local_path: Union[None, str, List[str]]
109+
exc: BaseException, staging_allowed_local_path: object
105110
) -> Optional[Exception]:
106111
"""The exception the Thrift backend raises for the local side of a failed
107112
Volume transfer, or None."""
108113
if getattr(exc, "error_code", None) == "VOLUME_PATH_NOT_ALLOWED":
109-
if staging_allowed_local_path:
114+
if _staging_paths(staging_allowed_local_path):
110115
return ProgrammingError(
111116
"Local file operations are restricted to paths within the configured staging_allowed_local_path"
112117
)

‎tests/unit/test_kernel_client.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import threading
2121
import types
2222
from concurrent.futures import ThreadPoolExecutor
23+
from pathlib import PurePosixPath
2324
from typing import Optional
2425
from unittest.mock import MagicMock
2526

@@ -1113,6 +1114,11 @@ def _execute_failing(staging_allowed_local_path, error, input_stream=None):
11131114
(None, None, [], None),
11141115
("/data", io.BytesIO(b"\x00bytes"), ["/data"], b"\x00bytes"),
11151116
(["/a", "/b"], io.StringIO("text"), ["/a", "/b"], b"text"),
1117+
(PurePosixPath("/data"), None, ["/data"], None),
1118+
((PurePosixPath("/a"), "/b"), None, ["/a", "/b"], None),
1119+
# A value that is not a path or a list of paths allows none.
1120+
(42, None, [], None),
1121+
(["/a", 42], None, [], None),
11161122
],
11171123
)
11181124
def test_execute_sets_the_volume_options(
@@ -1133,6 +1139,12 @@ def test_execute_sets_the_volume_options(
11331139
ProgrammingError,
11341140
"^You must provide at least one staging_allowed_local_path",
11351141
),
1142+
(
1143+
42,
1144+
{"error_code": "VOLUME_PATH_NOT_ALLOWED"},
1145+
ProgrammingError,
1146+
"^You must provide at least one staging_allowed_local_path",
1147+
),
11361148
(
11371149
"/data",
11381150
{"error_code": "VOLUME_PATH_NOT_ALLOWED"},
@@ -1602,6 +1614,25 @@ def test_get_execution_result_owning_handle_failure_can_retry_owning_handle():
16021614
c._kernel_session.attach_async_statement.assert_not_called()
16031615

16041616

1617+
def test_get_execution_result_maps_volume_errors_like_execute():
1618+
"""An async Volume PUT or GET reports local errors as a sync one does."""
1619+
c = _make_client()
1620+
c._kernel_session = MagicMock()
1621+
handle = MagicMock()
1622+
error = _FakeKernelError()
1623+
error.error_code = "VOLUME_PATH_NOT_ALLOWED"
1624+
handle.await_result.side_effect = error
1625+
cid = CommandId.from_sea_statement_id("async-volume")
1626+
c._async_handles[cid.guid] = handle
1627+
cursor = MagicMock()
1628+
cursor.connection.staging_allowed_local_path = "/data"
1629+
1630+
with pytest.raises(
1631+
ProgrammingError, match="^Local file operations are restricted to paths within"
1632+
):
1633+
c.get_execution_result(cid, cursor=cursor)
1634+
1635+
16051636
def test_get_execution_result_construction_failure_retains_marker_and_uses_reader():
16061637
"""If the owning handle's ``await_result()`` succeeds but result-set
16071638
construction then raises, the ``_async_result_stream_started`` marker

0 commit comments

Comments
 (0)