Title: feat(dataframe): add executeStream(allocator) for incremental batch iteration · Issue #50 · apache/datafusion-java · GitHub
Open Graph Title: feat(dataframe): add executeStream(allocator) for incremental batch iteration · Issue #50 · apache/datafusion-java
X Title: feat(dataframe): add executeStream(allocator) for incremental batch iteration · Issue #50 · apache/datafusion-java
Description: Is your feature request related to a problem or challenge? DataFrame.collect(allocator) (core/src/main/java/org/apache/datafusion/DataFrame.java) is the only way to retrieve query results today. Internally, the native side calls df.colle...
Open Graph Description: Is your feature request related to a problem or challenge? DataFrame.collect(allocator) (core/src/main/java/org/apache/datafusion/DataFrame.java) is the only way to retrieve query results today. In...
X Description: Is your feature request related to a problem or challenge? DataFrame.collect(allocator) (core/src/main/java/org/apache/datafusion/DataFrame.java) is the only way to retrieve query results today. In...
Opengraph URL: https://github.com/apache/datafusion-java/issues/50
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feat(dataframe): add executeStream(allocator) for incremental batch iteration","articleBody":"### Is your feature request related to a problem or challenge?\n\n`DataFrame.collect(allocator)` (`core/src/main/java/org/apache/datafusion/DataFrame.java`) is the only way to retrieve query results today. Internally, the native side calls `df.collect().await` (`native/src/lib.rs`) which materializes the entire result set into a `Vec\u003cRecordBatch>` on the Rust heap *before* the first batch crosses the FFI boundary into Java.\n\nFor TB-scale queries, or any query whose result set exceeds the native heap budget, this OOMs the Rust side regardless of how memory accounting is configured downstream. The Java caller can drain batches one at a time from the returned `ArrowReader`, but by the time the reader exists, every batch is already buffered in native memory.\n\n`SessionContext::execute_stream` already exists upstream — it returns a `SendableRecordBatchStream` (`Pin\u003cBox\u003cdyn RecordBatchStream + Send>>`) that yields one batch at a time. The Java binding just needs to wire that to the existing `FFI_ArrowArrayStream` path and hand the resulting reader back to Java.\n\nThis is what most embedders running production-shaped workloads need by default; `collect()` is the right API for small results but the wrong default for analytics-scale queries.\n\n### Describe the solution you'd like\n\nA new method on `DataFrame`, peer to `collect`:\n\n```java\ntry (DataFrame df = ctx.sql(\"SELECT ... FROM big_table\");\n ArrowReader reader = df.executeStream(allocator)) {\n while (reader.loadNextBatch()) {\n VectorSchemaRoot batch = reader.getVectorSchemaRoot();\n // process one batch at a time; nothing else materialized\n }\n}\n```\n\nSame return type as `collect` (`ArrowReader`), same lifecycle rules (caller closes, allocator must outlive). The semantic difference is purely \"one batch is in memory at a time\" instead of \"all batches in memory before the first is yielded.\"\n\n### Native shape\n\nThe Rust side already exports record batches via `FFI_ArrowArrayStream::new(Box::new(iter))` (see the existing `Java_org_apache_datafusion_DataFrame_collectDataFrame` handler in `native/src/lib.rs`). The streaming variant substitutes a `RecordBatchReader` that pulls from `df.execute_stream()` lazily:\n\n```rust\nstruct StreamingReader {\n schema: SchemaRef,\n stream: Pin\u003cBox\u003cSendableRecordBatchStream>>,\n}\n\nimpl Iterator for StreamingReader {\n type Item = Result\u003cRecordBatch, ArrowError>;\n fn next(&mut self) -> Option\u003cSelf::Item> {\n runtime().block_on(self.stream.next())\n .map(|r| r.map_err(|e| ArrowError::ExternalError(Box::new(e))))\n }\n}\n\nimpl RecordBatchReader for StreamingReader {\n fn schema(&self) -> SchemaRef { self.schema.clone() }\n}\n```\n\nEach `loadNextBatch()` call from Java drives one `runtime().block_on(stream.next())` on the Rust side. No `Vec\u003cRecordBatch>` is ever built; memory pressure stays bounded by what the executor's pipeline holds plus one batch in flight.\n\n### Lifecycle\n\n- Like `collect`, `executeStream` *consumes* the `DataFrame`: the native plan handle is released as the stream is established. Subsequent `executeStream` or `collect` on the same instance throws `IllegalStateException`.\n- `close()` on a successfully-streamed `DataFrame` is a no-op; the `ArrowReader` is the new owner of native resources.\n- If construction of the stream fails (e.g. plan error), the native handle must be cleaned up so the original `DataFrame.close()` doesn't double-free.\n\n### Coexistence with `collect`\n\nBoth methods stay. `collect` remains the right call for small results where you want a single owning buffer; `executeStream` is the right call for unbounded or large results. After this PR lands, the two paths could share a single Rust implementation (`collect` becomes `executeStream` + `concat`), but that re-implementation is out of scope here — keeping `collect` on its current code path keeps the diff small and lets a follow-up consolidate.\n\n### Describe alternatives you've considered\n\n1. **Replace `collect` with `executeStream` and offer `collectAll(allocator)` for the small-result case.** Cleaner long-term but breaks an unreleased-but-shipping API.\n\n2. **Add a `BatchSize` knob that limits how much `collect` materializes before yielding control.** Doesn't actually fix the OOM — the executor still buffers everything; just makes it harder to reason about.\n\n3. **Expose `execute_stream_partitioned`.** Useful for parallel consumers, but a single-stream API is the v1 most callers want. Partitioned can be a follow-up.\n\n### Additional context\n\n_No response_","author":{"url":"https://github.com/LantaoJin","@type":"Person","name":"LantaoJin"},"datePublished":"2026-05-14T09:22:54Z","url":"https://github.com/apache/datafusion-java/issues/50"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3dec6081-dd11-109e-f921-bdd22efd2949 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A58:2815D1:2BA629:3C50D2:6B046A13 |
| html-safe-nonce | 49b44ae0d4ea73ddb66447b7f6558be6a5db3d5fa2106e59266f7b7dfe3511e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTU4OjI4MTVEMToyQkE2Mjk6M0M1MEQyOjZCMDQ2QTEzIiwidmlzaXRvcl9pZCI6IjE0ODMzOTIwOTU2MzYxODc2NjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bfae5fa2e9d9a1f19a3e55ab0ae6362b408fd1cfb202a8e66da2572402a64125 |
| hovercard-subject-tag | issue:4444759258 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/apache/datafusion-java/50/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cc5dc131c774d3dff5fa0cceccfd616fb611b6943938523fd0835828a638df8b/apache/datafusion-java/issues/50 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cc5dc131c774d3dff5fa0cceccfd616fb611b6943938523fd0835828a638df8b/apache/datafusion-java/issues/50 |
| og:image:alt | Is your feature request related to a problem or challenge? DataFrame.collect(allocator) (core/src/main/java/org/apache/datafusion/DataFrame.java) is the only way to retrieve query results today. In... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LantaoJin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| go-import | github.com/apache/datafusion-java git https://github.com/apache/datafusion-java.git |
| octolytics-dimension-user_id | 47359 |
| octolytics-dimension-user_login | apache |
| octolytics-dimension-repository_id | 1237105117 |
| octolytics-dimension-repository_nwo | apache/datafusion-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1237105117 |
| octolytics-dimension-repository_network_root_nwo | apache/datafusion-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | ca4d07a7cef4e4b75f821dc25e7300d80c1450a9 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width