Skip to content

Commit 65483e3

Browse files
committed
fix(thrift): warn and fall back to 10 for invalid max_download_threads
A non-positive (or non-integer) max_download_threads was forwarded unvalidated to ThreadPoolExecutor(max_workers=...), so the first CloudFetch-sized query raised ValueError instead of running. Validate the knob in ThriftDatabricksClient.__init__: warn and use the default of 10, mirroring the kernel backend validation shape (which raises) but keeping the session open, as the conformance expectation requires. Fixes #903 (CLOUDFETCH-019). Signed-off-by: Atharva Chavhan <atharvachavhan18@gmail.com>
1 parent 72ac235 commit 65483e3

3 files changed

Lines changed: 74 additions & 2 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Release History
22

33
# Unreleased
4+
- Ignore invalid `max_download_threads` values on Thrift connections, warning and falling back to the default of 10 instead of failing the first CloudFetch-sized query with `ValueError: max_workers must be greater than 0`.
45
- Honor `_port` and `_connection_uri` on kernel connections (`use_kernel=True`), as Thrift does. Requires a databricks-sql-kernel release that includes databricks/databricks-sql-kernel#505.
56
- Speed up `fetchone()`, `fetchmany()` and `fetchall()` by building each result `Row` with fewer Python calls. The rows are unchanged. Large results and many concurrent cursors in one process benefit most.
67

‎src/databricks/sql/backend/thrift_backend.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,29 @@
103103
"_retry_delay_default": (float, 5, 1, 60),
104104
}
105105

106+
# Default number of threads for CloudFetch downloads on Thrift connections.
107+
_DEFAULT_MAX_DOWNLOAD_THREADS = 10
108+
109+
110+
def _coerce_max_download_threads(value) -> int:
111+
"""Validate the ``max_download_threads`` knob for Thrift connections.
112+
113+
A non-positive (or non-integer) value cannot back a
114+
``ThreadPoolExecutor`` — ``max_workers`` must be greater than 0 — so the
115+
first CloudFetch-sized query would raise ``ValueError`` instead of
116+
running. Warn and fall back to the default instead of failing the
117+
session (cf. the kernel backend, which raises ``ValueError`` for the
118+
same knob).
119+
"""
120+
if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
121+
logger.warning(
122+
"Invalid max_download_threads=%r; falling back to the default of %d.",
123+
value,
124+
_DEFAULT_MAX_DOWNLOAD_THREADS,
125+
)
126+
return _DEFAULT_MAX_DOWNLOAD_THREADS
127+
return value
128+
106129

107130
class ThriftDatabricksClient(DatabricksClient):
108131
CLOSED_OP_STATE = CommandState.CLOSED
@@ -192,7 +215,9 @@ def __init__(
192215
)
193216

194217
# Cloud fetch
195-
self._max_download_threads = kwargs.get("max_download_threads", 10)
218+
self._max_download_threads = _coerce_max_download_threads(
219+
kwargs.get("max_download_threads", _DEFAULT_MAX_DOWNLOAD_THREADS)
220+
)
196221

197222
self._ssl_options = ssl_options
198223
self._auth_provider = auth_provider

‎tests/unit/test_thrift_backend.py‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
from databricks.sql.thrift_api.TCLIService import ttypes
2020
from databricks.sql import *
2121
from databricks.sql.auth.authenticators import AuthProvider
22-
from databricks.sql.backend.thrift_backend import ThriftDatabricksClient
22+
from databricks.sql.backend.thrift_backend import (
23+
ThriftDatabricksClient,
24+
_coerce_max_download_threads,
25+
_DEFAULT_MAX_DOWNLOAD_THREADS,
26+
)
2327
from databricks.sql.result_set import ResultSet, ThriftResultSet
2428
from databricks.sql.backend.types import CommandId, CommandState, SessionId, BackendType
2529

@@ -3208,5 +3212,47 @@ def test_hive_schema_to_description(self):
32083212
self.assertEqual(description[i][1], expected_type)
32093213

32103214

3215+
def _make_thrift_client(**kwargs):
3216+
"""Build a ThriftDatabricksClient without touching the network."""
3217+
with patch("databricks.sql.auth.thrift_http_client.THttpClient"):
3218+
return ThriftDatabricksClient(
3219+
server_hostname="example.cloud.databricks.com",
3220+
port=443,
3221+
http_path="/sql/1.0/warehouses/abc",
3222+
http_headers={},
3223+
auth_provider=MagicMock(),
3224+
ssl_options=None,
3225+
http_client=MagicMock(),
3226+
**kwargs,
3227+
)
3228+
3229+
3230+
@pytest.mark.parametrize("value", [0, -1, None, "4", 1.5, True])
3231+
def test_coerce_max_download_threads_invalid_falls_back_to_default(value, caplog):
3232+
caplog.set_level(logging.WARNING)
3233+
assert _coerce_max_download_threads(value) == _DEFAULT_MAX_DOWNLOAD_THREADS
3234+
assert "max_download_threads" in caplog.text
3235+
3236+
3237+
@pytest.mark.parametrize("value", [1, 4, 10, 100])
3238+
def test_coerce_max_download_threads_valid_passthrough(value):
3239+
assert _coerce_max_download_threads(value) == value
3240+
3241+
3242+
@pytest.mark.parametrize("value", [0, -3])
3243+
def test_thrift_client_invalid_max_download_threads_falls_back_to_default(
3244+
value, caplog
3245+
):
3246+
caplog.set_level(logging.WARNING)
3247+
client = _make_thrift_client(max_download_threads=value)
3248+
assert client.max_download_threads == _DEFAULT_MAX_DOWNLOAD_THREADS
3249+
assert "max_download_threads" in caplog.text
3250+
3251+
3252+
def test_thrift_client_valid_max_download_threads_kept():
3253+
assert _make_thrift_client(max_download_threads=4).max_download_threads == 4
3254+
assert _make_thrift_client().max_download_threads == _DEFAULT_MAX_DOWNLOAD_THREADS
3255+
3256+
32113257
if __name__ == "__main__":
32123258
unittest.main()

0 commit comments

Comments
 (0)