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: 6 additions & 12 deletions src/client/sea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,19 +338,12 @@ impl SeaRestTransport {
.message(&error_msg)
.with_query_id(&response.statement_id);

if let Some(err) = service_error {
// SQLSTATE: prefer the structured field; fall back
// to parsing the message ("SQLSTATE: XXXXX" pattern).
let sqlstate = err
.sql_state
.as_ref()
.filter(|s| s.len() == 5)
.cloned()
.or_else(|| crate::kernel_error::extract_sqlstate_from_message(&error_msg));
if let Some(s) = sqlstate {
error = error.with_sql_state(s);
}
if let Some(s) = crate::kernel_error::extract_sqlstate_from_status(&response.status)
{
error = error.with_sql_state(s);
Comment thread
vuanhphung marked this conversation as resolved.
Resolved
Comment thread
vuanhphung marked this conversation as resolved.
Resolved
}

if let Some(err) = service_error {
// Databricks-specific error code slug
// (`STATEMENT_NOT_FOUND`, `WAREHOUSE_STOPPED`, …).
// Wrappers branch on this to map host-language
Expand Down Expand Up @@ -1454,6 +1447,7 @@ mod tests {
),
sql_state: None,
}),
sql_state: None,
num_modified_rows: None,
display_message: None,
diagnostic_info: None,
Expand Down
81 changes: 81 additions & 0 deletions src/kernel_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,29 @@ pub(crate) fn carry_extended_status(

// ─── SQLSTATE extraction helpers ────────────────────────────────────

/// Extract the SQLSTATE from a SEA statement status.
///
/// Prefer the canonical `status.sql_state`, then parse
/// `status.error.message`. The nested `status.error.sql_state` field
/// belongs to the shared transport-error shape and is intentionally
/// ignored for statement failures.
pub(crate) fn extract_sqlstate_from_status(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Low — extract_sqlstate_from_status now reads only top-level status.sql_state then falls back to parsing status.error.message. The prior code (both sync and async paths) also consulted the nested status.error.sql_state field; that path is now dropped entirely.

This is deliberate and documented, and the message-parsing fallback plus the message-substring branch in is_object_not_found mitigate most cases. But there is a narrow regression window: a server (e.g. an older DBSQL response) that reports SQLSTATE only in status.error.sql_state — with no top-level sql_state and no SQLSTATE: XXXXX substring in the message — would now silently lose the SQLSTATE. For metadata fan-out that specifically means a 42704 object-not-found delivered only in the nested field would no longer be recognized as not-found (via the SQLSTATE path), so list_tables wildcard enumeration would propagate the error instead of skipping the dropped catalog.

If you've confirmed no live/older server populates only the nested field for statement failures, this is fine as-is — worth a one-line note in the PR that the drop was validated against those responses, since the moved wiremock fixtures previously exercised the nested placement.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sql state should be derived from statementstatus directly, not sure why we use status.error.sql_state previously

https://docs.google.com/document/d/1xAt_7rPhnZItK75xh8WwEKDSSzd6cdlBxZzFagabAyA/edit?tab=t.0#heading=h.5key87w8p5td

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That design principle is reasonable — deriving SQLSTATE from StatementStatus.sql_state as the canonical source is cleaner than reaching into the transport-error shape, and the comment now documents that intent well. The reason the old code consulted status.error.sql_state is that it's the shared transport-error field, which older/edge DBSQL responses could populate instead of the top-level one. My concern isn't the design direction — it's the narrow behavioral case where a server delivers a 42704 only in the nested field with no SQLSTATE: substring in the message, which would now bypass the not-found recognition in metadata fan-out. The current code still reads only status.sql_state, so that gap is unaddressed. This stays a Low; a one-line PR note confirming the drop was validated against the older-response fixtures (the ones the wiremock move previously exercised) would close it out.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is fine, we should use status.sql_state as the source of truth for SEA path

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The design direction here is sound — treating StatementStatus.sql_state as the canonical source for the SEA path is cleaner than reaching into the transport-error shape, and I don't dispute that. My residual point was narrower and empirical: whether any older/edge DBSQL response delivers a 42704 only in status.error.sql_state with no top-level sql_state and no SQLSTATE: substring, which would slip past not-found recognition in metadata fan-out. The current code still reads only status.sql_state, so that gap isn't closed by the design rationale alone. This stays a non-blocking Low — a one-line note confirming the drop was validated against the older-response fixtures the wiremock move previously exercised would fully settle it, and I'm leaving the thread open for that.

status: &crate::types::sea::StatementStatus,
) -> Option<String> {
status
.sql_state
.as_ref()
.filter(|s| s.len() == 5)
.cloned()
.or_else(|| {
status
.error
.as_ref()
.and_then(|error| error.message.as_deref())
.and_then(extract_sqlstate_from_message)
})
}

/// Extract a SQLSTATE from a free-form error message.
///
/// Many Databricks error envelopes include `SQLSTATE: XXXXX` in the
Expand Down Expand Up @@ -828,6 +851,64 @@ mod tests {
assert_eq!(extract_sqlstate_from_message("nope"), None);
}

#[test]
fn extract_sqlstate_from_status_prefers_top_level_field() {
let status: crate::types::sea::StatementStatus = serde_json::from_str(
r#"{
"state": "FAILED",
"sql_state": "57KD0",
"error": {
"message": "legacy SQLSTATE: 42000"
}
}"#,
)
.unwrap();

assert_eq!(
extract_sqlstate_from_status(&status).as_deref(),
Some("57KD0")
);
}

#[test]
fn extract_sqlstate_from_status_falls_back_to_error_message() {
let status: crate::types::sea::StatementStatus = serde_json::from_str(
r#"{
"state": "FAILED",
"error": {
"message": "table not found. SQLSTATE: 42P01"
}
}"#,
)
.unwrap();

assert_eq!(
extract_sqlstate_from_status(&status).as_deref(),
Some("42P01")
);
}

