Title: feat(table): add SessionContext.registerStreamingTable for push-mode batch ingest by LantaoJin · Pull Request #98 · apache/datafusion-java · GitHub
Open Graph Title: feat(table): add SessionContext.registerStreamingTable for push-mode batch ingest by LantaoJin · Pull Request #98 · apache/datafusion-java
X Title: feat(table): add SessionContext.registerStreamingTable for push-mode batch ingest by LantaoJin · Pull Request #98 · apache/datafusion-java
Description: Which issue does this PR close?
Closes #95 .
Rationale for this change
PR #65 shipped a Java-implemented TableProvider and SessionContext.registerTable(String, TableProvider). That covered the pull shape: DataFusion calls scan(BufferAllocator) and reads the returned ArrowReader. For Java callers who already have batches in memory, or who can produce them on demand, that's the right surface.
It does not cover the push shape that event-driven batch sources need:
A coordinator reducing over shard responses arriving incrementally.
A Flight stream feeding into a query.
Any in-process producer that emits batches as side-effects of other work and doesn't know in advance how many will arrive.
Bridging these into PR #65 today requires writing a BlockingArrowReader adapter that buffers pushed batches and serves them through the pull interface. That's a serialisation point: the producer blocks waiting for loadNextBatch(), or DataFusion blocks waiting for the next batch -- the two ends can never run truly concurrently. The adapter also has to invent its own backpressure, error propagation, end-of-stream signalling, and thread-safety story.
DataFusion itself solves this on the Rust side with StreamingTable + PartitionStream plus an mpsc channel. This PR surfaces that exact shape from Java.
What changes are included in this PR?
New public Java API on SessionContext returning a typed sink:
TableSink sink = ctx.registerStreamingTable("shard_results", schema, /* capacity */ 16);
// Producer thread (any thread, including outside any Tokio runtime, and including a thread
// that is itself a Tokio worker -- see "Tokio context detection" below):
try {
while (hasMoreInput()) {
sink.write(batch); // backpressures when channel is full
}
sink.close(); // EOF: queries see end-of-stream cleanly
} catch (Throwable t) {
sink.fail(t); // signal error: queries see RuntimeException
}
public final class TableSink implements AutoCloseable {
void write(VectorSchemaRoot batch);
@Override void close();
void fail(Throwable cause);
}
After registration the table can be queried like any other:
DataFrame df = ctx.sql("SELECT count(*) FROM shard_results");
ArrowReader r = df.collect(allocator);
Single-scan semantics. The registered table can be queried at most once; subsequent scans against the same registration throw a RuntimeException. This matches the natural semantic for an event-driven producer (the data is consumed as it arrives) and avoids buffering every batch internally. Callers who need re-scan should use the pull-mode registerTable + SimpleTableProvider path from PR #65 instead. Documented loudly on registerStreamingTable's Javadoc and tested explicitly (secondScanThrows).
Schema constraints (validated at registration). The schema passed to registerStreamingTable must:
Have at least one column. Zero-column streaming would have no allocator to borrow for the FFI scratch; rejected up front rather than failing at first write.
Have no dictionary-encoded fields at any depth (recursive walk through Field.getChildren()). Data.exportVectorSchemaRoot would otherwise NPE on the missing DictionaryProvider -- v1 cannot supply one. The error message names the dotted path (e.g. 'row.code') so nested violations are debuggable. A future overload that accepts a DictionaryProvider would lift this restriction; out of scope here.
Native side, in a new native/src/streaming_table.rs.
JNI handlers in native/src/lib.rs.
The Java TableSink mirrors this with an AtomicLong nativeHandle and a ReentrantReadWriteLock over the lifetime; write() holds the read lock during the JNI call, close()/fail() flip the handle to 0 (forbidding new writes), call closeSinkNative/failSinkNative (waking parked writes), then take the write lock and call dropHandleNative (which now waits for all readers to drain).
The TableSink.write Java path derives the FFI scratch allocator from the producer's batch (batch.getFieldVectors().get(0).getAllocator()) so the exported buffers share an allocator-root with the producer's vectors. Using a separate RootAllocator would make Data.exportVectorSchemaRoot reject the cross-root transfer.
Cancellation propagates automatically: when DataFusion drops the consumer (query cancelled, LIMIT N short-circuits, error in another operator), the receiver is dropped, Sender::send resolves to Err(SendError), and the JNI handler surfaces that as a RuntimeException on the producer thread. Producers that ignore the exception leak no resources because the sender's Drop runs unconditionally.
tokio-stream is added as a direct Cargo dependency (it was already pulled in transitively by datafusion-physical-plan; declaring it directly insulates us from transitive resolution drift). The tokio dependency gains the sync feature for Notify.
Out of scope (for follow-ups):
Multi-partition registerStreamingTable. StreamingTable::try_new accepts Vec
Open Graph Description: Which issue does this PR close? Closes #95 . Rationale for this change PR #65 shipped a Java-implemented TableProvider and SessionContext.registerTable(String, TableProvider). That covered the pu...
X Description: Which issue does this PR close? Closes #95 . Rationale for this change PR #65 shipped a Java-implemented TableProvider and SessionContext.registerTable(String, TableProvider). That covered the pu...
Opengraph URL: https://github.com/apache/datafusion-java/pull/98
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
| 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 | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:90b48db4-2df4-231f-9a8f-c3f9079fadcc |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | A44C:2D066D:22F11F:2FFE5C:6B04681F |
| html-safe-nonce | 83ccab32a9ea13af2ee9bb478d6b0e307c0a92c553947db6d9ffe185e04a0b45 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNDRDOjJEMDY2RDoyMkYxMUY6MkZGRTVDOjZCMDQ2ODFGIiwidmlzaXRvcl9pZCI6IjMxODY1NTc4Mzc4MTE5MzUyNjMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 42d4b162b74e6dfac95aae91eb8f8129b37bef61997376cf2fd8759d22b23428 |
| hovercard-subject-tag | pull_request:3748931571 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,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/apache/datafusion-java/pull/98/files |
| twitter:image | https://avatars.githubusercontent.com/u/1853780?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/1853780?s=400&v=4 |
| og:image:alt | Which issue does this PR close? Closes #95 . Rationale for this change PR #65 shipped a Java-implemented TableProvider and SessionContext.registerTable(String, TableProvider). That covered the pu... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | 83e4c0e58c5162177f3c161c5fbad38527ae04c11ac14980f9f9d85ae2027b1f |
| turbo-cache-control | no-preview |
| diff-view | unified |
| 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 full-width |
| disable-turbo | true |
| 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 |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | no-cache |
Viewport: width=device-width