Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Release History

# Unreleased
- 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`.
- 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.
- 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.

Expand Down
27 changes: 26 additions & 1 deletion src/databricks/sql/backend/thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,29 @@
"_retry_delay_default": (float, 5, 1, 60),
}

# Default number of threads for CloudFetch downloads on Thrift connections.
_DEFAULT_MAX_DOWNLOAD_THREADS = 10


def _coerce_max_download_threads(value) -> int:
"""Validate the ``max_download_threads`` knob for Thrift connections.

A non-positive (or non-integer) value cannot back a
``ThreadPoolExecutor`` — ``max_workers`` must be greater than 0 — so the
first CloudFetch-sized query would raise ``ValueError`` instead of
running. Warn and fall back to the default instead of failing the
session (cf. the kernel backend, which raises ``ValueError`` for the
same knob).
"""
if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
logger.warning(
"Invalid max_download_threads=%r; falling back to the default of %d.",
value,
_DEFAULT_MAX_DOWNLOAD_THREADS,
)
return _DEFAULT_MAX_DOWNLOAD_THREADS
return value


class ThriftDatabricksClient(DatabricksClient):
CLOSED_OP_STATE = CommandState.CLOSED
Expand Down Expand Up @@ -192,7 +215,9 @@ def __init__(
)

# Cloud fetch
self._max_download_threads = kwargs.get("max_download_threads", 10)
self._max_download_threads = _coerce_max_download_threads(
kwargs.get("max_download_threads", _DEFAULT_MAX_DOWNLOAD_THREADS)
)

self._ssl_options = ssl_options
self._auth_provider = auth_provider
Expand Down
48 changes: 47 additions & 1 deletion tests/unit/test_thrift_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
from databricks.sql.thrift_api.TCLIService import ttypes
from databricks.sql import *
from databricks.sql.auth.authenticators import AuthProvider
from databricks.sql.backend.thrift_backend import ThriftDatabricksClient
from databricks.sql.backend.thrift_backend import (
ThriftDatabricksClient,
_coerce_max_download_threads,
_DEFAULT_MAX_DOWNLOAD_THREADS,
)
from databricks.sql.result_set import ResultSet, ThriftResultSet
from databricks.sql.backend.types import CommandId, CommandState, SessionId, BackendType

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


def _make_thrift_client(**kwargs):
"""Build a ThriftDatabricksClient without touching the network."""
with patch("databricks.sql.auth.thrift_http_client.THttpClient"):
return ThriftDatabricksClient(
server_hostname="example.cloud.databricks.com",
port=443,
http_path="/sql/1.0/warehouses/abc",
http_headers={},
auth_provider=MagicMock(),
ssl_options=None,
http_client=MagicMock(),
**kwargs,
)


@pytest.mark.parametrize("value", [0, -1, None, "4", 1.5, True])
def test_coerce_max_download_threads_invalid_falls_back_to_default(value, caplog):
caplog.set_level(logging.WARNING)
assert _coerce_max_download_threads(value) == _DEFAULT_MAX_DOWNLOAD_THREADS
assert "max_download_threads" in caplog.text


@pytest.mark.parametrize("value", [1, 4, 10, 100])
def test_coerce_max_download_threads_valid_passthrough(value):
assert _coerce_max_download_threads(value) == value


@pytest.mark.parametrize("value", [0, -3])
def test_thrift_client_invalid_max_download_threads_falls_back_to_default(
value, caplog
):
caplog.set_level(logging.WARNING)
client = _make_thrift_client(max_download_threads=value)
assert client.max_download_threads == _DEFAULT_MAX_DOWNLOAD_THREADS
assert "max_download_threads" in caplog.text


def test_thrift_client_valid_max_download_threads_kept():
assert _make_thrift_client(max_download_threads=4).max_download_threads == 4
assert _make_thrift_client().max_download_threads == _DEFAULT_MAX_DOWNLOAD_THREADS


if __name__ == "__main__":
unittest.main()