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
148 changes: 123 additions & 25 deletions napi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,20 @@ use serde_json::{json, Map, Value};
pub(crate) const ERROR_SENTINEL: &str = "__databricks_error__:";

/// Map a kernel [`Error`] into a `napi::Error`. Preserves the kernel
/// `ErrorCode` (mapped to the closest napi `Status`), and stuffs eight
/// of the ten structured kernel-`Error` fields (`code`, `message`,
/// `sql_state`, `error_code`, `vendor_code`, `http_status`, `retryable`,
/// `query_id`) into a JSON envelope on the reason so the TS layer can
/// reconstruct the typed error class. `location` and `cause` are
/// intentionally omitted from the v1 envelope — see the module-level
/// doc for rationale.
/// `ErrorCode` (mapped to the closest napi `Status`), and stuffs the
/// structured kernel-`Error` fields into a JSON envelope on the reason
/// so the TS layer can reconstruct the typed error class. `location`
/// and `cause` are intentionally omitted from the v1 envelope — see
/// the module-level doc for rationale.
///
/// **Wire-shape superset of pyo3 for Thrift parity:** the envelope
/// also carries `displayMessage` / `diagnosticInfo` / `errorDetailsJson`
/// — the three SEA extended-status fields that ride on the wire across
/// terminal-error states. These mirror the same-named fields on the
/// NodeJS Thrift driver's `TGetOperationStatusResp`, so JS callers
/// migrating from Thrift to the SEA-backed binding see the same field
/// set on a failed statement. The pyo3 binding does NOT (yet) attach
/// these — pyo3's `attach_attrs` stays at the original 8 fields.
///
/// The whole JSON-envelope-on-`reason` scheme is interim. The follow-up
/// PR that lands the first `#[napi]` entry points (PR #25) is expected
Expand Down Expand Up @@ -136,6 +143,21 @@ pub(crate) fn napi_err_from_kernel(e: KernelError) -> NapiError {
if let Some(qid) = e.query_id {
obj.insert("queryId".to_owned(), Value::from(qid));
}
// Thrift-parity extended status fields: surfaced on the napi
// envelope so a JS caller's catch-block sees the same fields the
// Thrift driver's `TGetOperationStatusResp` exposes. Only
// populated on terminal-error states (`FAILED` / `CANCELED` /
// `CLOSED-no-data`) — the kernel's
// `terminal_outcome::carry_extended_status` is the upstream.
if let Some(dm) = e.display_message {
obj.insert("displayMessage".to_owned(), Value::from(dm));
}
if let Some(di) = e.diagnostic_info {
obj.insert("diagnosticInfo".to_owned(), Value::from(di));
}
if let Some(ej) = e.error_details_json {
obj.insert("errorDetailsJson".to_owned(), Value::from(ej));
}

// `to_string` on a JSON object is infallible: every field above is
// a `String` / `i64` / `u16` / `bool` whose serialization can't fail.
Expand Down Expand Up @@ -291,21 +313,26 @@ mod tests {
);
}

