Skip to content

Commit eb04529

Browse files
feat(results): make Arrow LZ4 compression configurable (#511)
Status: merge awaits current-head CI and review. ## Feature or gap The released SQL kernel always requests LZ4 compression for Arrow results, so applications cannot opt out. ## What this PR addresses LZ4 remains enabled by default. The kernel accepts a session default and a statement override, and uses the corresponding codec for inline and CloudFetch requests. The Rust API and the Python (PyO3) and Node.js (napi) bindings expose statement overrides. The C and Arrow Database Connectivity (ADBC) interfaces expose only the session default. ## Dependencies None. ## Tests and validation Request tests cover defaults, overrides, reset, and metadata. Optimized source-built drivers returned complete values with both codecs and delivery modes. Comparable timings met parity or improved except Node.js `runAsync: true` queries with compressed inline results compared with the Thrift backend (542→664 ms, +22.4%); that gap is not yet resolved. --------- Signed-off-by: Aakash Saravanan <aakash.saravanan@databricks.com>
1 parent 74c5cad commit eb04529

25 files changed

Lines changed: 242 additions & 69 deletions

‎CONNECTION_PARAMETERS.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ Setting only one of `client_cert_pem` / `client_key_pem` is rejected at
228228
| Field | Description | Default |
229229
|-------|-------------|---------|
230230
| `cloudfetch_enabled` | Opt into CloudFetch (cloud-storage-backed download of large results via presigned URLs). Set `false` to force the inline-Arrow path (mirrors JDBC `EnableQueryResultDownload=0`). | `true` |
231+
| `lz4_compression_enabled` | Request LZ4 compression for Arrow results, including inline and CloudFetch delivery. Statements can override this default via `StatementSpec::lz4_compression`. | `true` |
231232
| `cloudfetch_link_prefetch_window` | Number of presigned URLs to prefetch ahead of the consumer. | `10` |
232233
| `cloudfetch_max_chunks_in_memory` | Max decompressed CloudFetch chunks held in memory at once (bounds download parallelism and peak RSS). | `16` |
233234
| `inline_max_chunks_in_memory` | Prefetch window for the inline-Arrow path (used when CloudFetch is disabled). `1` disables prefetching. | `4` |
@@ -314,12 +315,12 @@ Each key mirrors a field of [`ResultConfig`](#resultconfig--result-handling-opti
314315
| Key | Description | Default |
315316
|-----|-------------|---------|
316317
| `cloudfetch_enabled` | Same knob as `ResultConfig::cloudfetch_enabled` — opt into CloudFetch (`false` forces the inline-Arrow path). | `true` |
318+
| `lz4_compression_enabled` | Same knob as `ResultConfig::lz4_compression_enabled` — request LZ4 compression for Arrow results. | `true` |
317319
| `cloudfetch_link_prefetch_window` | Same knob as `ResultConfig::cloudfetch_link_prefetch_window` — number of presigned URLs to prefetch ahead of the consumer. | `10` |
318320
| `cloudfetch_max_chunks_in_memory` | Same knob as `ResultConfig::cloudfetch_max_chunks_in_memory` — max decompressed CloudFetch chunks held in memory at once. Values above `256` are clamped (with a warning) rather than rejected (peak RSS scales with this, so the ceiling bounds OOM risk). | `16` |
319321
| `inline_max_chunks_in_memory` | Same knob as `ResultConfig::inline_max_chunks_in_memory` — prefetch window for the inline-Arrow path. `1` disables prefetching. | `4` |
320322
| `complex_types_as_json` | Same knob as `ResultConfig::complex_types_as_json` — render complex columns as compact JSON strings instead of native Arrow nested types. | `false` |
321323
| `intervals_as_string` | Same knob as `ResultConfig::intervals_as_string` — render `INTERVAL` / `DURATION` columns as canonical Databricks text instead of native Arrow interval types. | `false` |
322324

323-
Compression (LZ4) is **not** a parameter: the kernel detects it from the
324-
server's result manifest (`result_compression`) and decompresses
325-
`LZ4_FRAME` payloads automatically.
325+
The kernel reads the codec from the server's result manifest
326+
(`result_compression`) and decompresses `LZ4_FRAME` payloads automatically.

‎NEXT_CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ changes go in their respective binding's NEXT_CHANGELOG.
2828
allowed directories. A transfer fails after `HttpConfig::request_timeout`
2929
without progress. On Unix a GET can also write into an existing pipe or
3030
character device as the download arrives.
31+
- Configure the Arrow result LZ4 session default with `ResultConfig.lz4_compression_enabled`
32+
(client config key `lz4_compression_enabled`) and override it with `StatementSpec::lz4_compression`.
33+
ADBC exposes the default through `adbc.databricks.enable_query_result_lz4_compression`.
3134

3235
### Changed
3336

‎adbc/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ and proxy behavior, TLS, logging, telemetry, result fetching, query execution,
6363
and metadata. Supply credentials at runtime and do not place them in source
6464
code, manifests, logs, or connection URLs.
6565

66+
LZ4 compression is enabled by default for Arrow query results. Set the database
67+
option `adbc.databricks.enable_query_result_lz4_compression` to `"false"` or
68+
integer `0` to request uncompressed results.
69+
6670
## Supported functionality
6771

6872
The driver supports query and update execution, parameter binding,

‎adbc/src/config.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ const GEOSPATIAL_REPRESENTATION: &str = "adbc.databricks.geospatial.result_repre
6363
const ENABLE_GEOSPATIAL: &str = "adbc.databricks.enable_geospatial_support";
6464
const CLOUDFETCH_MAX_CHUNKS_IN_MEMORY: &str = "adbc.databricks.cloudfetch.max_chunks_in_memory";
6565
const CLOUDFETCH_ENABLED: &str = "adbc.databricks.cloudfetch.enabled";
66+
const ENABLE_QUERY_RESULT_LZ4_COMPRESSION: &str =
67+
"adbc.databricks.enable_query_result_lz4_compression";
6668
const PROTOCOL: &str = "adbc.databricks.protocol";
6769
pub(crate) const ESCAPE_CATALOG_NAME: &str = "adbc.databricks.metadata.escape_catalog_name";
6870

@@ -372,6 +374,9 @@ impl DatabaseConfig {
372374
CLOUDFETCH_ENABLED | "databricks.cloudfetch.enabled" => {
373375
self.result.cloudfetch_enabled = boolean_compat(value, key)?
374376
}
377+
ENABLE_QUERY_RESULT_LZ4_COMPRESSION => {
378+
self.result.lz4_compression_enabled = boolean_compat(value, key)?
379+
}
375380
PROTOCOL => {
376381
let protocol = string_value(value, key)?;
377382
if !protocol.eq_ignore_ascii_case("rest") {
@@ -1248,6 +1253,28 @@ mod tests {
12481253
assert!(config.result.cloudfetch_enabled);
12491254
}
12501255

1256+
#[test]
1257+
fn lz4_compression_option_configures_kernel_result_codec() {
1258+
let mut config = DatabaseConfig::default();
1259+
assert!(config.result.lz4_compression_enabled);
1260+
1261+
let key = ENABLE_QUERY_RESULT_LZ4_COMPRESSION;
1262+
for (value, expected) in [
1263+
("false".into(), false),
1264+
("true".into(), true),
1265+
(OptionValue::Int(0), false),
1266+
(OptionValue::Int(1), true),
1267+
] {
1268+
config.set_option(key, value).unwrap();
1269+
assert_eq!(config.result.lz4_compression_enabled, expected);
1270+
}
1271+
for value in ["bogus".into(), OptionValue::Int(2)] {
1272+
let error = config.set_option(key, value).unwrap_err();
1273+
assert_eq!(error.status, Status::InvalidArguments);
1274+
assert!(config.result.lz4_compression_enabled);
1275+
}
1276+
}
1277+
12511278
#[test]
12521279
fn complex_datatype_support_configures_kernel_result_shape() {
12531280
let mut config = DatabaseConfig::default();

‎include/databricks_kernel.h‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,8 @@ KernelStatusCode kernel_session_config_set_u2m_token_cache_config(KernelSessionC
628628
*
629629
* Counterpart to kernel_session_config_set_session_conf: that one carries
630630
* SERVER session confs (ANSI_MODE, TIMEZONE, ...) to the SEA wire; this one
631-
* sets CLIENT-SIDE knobs that tune how the kernel fetches, buffers, and
632-
* post-processes results and are NEVER sent to the server. The two channels
633-
* are deliberately separate (conflating them is what made a client flag
634-
* masquerade as a rejected server conf).
631+
* configures how the kernel requests, fetches, buffers, and post-processes
632+
* results. These keys are kept out of the server session configuration.
635633
*
636634
* Key/value (rather than one typed setter per knob) so the signature never
637635
* changes as new result knobs are added, and so a host that already has DSN
@@ -642,6 +640,9 @@ KernelStatusCode kernel_session_config_set_u2m_token_cache_config(KernelSessionC
642640
* Recognised keys (case-insensitive):
643641
* - "cloudfetch_enabled" = "true"|"false" — false serves every result
644642
* through paged inline delivery without cloud-storage downloads.
643+
* - "lz4_compression_enabled" = "true"|"false", for LZ4 compression
644+
* for Arrow results. Sets the session default; defaults to "true".
645+
* The C ABI has no per-statement compression setter.
645646
* - "cloudfetch_link_prefetch_window" = positive integer.
646647
* - "cloudfetch_max_chunks_in_memory" = positive integer (bounds peak RSS;
647648
* values above the kernel ceiling of 256 are clamped with a warning).

‎napi/NEXT_CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ layout, or npm publishing. Pure-kernel changes go in the root
1717
- `ConnectionOptions.port`: an optional connection port. A value outside
1818
`0..=65535`, or a host the kernel can't combine with the port, gives
1919
`InvalidArgument`.
20+
- `ExecuteOptions.lz4Compression` overrides Arrow result LZ4 compression per statement.
2021

2122
### Changed
2223

‎napi/index.d.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* `key:value` wire shape NodeJS Thrift's `serializeQueryTags`
1717
* produces (`lib/utils/queryTags.ts`). Backslashes in keys are
1818
* doubled; backslash/colon/comma in values are backslash-escaped.
19+
* - `lz4Compression` — override the session default for Arrow result compression
1920
*
2021
* `rowLimit` (SEA `row_limit`) is exposed here and threaded onto the kernel
2122
* `StatementSpec`. `positionalParams` (`?`) and `namedParams` (`:name`)
@@ -68,6 +69,8 @@ export interface ExecuteOptions {
6869
* `StatementSpec.row_limit`. Omitted ⇒ no driver-imposed cap.
6970
*/
7071
rowLimit?: number
72+
/** Request LZ4 compression for Arrow results. Omit to use the session default. */
73+
lz4Compression?: boolean
7174
/**
7275
* Positional parameters, in 1-based wire order. Index `i` in this
7376
* Vec corresponds to the `i+1`-th `?` placeholder in the SQL.
@@ -1077,7 +1080,7 @@ export declare class Connection {
10771080
* uses (`runAsync: true`): the SEA backend submits, returns a
10781081
* pending operation handle, and polls to terminal during
10791082
* fetch. Option semantics (statementConf / queryTags /
1080-
* rowLimit / positional + named params) match `executeStatement`.
1083+
* rowLimit / lz4Compression / positional + named params) match `executeStatement`.
10811084
* Submit always sends `wait_timeout=0s` so the call returns
10821085
* immediately; the caller drives completion via `status()` /
10831086
* `awaitResult()`. Only the blocking-vs-pending return contract

‎napi/src/connection.rs‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use crate::util::guarded;
5252
/// `key:value` wire shape NodeJS Thrift's `serializeQueryTags`
5353
/// produces (`lib/utils/queryTags.ts`). Backslashes in keys are
5454
/// doubled; backslash/colon/comma in values are backslash-escaped.
55+
/// - `lz4Compression` — override the session default for Arrow result compression
5556
///
5657
/// `rowLimit` (SEA `row_limit`) is exposed here and threaded onto the kernel
5758
/// `StatementSpec`. `positionalParams` (`?`) and `namedParams` (`:name`)
@@ -102,6 +103,8 @@ pub struct ExecuteOptions {
102103
/// (SEA `row_limit`), independent of any SQL `LIMIT`. Maps to
103104
/// `StatementSpec.row_limit`. Omitted ⇒ no driver-imposed cap.
104105
pub row_limit: Option<i64>,
106+
/// Request LZ4 compression for Arrow results. Omit to use the session default.
107+
pub lz4_compression: Option<bool>,
105108
/// Positional parameters, in 1-based wire order. Index `i` in this
106109
/// Vec corresponds to the `i+1`-th `?` placeholder in the SQL.
107110
/// Each entry is a `{ sqlType, value }` pair — `value` is the
@@ -190,6 +193,7 @@ fn build_statement(
190193
statement_conf: Option<HashMap<String, String>>,
191194
query_tags: Option<HashMap<String, Option<String>>>,
192195
row_limit: Option<i64>,
196+
lz4_compression: Option<bool>,
193197
positional_params: Vec<TypedValue>,
194198
named_params: Vec<(String, TypedValue)>,
195199
raw_params: Vec<RawParameterInput>,
@@ -215,6 +219,7 @@ fn build_statement(
215219
}
216220
// `None` leaves the kernel's omit-the-field default in place.
217221
stmt.spec().row_limit(row_limit);
222+
stmt.spec().lz4_compression(lz4_compression);
218223
// Bind positional params 1-based on the wire.
219224
for (i, value) in positional_params.into_iter().enumerate() {
220225
stmt.spec()
@@ -293,11 +298,10 @@ impl Connection {
293298
mut options: Option<ExecuteOptions>,
294299
) -> napi::Result<Statement> {
295300
let inner = Arc::clone(&self.inner);
296-
// Pull the scalar knobs out (both `i64`/`u32` are `Copy`) before
297-
// the `.take()`s below consume the map fields out of `options`.
298-
let row_limit = options.as_ref().and_then(|o| o.row_limit); // Decode positional params before `options` is consumed below; a
299-
// malformed entry surfaces as InvalidArgument here, before any wire
300-
// round-trip.
301+
// Read scalar options before consuming the other fields in `options`.
302+
let row_limit = options.as_ref().and_then(|o| o.row_limit);
303+
let lz4_compression = options.as_ref().and_then(|o| o.lz4_compression);
304+
// Invalid positional parameters fail here before any wire round-trip.
301305
let positional_params = options
302306
.as_mut()
303307
.and_then(|o| o.positional_params.take())
@@ -342,6 +346,7 @@ impl Connection {
342346
statement_conf,
343347
query_tags,
344348
row_limit,
349+
lz4_compression,
345350
positional_params,
346351
named_params,
347352
raw_params,
@@ -387,6 +392,7 @@ impl Connection {
387392
) -> napi::Result<napi::Either<Statement, AsyncStatement>> {
388393
let inner = Arc::clone(&self.inner);
389394
let row_limit = options.as_ref().and_then(|o| o.row_limit);
395+
let lz4_compression = options.as_ref().and_then(|o| o.lz4_compression);
390396
let positional_params = options
391397
.as_mut()
392398
.and_then(|o| o.positional_params.take())
@@ -415,6 +421,7 @@ impl Connection {
415421
statement_conf,
416422
query_tags,
417423
row_limit,
424+
lz4_compression,
418425
positional_params,
419426
named_params,
420427
raw_params,
@@ -460,6 +467,7 @@ impl Connection {
460467
let inner = Arc::clone(&self.inner);
461468
// Same option decoding as `execute_statement`.
462469
let row_limit = options.as_ref().and_then(|o| o.row_limit);
470+
let lz4_compression = options.as_ref().and_then(|o| o.lz4_compression);
463471
let positional_params = options
464472
.as_mut()
465473
.and_then(|o| o.positional_params.take())
@@ -495,6 +503,7 @@ impl Connection {
495503
statement_conf,
496504
query_tags,
497505
row_limit,
506+
lz4_compression,
498507
positional_params,
499508
named_params,
500509
raw_params,
@@ -518,7 +527,7 @@ impl Connection {
518527
/// uses (`runAsync: true`): the SEA backend submits, returns a
519528
/// pending operation handle, and polls to terminal during
520529
/// fetch. Option semantics (statementConf / queryTags /
521-
/// rowLimit / positional + named params) match `executeStatement`.
530+
/// rowLimit / lz4Compression / positional + named params) match `executeStatement`.
522531
/// Submit always sends `wait_timeout=0s` so the call returns
523532
/// immediately; the caller drives completion via `status()` /
524533
/// `awaitResult()`. Only the blocking-vs-pending return contract
@@ -533,6 +542,7 @@ impl Connection {
533542
// Same option decoding as `execute_statement` — malformed
534543
// params surface as InvalidArgument before any wire round-trip.
535544
let row_limit = options.as_ref().and_then(|o| o.row_limit);
545+
let lz4_compression = options.as_ref().and_then(|o| o.lz4_compression);
536546
let positional_params = options
537547
.as_mut()
538548
.and_then(|o| o.positional_params.take())
@@ -573,6 +583,7 @@ impl Connection {
573583
statement_conf,
574584
query_tags,
575585
row_limit,
586+
lz4_compression,
576587
positional_params,
577588
named_params,
578589
raw_params,
@@ -756,6 +767,7 @@ mod tests {
756767
None,
757768
None,
758769
None,
770+
Some(false),
759771
vec![],
760772
vec![],
761773
vec![raw()],
@@ -778,6 +790,7 @@ mod tests {
778790
assert_eq!(statement_requests.len(), 1);
779791
let body: serde_json::Value =
780792
serde_json::from_slice(&statement_requests[0].body).expect("statement request is JSON");
793+
assert_eq!(body["result_compression"], "NONE");
781794
assert_eq!(
782795
body["parameters"],
783796
json!([{ "ordinal": 1, "type": "INTERVAL MONTH", "value": "13" }])
@@ -789,6 +802,7 @@ mod tests {
789802
None,
790803
None,
791804
None,
805+
None,
792806
vec![TypedValue::Int32(1)],
793807
vec![],
794808
vec![raw()],

‎pyo3/NEXT_CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ changes go in the root `NEXT_CHANGELOG.md`.
1616
- `Session(..., port=None)`: an optional connection port. A value outside
1717
`0..=65535` raises `ValueError`; a host the kernel can't combine with the port
1818
raises `KernelError` (`InvalidArgument`).
19+
- `Statement.set_lz4_compression` overrides Arrow result LZ4 compression per statement.
1920

2021
### Changed
2122
- Allow pyarrow 24.x through 26.x on Python 3.10+ (`pyarrow>=23.0.1,<27`),

‎pyo3/src/statement.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ impl Statement {
6666
Ok(())
6767
}
6868

69+
/// Set LZ4 compression for Arrow results.
70+
///
71+
/// The setting persists across ``set_sql`` calls until changed or reset with
72+
/// ``None``. ``None`` uses the session default.
73+
#[pyo3(signature = (enabled))]
74+
fn set_lz4_compression(&mut self, enabled: Option<bool>) -> PyResult<()> {
75+
let inner = alive_mut!(self, "Statement");
76+
inner.stmt.spec().lz4_compression(enabled);
77+
Ok(())
78+
}
79+
6980
/// Set the server-side row cap for this statement. ``None`` clears it.
7081
fn set_row_limit(&mut self, row_limit: Option<i64>) -> PyResult<()> {
7182
let inner = alive_mut!(self, "Statement");

0 commit comments

Comments
 (0)