arrowbricks 5.1.0__tar.gz → 5.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/PKG-INFO +1 -1
  2. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/pyproject.toml +1 -1
  3. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/Cargo.lock +1 -1
  4. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/Cargo.toml +6 -2
  5. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/alloc.rs +54 -2
  6. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/download.rs +82 -4
  7. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/lib.rs +5 -5
  8. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/ndjson.rs +8 -12
  9. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/thrift_exec.rs +27 -0
  10. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/_streaming.py +1 -4
  11. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/LICENSE +0 -0
  12. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/README.md +0 -0
  13. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/.gitignore +0 -0
  14. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/README.md +0 -0
  15. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/examples/duckdb_query.py +0 -0
  16. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/examples/fastapi_sse.py +0 -0
  17. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/proptest-regressions/json_convert.txt +0 -0
  18. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/rustfmt.toml +0 -0
  19. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/arrow_ffi.rs +0 -0
  20. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/error.rs +0 -0
  21. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/model.rs +0 -0
  22. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/sea.rs +0 -0
  23. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/thrift_rpc.rs +0 -0
  24. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client/volume.rs +0 -0
  25. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/client.rs +0 -0
  26. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/heartbeat.rs +0 -0
  27. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/json_convert.rs +0 -0
  28. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/reorder.rs +0 -0
  29. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/sea.rs +0 -0
  30. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/stats.rs +0 -0
  31. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline/test_support.rs +0 -0
  32. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/pipeline.rs +0 -0
  33. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/src/thrift.rs +0 -0
  34. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests/common/mod.rs +0 -0
  35. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests/wiremock_pipeline.rs +0 -0
  36. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests/wiremock_thrift.rs +0 -0
  37. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests/wiremock_volume_files.rs +0 -0
  38. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/conftest.py +0 -0
  39. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_ipc_stream.py +0 -0
  40. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_parameters.py +0 -0
  41. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_stream_ndjson_lines.py +0 -0
  42. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_streaming.py +0 -0
  43. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_thrift.py +0 -0
  44. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_token_provider.py +0 -0
  45. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/test_volume_files.py +0 -0
  46. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/rust/arrowbricks_core/tests_py/thrift_mock.py +0 -0
  47. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/__init__.py +0 -0
  48. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/_core.pyi +0 -0
  49. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/client.py +0 -0
  50. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/cursor.py +0 -0
  51. {arrowbricks-5.1.0 → arrowbricks-5.1.1}/src/arrowbricks/py.typed +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: arrowbricks
3
- Version: 5.1.0
3
+ Version: 5.1.1
4
4
  Requires-Dist: arro3-core>=0.8 ; extra == 'arro3'
5
5
  Provides-Extra: arro3
6
6
  License-File: LICENSE
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "arrowbricks"
3
- version = "5.1.0"
3
+ version = "5.1.1"
4
4
  description = "Runs SQL against a Databricks SQL warehouse via the Statement Execution API and hands you the result as Arrow -- a DB-API-ish Cursor (fetchone/fetchmany/fetchall/fetchall_arrow) or NDJSON streaming. Rust/PyO3 core throughout -- zero required runtime dependencies."
5
5
  readme = "README.md"
6
6
  license = "MIT"
