Skip to content

Commit c4f02b4

Browse files
cathleenyIsaac
andcommitted
fix(kernel): accept a digit-string _port on the kernel path
The kernel binding takes only an int, so _port="443" (from an env var or a DSN) raised OperationalError, while Thrift accepts it. Coerce a digit string to the int it names before passing the port on; any other value still goes to the kernel as is, which validates it. Signed-off-by: Cathleen Yan <58714163+cathleeny@users.noreply.github.com> Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 976b80c commit c4f02b4

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

‎CONNECTION_PARAMETERS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ to change without notice.
7373
| ----------------- | ----- | :----: | :----: | ------------- | ----------------------------------------------------------------------------------------------------------------------- |
7474
| `server_hostname` | `str` | ✅ | ✅ | — (required) | Workspace hostname, e.g. `dbc-12345.cloud.databricks.com`. |
7575
| `http_path` | `str` | ✅ | ✅ | — (required) | Thrift accepts a SQL-warehouse path **or** an all-purpose-cluster path; Kernel requires a warehouse/endpoint path. |
76-
| `_port` | `int` | ✅ | ✅ | `443` | TCP port (advanced). On Kernel it is passed to the kernel as is, which validates and applies it: a port other than 443 makes the connection use `host:port`; `0`, unset and `443` change nothing; a non-default port embedded in `server_hostname` wins over a differing `_port` (with a warning). An IPv6 host needs brackets (`[::1]`). An invalid port raises `OperationalError` (wrapping the kernel's `ValueError`); a host that can't take the port raises `ProgrammingError`, and the error never repeats the host. Needs a databricks-sql-kernel release that includes databricks-sql-kernel#505. |
76+
| `_port` | `int` | ✅ | ✅ | `443` | TCP port (advanced). A digit string such as `"8443"` is accepted as that int. On Kernel it is passed to the kernel, which validates and applies it: a port other than 443 makes the connection use `host:port`; `0`, unset and `443` change nothing; a non-default port embedded in `server_hostname` wins over a differing `_port` (with a warning). An IPv6 host needs brackets (`[::1]`). An invalid port raises `OperationalError` (wrapping the kernel's `ValueError`); a host that can't take the port raises `ProgrammingError`, and the error never repeats the host. Needs a databricks-sql-kernel release that includes databricks-sql-kernel#505. |
7777
| `_connection_uri` | `str` | ✅ | ✅ | `None` | Overrides `server_hostname`, `_port` and `http_path` on both, and wins over `_port`. On Kernel, OAuth also uses the URI's host:port (Thrift keeps OAuth on `server_hostname`). Credentials in the URI are rejected. |
7878
| `http_headers` | `List[Tuple[str, str]]` | ✅ | ⚠️ | `None` | Extra headers sent on requests. Kernel filters `Authorization` and `x-databricks-org-id` because it manages them itself; other headers are forwarded. |
7979
| `user_agent_entry`| `str` | ✅ | ✅ | `None` | Custom tag folded into the composed `User-Agent` on both. (`_user_agent_entry` is a deprecated alias that warns.) |

‎src/databricks/sql/session.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ def _kernel_endpoint(
5757
"""
5858
uri = kwargs.get("_connection_uri")
5959
if not uri:
60-
return server_hostname, http_path, kwargs.get("_port")
60+
port = kwargs.get("_port")
61+
# A digit string (e.g. from an env var or DSN) becomes the int it names, as Thrift
62+
# accepts it. This is type coercion only; any other value reaches the kernel as is.
63+
if isinstance(port, str) and port.strip().isascii() and port.strip().isdigit():
64+
port = int(port)
65+
return server_hostname, http_path, port
6166
parts = urlsplit(uri)
6267
# Credentials in the URI would reach the transport, so they are refused here.
6368
if parts.scheme not in ("http", "https") or not parts.netloc or "@" in parts.netloc:

‎tests/unit/test_session.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,25 @@ def test_thrift_receives_host_and_port_unchanged(mock_client_class, server, port
13061306
8443,
13071307
),
13081308
({"server_hostname": "h.io", "http_path": _P, "_port": 443}, "h.io", _P, 443),
1309+
# A digit string (env var or DSN) becomes an int; any other value passes as is.
1310+
(
1311+
{"server_hostname": "h.io", "http_path": _P, "_port": "8443"},
1312+
"h.io",
1313+
_P,
1314+
8443,
1315+
),
1316+
(
1317+
{"server_hostname": "h.io", "http_path": _P, "_port": " 443 "},
1318+
"h.io",
1319+
_P,
1320+
443,
1321+
),
1322+
(
1323+
{"server_hostname": "h.io", "http_path": _P, "_port": "abc"},
1324+
"h.io",
1325+
_P,
1326+
"abc",
1327+
),
13091328
# Unset sends no port.
13101329
({"server_hostname": "h.io", "http_path": _P}, "h.io", _P, None),
13111330
# _connection_uri supplies the host (with its port) and the path; no port.

0 commit comments

Comments
 (0)