Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ under [`pyo3/CHANGELOG.md`](pyo3/CHANGELOG.md) and
[`napi/CHANGELOG.md`](napi/CHANGELOG.md); they ship on independent
cadences and may not move in lockstep with the crate.

## [Unreleased]

### Added
- **Transaction control on `Session`** — `set_autocommit(bool)` /
`autocommit()` / `commit()` / `rollback()`, exposed over the C ABI as
`kernel_session_set_autocommit` / `_get_autocommit` / `_commit` /
`_rollback`. Implemented as session-scoped SQL (`SET AUTOCOMMIT`,
`COMMIT`, `ROLLBACK`), mirroring the reference/JDBC drivers, so every
wrapper (ODBC / Python / Go / Node) gets identical transaction
semantics from one implementation. Gated by the new
`DefaultOpts::ignore_transactions` (builder shortcut
`SessionBuilder::ignore_transactions`; C ABI
`kernel_session_config_set_ignore_transactions`), which defaults to
`true` — matching the reference/JDBC `IgnoreTransactions=1`: when set,
the control methods are silent no-ops. Set `false` to opt into real
multi-statement transactions (Private Preview; requires catalog-managed
tables server-side).

## [v0.2.0] - 2026-06-08

**Beta release of the Databricks SQL Kernel.** Bumps the crate out
Expand Down
32 changes: 32 additions & 0 deletions include/databricks_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,14 @@ KernelStatusCode kernel_session_config_set_retry_config(KernelSessionConfig* con
uint32_t max_retries,
uint64_t overall_timeout_ms);

/* Whether transaction control is ignored (no-oped). `ignore = true` (the
* default) gives IgnoreTransactions=1 semantics: commit / rollback /
* autocommit-off become silent no-ops. `ignore = false` enables real
* multi-statement transactions (Private Preview; requires catalog-managed
* tables server-side). Shared by all drivers (ODBC / Python / Go / Node). */
KernelStatusCode kernel_session_config_set_ignore_transactions(KernelSessionConfig* config,
bool ignore);

/* ─── Session lifecycle ───────────────────────────────────────────────*/

/*
Expand All @@ -360,6 +368,30 @@ KernelStatusCode kernel_session_is_open(const kernel_session_t* session, bool* o
KernelStatusCode kernel_session_new_statement(kernel_session_t* session,
kernel_statement_t** out);

/* ─── Transaction control ─────────────────────────────────────────────
*
* Transaction control is expressed as session-scoped SQL and shared by all
* drivers. When the session was opened with ignore_transactions = true (the
* default), these are silent no-ops returning Success. When false (opt-in),
* they issue SET AUTOCOMMIT / COMMIT / ROLLBACK on the session; real
* multi-statement transactions require catalog-managed tables server-side.
* The three mutating calls run SQL and MUST be invoked from a native
* (non-async-runtime) thread. */

/* Set autocommit mode. false begins an explicit transaction; true returns to
* auto-commit. Issues `SET AUTOCOMMIT = TRUE|FALSE`. No-op when transactions
* are ignored. */
KernelStatusCode kernel_session_set_autocommit(kernel_session_t* session, bool enabled);

/* Read the current autocommit mode into `*out`. Local read — no round-trip. */
KernelStatusCode kernel_session_get_autocommit(const kernel_session_t* session, bool* out);

/* Commit the current transaction (issues `COMMIT`). No-op when ignored. */
KernelStatusCode kernel_session_commit(kernel_session_t* session);

/* Roll back the current transaction (issues `ROLLBACK`). No-op when ignored. */
KernelStatusCode kernel_session_rollback(kernel_session_t* session);

/* ─── Statement ───────────────────────────────────────────────────────*/