@@ -177,7 +177,7 @@ dependencies = [
177
177
 
178
178
  [[package]]
179
179
  name = "arrowbricks_core"
180
- version = "5.1.0"
180
+ version = "5.1.1"
181
181
  dependencies = [
182
182
  "arrow-array",
183
183
  "arrow-buffer",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "arrowbricks_core"
3
- version = "5.1.0"
3
+ version = "5.1.1"
4
4
  edition = "2024"
5
5
  readme = "README.md"
6
6
 
@@ -74,13 +74,17 @@ tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros"] }
74
74
  libc = "0.2"
75
75
 
76
76
  [features]
77
- default = ["extension-module"]
77
+ default = ["extension-module", "large-block-alloc"]
78
78
  # `extension-module` skips linking against libpython -- correct for the
79
79
  # cdylib maturin actually ships (Python provides those symbols at import
80
80
  # time), but it breaks a plain `cargo test`/`cargo bench` binary, which does
81
81
  # need to link libpython itself. Tests/benches build with
82
82
  # `--no-default-features` to get a normal link.
83
83
  extension-module = ["pyo3/extension-module"]
84
+ # Linux/glibc only: returns buffers of 1 MiB or more to the OS when freed (src/alloc.rs).
85
+ # A Rust crate depending on this one with its own global allocator opts out with
86
+ # `default-features = false`.
87
+ large-block-alloc = []
84
88
 
85
89
  [dev-dependencies]
86
90
  tokio = { version = "1.53.1", features = ["rt-multi-thread", "macros", "test-util", "net", "io-util"] }
@@ -6,7 +6,14 @@
6
6
  //! idle RSS from 200 MB to 1.4 GB. Blocks of `LARGE` bytes or more bypass
7
7
  //! malloc here (`mmap`/`munmap`, `mremap` to grow without copying); smaller
8
8
  //! ones use the system allocator. Only Rust allocations in this extension are
9
- //! affected, not the host process's malloc. Linux only.
9
+ //! affected, not the host process's malloc. Linux with glibc only (the one
10
+ //! configuration this was measured on); see `lib.rs` for the gate.
11
+ //!
12
+ //! Failure mode: `mmap`/`mremap` return null when the address-space limit is
13
+ //! hit (`RLIMIT_AS`, strict overcommit, `vm.max_map_count`), which Rust turns
14
+ //! into an abort like any other failed allocation.
15
+
16
+ #![warn(clippy::undocumented_unsafe_blocks)]
10
17
 
11
18
  use std::alloc::{GlobalAlloc, Layout, System};
12
19
  use std::ptr;
@@ -16,19 +23,32 @@ const PAGE: usize = 4096;
16
23
 
17
24
  pub struct LargeBlockAlloc;
18
25
 
26
+ /// Must stay a pure function of the `Layout`: `alloc`, `realloc` and `dealloc`
27
+ /// each call it on the same block and must get the same answer, otherwise a
28
+ /// block is freed by the wrong allocator.
19
29
  fn is_large(layout: Layout) -> bool {
20
30
  layout.size() >= LARGE && layout.align() <= PAGE
21
31
  }
22
32
 
33
+ /// The length the kernel maps for `size` bytes. Cannot overflow: a `Layout`
34
+ /// size is at most `isize::MAX`.
23
35
  fn mapped_len(size: usize) -> usize {
24
36
  size.next_multiple_of(PAGE)
25
37
  }
26
38
 
39
+ // SAFETY: every method routes a block to `mmap` or to `System` based only on
40
+ // `is_large(layout)`, and the caller must pass the block's original layout to
41
+ // `dealloc`/`realloc` (the `GlobalAlloc` contract), so a block is always
42
+ // released by the allocator that produced it. Mappings are page-aligned, which
43
+ // satisfies every `align <= PAGE`; larger alignments go to `System`.
27
44
  unsafe impl GlobalAlloc for LargeBlockAlloc {
28
45
  unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
29
46
  if !is_large(layout) {
47
+ // SAFETY: `layout` is forwarded unchanged; the caller upholds `alloc`'s contract.
30
48
  return unsafe { System.alloc(layout) };
31
49
  }
50
+ // SAFETY: an anonymous private mapping with no address hint; the length is
51
+ // non-zero because `layout.size() >= LARGE`.
32
52
  let p = unsafe {
33
53
  libc::mmap(
34
54
  ptr::null_mut(),
@@ -49,29 +69,40 @@ unsafe impl GlobalAlloc for LargeBlockAlloc {
49
69
  // Fresh anonymous mappings are already zeroed.
50
70
  unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
51
71
  if is_large(layout) {
72
+ // SAFETY: same contract as `alloc`, which returns zeroed pages for large layouts.
52
73
  unsafe { self.alloc(layout) }
53
74
  } else {
75
+ // SAFETY: `layout` is forwarded unchanged to the system allocator.
54
76
  unsafe { System.alloc_zeroed(layout) }
55
77
  }
56
78
  }
57
79
 
58
80
  unsafe fn dealloc(&self, p: *mut u8, layout: Layout) {
59
81
  if is_large(layout) {
60
- unsafe { libc::munmap(p.cast(), mapped_len(layout.size())) };
82
+ // SAFETY: `p` and `layout` came from this allocator, so a large layout means `p`
83
+ // is the start of a mapping of exactly `mapped_len(layout.size())` bytes.
84
+ let rc = unsafe { libc::munmap(p.cast(), mapped_len(layout.size())) };
85
+ debug_assert_eq!(rc, 0, "munmap failed: the layout does not match the block");
61
86
  } else {
87
+ // SAFETY: a small layout means `p` came from `System` with this same layout.
62
88
  unsafe { System.dealloc(p, layout) };
63
89
  }
64
90
  }
65
91
 
66
92
  unsafe fn realloc(&self, p: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
93
+ // SAFETY: the caller guarantees `new_size`, rounded up to `layout.align()`, does not
94
+ // overflow `isize::MAX`, and `layout.align()` is already a valid alignment.
67
95
  let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, layout.align()) };
68
96
  match (is_large(layout), is_large(new_layout)) {
97
+ // SAFETY: both layouts are small, so `p` is a `System` block of `layout`.
69
98
  (false, false) => unsafe { System.realloc(p, layout, new_size) },
70
99
  (true, true) => {
71
100
  let (old_len, new_len) = (mapped_len(layout.size()), mapped_len(new_size));
72
101
  if old_len == new_len {
73
102
  return p;
74
103
  }
104
+ // SAFETY: `p` is the start of a mapping of `old_len` bytes; on failure
105
+ // `mremap` leaves that mapping intact and the null result tells the caller.
75
106
  let q = unsafe { libc::mremap(p.cast(), old_len, new_len, libc::MREMAP_MAYMOVE) };
76
107
  if q == libc::MAP_FAILED {
77
108
  ptr::null_mut()
@@ -80,8 +111,12 @@ unsafe impl GlobalAlloc for LargeBlockAlloc {
80
111
  }
81
112
  }
82
113
  _ => {
114
+ // Crossing the threshold: new block from the matching allocator, copy, free old.
115
+ // SAFETY: `new_layout` is valid (see above) and non-zero sized.
83
116
  let q = unsafe { self.alloc(new_layout) };
84
117
  if !q.is_null() {
118
+ // SAFETY: `p` is valid for `layout.size()` bytes and `q` for `new_size`, so
119
+ // both are valid for the smaller of the two; they are distinct blocks.
85
120
  unsafe {
86
121
  ptr::copy_nonoverlapping(p, q, layout.size().min(new_size));
87
122
  self.dealloc(p, layout);
@@ -94,6 +129,8 @@ unsafe impl GlobalAlloc for LargeBlockAlloc {
94
129
  }
95
130
 
96
131
  #[cfg(test)]
132
+ // The tests drive the allocator's raw interface directly, so each block would repeat the same note.
133
+ #[allow(clippy::undocumented_unsafe_blocks)]
97
134
  mod tests {
98
135
  use super::*;
99
136
 
@@ -109,6 +146,21 @@ mod tests {
109
146
  }
110
147
  }
111
148
 
149
+ /// Fails if the `#[global_allocator]` static in `lib.rs` is removed or mis-gated: only
150
+ /// this allocator hands out page-aligned multi-MiB blocks (glibc's mmap path returns
151
+ /// a 16-byte header offset).
152
+ #[test]
153
+ fn global_allocator_serves_large_vecs_from_page_aligned_mappings() {
154
+ let blocks: Vec<Vec<u8>> = (0..8).map(|_| Vec::with_capacity(2 * LARGE)).collect();
155
+ for b in &blocks {
156
+ assert_eq!(
157
+ b.as_ptr() as usize % PAGE,
158
+ 0,
159
+ "LargeBlockAlloc is not the global allocator"
160
+ );
161
+ }
162
+ }
163
+
112
164
  #[test]
113
165
  fn realloc_keeps_contents_across_the_large_threshold() {
114
166
  let a = LargeBlockAlloc;
@@ -74,15 +74,25 @@ pub(crate) fn lz4_frame_decode_into(src: &[u8], out: &mut Vec<u8>) -> Result<(),
74
74
  Ok(())
75
75
  }
76
76
 
77
+ /// Most a download reserves up front from `Content-Length`; a longer body
78
+ /// simply grows the buffer.
79
+ const MAX_PREALLOC: usize = 256 << 20;
80
+
77
81
  /// Reads the body into one buffer sized from `Content-Length`. Frames are
78
82
  /// freed as they are consumed, so a chunk peaks at about its own size instead
79
83
  /// of the ~2x `Response::bytes()` reaches while joining collected frames.
80
84
  async fn read_body(mut resp: reqwest::Response) -> Result<Bytes, reqwest::Error> {
81
- // Cap the up-front reservation so a bogus header can't demand gigabytes.
82
- let Some(len) = resp.content_length().map(|n| n.min(1 << 30) as usize) else {
85
+ // The header is untrusted: reserve at most `MAX_PREALLOC`, and if that
86
+ // reservation fails (address-space limits) or there is no length, fall
87
+ // back to `bytes()` instead of aborting the process on allocation failure.
88
+ let mut buf = Vec::new();
89
+ let reserved = resp
90
+ .content_length()
91
+ .map(|len| usize::try_from(len).unwrap_or(usize::MAX).min(MAX_PREALLOC))
92
+ .is_some_and(|len| buf.try_reserve_exact(len).is_ok());
93
+ if !reserved {
83
94
  return resp.bytes().await;
84
- };
85
- let mut buf = Vec::with_capacity(len);
95
+ }
86
96
  while let Some(frame) = resp.chunk().await? {
87
97
  buf.extend_from_slice(&frame);
88
98
  }
@@ -622,3 +632,71 @@ mod proptests {
622
632
  }
623
633
  }
624
634
  }
635
+
636
+ #[cfg(test)]
637
+ mod read_body_tests {
638
+ use std::io::{Read, Write};
639
+ use std::net::TcpListener;
640
+
641
+ use super::*;
642
+
643
+ /// The production HTTP client (`DbClient::new` installs the rustls crypto provider).
644
+ async fn get(url: String) -> reqwest::Response {
645
+ DbClient::new("http://unused.invalid", "wh", "token")
646
+ .http
647
+ .get(url)
648
+ .send()
649
+ .await
650
+ .unwrap()
651
+ }
652
+
653
+ /// Serves `response` verbatim to the first connection, then closes it.
654
+ fn serve_once(response: &'static [u8]) -> String {
655
+ let listener = TcpListener::bind("127.0.0.1:0").unwrap();
656
+ let url = format!("http://{}/", listener.local_addr().unwrap());
657
+ std::thread::spawn(move || {
658
+ let (mut stream, _) = listener.accept().unwrap();
659
+ let mut request = [0u8; 1024];
660
+ let _ = stream.read(&mut request);
661
+ stream.write_all(response).unwrap();
662
+ });
663
+ url
664
+ }
665
+
666
+ #[tokio::test]
667
+ async fn read_body_reads_a_content_length_body() {
668
+ let url = serve_once(b"HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\nhello world");
669
+ let resp = get(url).await;
670
+ assert_eq!(read_body(resp).await.unwrap(), Bytes::from_static(b"hello world"));
671
+ }
672
+
673
+ #[tokio::test]
674
+ async fn read_body_falls_back_when_there_is_no_content_length() {
675
+ let url =
676
+ serve_once(b"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n5\r\nhello\r\n6\r\n world\r\n0\r\n\r\n");
677
+ let resp = get(url).await;
678
+ assert_eq!(read_body(resp).await.unwrap(), Bytes::from_static(b"hello world"));
679
+ }
680
+
681
+ #[tokio::test]
682
+ async fn read_body_errors_on_a_body_shorter_than_content_length() {
683
+ let url = serve_once(b"HTTP/1.1 200 OK\r\nContent-Length: 100\r\n\r\nabc");
684
+ let resp = get(url).await;
685
+ assert!(read_body(resp).await.is_err());
686
+ }
687
+
688
+ #[test]
689
+ fn lz4_frame_decode_into_appends_every_frame_including_an_empty_one() {
690
+ fn frame(data: &[u8]) -> Vec<u8> {
691
+ let mut encoder = lz4_flex::frame::FrameEncoder::new(Vec::new());
692
+ encoder.write_all(data).unwrap();
693
+ encoder.finish().unwrap()
694
+ }
695
+ let mut src = frame(b"abc");
696
+ src.extend(frame(b""));
697
+ src.extend(frame(b"def"));
698
+ let mut out = b"prefix-".to_vec();
699
+ lz4_frame_decode_into(&src, &mut out).unwrap();
700
+ assert_eq!(out, b"prefix-abcdef");
701
+ }
702
+ }
@@ -1,4 +1,4 @@
1
- #[cfg(target_os = "linux")]
1
+ #[cfg(all(target_os = "linux", target_env = "gnu", feature = "large-block-alloc"))]
2
2
  mod alloc;
3
3
  mod arrow_ffi;
4
4
  pub mod client;
@@ -9,10 +9,6 @@ pub mod thrift;
9
9
 
10
10
  use std::sync::{Arc, Mutex};
11
11
 
12
- #[cfg(target_os = "linux")]
13
- #[global_allocator]
14
- static GLOBAL: alloc::LargeBlockAlloc = alloc::LargeBlockAlloc;
15
-
16
12
  use arrow_array::RecordBatch;
17
13
  use arrow_schema::{Schema, SchemaRef};
18
14
  use pyo3::exceptions::{PyRuntimeError, PyStopAsyncIteration, PyValueError};
@@ -30,6 +26,10 @@ use client::{
30
26
  use heartbeat::{HeartbeatStream, HeartbeatWait, Tick};
31
27
  use pipeline::{NdjsonStream, ResultStream};
32
28
 
29
+ #[cfg(all(target_os = "linux", target_env = "gnu", feature = "large-block-alloc"))]
30
+ #[global_allocator]
31
+ static GLOBAL: alloc::LargeBlockAlloc = alloc::LargeBlockAlloc;
32
+
33
33
  /// Writes any object implementing `__arrow_c_stream__` (a `Table`/
34
34
  /// `RecordBatchReader` from this crate, arro3, pyarrow, or anything else
35
35
  /// Arrow-C-Data-Interface-compatible) as Arrow-IPC stream bytes to `buf` (a
@@ -54,6 +54,10 @@ impl Write for LineCollector {
54
54
  }
55
55
  }
56
56
 
57
+ fn encode_err(e: impl std::fmt::Display) -> ApiError {
58
+ ApiError::permanent(format!("NDJSON encode error: {e}"))
59
+ }
60
+
57
61
  /// Converts one chunk's decoded batches into NDJSON lines, one per row, in
58
62
  /// arro3-`write_ndjson(explicit_nulls=True)`-compatible format: null-valued
59
63
  /// keys stay present as JSON `null` rather than being omitted, and a
@@ -83,12 +87,8 @@ fn encode_ndjson_lines(batches: &[RecordBatch], non_finite_as_string: bool) -> R
83
87
  let builder = arrow_json::WriterBuilder::new().with_explicit_nulls(true);
84
88
  let mut writer = builder.build::<_, arrow_json::writer::LineDelimited>(&mut output);
85
89
  let refs: Vec<&RecordBatch> = batches.iter().collect();
86
- writer
87
- .write_batches(&refs)
88
- .map_err(|e| ApiError::permanent(format!("NDJSON encode error: {e}")))?;
89
- writer
90
- .finish()
91
- .map_err(|e| ApiError::permanent(format!("NDJSON encode error: {e}")))?;
90
+ writer.write_batches(&refs).map_err(encode_err)?;
91
+ writer.finish().map_err(encode_err)?;
92
92
  }
93
93
  if !output.pending.is_empty() {
94
94
  return Err(ApiError::permanent("NDJSON encode produced an unterminated row"));
@@ -403,12 +403,8 @@ mod tests {
403
403
  let builder = arrow_json::WriterBuilder::new().with_explicit_nulls(true);
404
404
  let mut writer = builder.build::<_, arrow_json::writer::LineDelimited>(&mut buf);
405
405
  let refs: Vec<&RecordBatch> = batches.iter().collect();
406
- writer
407
- .write_batches(&refs)
408
- .map_err(|e| ApiError::permanent(format!("NDJSON encode error: {e}")))?;
409
- writer
410
- .finish()
411
- .map_err(|e| ApiError::permanent(format!("NDJSON encode error: {e}")))?;
406
+ writer.write_batches(&refs).map_err(encode_err)?;
407
+ writer.finish().map_err(encode_err)?;
412
408
  }
413
409
  let mut lines: Vec<String> = String::from_utf8(buf)
414
410
  .map_err(|e| ApiError::permanent(format!("NDJSON encode produced invalid UTF-8: {e}")))?
@@ -867,6 +867,33 @@ fn build_inline_blob(
867
867
  mod tests {
868
868
  use super::*;
869
869
 
870
+ #[test]
871
+ fn build_inline_blob_decodes_every_lz4_frame_including_an_empty_one() {
872
+ use std::io::Write;
873
+
874
+ fn frame(data: &[u8]) -> bytes::Bytes {
875
+ let mut encoder = lz4_flex::frame::FrameEncoder::new(Vec::new());
876
+ encoder.write_all(data).unwrap();
877
+ bytes::Bytes::from(encoder.finish().unwrap())
878
+ }
879
+ let mut one_batch = frame(b"AAAA").to_vec();
880
+ one_batch.extend(frame(b"")); // a valid frame with no content must not end decoding
881
+ one_batch.extend(frame(b"BBBB"));
882
+ let batches = vec![
883
+ thrift::ArrowBatch {
884
+ batch: bytes::Bytes::from(one_batch),
885
+ row_count: 3,
886
+ },
887
+ thrift::ArrowBatch {
888
+ batch: frame(b"CC"),
889
+ row_count: 2,
890
+ },
891
+ ];
892
+ let (blob, rows) = build_inline_blob(Some(bytes::Bytes::from_static(b"S-")), batches, true).unwrap();
893
+ assert_eq!(&blob[..], b"S-AAAABBBBCC");
894
+ assert_eq!(rows, 5);
895
+ }
896
+
870
897
  fn ok_worker() -> tokio::task::JoinHandle<()> {
871
898
  tokio::spawn(async {})
872
899
  }
@@ -13,14 +13,11 @@ from __future__ import annotations
13
13
 
14
14
  import contextlib
15
15
  from collections.abc import AsyncIterator, Awaitable, Iterator
16
- from typing import TYPE_CHECKING, Any, BinaryIO, TypeVar, cast
16
+ from typing import Any, BinaryIO, TypeVar, cast
17
17
 
18
18
  from . import _core
19
19
  from .client import DatabricksClient
20
20
 
21
- if TYPE_CHECKING:
22
- import asyncio
23
-
24
21
  __all__ = [
25
22
  "HEARTBEAT",
26
23
  "QueryTimeout",
File without changes
File without changes