Skip to content

Commit 7fc2d5f

Browse files
refactor(kernel): simplify _port folding to the bare-host case
server_hostname reaches the backend as a bare host on this path, so drop the defensive scheme peel/re-add: just append the port when the host has none, and let the kernel's normalise_host add the scheme. Removes the now-moot scheme-preservation test. Co-authored-by: Isaac Signed-off-by: eric-wang-1990 <e.wang@databricks.com>
1 parent 1adcb29 commit 7fc2d5f

2 files changed

Lines changed: 7 additions & 18 deletions

File tree

src/databricks/sql/session.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ def _kernel_host_and_path(
5656

5757
port = kwargs.get("_port")
5858
if port is not None:
59-
# Split off any scheme so we can inspect the authority; the kernel
60-
# re-adds https:// when it is absent. Only append the port when the
61-
# authority does not already carry one.
62-
scheme_match = re.match(r"^(https?://)(.*)$", server_hostname)
63-
scheme = scheme_match.group(1) if scheme_match else ""
64-
authority = (scheme_match.group(2) if scheme_match else server_hostname).rstrip(
65-
"/"
66-
)
67-
if ":" not in authority:
68-
authority = "{}:{}".format(authority, port)
69-
return "{}{}".format(scheme, authority), http_path
59+
# server_hostname is a bare host on this path (e.g.
60+
# ``dbc-123.cloud.databricks.com``); the kernel adds the scheme.
61+
# Append the port unless the host already carries one.
62+
host = server_hostname.rstrip("/")
63+
if ":" not in host:
64+
host = "{}:{}".format(host, port)
65+
return host, http_path
7066

7167
return server_hostname, http_path
7268

tests/unit/test_session.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -643,13 +643,6 @@ def test_port_folded_into_bare_host(self):
643643
assert host == "{}:8443".format(self.HOST)
644644
assert path == self.PATH
645645

646-
def test_port_preserves_existing_scheme(self):
647-
host, path = _kernel_host_and_path(
648-
"https://" + self.HOST, self.PATH, {"_port": 8443}
649-
)
650-
assert host == "https://{}:8443".format(self.HOST)
651-
assert path == self.PATH
652-
653646
def test_port_not_double_appended_when_host_has_port(self):
654647
host, _ = _kernel_host_and_path(self.HOST + ":7000", self.PATH, {"_port": 8443})
655648
assert host == self.HOST + ":7000"

0 commit comments

Comments
 (0)