Skip to content

feat(statement): make execute() the directResults call (Thrift/JDBC model) - #136

Closed
msrathore-db wants to merge 2 commits into
mainfrom
msrathore/sea-execute-directresults
Closed

msrathore-db wants to merge 2 commits into
mainfrom
msrathore/sea-execute-directresults

Conversation

@msrathore-db

@msrathore-db msrathore-db commented Jun 5, 2026 •

Copy link
Copy Markdown
Collaborator

What

Make Statement::execute() the directResults call (the Thrift / JDBC / Python use_sea model), returning a single ExecutedAsyncStatement — no enum, no caller branching.

execute() sends ExecuteStatement with the server inline wait (omits wait_timeout, so the server applies its ~10s default + on_wait_timeout=CONTINUE) and returns without the internal poll loop:

  • fast query (finished within the inline wait) → the handle is seeded with the inline result (ExecutedAsyncStatement::new_with_inline). status() reports Succeeded and await_result() returns the result with zero extra round-trips (1 RPC = the POST). This is the latency fast path and is 404-proof — a terminal handle never issues GetStatementStatus on a statement the server may have already released.
  • slow query (still running after the inline wait) → an un-seeded poll/cancel handle the caller drives (status() / await_result() / cancel()).

Why one handle, not an enum or a blocking drive

Single-threaded callers (napi, the Node driver) need the handle back before a long query is terminal so a concurrent cancel() can reach it — while a fast query must commit inline before close() races it. The inline-wait POST threads exactly that needle. JDBC's pure blocking execute() works only because it is multi-threaded. The blocking variant is preserved as execute_to_completion() (returns the eager sync ExecutedStatement), so no surface is orphaned.

This replaces the earlier enum design (DirectStatement::Completed | Running / napi Either<Statement, AsyncStatement>) — per review, one handle the caller never has to branch on.

Auto-close is warehouse-dependent

Omitting wait_timeout yields SUCCEEDED on some warehouses and CLOSED (statement auto-released after delivering its inline result) on others — verified empirically against two PECOTESTING warehouses. A seeded handle maps CLOSED → Succeeded (here CLOSED means "succeeded and released", result is inline), so directResults behaves identically across both and downstream consumers never see a spurious closed-state error.

Bindings

  • napi Connection.executeStatement → AsyncStatement
  • pyo3 Statement.execute → ExecutedAsyncStatement (plus execute_to_completion → ExecutedStatement for the blocking shape)

Testing

  • cargo +stable fmt / cargo clippy --all-targets -D warnings clean; cargo test --lib 620 passed.
  • Validated e2e through both bindings against auto-closing and non-auto-closing live warehouses: Node SEA fire-and-forget + fetch 12/12 = Thrift parity, no close-drive (close() ~140ms), mid-run cancel; Python use_kernel OK on both. SELECT 1 p50 SEA 179ms vs Thrift 168ms.

Supersedes #133 (intent-hold). Consumed by databricks/databricks-sql-nodejs#425 and databricks/databricks-sql-python#837.

This pull request and its description were written by Isaac.

@msrathore-db msrathore-db changed the title feat(statement): add directResults execute (execute_direct) — Thrift/JDBC model feat(statement): make execute() the directResults call (Thrift/JDBC model) Jun 6, 2026
@msrathore-db
msrathore-db force-pushed the msrathore/sea-execute-directresults branch from db91c25 to 3ee7e54 Compare June 6, 2026 12:15
msrathore-db added a commit to databricks/databricks-sql-nodejs that referenced this pull request Jun 6, 2026
…rives, keep mid-run cancel

The default sync path now calls the kernel's folded directResults `execute()`
(`Connection.executeStatement`, which returns a terminal `Statement` for a fast
query or an `AsyncStatement` for a still-running one). The session
feature-detects the arm via `awaitResult` and wraps it with the operation
backend's existing `statement` / `asyncStatement` arms.

Because the returned handle always corresponds to a server-owned statement:
  - fire-and-forget `CREATE`/`INSERT` commits (the server runs it inline during
    the POST) — fixes the prior lazy-execute "CREATE didn't run" bug;
  - `close()` is a clean release (DELETE), never a drive-to-terminal — the
    eager-handle + close-drives workaround is gone;
  - a long query is cancellable via `op.cancel()` once the handle is held
    (~80-300ms), at parity with the Thrift backend;
  - errors surface at `executeStatement`, matching Thrift / Python use_kernel.