/* Set the statement's SQL text, replacing any previously set SQL. Also clears
Expand Down
148 changes: 140 additions & 8 deletions src/c_abi/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
//! `set_auth_u2m`). A host may also drive its own token flow and inject
//! it via PAT.
//!
//! Transaction methods (autocommit / commit / rollback) are deferred to
//! v0.1 per `docs/spec-v0.md` addendum A5.
//! Transaction control (autocommit / commit / rollback) is exposed via
//! `kernel_session_set_autocommit` / `_get_autocommit` / `_commit` /
//! `_rollback`, gated by `kernel_session_config_set_ignore_transactions`
//! (default = ignore). Backed by the shared `Session` transaction methods.

use super::{block_on_ffi, KernelStatusCode};
use crate::config::{AuthConfig, ConnectionConfig, HttpConfig, ProxyConfig, TlsConfig};
Expand Down Expand Up @@ -102,6 +104,11 @@ pub struct KernelSessionConfig {
/// lets a host make its own backoff/attempt policy authoritative (a
/// JDBC-style `RetryWaitMin` / `RetryWaitMax` / `RetryMax` surface).
retry: Option<RetrySpec>,
/// Whether transaction control is ignored (no-oped). Defaults to `true`
/// (matching the reference/JDBC `IgnoreTransactions=1`); a host opts into
/// real transactions via `kernel_session_config_set_ignore_transactions`
/// with `false`. Maps to [`DefaultOpts::ignore_transactions`].
ignore_transactions: bool,
}

/// C-side proxy config captured from `kernel_session_config_set_proxy`.
Expand Down Expand Up @@ -177,6 +184,8 @@ pub unsafe extern "C" fn kernel_session_config_new(
tls: TlsSpec::default(),
custom_headers: Vec::new(),
retry: None,
// Default to ignoring transactions, matching IgnoreTransactions=1.
ignore_transactions: true,
});
std::ptr::write(out, Box::into_raw(config));
KernelStatusCode::Success
Expand Down Expand Up @@ -590,6 +599,26 @@ pub unsafe extern "C" fn kernel_session_config_set_tls_skip_hostname_verificatio
})
}

/// Set whether transaction control is ignored (no-oped). Pass `true`
/// (the default) for `IgnoreTransactions=1` semantics — `SQLEndTran` /
/// commit / rollback / autocommit-off become silent no-ops. Pass `false`
/// to enable real multi-statement transactions (Private Preview; requires
/// catalog-managed tables server-side). Maps to
/// [`DefaultOpts::ignore_transactions`].
///
/// # Safety
/// `config` must be a live config handle.
#[no_mangle]
pub unsafe extern "C" fn kernel_session_config_set_ignore_transactions(
config: *mut KernelSessionConfig,
ignore: bool,
) -> KernelStatusCode {
set_on_config(config, |c| {
c.ignore_transactions = ignore;
Ok(())
})
}

/// Shared body for the config setters: null-check the handle, clear the
/// thread-local error, run `f` against the borrowed config, and store
/// any error.
Expand Down Expand Up @@ -722,6 +751,7 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod
tls,
custom_headers,
retry,
ignore_transactions,
} = config;

block_on_ffi(move || {
Expand Down Expand Up @@ -769,7 +799,11 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod
let mut builder = Session::builder()
.connection(connection)
.auth(auth)
.session_conf(session_conf);
.session_conf(session_conf)
.defaults(crate::config::DefaultOpts {
ignore_transactions,
..Default::default()
});

// Skip `.http()` when neither headers nor a retry spec was set, so
// the kernel's full `HttpConfig::default()` applies untouched.
Expand Down Expand Up @@ -910,11 +944,109 @@ pub unsafe extern "C" fn kernel_session_new_statement(
KernelStatusCode::Success
}

// Deferred to v0.1 (see addendum A5):
//
// pub unsafe extern "C" fn kernel_session_set_autocommit(...) -> KernelStatusCode;
// pub unsafe extern "C" fn kernel_session_commit(...) -> KernelStatusCode;
// pub unsafe extern "C" fn kernel_session_rollback(...) -> KernelStatusCode;
/// Set autocommit mode on the session. `enabled = false` begins explicit
/// transaction mode (subsequent statements run in a transaction until
/// `kernel_session_commit` / `kernel_session_rollback`); `true` returns to
/// auto-commit. Issues `SET AUTOCOMMIT = TRUE|FALSE` on the server.
///
/// A no-op returning `Success` when the session was configured with
/// `ignore_transactions = true` (the default). Must be called from a
/// native (non-async-runtime) thread.
///
/// # Safety
/// `session` must be a live session handle from [`kernel_session_open`].
#[no_mangle]
pub unsafe extern "C" fn kernel_session_set_autocommit(
session: *mut kernel_session_t,
enabled: bool,
) -> KernelStatusCode {
crate::c_abi::error::clear_error();
let Some(session) = (session as *const Session).as_ref() else {
crate::c_abi::error::store_error(
Error::invalid_argument()
.message("kernel_session_set_autocommit: session must not be null"),
);
return KernelStatusCode::InvalidArgument;
};
match block_on_ffi(|| session.set_autocommit(enabled)) {
Ok(()) => KernelStatusCode::Success,
Err(code) => code,
}
}

/// Read the session's current autocommit mode into `*out`. Local read — no
/// server round-trip.
///
/// # Safety
/// `session` must be a live session handle; `out` must be a writable `bool*`.
#[no_mangle]
pub unsafe extern "C" fn kernel_session_get_autocommit(
session: *const kernel_session_t,
out: *mut bool,
) -> KernelStatusCode {
crate::c_abi::error::clear_error();
let Some(session) = (session as *const Session).as_ref() else {
crate::c_abi::error::store_error(
Error::invalid_argument()
.message("kernel_session_get_autocommit: session must not be null"),
);
return KernelStatusCode::InvalidArgument;
};
if out.is_null() {
crate::c_abi::error::store_error(
Error::invalid_argument()
.message("kernel_session_get_autocommit: out must not be null"),
);
return KernelStatusCode::InvalidArgument;
}
std::ptr::write(out, session.autocommit());
KernelStatusCode::Success
}

/// Commit the current transaction (issues `COMMIT`). A no-op returning
/// `Success` when the session has `ignore_transactions = true`. Must be
/// called from a native (non-async-runtime) thread.
///
/// # Safety
/// `session` must be a live session handle from [`kernel_session_open`].
#[no_mangle]
pub unsafe extern "C" fn kernel_session_commit(session: *mut kernel_session_t) -> KernelStatusCode {
crate::c_abi::error::clear_error();
let Some(session) = (session as *const Session).as_ref() else {
crate::c_abi::error::store_error(
Error::invalid_argument().message("kernel_session_commit: session must not be null"),
);
return KernelStatusCode::InvalidArgument;
};
match block_on_ffi(|| session.commit()) {
Ok(()) => KernelStatusCode::Success,
Err(code) => code,
}
}

/// Roll back the current transaction (issues `ROLLBACK`). A no-op returning
/// `Success` when the session has `ignore_transactions = true`. Requires
/// catalog-managed tables server-side for the rollback to take effect. Must
/// be called from a native (non-async-runtime) thread.
///
/// # Safety
/// `session` must be a live session handle from [`kernel_session_open`].
#[no_mangle]
pub unsafe extern "C" fn kernel_session_rollback(
session: *mut kernel_session_t,
) -> KernelStatusCode {
crate::c_abi::error::clear_error();
let Some(session) = (session as *const Session).as_ref() else {
crate::c_abi::error::store_error(
Error::invalid_argument().message("kernel_session_rollback: session must not be null"),
);
return KernelStatusCode::InvalidArgument;
};
match block_on_ffi(|| session.rollback()) {
Ok(()) => KernelStatusCode::Success,
Err(code) => code,
}
}

#[cfg(test)]
mod tests {
Expand Down
24 changes: 23 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,32 @@ fn parse_org_id(http_path: &str) -> Option<String> {
/// Optional: per-session defaults applied to statements that don't
/// override.
#[non_exhaustive]
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct DefaultOpts {
pub catalog: Option<String>,
pub schema: Option<String>,

/// When `true` (the default), transaction-control operations are
/// silently no-oped: `Session::set_autocommit(false)`,
/// `Session::commit()`, and `Session::rollback()` return `Ok(())`
/// without issuing any server statement. Mirrors the reference/JDBC
/// `IgnoreTransactions` connection property, whose default is `1`
/// (transactions disabled). Set to `false` to opt into real
/// multi-statement transactions (a Private Preview feature; requires
/// catalog-managed tables server-side).
pub ignore_transactions: bool,
}

impl Default for DefaultOpts {
fn default() -> Self {
Self {
catalog: None,
schema: None,
// Match the reference/JDBC default of IgnoreTransactions=1:
// transaction control is no-oped unless a caller opts in.
ignore_transactions: true,
}
}
}

// ─── Authentication ─────────────────────────────────────────────────────────
Expand Down
Loading
Loading