Skip to content

Commit 043df78

Browse files
ai: apply changes for #915 (1 review thread)
Addresses: - #3801996184 at src/databricks/sql/session.py:272 Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent a5e0a08 commit 043df78

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/databricks/sql/session.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,16 @@ def _create_backend(
266266
# different workspace (a different ``?o=`` or cluster path), so
267267
# re-derive the routing header from the resolved path and swap it
268268
# in — otherwise the kernel would receive an org-id that points at
269-
# the pre-override workspace (a silent mis-routing). A caller-set
270-
# header still wins: ``_spog_headers`` is empty in that case, so the
271-
# explicit header is left untouched below.
272-
if kernel_http_path != http_path and self._spog_headers:
269+
# the pre-override workspace (a silent mis-routing). This must fire
270+
# whenever the path changed, in *both* directions: when the original
271+
# path carried routing and the override drops or changes it, and
272+
# when the original had none but the override introduces one — the
273+
# latter is skipped if we also gate on ``self._spog_headers``. Any
274+
# stale extracted header is stripped first, then re-derived; when
275+
# ``_spog_headers`` is empty the filter strips nothing. A caller-set
276+
# header still wins because ``_extract_spog_headers`` re-checks the
277+
# existing headers and returns ``{}`` in that case.
278+
if kernel_http_path != http_path:
273279
base_headers = [
274280
h for h in all_headers if h not in self._spog_headers.items()
275281
]

tests/unit/test_session.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,20 @@ def test_org_id_dropped_when_override_has_no_workspace(self):
639639
)
640640
assert "x-databricks-org-id" not in headers
641641

642+
def test_org_id_added_when_override_introduces_workspace(self):
643+
# The original path carries no workspace routing (so no org-id header is
644+
# set in __init__), but the _connection_uri override introduces one —
645+
# the kernel must see org-id 222 rather than falling back to default
646+
# routing. This exercises the none -> o=222 direction, which is skipped
647+
# if re-derivation is gated on the original path having had a header.
648+
headers = self._connect_and_get_kernel_headers(
649+
{
650+
"http_path": "/sql/1.0/warehouses/abc",
651+
"_connection_uri": "https://direct.example.com/sql/1.0/warehouses/xyz?o=222",
652+
}
653+
)
654+
assert headers.get("x-databricks-org-id") == "222"
655+
642656
def test_org_id_preserved_when_no_override(self):
643657
headers = self._connect_and_get_kernel_headers(
644658
{"http_path": "/sql/1.0/warehouses/abc?o=111"}

0 commit comments

Comments
 (0)