/// Parity guard: the napi envelope must surface the same kernel
/// Parity guard: the napi envelope must carry the same kernel
/// `Error` field set that pyo3's typed `KernelError` exposes via
/// `setattr`. The two bindings diverge on *wire mechanism* (typed
/// exception attrs vs JSON envelope) and on *naming convention*
/// (snake_case attrs in pyo3 vs camelCase JSON keys in napi) — but
/// the *field set* must stay aligned or downstream wrappers that
/// claim Python ↔ Node parity will silently drop information when
/// crossing the language boundary.
/// (snake_case attrs in pyo3 vs camelCase JSON keys in napi).
///
/// **Thrift-parity superset.** The napi envelope is a strict
/// superset of pyo3's field set: it adds `displayMessage` /
/// `diagnosticInfo` / `errorDetailsJson` so JS consumers see the
/// same fields the NodeJS Thrift driver's `TGetOperationStatusResp`
/// exposes on terminal-error states. The pyo3 binding may grow
/// these later via a separate `attach_attrs` extension; the two
/// bindings stay in sync on the *pyo3-side 8 fields*, and napi
/// extends from there for its host-language's contract.
///
/// pyo3 attaches via `attach_attrs(...)` in `pyo3/src/errors.rs:67`:
/// `code, message, sql_state, error_code, vendor_code, http_status,
/// retryable, query_id`. Both bindings intentionally omit `location`
/// (kernel-internal `file:line` for `Internal` errors) and `cause`
/// (recursive — needs an explicit schema decision). This test pins
/// the envelope to that 8-field contract.
/// (recursive — needs an explicit schema decision).
///
/// Reviewer-flagged at PR #27 / error.rs:76 — see comment text
/// `pyo3 attaches kernel error fields as Python attributes…`.
Expand All @@ -321,6 +348,11 @@ mod tests {
e.http_status = Some(503);
e.retryable = true;
e.query_id = Some("01eedeadbeefdeadbeefdeadbeefdead".to_string());
// Thrift-parity extension fields: populated too so the
// superset relation is exercised in the same fixture.
e.display_message = Some("Statement failed at operator Scan".to_string());
e.diagnostic_info = Some("operator=Scan; stage=Compile".to_string());
e.error_details_json = Some(r#"{"errorClass":"COMPILE_FAILURE"}"#.to_string());
// Set the *intentionally-omitted* fields too — the test should
// confirm they do NOT appear on the wire.
e.location = Some("kernel/src/foo.rs:42".to_string());
Expand All @@ -335,10 +367,10 @@ mod tests {
.as_object()
.expect("envelope payload must be a JSON object");

// Mirrors pyo3's `attach_attrs` field list, in pyo3's snake_case
// → napi's camelCase. Update both lists together when the
// contract changes.
let expected_keys: &[&str] = &[
// pyo3's `attach_attrs` field list, in pyo3's snake_case →
// napi's camelCase. These 8 keys must stay in lockstep across
// bindings — any divergence on these is a parity regression.
let pyo3_parity_keys: &[&str] = &[
"code", // pyo3: "code"
"message", // pyo3: "message"
"sqlState", // pyo3: "sql_state"
Expand All @@ -348,14 +380,30 @@ mod tests {
"retryable", // pyo3: "retryable"
"queryId", // pyo3: "query_id"
];
for k in expected_keys {
for k in pyo3_parity_keys {
assert!(
obj.contains_key(*k),
"envelope is missing the `{k}` key — pyo3 sets this via \
setattr, napi binding must keep parity. payload: {payload}"
);
}

// Thrift-parity superset: these 3 are napi-only today (pyo3
// may add them later via attach_attrs). They mirror the
// same-named optional fields on the NodeJS Thrift
// `TGetOperationStatusResp`.
let thrift_parity_keys: &[&str] = &[
"displayMessage", // Thrift: displayMessage
"diagnosticInfo", // Thrift: diagnosticInfo
"errorDetailsJson", // Thrift: errorDetailsJson
];
for k in thrift_parity_keys {
assert!(
obj.contains_key(*k),
"envelope is missing the Thrift-parity key `{k}`. payload: {payload}"
);
}

// Intentionally-omitted fields must NOT leak into the wire
// shape (the module doc and the kernel `Error` struct both
// identify `location` and `cause` as v1-omitted). If a future
Expand All @@ -369,18 +417,63 @@ mod tests {
);
}

// No surprise keys: the wire shape stays the documented set
// (exact-size check, not subset). A future field addition
// requires updating both pyo3's attach_attrs and this list.
// No surprise keys: the wire shape stays the documented set.
// Exact-size check (not subset) catches accidental additions.
let expected_total = pyo3_parity_keys.len() + thrift_parity_keys.len();
assert_eq!(
obj.len(),
expected_keys.len(),
"envelope has unexpected extra keys vs the pyo3 parity set. \
expected: {expected_keys:?}, got: {:?}",
expected_total,
"envelope has unexpected extra keys. expected pyo3 parity \
{pyo3_parity_keys:?} + thrift parity {thrift_parity_keys:?}, \
got: {:?}",
obj.keys().collect::<Vec<_>>()
);
}

/// Thrift parity: on a non-terminal-error code (where the kernel
/// doesn't populate the SEA extended fields) the three Thrift
/// extension keys are omitted from the envelope. Mirrors the
/// optional-field-omitted-when-None convention the existing
/// envelope already enforces for the pyo3-parity fields.
#[test]
fn envelope_omits_thrift_extension_fields_when_unpopulated() {
let e = KernelError::invalid_argument().message("bad arg");
let napi_err = napi_err_from_kernel(e);
let payload = &napi_err.reason.to_string()[ERROR_SENTINEL.len()..];
let v: serde_json::Value = serde_json::from_str(payload).unwrap();
let obj = v.as_object().unwrap();
for absent in &["displayMessage", "diagnosticInfo", "errorDetailsJson"] {
assert!(
!obj.contains_key(*absent),
"key `{absent}` should be omitted when kernel field is \
None, got: {payload}"
);
}
}

/// Wire round-trip for the Thrift parity fields: when the kernel
/// `Error` carries the 3 extended-status strings, the envelope
/// surfaces them under their camelCase keys with verbatim content.
/// Pins the exact key names so the TS adapter's typed-error
/// reviver can rely on them.
#[test]
fn envelope_surfaces_thrift_extension_fields_verbatim() {
let dm = "Statement was canceled by user; runtime 42s";
let di = "operator=Scan->Filter->Project; stage=Execute; thread=0xfe";
let ej = r#"{"errorClass":"USER_CANCELLATION","detail":{"reason":"sigint"}}"#;
let e = KernelError::sql_error()
.message("canceled")
.with_display_message(dm)
.with_diagnostic_info(di)
.with_error_details_json(ej);
let napi_err = napi_err_from_kernel(e);
let payload = &napi_err.reason.to_string()[ERROR_SENTINEL.len()..];
let v: serde_json::Value = serde_json::from_str(payload).unwrap();
assert_eq!(v["displayMessage"], dm);
assert_eq!(v["diagnosticInfo"], di);
assert_eq!(v["errorDetailsJson"], ej);
}

/// Optional fields are omitted from the wire shape when `None` (no
/// `null` entries). This keeps the envelope compact AND means a TS
/// reviver doesn't need to special-case `null` separately from
Expand All @@ -404,6 +497,11 @@ mod tests {
"httpStatus",
"retryable",
"queryId",
// Thrift-parity extension fields also follow the
// omit-when-None convention.
"displayMessage",
"diagnosticInfo",
"errorDetailsJson",
] {
assert!(
!obj.contains_key(*absent),
Expand Down
104 changes: 104 additions & 0 deletions napi/src/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,110 @@ impl Statement {
&self.statement_id
}

// ─── SEA extended status accessors (Thrift parity) ─────────────────
//
// Mirror the same-named optional fields on the NodeJS Thrift
// driver's `TGetOperationStatusResp`. Populated whenever the SEA
// server sets them on the terminal `StatementStatus` envelope; the
// kernel stores the status struct once at construction so the
// accessors are read-only and lock-free at the kernel layer.
//
// These methods are `async` because the napi binding's inner
// state sits inside `Arc<Mutex<Option<StatementInner>>>` and the
// lock shape mirrors `fetchNextBatch` / `cancel` — a brief tokio
// mutex held across the kernel accessor call. After `close()` the
// inner state has been taken; the accessors throw an
// `InvalidStatementHandle` error in that case, matching the
// existing `cancel()` semantics.

/// Number of rows modified by the statement (UPDATE / INSERT /
/// DELETE / MERGE). `null` for SELECT and on warehouses that don't
/// surface the counter. Mirrors Thrift's
/// `TGetOperationStatusResp.numModifiedRows`.
#[napi]
pub async fn num_modified_rows(&self) -> napi::Result<Option<i64>> {
let inner = Arc::clone(&self.inner);
guarded(async move {
let guard = inner.lock().await;
let inner_state = guard.as_ref().ok_or_else(statement_closed_err)?;
Ok(inner_state.executed.num_modified_rows())
})
.await
}

// TODO(pr23-followup): mirror a `progress()` accessor here once
// the kernel exposes one. Empirically, the SEA server does not
// populate progress today (verified 2026-05-27 against pecotesting:
// a 37s heavy TPC-H query polled 7 times with
// `getProgressUpdate=true` on the Thrift NodeJS path returned
// `progressUpdateResponse = null` on every poll). When the server
// grows progress support, expose it here under the same name JS
// callers expect from Thrift's `TGetOperationStatusResp.progressUpdateResponse`
// — likely as a struct mirroring `TProgressUpdateResp`, not a bare
// float.

/// Server-supplied user-facing message. Mirrors Thrift's
/// `TGetOperationStatusResp.displayMessage`. **PII / sensitive-
/// data note:** may contain SQL fragments or parameter values —
/// redact before centralised logging.
///
/// Populated on `Succeeded` / `Closed-with-inline-data` paths.
/// On terminal-error states (`Failed` / `Cancelled` /
/// `Closed-no-data`) the kernel returns an Error instead of a
/// `Statement`, and the same field rides on the JS Error envelope
/// under the same `displayMessage` key.
#[napi]
pub async fn display_message(&self) -> napi::Result<Option<String>> {
let inner = Arc::clone(&self.inner);
guarded(async move {
let guard = inner.lock().await;
let inner_state = guard.as_ref().ok_or_else(statement_closed_err)?;
Ok(inner_state.executed.display_message().map(str::to_owned))
})
.await
}

/// Server-supplied diagnostic detail — multi-line operator /
/// stack context. Mirrors Thrift's
/// `TGetOperationStatusResp.diagnosticInfo`. For support surfaces,
/// not user-facing. Same reachability + PII caveats as
/// `displayMessage`.
#[napi]
pub async fn diagnostic_info(&self) -> napi::Result<Option<String>> {
let inner = Arc::clone(&self.inner);
guarded(async move {
let guard = inner.lock().await;
let inner_state = guard.as_ref().ok_or_else(statement_closed_err)?;
Ok(inner_state.executed.diagnostic_info().map(str::to_owned))
})
.await
}

/// Server-supplied JSON blob with extended error details. Mirrors
/// Thrift's `TGetOperationStatusResp.errorDetailsJson`.
/// Pass-through string — JS callers parse with `JSON.parse` if
/// they need structured access.
///
/// **Server-side gating:** populated only when the workspace has
/// `spark.databricks.sql.errorDetailsJson.enabled = true` on the
/// underlying SQL cluster. The flag is internal-only / default-
/// false in the Databricks runtime, so for most JS callers this
/// will return `null`. Admin-enabled workspaces return content
/// shaped like `{"errorClass": "...", "messageTemplate": "..."}`.
///
/// **Unbounded:** when populated, server can return a multi-MB
/// blob; size before logging.
#[napi]
pub async fn error_details_json(&self) -> napi::Result<Option<String>> {
let inner = Arc::clone(&self.inner);
guarded(async move {
let guard = inner.lock().await;
let inner_state = guard.as_ref().ok_or_else(statement_closed_err)?;
Ok(inner_state.executed.error_details_json().map(str::to_owned))
})
.await
}

/// Pull the next batch of results. Returns `null` when the stream
/// is exhausted. The returned `ArrowBatch.ipcBytes` is a complete
/// Arrow IPC stream (schema header + 1 record-batch message)
Expand Down
Loading