Skip to content

Commit dda710f

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

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

src/databricks/sql/session.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def _kernel_host_and_path(
3737
(returned as ``host``) and its path+query (returned as ``http_path``).
3838
``_connection_uri`` wins over ``_port``, matching the Thrift backend.
3939
When the URI omits a path (e.g. ``https://host:8443`` or
40-
``https://host:8443?o=222``) the connection's original ``http_path`` is
40+
``https://host:8443?o=222``, and likewise a lone trailing slash such as
41+
``https://host:8443/``) the connection's original ``http_path`` is
4142
retained, and any query on the URI is applied to that retained path,
4243
replacing any query the retained path already carried — so a path-less,
4344
query-bearing URI overrides only the host and query while keeping the
@@ -67,8 +68,12 @@ def _kernel_host_and_path(
6768
host = "{}://{}".format(parts.scheme, parts.netloc)
6869
# A path-less URI keeps the connection's original http_path; any query
6970
# on the URI is then applied to that retained path (host + query
70-
# override, path preserved). See the docstring for the rationale.
71-
path = parts.path or http_path
71+
# override, path preserved). A lone ``"/"`` path (common from a
72+
# copy-pasted base URL like ``https://host:8443/``) is treated the same
73+
# as an absent path so it does not silently point the kernel at the host
74+
# root instead of the warehouse. See the docstring for the rationale.
75+
uri_path = parts.path if parts.path not in ("", "/") else None
76+
path = uri_path or http_path
7277
if parts.query:
7378
# Drop any query already on the retained path before applying the
7479
# URI's query, so the URI's query fully overrides it. Appending

tests/unit/test_session.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,26 @@ def test_connection_uri_without_path_retains_original_path(self):
771771
assert host == "https://h.example.com:8443"
772772
assert path == self.PATH
773773

774+
def test_connection_uri_trailing_slash_retains_original_path(self):
775+
# A lone trailing slash (common when a base URL is copy-pasted, e.g.
776+
# ``https://host:8443/``) must be treated the same as an absent path so
777+
# the original warehouse http_path is retained rather than silently
778+
# replaced with the host root ``"/"``.
779+
host, path = _kernel_host_and_path(
780+
self.HOST, self.PATH, {"_connection_uri": "https://h.example.com:8443/"}
781+
)
782+
assert host == "https://h.example.com:8443"
783+
assert path == self.PATH
784+
785+
def test_connection_uri_trailing_slash_applies_query_to_retained_path(self):
786+
# A trailing-slash, query-bearing URI overrides the host and applies the
787+
# query to the retained original path, just like the path-less case.
788+
host, path = _kernel_host_and_path(
789+
self.HOST, self.PATH, {"_connection_uri": "https://h.example.com:8443/?o=222"}
790+
)
791+
assert host == "https://h.example.com:8443"
792+
assert path == "{}?o=222".format(self.PATH)
793+
774794
def test_connection_uri_without_path_applies_query_to_retained_path(self):
775795
# A path-less, query-bearing URI overrides the host and applies the
776796
# query to the retained original path (documented fallback semantics).

0 commit comments

Comments
 (0)