diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index 4d2fdcf5..c909fcf4 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -183,9 +183,8 @@ typedef struct kernel_session_config_t KernelSessionConfig; typedef struct kernel_session_t kernel_session_t; typedef struct kernel_statement_t kernel_statement_t; typedef struct kernel_executed_statement_t kernel_executed_statement_t; -/* Async-consumption executed handle. Reserved: the async submit path is - * deferred in v0 (see kernel_statement_submit), so no consumer receives a - * live instance yet. */ +/* Async-consumption executed handle, returned by kernel_statement_submit + * and polled via kernel_executed_async_statement_status. */ typedef struct kernel_executed_async_statement_t kernel_executed_async_statement_t; typedef struct kernel_result_stream_t kernel_result_stream_t; @@ -285,15 +284,47 @@ KernelStatusCode kernel_statement_execute(kernel_statement_t* stmt, kernel_executed_statement_t** out); /* - * Submit-and-return (async). DEFERRED in v0: this always returns - * `KernelStatusCode_InvalidArgument` (with an explanatory last error) and - * writes nothing to `*out`. Use the synchronous `kernel_statement_execute` - * path. Declared so the symbol/contract is visible; wired when an ODBC / - * Go consumer needs caller-driven polling. + * Submit-and-return (async). Submits the statement for asynchronous + * execution and writes an async handle to `*out`; the caller then polls + * `kernel_executed_async_statement_status` (sleeping between polls, so no + * server-side worker thread is held), fetches the result once + * terminal-succeeded, and may cancel a running statement. This is the path + * a host driver uses to honor caller-side cancellation/deadlines. The + * returned handle must be released with `kernel_executed_async_statement_close`. */ KernelStatusCode kernel_statement_submit(kernel_statement_t* stmt, kernel_executed_async_statement_t** out); +/* + * Poll the server-side status of an async statement into `*out_kind`. + * A `Failed` status stores the underlying error (retrievable via + * `kernel_get_last_error`) and writes `KernelStatementStatusKind_Failed`; + * the poll call itself still returns `KernelStatusCode_Success`. + */ +KernelStatusCode kernel_executed_async_statement_status( + kernel_executed_async_statement_t* aexec, KernelStatementStatusKind* out_kind); + +/* + * Fetch the result stream once the statement is terminal-succeeded. `*out` + * receives an owned result stream that is driven lazily (CloudFetch chunks + * are fetched on demand, not materialised up front). Release it with + * `kernel_result_stream_close`. Calling this before the statement succeeds + * returns an error. + */ +KernelStatusCode kernel_executed_async_statement_get_result_stream( + kernel_executed_async_statement_t* aexec, kernel_result_stream_t** out); + +/* Cancel a running async statement. */ +KernelStatusCode kernel_executed_async_statement_cancel( + kernel_executed_async_statement_t* aexec); + +/* + * Free an async executed handle. Does NOT touch the parent statement, + * which you release separately with `kernel_statement_close`. + */ +KernelStatusCode kernel_executed_async_statement_close( + kernel_executed_async_statement_t* aexec); + /* * Free the statement lifecycle owner. Does NOT free executed-handle * boxes — each executed handle is a separate allocation that you must diff --git a/src/c_abi/mod.rs b/src/c_abi/mod.rs index bdb54a4c..31b8f57e 100644 --- a/src/c_abi/mod.rs +++ b/src/c_abi/mod.rs @@ -74,6 +74,12 @@ static RUNTIME: OnceLock = OnceLock::new(); /// /// [`Error`]: crate::kernel_error::Error pub(crate) fn runtime() -> crate::kernel_error::Result<&'static tokio::runtime::Runtime> { + // Initialise tracing once (idempotent via the logging module's OnceLock) so + // RUST_LOG=databricks_sql_kernel=debug surfaces the kernel's per-request logs + // for a C-ABI consumer (Go/ODBC) that installs no Rust-side subscriber. + // Without this, RUST_LOG is a no-op over the C ABI. Called here because + // runtime() is the funnel every async entry point passes through. + crate::logging::init_logging(&crate::logging::LogConfig::default()); // `OnceLock::get_or_init` can't carry a fallible initialiser, so do // the build-or-fetch dance by hand: try to fetch, and on a miss // build once and install. A lost race just drops our spare runtime. diff --git a/src/c_abi/result.rs b/src/c_abi/result.rs index 5682d9aa..f93228e9 100644 --- a/src/c_abi/result.rs +++ b/src/c_abi/result.rs @@ -37,7 +37,7 @@ use super::error::{store_and_code, store_invalid_arg}; use super::{catch_ffi, KernelStatusCode}; use crate::kernel_error::{Error, ErrorCode}; -use crate::result_stream::{ResultBatch, SchemaRef}; +use crate::result_stream::{ResultBatch, ResultStream, SchemaRef}; use crate::statement::executed::ExecutedStatement; use crate::statement::validity::ValidityFlag; use arrow_array::ffi::{FFI_ArrowArray, FFI_ArrowSchema}; @@ -68,6 +68,14 @@ enum ResultStreamInner { exec: *mut ExecutedStatement, valid: ValidityFlag, }, + /// Owns a `ResultStream` and drives it lazily, one batch per + /// `next_batch` call (async execute path, via + /// `kernel_executed_async_statement_get_result_stream`). Unlike + /// `Owned`, batches are NOT pre-drained — a large CloudFetch result + /// streams chunk-by-chunk instead of materialising in memory. Unlike + /// `Executed`, the stream is owned outright (no borrow / validity + /// flag), so it stays valid regardless of the async handle's lifetime. + OwnedLazy { stream: Box }, } /// Crate-internal: build an owned result-stream handle from a schema and @@ -98,6 +106,16 @@ pub(crate) unsafe fn into_raw_executed_stream( as *mut kernel_result_stream_t } +/// Crate-internal: build a result-stream handle that owns a `ResultStream` +/// and drives it lazily (async execute path). The stream is driven one +/// batch per `next_batch` call on the runtime, so CloudFetch chunks are +/// fetched on demand rather than pre-materialised. +pub(crate) fn into_raw_owned_lazy_stream(stream: ResultStream) -> *mut kernel_result_stream_t { + Box::into_raw(Box::new(ResultStreamInner::OwnedLazy { + stream: Box::new(stream), + })) as *mut kernel_result_stream_t +} + /// Resolve the backing `ExecutedStatement` for the `Executed` shape. /// /// Two independent stale-handle guards, checked in this order so the @@ -166,6 +184,7 @@ pub unsafe extern "C" fn kernel_result_stream_get_schema( Ok(e) => e.schema(), Err(code) => return code, }, + ResultStreamInner::OwnedLazy { stream } => stream.schema(), }; match FFI_ArrowSchema::try_from(schema.as_ref()) { Ok(ffi_schema) => { @@ -243,6 +262,33 @@ pub unsafe extern "C" fn kernel_result_stream_next_batch( Err(code) => return code, // error already stored } } + ResultStreamInner::OwnedLazy { stream } => { + // Owned stream: drive its next_batch on the runtime so lazy + // CloudFetch link fetches keep working, cloning the borrowed + // Arrow batch into an owned RecordBatch for FFI export. Same + // #[non_exhaustive] fallback as the Executed arm. + match super::block_on_ffi(move || { + let stream: &mut ResultStream = stream; + Box::pin(async move { + #[allow( + unreachable_patterns, + reason = "forward-compat fallback for future #[non_exhaustive] ResultBatch variants" + )] + match stream.next_batch().await? { + Some(ResultBatch::Arrow(rb)) => Ok(Some(rb.clone())), + Some(_) => Err(Error::not_implemented().message( + "result stream yielded a non-Arrow batch; the C ABI exports \ + Arrow only", + )), + None => Ok(None), + } + }) + }) { + Ok(Some(rb)) => Some(Ok(rb)), + Ok(None) => None, + Err(code) => return code, // error already stored + } + } }; match next { diff --git a/src/c_abi/statement.rs b/src/c_abi/statement.rs index 0ee34b94..a739fcd6 100644 --- a/src/c_abi/statement.rs +++ b/src/c_abi/statement.rs @@ -16,14 +16,13 @@ //! //! ## Scope in v0 //! -//! The C ABI wires the **synchronous** execute path -//! (`kernel_statement_execute` → `kernel_executed_statement_t` → -//! `kernel_result_stream_t`), which is what the ODBC consumer needs. The -//! **async** submit path (`kernel_statement_submit` + -//! `kernel_executed_async_statement_*`) keeps its skeleton declarations -//! and `unimplemented!()` bodies until a consumer needs caller-driven -//! polling over the C ABI; Rust-crate consumers use `Statement::submit` -//! directly. +//! The C ABI wires both execute paths: +//! - **synchronous** (`kernel_statement_execute` → +//! `kernel_executed_statement_t` → `kernel_result_stream_t`), and +//! - **async submit + poll** (`kernel_statement_submit` → +//! `kernel_executed_async_statement_*`), which lets a host driver poll +//! status and cancel a running statement to honor caller-side +//! cancellation/deadlines. //! //! ## Error reporting convention //! @@ -36,10 +35,11 @@ #![allow(non_camel_case_types)] use super::error::{store_and_code, store_invalid_arg}; -use super::result::{into_raw_executed_stream, kernel_result_stream_t}; +use super::result::{into_raw_executed_stream, into_raw_owned_lazy_stream, kernel_result_stream_t}; use super::{block_on_ffi, catch_ffi, KernelStatusCode}; use crate::kernel_error::Error; use crate::statement::executed::ExecutedStatement; +use crate::statement::executed_async::{ExecutedAsyncStatement, StatementStatus}; use crate::statement::mutable::Statement; use crate::statement::spec::TypedValue; use crate::statement::validity::ValidityFlag; @@ -96,7 +96,7 @@ pub struct kernel_executed_statement_t { } /// Async-consumption executed handle. Returned by -/// `kernel_statement_submit` (deferred in v0 — see module docs). +/// `kernel_statement_submit`. #[repr(C)] pub struct kernel_executed_async_statement_t { _private: [u8; 0], @@ -467,13 +467,32 @@ pub unsafe extern "C" fn kernel_executed_statement_close( KernelStatusCode::Success } -// ─── ExecutedAsyncStatement (async) — deferred in v0 ───────────────────────── +// ─── ExecutedAsyncStatement (async submit + poll) ──────────────────────────── // -// The async submit path keeps its skeleton until a consumer needs -// caller-driven polling over the C ABI. Rust-crate consumers use -// `Statement::submit` + `ExecutedAsyncStatement` directly. +// The async path lets a C-ABI consumer submit a statement, poll its status +// while sleeping between polls (holding no worker thread server-side), fetch +// the result stream once terminal-succeeded, and cancel a running statement. +// This is what gives Go's database/sql path context-cancellation parity with +// the Thrift backend: the host polls and calls _cancel on ctx.Done(). + +/// Boxed behind `kernel_executed_async_statement_t`. Owns the async handle. +struct AsyncHandle { + aexec: ExecutedAsyncStatement, +} + +/// Borrow the boxed [`AsyncHandle`] behind an opaque async handle pointer. +/// +/// # Safety +/// `aexec` must be a live handle from `kernel_statement_submit`. +unsafe fn async_handle<'a>( + aexec: *mut kernel_executed_async_statement_t, +) -> Option<&'a mut AsyncHandle> { + (aexec as *mut AsyncHandle).as_mut() +} -/// Submit-and-return. **Deferred in v0** — see module docs. +/// Submit the statement for asynchronous execution, returning immediately +/// with an async handle the caller polls via +/// [`kernel_executed_async_statement_status`]. /// /// # Safety /// `stmt` / `out` must be valid pointers. @@ -482,11 +501,147 @@ pub unsafe extern "C" fn kernel_statement_submit( stmt: *mut kernel_statement_t, out: *mut *mut kernel_executed_async_statement_t, ) -> KernelStatusCode { - let _ = (stmt, out); - crate::c_abi::error::clear_error(); - store_invalid_arg( - "async submit is not exposed over the v0 C ABI; use the synchronous execute path", - ) + catch_ffi(|| { + let Some(stmt) = statement_mut(stmt) else { + return store_invalid_arg("kernel_statement_submit: stmt must not be null"); + }; + if out.is_null() { + return store_invalid_arg("kernel_statement_submit: out must not be null"); + } + // Raw-pointer the statement across the block_on so the borrow doesn't + // have to outlive the future (mirrors kernel_statement_execute). + let stmt_ptr: *mut Statement = stmt; + match block_on_ffi(move || { + let stmt: &mut Statement = unsafe { &mut *stmt_ptr }; + Box::pin(async move { stmt.submit().await }) + }) { + Ok(aexec) => { + std::ptr::write( + out, + Box::into_raw(Box::new(AsyncHandle { aexec })) + as *mut kernel_executed_async_statement_t, + ); + KernelStatusCode::Success + } + Err(code) => code, + } + }) +} + +/// Poll the server-side status into `*out_kind`. A `Failed` status stores the +/// underlying error (retrievable via `kernel_get_last_error`) and writes +/// `Failed`; the poll itself still returns `Success`. +/// +/// # Safety +/// `aexec` / `out_kind` must be valid pointers. +#[no_mangle] +pub unsafe extern "C" fn kernel_executed_async_statement_status( + aexec: *mut kernel_executed_async_statement_t, + out_kind: *mut KernelStatementStatusKind, +) -> KernelStatusCode { + catch_ffi(|| { + let Some(handle) = async_handle(aexec) else { + return store_invalid_arg("async status: handle must not be null"); + }; + if out_kind.is_null() { + return store_invalid_arg("async status: out_kind must not be null"); + } + let aexec_ptr: *const ExecutedAsyncStatement = &handle.aexec; + match block_on_ffi(move || { + let a: &ExecutedAsyncStatement = unsafe { &*aexec_ptr }; + Box::pin(async move { a.status().await }) + }) { + Ok(st) => { + let kind = match st { + StatementStatus::Pending => KernelStatementStatusKind::Pending, + StatementStatus::Running => KernelStatementStatusKind::Running, + StatementStatus::Succeeded => KernelStatementStatusKind::Succeeded, + StatementStatus::Cancelled => KernelStatementStatusKind::Cancelled, + StatementStatus::Closed => KernelStatementStatusKind::Closed, + StatementStatus::Failed(e) => { + let _ = store_and_code(e); + KernelStatementStatusKind::Failed + } + }; + std::ptr::write(out_kind, kind); + KernelStatusCode::Success + } + Err(code) => code, + } + }) +} + +/// Fetch the result stream once the statement is terminal-succeeded. `*out` +/// receives an owned, lazily-driven `kernel_result_stream_t` (CloudFetch +/// chunks are fetched on demand, not pre-materialised). +/// +/// # Safety +/// `aexec` / `out` must be valid pointers. +#[no_mangle] +pub unsafe extern "C" fn kernel_executed_async_statement_get_result_stream( + aexec: *mut kernel_executed_async_statement_t, + out: *mut *mut kernel_result_stream_t, +) -> KernelStatusCode { + catch_ffi(|| { + let Some(handle) = async_handle(aexec) else { + return store_invalid_arg("async get_result_stream: handle must not be null"); + }; + if out.is_null() { + return store_invalid_arg("async get_result_stream: out must not be null"); + } + let aexec_ptr: *const ExecutedAsyncStatement = &handle.aexec; + match block_on_ffi(move || { + let a: &ExecutedAsyncStatement = unsafe { &*aexec_ptr }; + Box::pin(async move { a.fetch_result().await }) + }) { + Ok(stream) => { + std::ptr::write(out, into_raw_owned_lazy_stream(stream)); + KernelStatusCode::Success + } + Err(code) => code, + } + }) +} + +/// Cancel a running statement. +/// +/// # Safety +/// `aexec` must be a live async executed handle. +#[no_mangle] +pub unsafe extern "C" fn kernel_executed_async_statement_cancel( + aexec: *mut kernel_executed_async_statement_t, +) -> KernelStatusCode { + catch_ffi(|| { + let Some(handle) = async_handle(aexec) else { + return store_invalid_arg("async cancel: handle must not be null"); + }; + let aexec_ptr: *const ExecutedAsyncStatement = &handle.aexec; + match block_on_ffi(move || { + let a: &ExecutedAsyncStatement = unsafe { &*aexec_ptr }; + Box::pin(async move { a.canceller().cancel().await }) + }) { + Ok(()) => KernelStatusCode::Success, + Err(code) => code, + } + }) +} + +/// Free an async executed handle. Does not touch the parent statement. +/// +/// # Safety +/// `aexec` must have been returned by `kernel_statement_submit` and not +/// previously closed. +#[no_mangle] +pub unsafe extern "C" fn kernel_executed_async_statement_close( + aexec: *mut kernel_executed_async_statement_t, +) -> KernelStatusCode { + catch_ffi(|| { + if aexec.is_null() { + return store_invalid_arg("async close: handle must not be null"); + } + drop(Box::from_raw(aexec as *mut AsyncHandle)); + KernelStatusCode::Success + }) } // ─── Shared helpers ────────────────────────────────────────────────────────── @@ -513,3 +668,49 @@ unsafe fn with_statement( } }) } + +#[cfg(test)] +mod tests { + use super::*; + + // The async entry points need a live server to do real work, so the + // server-driven paths are covered by the e2e/wiremock integration tests. + // Here we pin the pointer-safety contract: every async function returns + // InvalidArgument (never crashes / dereferences null) when handed a null + // handle or null out-param, matching the guard style of the sync path. + + #[test] + fn submit_null_guards_return_invalid_argument() { + let mut out: *mut kernel_executed_async_statement_t = std::ptr::null_mut(); + let rc = unsafe { kernel_statement_submit(std::ptr::null_mut(), &mut out) }; + assert_eq!(rc, KernelStatusCode::InvalidArgument); + } + + #[test] + fn status_null_guards_return_invalid_argument() { + let mut kind = KernelStatementStatusKind::Pending; + let rc = unsafe { kernel_executed_async_statement_status(std::ptr::null_mut(), &mut kind) }; + assert_eq!(rc, KernelStatusCode::InvalidArgument); + } + + #[test] + fn get_result_stream_null_guards_return_invalid_argument() { + let mut out: *mut kernel_result_stream_t = std::ptr::null_mut(); + let rc = unsafe { + kernel_executed_async_statement_get_result_stream(std::ptr::null_mut(), &mut out) + }; + assert_eq!(rc, KernelStatusCode::InvalidArgument); + } + + #[test] + fn cancel_null_guard_returns_invalid_argument() { + let rc = unsafe { kernel_executed_async_statement_cancel(std::ptr::null_mut()) }; + assert_eq!(rc, KernelStatusCode::InvalidArgument); + } + + #[test] + fn close_null_guard_returns_invalid_argument() { + let rc = unsafe { kernel_executed_async_statement_close(std::ptr::null_mut()) }; + assert_eq!(rc, KernelStatusCode::InvalidArgument); + } +}