Commit 4ed862d
authored
Lazy-load Apache Thrift so the SEA/kernel paths never import it (#907)
* Lazy-load Apache Thrift so SEA/kernel paths never import it
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
* Address PR review: robust guard test + preserve client.py re-exports
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
---------
Signed-off-by: Vikrant Puppala <vikrant.puppala@databricks.com>1 parent 97fa9c1 commit 4ed862d
13 files changed
Lines changed: 408 additions & 42 deletions
File tree
- src/databricks/sql
- backend
- kernel
- sea
- cloudfetch
- parameters
- tests/unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
11 | 17 | | |
12 | 18 | | |
13 | 19 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | | - | |
51 | 50 | | |
52 | 51 | | |
53 | 52 | | |
54 | 53 | | |
55 | 54 | | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
56 | 63 | | |
57 | 64 | | |
58 | 65 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
24 | | - | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
31 | 37 | | |
32 | 38 | | |
33 | 39 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
20 | 20 | | |
21 | 21 | | |
22 | 22 | | |
23 | | - | |
24 | 23 | | |
25 | 24 | | |
26 | 25 | | |
27 | 26 | | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
28 | 34 | | |
29 | 35 | | |
30 | 36 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
26 | 34 | | |
27 | 35 | | |
28 | | - | |
29 | 36 | | |
30 | 37 | | |
31 | 38 | | |
| |||
262 | 269 | | |
263 | 270 | | |
264 | 271 | | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
265 | 276 | | |
266 | 277 | | |
267 | 278 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | | - | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
9 | 19 | | |
10 | 20 | | |
11 | 21 | | |
| |||
60 | 70 | | |
61 | 71 | | |
62 | 72 | | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
63 | 78 | | |
64 | 79 | | |
65 | 80 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | | - | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
3 | 15 | | |
4 | 16 | | |
5 | 17 | | |
| |||
25 | 37 | | |
26 | 38 | | |
27 | 39 | | |
28 | | - | |
29 | | - | |
30 | 40 | | |
31 | 41 | | |
32 | 42 | | |
| |||
49 | 59 | | |
50 | 60 | | |
51 | 61 | | |
52 | | - | |
| 62 | + | |
53 | 63 | | |
54 | 64 | | |
55 | 65 | | |
| |||
60 | 70 | | |
61 | 71 | | |
62 | 72 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
68 | 85 | | |
69 | 86 | | |
70 | 87 | | |
| |||
95 | 112 | | |
96 | 113 | | |
97 | 114 | | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
98 | 142 | | |
99 | 143 | | |
100 | 144 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
4 | | - | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
| |||
10 | 12 | | |
11 | 13 | | |
12 | 14 | | |
13 | | - | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
14 | 21 | | |
15 | 22 | | |
16 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | | - | |
| 5 | + | |
4 | 6 | | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | | - | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
15 | 24 | | |
16 | 25 | | |
17 | 26 | | |
| |||
0 commit comments