Skip to content

Commit 48e4714

Browse files
committed
feat(napi): async execute — submitStatement + AsyncStatement/AsyncResultHandle
Surfaces the kernel's submit/poll/await_result path through napi as two new opaque classes plus a new method on `Connection`: - `Connection.submitStatement(sql, options?) -> AsyncStatement` — calls the kernel `Statement::submit()` (wait_timeout=0s) so the server returns immediately with a statement_id while the query is still Pending/Running. Long-running queries that would blow past the sync `executeStatement` polling budget go through this path. Shares `ExecuteOptions` (statementConf + queryTags) with the sync path via the existing `build_merged_statement_conf`. - `AsyncStatement.status() -> string` — one-shot GetStatementStatus, mapped to a JS string enum `'Pending' | 'Running' | 'Succeeded' | 'Failed' | 'Cancelled' | 'Closed' | 'Unknown'`. The `Unknown` variant is a forward-compat arm for `#[non_exhaustive]` `StatementStatus` — a future kernel variant surfaces deterministically instead of panicking. The `Failed` variant collapses to the string; callers needing the typed error envelope (sql_state / error_code / query_id) get it via `awaitResult()`'s rejection. - `AsyncStatement.awaitResult() -> AsyncResultHandle` — blocks until the server reaches a terminal state, then returns a fetchable handle over the materialised `ResultStream`. The kernel's `AwaitResultCancelGuard` already covers drop-cancel safety: if the JS Promise's underlying tokio future is dropped mid-poll (Promise.race loser, timeout wrapper, AbortController), the kernel fires a fire-and-forget `cancel_statement` so the server stops charging DBUs on the orphaned query. The `util::guarded` `catch_unwind` wrapper covers V8 panic-across- boundary on top. - `AsyncStatement.cancel()` / `.close()` — same shape as the sync `Statement` methods via the shared `ExecutedStatementHandle` trait. Idempotent close; cancel is a no-op against a terminal-state server. - `AsyncResultHandle.fetchNextBatch() -> Option<ArrowBatch>` / `.schema() -> ArrowSchema` — byte-identical IPC payload to the sync `Statement.fetchNextBatch()` / `.schema()` for the same query. No `cancel()` / `close()` — the parent `AsyncStatement` owns server-side lifecycle (avoids dual-ownership of the same statement_id with inconsistent close semantics). Zero kernel-core changes — the kernel's `ExecutedAsyncStatement` + `ResultStream` + `ExecutedStatementHandle` trait already provide everything this napi surface needs. Concurrency shape mirrors the sync Statement: every method takes `inner.lock()` and holds the guard across the kernel `.await`. tokio `Mutex` is FIFO. Cancel/close queue behind any in-flight `awaitResult()`. The kernel covers the drop-the-future case independently. The IPC encoder is duplicated between `statement.rs` and `async_statement.rs` — both call sites produce byte-identical output, so re-emitting it is cheaper than expanding the surface of `statement` for one re-use site. If a third caller appears, this becomes the trigger to lift into `bindings_common`. Tests: - `status_to_string` covers all `StatementStatus` variants (including the non_exhaustive Unknown arm) - `closed_err` envelope shape (sentinel prefix + InvalidStatementHandle code + human message) - `closed_err` status collapses to napi `InvalidArg` Co-authored-by: Isaac Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
1 parent 9fbc009 commit 48e4714

3 files changed

Lines changed: 532 additions & 0 deletions

File tree

0 commit comments

Comments
 (0)