Lazy-load Apache Thrift so the SEA/kernel paths never import it - #907
Merged
Conversation
vikrantpuppala
temporarily deployed
to
azure-prod
August 13, 2026 08:51 — with
GitHub Actions
Inactive
vikrantpuppala
temporarily deployed
to
azure-prod
August 13, 2026 08:51 — with
GitHub Actions
Inactive
There was a problem hiding this comment.
Verdict: 1 Medium · 1 Low
Solid, well-scoped lazy-import refactor — the annotation-only moves are all correctly guarded by from __future__ import annotations, every runtime ttypes use on Thrift-only paths has a function-local import, and the patch(...) test seams are preserved via PEP 562 __getattr__. Two items: a Medium test-robustness bug where the new regression test's exit-code scheme can mask an import failure (notably a false-pass for the Thrift-backend counterpart test), and a Low back-compat note on dropped client.py re-exports.
vikrantpuppala
temporarily deployed
to
azure-prod
August 13, 2026 19:37 — with
GitHub Actions
Inactive
vikrantpuppala
temporarily deployed
to
azure-prod
August 13, 2026 19:37 — with
GitHub Actions
Inactive
vikrantpuppala
had a problem deploying
to
azure-prod
August 13, 2026 19:37 — with
GitHub Actions
Failure
msrathore-db
approved these changes
Aug 26, 2026
vikrantpuppala
enabled auto-merge
August 26, 2026 17:52
vikrantpuppala
disabled auto-merge
August 26, 2026 17:52
The connector declared `thrift` as a hard dependency and imported it
eagerly the moment `connect()` loaded `databricks.sql.client` -- 16
thrift modules, including the top-level `thrift` package -- regardless
of whether the caller selected `use_sea=True` or `use_kernel=True`.
Build systems that vendor their own `thrift` (e.g. Meta's Buck) then hit
a namespace collision even on the SEA / kernel code paths, which never
speak Thrift on the wire.
This makes the thrift import lazy without changing any public API or
install semantics: `thrift` stays a base dependency, but it is only
imported when the Thrift backend is actually used.
Mechanism, per module on the connect/execute chain:
- Annotation-only uses (the cloud-fetch download manager/downloader,
the DatabricksClient ABC's `execute_command`, and the SEA/kernel
`TSparkParameter` annotations): add `from __future__ import
annotations` and move the thrift import under `TYPE_CHECKING`, so the
annotations are never evaluated at runtime.
- Runtime uses on Thrift-only paths (`from_thrift_state`, the queue
factory's `TSparkRowSetType`, `TProtocolVersion`, the SEA
`_convert_to_thrift_link` construction, and every `TSparkParameter*`
construction in parameters/native): move the import into the function
body.
- Backend selection in `Session.open`: resolve `ThriftDatabricksClient`
/ `SeaDatabricksClient` via a module-level `__getattr__` (PEP 562) and
reference them through the module namespace, so thrift is imported
only on the branch that needs it. This also preserves the
`patch("...session.ThriftDatabricksClient")` test seam.
- Preserve the historical re-exports `parameters.native.TSparkParameter*`
and `client.ThriftDatabricksClient` via lazy `__getattr__` so existing
importers (and tests) keep working without importing thrift at load.
Add tests/unit/test_lazy_thrift_import.py, which imports the connector
and each non-Thrift backend in a fresh subprocess and asserts the
top-level `thrift` package is absent from sys.modules (and, conversely,
that the Thrift backend still imports it). This locks the invariant --
a single stray module-level thrift import re-poisons the whole path.
Verified empirically: importing `databricks.sql.client` and the SEA /
kernel backend modules loads zero thrift modules, while the Thrift
backend still loads thrift. Full unit suite passes (the two failures
present also reproduce unchanged on main: a kernel test-ordering issue
and a realkernel-marked test).
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>
Co-authored-by: Isaac
Follow-up to the lazy-thrift change addressing two review-bot findings. Medium (test robustness / CI false-positive): the guard test's child subprocess exited with code 1 on any failure, which is also Python's generic uncaught-exception code -- so an import failure was indistinguishable from "thrift was imported". This caused a false CI failure: in the "default deps" job (no pyarrow), importing `kernel.type_mapping` raises `ModuleNotFoundError: pyarrow` (exit 1), which the test misread as a thrift leak. It was also a false *pass* risk for the Thrift-backend counterpart test. The child now emits dedicated sentinel exit codes only after the import completes, captures stderr, and reports import failure separately; the parametrized test skips modules that can't import due to a missing optional dependency (rather than failing), while still detecting a genuine thrift leak. Low (back-compat): `client.py`'s `__getattr__` only re-exported `ThriftDatabricksClient`. Extend it to also lazily resolve the other names `client.py` historically exposed as importable (`ThriftResultSet`, `TOpenSessionResp`, `TSparkParameter`, `TOperationState`), so `from databricks.sql.client import <name>` keeps working without importing the `thrift` package at module load. Verified: importing `databricks.sql.client` still loads zero thrift modules; touching any re-export resolves correctly (and only then pulls thrift). Guard test passes with full deps (14/14) and correctly skips the kernel modules when pyarrow/kernel are absent. Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com> Co-authored-by: Isaac
vikrantpuppala
force-pushed
the
lazy-load-thrift
branch
from
August 26, 2026 18:08
200201d to
88b1b04
Compare
vikrantpuppala
had a problem deploying
to
azure-prod
August 26, 2026 18:08 — with
GitHub Actions
Failure
vikrantpuppala
temporarily deployed
to
azure-prod
August 26, 2026 18:08 — with
GitHub Actions
Inactive
vikrantpuppala
temporarily deployed
to
azure-prod
August 26, 2026 18:08 — with
GitHub Actions
Inactive
vikrantpuppala
enabled auto-merge
August 26, 2026 18:20
vikrantpuppala
temporarily deployed
to
azure-prod
August 26, 2026 18:32 — with
GitHub Actions
Inactive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The connector imports the PyPI
thriftpackage eagerly the momentconnect()loadsdatabricks.sql.client— 16 thrift modules including the top-levelthriftpackage — regardless of whether the caller selecteduse_sea=Trueoruse_kernel=True. The SEA and kernel backends never speak Thrift on the wire, yet they still drag it in at connect time.This breaks build systems that vendor their own
thriftunder thethrifttop-level namespace (e.g. Meta's Buck): two packages own the same namespace and the build is rejected, even though the connector's SEA/kernel path never uses Apache Thrift.Reproduction (before this change):
What this changes
Makes the thrift import lazy — imported only when the Thrift backend is actually used. No public API changes and no install-semantics changes:
thriftstays a base dependency; it is simply not imported on the SEA/kernel path.Per module on the connect/execute chain:
DatabricksClientABC'sexecute_command, and the SEA/kernelTSparkParameterannotations): addfrom __future__ import annotationsand move the thrift import underTYPE_CHECKING, so annotations are never evaluated at runtime.from_thrift_state, the queue factory'sTSparkRowSetType,TProtocolVersion, the SEA_convert_to_thrift_linkconstruction, and theTSparkParameter*constructions inparameters/native): move the import into the function body.Session.open: resolveThriftDatabricksClient/SeaDatabricksClientvia a module-level__getattr__(PEP 562) and reference them through the module namespace, so thrift is imported only on the branch that needs it. This also preserves thepatch("...session.ThriftDatabricksClient")test seam.parameters.native.TSparkParameter*andclient.ThriftDatabricksClientvia lazy__getattr__, so existing importers (and tests) keep working without importing thrift at load.New test
tests/unit/test_lazy_thrift_import.pyimports the connector and each non-Thrift backend in a fresh subprocess and asserts the top-levelthriftpackage is absent fromsys.modules(and, conversely, that the Thrift backend still imports it).sys.modulesis process-global, so the subprocess isolation is what makes the check reliable — a single stray module-level thrift import anywhere on the chain re-poisons the whole path, and this test catches that.Verification
databricks.sql.clientand every SEA/kernel backend module loads zero thrift modules; the Thrift backend still loads thrift. ✅main: a kernel test-ordering issue intest_session.pyand arealkernel-marked test.blackclean on all changed files.Notes for reviewers / follow-ups
thriftphysically absent from the install (disk-presence collision), that's a separate packaging change (thrift → optional extra) with a default-install-backend compatibility impact — intentionally not included here.Cursor.executewas already thrift-free (it takes plain Python params); only the internalexecute_commandABC referencedTSparkParameter, and that is annotation-only.This pull request and its description were written by Isaac.