Repository navigation
Run Volume PUT, GET and REMOVE through the kernel backend #1057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
188aec4
4e58b27
6a7ce78
c3cce98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| a29235cbea9532efa5572ebff010045583a19cbe | ||
| d18b044bdd8d6a3ef7c7dd763a5f38c1532a213f |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,18 +16,18 @@ | |
|
|
||
| - ``query_tags`` on execute is not supported (kernel exposes | ||
| ``statement_conf`` but PyO3 doesn't surface it). | ||
| - Volume PUT/GET (staging operations): kernel has no Volume API | ||
| yet. Users on Thrift-only paths. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| import logging | ||
| import os | ||
| import threading | ||
| import uuid | ||
| from typing import ( | ||
| Any, | ||
| BinaryIO, | ||
| Dict, | ||
| FrozenSet, | ||
| List, | ||
|
|
@@ -84,40 +84,49 @@ | |
| # per-request skip-and-warn. | ||
| _KERNEL_MANAGED_HEADERS = frozenset({"authorization", "x-databricks-org-id"}) | ||
|
|
||
| # Leading verbs of SQL volume/staging statements. Detected by the | ||
| # leading token (case-insensitive) so the kernel backend can fail loud | ||
| # on staging ops it can't service — see ``execute_command``. | ||
| _STAGING_VERBS = ("PUT", "GET", "REMOVE") | ||
|
|
||
| def _staging_paths(paths: object) -> List[str]: | ||
| """``staging_allowed_local_path`` as a list. A value that is not a path or | ||
| a list of paths allows none, so a PUT or GET fails while a REMOVE still | ||
| runs, as on Thrift.""" | ||
| if isinstance(paths, (str, os.PathLike)): | ||
| paths = [paths] | ||
| if isinstance(paths, (list, tuple)) and all( | ||
| isinstance(path, (str, os.PathLike)) for path in paths | ||
| ): | ||
| return [os.fspath(path) for path in paths] | ||
| return [] | ||
|
|
||
| def _strip_leading_sql_comments(sql: str) -> str: | ||
| """Strip leading whitespace and SQL comments (``-- …`` line and | ||
| ``/* … */`` block, possibly several) from ``sql``, returning the | ||
| remainder. | ||
|
|
||
| Needed so staging detection sees the real leading verb: a | ||
| comment-prefixed staging op (``-- upload\\nPUT …`` or | ||
| ``/* c */ PUT …``, common in ETL scripts) must still be classified | ||
| as staging, or it would slip past the guard into the silent-no-op | ||
| bug. Block comments do not nest in Databricks SQL, so a simple | ||
| scan-to-``*/`` is correct. | ||
| """ | ||
| i = 0 | ||
| n = len(sql) | ||
| while i < n: | ||
| if sql[i].isspace(): | ||
| i += 1 | ||
| elif sql.startswith("--", i): | ||
| # Line comment: skip to end of line (or string). | ||
| nl = sql.find("\n", i) | ||
| i = n if nl == -1 else nl + 1 | ||
| elif sql.startswith("/*", i): | ||
| # Block comment: skip to closing */ (or end if unterminated). | ||
| close = sql.find("*/", i + 2) | ||
| i = n if close == -1 else close + 2 | ||
| else: | ||
| break | ||
| return sql[i:] | ||
| def _read_stream(stream: Optional[BinaryIO]) -> Optional[bytes]: | ||
| if stream is None: | ||
| return None | ||
| data = stream.read() | ||
| return data.encode("utf-8") if isinstance(data, str) else data | ||
|
|
||
|
|
||
| def _volume_error( | ||
| exc: BaseException, staging_allowed_local_path: object | ||
| ) -> Optional[Exception]: | ||
| """The exception the Thrift backend raises for the local side of a failed | ||
| Volume transfer, or None.""" | ||
| if getattr(exc, "error_code", None) == "VOLUME_PATH_NOT_ALLOWED": | ||
| if _staging_paths(staging_allowed_local_path): | ||
| return ProgrammingError( | ||
| "Local file operations are restricted to paths within the configured staging_allowed_local_path" | ||
| ) | ||
| return ProgrammingError( | ||
| "You must provide at least one staging_allowed_local_path when initialising a connection to perform ingestion commands" | ||
| ) | ||
| os_code = getattr(exc, "local_os_error", None) | ||
| if os_code is None: | ||
| return None | ||
| local_path = getattr(exc, "local_path", None) | ||
| if os.name == "nt": | ||
| # On Windows the kernel reports a Windows error code, which OSError | ||
| # maps to errno itself. | ||
| return OSError(0, "Local file operation failed", local_path, os_code) | ||
| return OSError(os_code, os.strerror(os_code), local_path) | ||
|
|
||
|
|
||
| def _is_not_found(exc: BaseException) -> bool: | ||
|
|
@@ -155,21 +164,6 @@ def _catalog_or_none(value: Optional[str]) -> Optional[str]: | |
| return value | ||
|
|
||
|
|
||
| def _is_staging_statement(operation: str) -> bool: | ||
| """True iff ``operation`` is a volume/staging statement (PUT / GET / | ||
| REMOVE). | ||
|
|
||
| Strips leading whitespace + SQL comments first (so a comment- | ||
| prefixed staging op is still caught), then matches the leading token | ||
| only — so a normal query that merely *contains* the word (e.g. | ||
| ``SELECT 'GET' AS x``) isn't misflagged. | ||
| """ | ||
| stripped = _strip_leading_sql_comments(operation) | ||
| # First whitespace-delimited token, uppercased. | ||
| verb = stripped.split(None, 1)[0].upper() if stripped.strip() else "" | ||
| return verb in _STAGING_VERBS | ||
|
|
||
|
|
||
| class _KernelSessionParameterSupport(NamedTuple): | ||
| session_type: Any | ||
| parameters: FrozenSet[str] | ||
|
|
@@ -643,6 +637,7 @@ def execute_command( | |
| enforce_embedded_schema_correctness: bool, | ||
| row_limit: Optional[int] = None, | ||
| query_tags: Optional[Dict[str, Optional[str]]] = None, | ||
| input_stream: Optional[BinaryIO] = None, | ||
| ) -> Union["ResultSet", None]: | ||
| if self._kernel_session is None: | ||
| raise InterfaceError("Cannot execute_command without an open session.") | ||
|
|
@@ -655,24 +650,6 @@ def execute_command( | |
| # ``_async_statements`` and closed by ``close_command``); the sync | ||
| # path drops it in finally. ``close_stmt`` is the post-success | ||
| # decision flag — it stays True on sync, flips to False on async. | ||
| # Volume/staging (PUT/GET/REMOVE) is not supported on the kernel | ||
| # path: the kernel returns the staging control row as a normal | ||
| # result set (``KernelResultSet.is_staging_operation`` is always | ||
| # False), so the connector's ``_handle_staging_operation`` never | ||
| # fires and NO file is transferred. Rather than silently no-op | ||
| # (the Thrift path performs the presigned-URL upload/download), | ||
| # fail loud at the call site so ETL scripts don't ingest | ||
| # stale/missing data. Detected by the leading SQL verb — the | ||
| # only signal available pre-execute, since the kernel exposes no | ||
| # staging marker today. | ||
| if _is_staging_statement(operation): | ||
| raise NotSupportedError( | ||
| "Volume / staging operations (PUT / GET / REMOVE) are not " | ||
| "supported on the kernel backend (use_kernel=True); the file " | ||
| "transfer would silently not happen. Use the Thrift backend " | ||
| "for staging operations." | ||
| ) | ||
|
|
||
| close_stmt = True | ||
| try: | ||
| try: | ||
|
|
@@ -687,6 +664,12 @@ def execute_command( | |
| stmt.set_query_tags(query_tags) | ||
| if parameters: | ||
| bind_tspark_params(stmt, parameters) | ||
| # The kernel then runs the file transfer of a Volume PUT, GET or | ||
| # REMOVE inside execute. | ||
| stmt.set_volume_options( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High — 🔀 Found by both reviewers · confirmed by the judge
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same release gate as #1057 (comment). databricks/databricks-sql-kernel#520 and #521 are merged (KERNEL_REV now pins d18b044b), but the floor and lock in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still open at c3cce98: client.py:669 calls set_volume_options unconditionally, and the kernel floor still allows wheels that lack it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High — set_volume_options is called on every statement, and KernelResultSet reads kernel_handle.is_volume_operation on every result. Neither is gated, and pyproject.toml still allows kernel ^1.5.0. If 1.5.0 lacks these APIs, every execute() fails. Gate both with hasattr and keep rejecting PUT/GET/REMOVE on old wheels, or raise the kernel floor in pyproject.toml and poetry.lock to a release that has them. 🔀 Found by both reviewers · confirmed by the judge
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same release gate as #1057 (comment). databricks/databricks-sql-kernel#520 and #521 are merged (KERNEL_REV now pins d18b044b), but the floor and lock in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still open at c3cce98: set_volume_options at client.py:669 isn't gated, and the kernel floor in pyproject.toml/poetry.lock is still ^1.5.0. |
||
| _staging_paths(cursor.connection.staging_allowed_local_path), | ||
| input_bytes=_read_stream(input_stream), | ||
| ) | ||
| if async_op: | ||
| async_exec = stmt.submit() | ||
| command_id = CommandId.from_sea_statement_id( | ||
|
|
@@ -749,7 +732,10 @@ def execute_command( | |
| ) | ||
| except Exception: | ||
| pass | ||
| raise _wrap_kernel_exception("execute_command", exc) from exc | ||
| raise ( | ||
| _volume_error(exc, cursor.connection.staging_allowed_local_path) | ||
| or _wrap_kernel_exception("execute_command", exc) | ||
| ) from exc | ||
| finally: | ||
| with self._sync_cancellers_lock: | ||
| self._sync_cancellers.pop(id(cursor), None) | ||
|
|
@@ -1031,7 +1017,10 @@ def get_execution_result( | |
| if uses_owning_handle: | ||
| with self._async_handles_lock: | ||
| self._async_result_stream_started.discard(command_id.guid) | ||
| raise _wrap_kernel_exception("get_execution_result", exc) from exc | ||
| raise ( | ||
|
aakash-saravanan-db marked this conversation as resolved.
Resolved
|
||
| _volume_error(exc, cursor.connection.staging_allowed_local_path) | ||
| or _wrap_kernel_exception("get_execution_result", exc) | ||
| ) from exc | ||
| # ``KernelResultSet.__init__`` calls ``arrow_schema()`` which | ||
| # can raise — map that to PEP 249 too. | ||
| # | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,6 +100,11 @@ def __init__( | |
| # stays O(1) instead of walking the deque. | ||
| self._buffered_count: int = 0 | ||
| self._exhausted: bool = False | ||
| if kernel_handle.is_volume_operation: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟠 High — 🔀 Found by Claude Opus 5.5 · confirmed by the judge
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same release gate as #1057 (comment). databricks/databricks-sql-kernel#520 and #521 are merged (KERNEL_REV now pins d18b044b), but the floor and lock in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still open at c3cce98: result_set.py:103 reads is_volume_operation without a guard, and the kernel floor hasn't been raised. Waiting on the kernel release. |
||
| # The transfer ran inside execute. Like the Thrift backend, which | ||
| # reads the instruction row to run it, return no rows. | ||
| self._exhausted = True | ||
| self.has_more_rows = False | ||
|
|
||
| # ----- internal helpers ----- | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟠 High — The kernel client now calls
stmt.set_volume_optionsand readsis_volume_operationon every statement. pyproject still allows kernel ^1.5.0, which lacks those APIs, so every execute can fail with AttributeError. Raise the dependency floor and the lock to the kernel release that has the Volume API.🔀 Found by GPT-6.1 Sol · confirmed by the judge
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same release gate as #1057 (comment). databricks/databricks-sql-kernel#520 and #521 are merged (KERNEL_REV now pins d18b044b), but the floor and lock in
pyproject.tomlandpoetry.lockcan only move once a kernel release ships, so this stays open until then.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still open at c3cce98: the kernel ^1.5.0 floor and lock remain unchanged, so this stays release-gated until a kernel release with the Volume API ships.