Title: feat: query cancellation via `CancellationToken` on `SessionContext` · Issue #68 · apache/datafusion-java · GitHub
Open Graph Title: feat: query cancellation via `CancellationToken` on `SessionContext` · Issue #68 · apache/datafusion-java
X Title: feat: query cancellation via `CancellationToken` on `SessionContext` · Issue #68 · apache/datafusion-java
Description: Is your feature request related to a problem or challenge? A long-running DataFrame.collect(allocator) or DataFrame.executeStream(allocator) call blocks the calling Java thread for the entire duration of the query. Thread.interrupt() doe...
Open Graph Description: Is your feature request related to a problem or challenge? A long-running DataFrame.collect(allocator) or DataFrame.executeStream(allocator) call blocks the calling Java thread for the entire durat...
X Description: Is your feature request related to a problem or challenge? A long-running DataFrame.collect(allocator) or DataFrame.executeStream(allocator) call blocks the calling Java thread for the entire durat...
Opengraph URL: https://github.com/apache/datafusion-java/issues/68
X: @github
Domain: github.com
IPv4 address: 140.82.114.3
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"feat: query cancellation via `CancellationToken` on `SessionContext`","articleBody":"### Is your feature request related to a problem or challenge?\n\nA long-running `DataFrame.collect(allocator)` or `DataFrame.executeStream(allocator)` call blocks the calling Java thread for the entire duration of the query. `Thread.interrupt()` does nothing — the JNI thread is parked inside `runtime().block_on(...)` (`native/src/lib.rs`), and the interrupt flag is ignored by the Tokio runtime. There is no way to abort an in-flight query, free its native resources early, or unblock the calling thread short of waiting for the query to finish.\n\nFor any embedder running multi-tenant workloads — request timeouts, user-cancel actions, node shutdown, leader-election handover — this is a hard operational gap. The OpenSearch analytics backend (`OpenSearch/sandbox/plugins/analytics-backend-datafusion/rust/src/cancellation.rs` and `query_tracker.rs`) carries a `CancellationToken`-based wrapper precisely because upstream offers nothing.\n\nThis is complementary to issue #40 (close()/JNI use-after-free race) but distinct: #40 is about safely tearing down a *finished* handle; this is about signalling an *in-flight* future to stop. Both eventually share the atomic-handle scaffolding from #40's option 2, so coordination is worthwhile, but the surface lands cleanly without #40 having to merge first.\n\n### Describe the solution you'd like\n\nA token-based cancellation API on `SessionContext`, modeled on Spark 4.0's `interruptTag` shape (cancel lives on the session, not on the DataFrame). The token is a separate handle from the DataFrame so cancel can fire from a thread that does not hold the DataFrame.\n\n### v1 surface\n\n```java\ntry (SessionContext ctx = new SessionContext();\n CancellationToken token = ctx.newCancellationToken();\n DataFrame df = ctx.sql(\"SELECT ... FROM big_table\")) {\n\n Future\u003cArrowReader> fut = pool.submit(() -> df.collect(allocator, token));\n\n // from another thread (timeout watcher, user-cancel handler, ...):\n token.cancel();\n\n // fut completes with CancellationException\n}\n```\n\nNew methods:\n\n- `SessionContext.newCancellationToken()` -- returns a fresh `CancellationToken` bound to this session.\n- `CancellationToken.cancel()` -- fires the token; idempotent.\n- `CancellationToken.isCancelled()` -- non-blocking check.\n- `CancellationToken.close()` -- releases the native handle; the token is `AutoCloseable` so try-with-resources handles cleanup.\n- `DataFrame.collect(BufferAllocator, CancellationToken)` -- overload that takes a token. The existing zero-token `collect(BufferAllocator)` is unchanged.\n- `DataFrame.executeStream(BufferAllocator, CancellationToken)` -- same overload pattern. Token is held by the returned `ArrowReader` for its full lifetime; cancel mid-stream aborts the next `loadNextBatch()`.\n\n\n### Describe alternatives you've considered\n\n_No response_\n\n### Additional context\n\n### Out of scope\n- *Tag form.* Ship the token primitive first; tag is sugar that can land in a follow-up if a user actually asks for it.\n- *Sync-API breakage.* `df.collect(allocator)` keeps working unchanged; the new method is `df.collect(allocator, token)` (overload).\n- *Per-operator cancel granularity.* Today the cancel point is each `block_on` site; sub-operator cancellation is upstream-DataFusion territory.","author":{"url":"https://github.com/LantaoJin","@type":"Person","name":"LantaoJin"},"datePublished":"2026-05-19T08:48:34Z","url":"https://github.com/apache/datafusion-java/issues/68"}
| 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:f45b6b0d-5dad-fbc4-c637-4307390275ca |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B974:133E28:1D1338:28B604:6B0467EF |
| html-safe-nonce | b95ce7050c2b290685aeb6439573fcd2179df6540903564b019bb3d48a3b8e85 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOTc0OjEzM0UyODoxRDEzMzg6MjhCNjA0OjZCMDQ2N0VGIiwidmlzaXRvcl9pZCI6IjYxNTA2NzMxMjg1MjA3MDYwMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e223f26f103000e79799ba0915040807f31f3b34e04a2272d9567f66625ab907 |
| hovercard-subject-tag | issue:4475871180 |
| 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/68/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2aebbb22f9e19d7d3cb241b3d95fe3bb9943264d45d9e2a5313c8f9a85b12d10/apache/datafusion-java/issues/68 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2aebbb22f9e19d7d3cb241b3d95fe3bb9943264d45d9e2a5313c8f9a85b12d10/apache/datafusion-java/issues/68 |
| og:image:alt | Is your feature request related to a problem or challenge? A long-running DataFrame.collect(allocator) or DataFrame.executeStream(allocator) call blocks the calling Java thread for the entire durat... |
| 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