Requires the kernel folded directResults execute
(databricks/databricks-sql-kernel#136). Regenerated napi router types
(`executeStatement` now returns `Statement | AsyncStatement`); fallback package
names stay on the driver's canonical `@databricks/sql-kernel-*`.

Validated e2e (pecotesting): CREATE fire-and-forget commits, dependent ordering,
100k read, error-at-execute, mid-run cancel, close() cheap on an abandoned long
query. Unit: SEA suite 247 passing; eslint clean.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
…odel)

`Statement::execute()` now sends ExecuteStatement with the server inline wait
(omits `wait_timeout`, so the server applies its ~10s default +
`on_wait_timeout=CONTINUE`) and returns WITHOUT the internal poll loop, handing
back a SINGLE `ExecutedAsyncStatement` (no enum, no caller branching):

  - fast query — finished within the inline wait: the handle is seeded with the
    inline result (`new_with_inline`), so `status()` reports `Succeeded` and
    `await_result()` returns it with ZERO extra round-trips (1 RPC = the POST).
    This is the latency fast path and is 404-proof — a terminal handle never
    issues GetStatementStatus on a statement the server may have released;
  - slow query — still running after the inline wait: an un-seeded poll/cancel
    handle the caller drives (`status()` / `await_result()` / `cancel()`).