#[test]
fn extract_sqlstate_from_status_ignores_nested_field() {
use crate::types::sea::{ServiceError, StatementState, StatementStatus};

let status = StatementStatus {
state: StatementState::Failed,
error: Some(ServiceError {
error_code: None,
message: Some("table not found".to_string()),
sql_state: Some("42P01".to_string()),
}),
sql_state: None,
num_modified_rows: None,
display_message: None,
diagnostic_info: None,
error_details_json: None,
};

assert_eq!(extract_sqlstate_from_status(&status), None);
}

#[test]
fn display_includes_code_message_and_sqlstate() {
let e = Error::sql_error()
Expand Down
1 change: 1 addition & 0 deletions src/reader/cloudfetch/link_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ mod tests {
status: StatementStatus {
state: StatementState::Succeeded,
error: None,
sql_state: None,
num_modified_rows: None,
display_message: None,
diagnostic_info: None,
Expand Down
1 change: 1 addition & 0 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,7 @@ mod tests {
StatementStatus {
state: StatementState::Succeeded,
error: None::<ServiceError>,
sql_state: None,
num_modified_rows: None,
display_message: None,
diagnostic_info: None,
Expand Down
13 changes: 4 additions & 9 deletions src/statement/executed_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,11 @@ impl StatementStatus {
let mut error = Error::sql_error()
.message(&error_msg)
.with_query_id(&response.statement_id);
if let Some(s) = crate::kernel_error::extract_sqlstate_from_status(&response.status)
{
error = error.with_sql_state(s);
}
if let Some(err) = service_error {
let sqlstate = err
.sql_state
.as_ref()
.filter(|s| s.len() == 5)
.cloned()
.or_else(|| crate::kernel_error::extract_sqlstate_from_message(&error_msg));
if let Some(s) = sqlstate {
error = error.with_sql_state(s);
}
if let Some(ec) = err.error_code.as_deref().filter(|s| !s.is_empty()) {
error = error.with_error_code(ec);
}
Expand Down
27 changes: 26 additions & 1 deletion src/types/sea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ pub struct StatementStatus {
pub state: StatementState,
#[serde(default)]
pub error: Option<ServiceError>,
/// SQLSTATE reported for the statement. This is the canonical SEA
/// wire location used by Reyden and newer DBSQL responses.
#[serde(default)]
pub sql_state: Option<String>,
/// Number of rows modified by the statement (UPDATE / INSERT /
/// DELETE / MERGE). Absent on SELECT and on warehouses that don't
/// surface the counter.
Expand Down Expand Up @@ -123,6 +127,7 @@ impl std::fmt::Debug for StatementStatus {
f.debug_struct("StatementStatus")
.field("state", &self.state)
.field("error", &self.error)
.field("sql_state", &self.sql_state)
.field("num_modified_rows", &self.num_modified_rows)
.field("display_message", &summarise(&self.display_message))
.field("diagnostic_info", &summarise(&self.diagnostic_info))
Expand Down Expand Up @@ -150,7 +155,9 @@ pub struct ServiceError {
pub error_code: Option<String>,
#[serde(default)]
pub message: Option<String>,
/// SQL state code if provided by server (typically in the message, but may be a separate field)
/// SQLSTATE accepted by the shared transport-error decoder.
/// Statement failures use [`StatementStatus::sql_state`] or parse
/// their error message instead.
#[serde(default)]
pub sql_state: Option<String>,
}
Expand Down Expand Up @@ -595,6 +602,24 @@ mod tests {
assert_eq!(err.error_code.as_deref(), Some("BAD_REQUEST"));
}

#[test]
fn test_failed_response_deserializes_status_sql_state() {
let json = r#"{
"statement_id": "stmt-failed",
"status": {
"state": "FAILED",
"sql_state": "57KD0",
"error": {
"error_code": "BAD_REQUEST",
"message": "Statement has timed out after 2 seconds."
}
}
}"#;

let response: StatementExecutionResponse = serde_json::from_str(json).unwrap();
assert_eq!(response.status.sql_state.as_deref(), Some("57KD0"));
}

#[test]
fn test_chunk_info_byte_count_optional() {
// Per the bounded SEA API contract, `byte_count` on
Expand Down
2 changes: 2 additions & 0 deletions tests/v0_async_execute_wiremock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ fn failed_body(statement_id: &str, message: &str, sql_state: &str) -> String {
"statement_id": statement_id,
"status": {
"state": "FAILED",
"sql_state": sql_state,
"error": {
"error_code": "INVALID_PARAMETER_VALUE",
"message": format!("{message}. SQLSTATE: {sql_state}"),
Expand All @@ -187,6 +188,7 @@ fn failed_body_with_extended_fields(
"statement_id": statement_id,
"status": {
"state": "FAILED",
"sql_state": sql_state,
"error": {
"error_code": "INVALID_PARAMETER_VALUE",
"message": format!("{message}. SQLSTATE: {sql_state}"),
Expand Down
2 changes: 2 additions & 0 deletions tests/v0_execute_wiremock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ fn failed_body(statement_id: &str, message: &str, sql_state: &str) -> String {
"statement_id": statement_id,
"status": {
"state": "FAILED",
"sql_state": sql_state,
"error": {
"error_code": "INVALID_PARAMETER_VALUE",
"message": format!("{message}. SQLSTATE: {sql_state}"),
Expand All @@ -255,6 +256,7 @@ fn failed_body_with_extended_fields(
"statement_id": statement_id,
"status": {
"state": "FAILED",
"sql_state": sql_state,
"error": {
"error_code": "INVALID_PARAMETER_VALUE",
"message": format!("{message}. SQLSTATE: {sql_state}"),
Expand Down
2 changes: 1 addition & 1 deletion tests/v0_metadata_wiremock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ fn failed_response_body(sql_state: &str, message: &str) -> String {
"statement_id": "stmt-test",
"status": {
"state": "FAILED",
"sql_state": sql_state,
"error": {
"error_code": "BAD_REQUEST",
"message": message,
"sql_state": sql_state,
}
},
})
Expand Down
Loading