This is the directResults model JDBC / Thrift / Python `use_sea` use, folded
into `execute()` itself. It gives consumers fire-and-forget commit (the server
runs the statement inline during the POST), a clean non-driving `close()`, and
mid-run cancel (the handle's id is published from the POST response) — without
the eager-handle / close-drives workaround.

A single handle (rather than a blocking drive-to-terminal) is required for
single-threaded callers (napi, the Node driver): the handle must return BEFORE a
long query is terminal so a concurrent `cancel()` can reach it, while a fast
query must commit inline before `close()` races it. The inline-wait POST threads
that needle; JDBC's pure blocking `execute()` works only because it is
multi-threaded. The blocking variant is preserved as `execute_to_completion()`
(returns the eager sync `ExecutedStatement`), so no surface is orphaned.

Auto-close is warehouse-dependent: omitting `wait_timeout` yields `SUCCEEDED` on
some warehouses and `CLOSED` (statement auto-released after delivering its inline
result) on others. A seeded handle maps `CLOSED -> Succeeded` (here CLOSED means
"succeeded and released", result is inline), so directResults behaves identically
across both.

Bindings expose the folded `execute()`: napi `Connection.executeStatement` ->
`AsyncStatement`; pyo3 `Statement.execute` -> `ExecutedAsyncStatement` (with
`execute_to_completion` -> `ExecutedStatement` for the blocking shape).

Replaces the earlier enum (`DirectStatement::Completed | Running`) /
`Either<Statement, AsyncStatement>` design (per review: one handle, not an enum
the caller must branch on). Supersedes #133 (intent-hold).

Tests: fmt + clippy (--all-targets -D warnings) clean; cargo test --lib 620
passed. Validated e2e through both bindings against live warehouses (auto-closing
and non-auto-closing): Node SEA fire-and-forget + fetch 12/12 = Thrift parity, no
close-drive, mid-run cancel; Python use_kernel OK on both.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
@msrathore-db
msrathore-db force-pushed the msrathore/sea-execute-directresults branch from 3ee7e54 to 33ce0b6 Compare June 6, 2026 14:19
msrathore-db added a commit to databricks/databricks-sql-nodejs that referenced this pull request Jun 6, 2026
…rives, keep mid-run cancel

The default sync path now calls the kernel's folded directResults `execute()`
(`Connection.executeStatement`), which returns a SINGLE `AsyncStatement` handle.
The session wraps it with the operation backend's existing `asyncStatement` arm —
no arm to feature-detect.

The returned handle always corresponds to a server-owned statement:
  - fire-and-forget `CREATE`/`INSERT` commits (the server runs it inline during
    the POST) — fixes the prior lazy-execute "CREATE didn't run" bug;
  - `close()` is a clean release, never a drive-to-terminal — the eager-handle +
    close-drives workaround is gone;
  - a long query is cancellable via `op.cancel()` (`asyncStatement.cancel()`)
    once the handle is held (~80-300ms), at parity with the Thrift backend;
  - errors surface at `executeStatement`, matching Thrift / Python use_kernel.

On the fast path the kernel handle is seeded with the inline result, so the
first fetch/status is served with zero extra round-trips (and is 404-proof — a
terminal handle never polls a released statement). A warehouse that auto-closes
the statement (`state=CLOSED`) after delivering its inline result is handled by
the kernel mapping `CLOSED -> Succeeded`, so this path no longer throws
`OperationStateError(Closed)` on those warehouses.

Requires the kernel folded directResults execute
(databricks/databricks-sql-kernel#136). Regenerated napi router types
(`executeStatement` now returns `AsyncStatement`); fallback package names stay on
the driver's canonical `@databricks/sql-kernel-*`.

Validated e2e (pecotesting) against auto-closing AND non-auto-closing warehouses:
CREATE fire-and-forget commits (12/12 = Thrift parity), fetch-then-close (12/12),
mid-run cancel, close() cheap (~140ms) on an abandoned long query, SELECT 1 p50
179ms vs Thrift 168ms. Unit: SEA suite 247 passing; eslint clean.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
msrathore-db added a commit to databricks/databricks-sql-python that referenced this pull request Jun 6, 2026
The kernel's `Statement.execute()` is now the directResults call
(databricks/databricks-sql-kernel#136): it returns a SINGLE
`ExecutedAsyncStatement` -- seeded with the inline result when the query finished
within the server inline wait (fast path, zero extra round-trips), or a
poll/cancel handle when still running -- instead of always blocking to terminal.

`KernelDatabricksClient.execute_command` now drives ONE uniform path (no
`hasattr(await_result)` arm to feature-detect): register the handle (so
`cursor.cancel()` / `close()` reach it), then `await_result()` to drive it to a
ready result set -- preserving execute()'s blocking contract.

`cursor.execute()` behaviour is unchanged for callers (still blocks to a ready
result set); this just adapts to the kernel's new single-handle return. Mid-run
cancel still works via the `_sync_cancellers` canceller registered before
execute.

Tested e2e against live warehouses (auto-closing and non-auto-closing): SELECT 1,
range(N), and CREATE+count all succeed on both. The kernel maps an auto-closed
`CLOSED` statement to `Succeeded`, so the connector sees a uniform success
regardless of warehouse auto-close behaviour.

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
…rror

`terminal_outcome` rejected a `CLOSED` response that carried no result payload
with `invalid_state("Statement was closed")`. That was both stricter than JDBC
and internally inconsistent with this crate's own `create_reader`, which already
treats `Succeeded | Closed` with no data as a valid EMPTY result set (builds an
`EmptyReader` from the manifest schema).

JDBC's `DatabricksSdkClient` is the reference: it polls while `PENDING`/`RUNNING`
and treats everything else as terminal, with `SUCCEEDED` AND `CLOSED` as the two
success states (`if state != SUCCEEDED && state != CLOSED -> handleFailedExecution`)
— `CLOSED` is the server's auto-close-on-completion signal, common for DDL/DML
and no-row queries that finish and are reaped server-side in one round-trip.

Make `CLOSED` an unconditional terminal success (parity with both JDBC and
`create_reader`): a `CLOSED` response with no result becomes an empty result set
rather than a spurious error. `FAILED` / `CANCELED` remain errors;
`PENDING`/`RUNNING` remain non-terminal. Affects the shared poll path used by
`execute()` and the directResults `execute_direct()`.

Tests: clippy clean; cargo test --lib 620 passed. Re-verified e2e via the Node
binding (CREATE/INSERT fire-and-forget commit, mid-run cancel, no close-drive).

Co-authored-by: Isaac
Signed-off-by: Madhavendra Rathore <madhavendra.rathore@databricks.com>
@msrathore-db

Copy link
Copy Markdown
Collaborator Author

Superseded by #140 (additive). The in-place fold here renamed execute()→execute_to_completion and migrated ~50 kernel tests onto it; #140 instead adds execute_direct() alongside an unchanged execute(), so there's no test churn and Python use_kernel needs no change. Same directResults behavior + CLOSED parity.

This branch was previously deployed

1 inactive deployment
azure-prod — 41c3d7ac Deployed Jun 6, 2026 by msrathore-db via PyO3 build + test (Python 3.10) #106
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant