Skip to content
Open
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
3 changes: 3 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ changes go in their respective binding's NEXT_CHANGELOG.
with an optional partner entry from `adbc.databricks.user_agent_entry`
(`adbc.spark.user_agent_entry` is a compatibility alias). `User-Agent` in
`adbc.databricks.http.headers` is now rejected; use the dedicated entry option.
- When an execute response already holds the whole result inline, `execute()`
closes the server statement right away instead of when the caller finishes
reading it, and `cancel()` on a closed statement no longer sends a request.

### Updated

Expand Down
203 changes: 196 additions & 7 deletions src/client/sea.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ pub(crate) struct SeaRestTransport {
session_version: AtomicI64,
reader_factory: OnceLock<ResultReaderFactory>,
runtime_handle: OnceLock<RuntimeHandle>,
/// `{base_url}/statements`, parsed once the session route is known, so
/// per-statement requests don't format and re-parse the URL.
statements_url: OnceLock<reqwest::Url>,
}

impl std::fmt::Debug for SeaRestTransport {
Expand Down Expand Up @@ -224,9 +227,35 @@ impl SeaRestTransport {
session_version: AtomicI64::new(NO_SESSION_VERSION),
reader_factory: OnceLock::new(),
runtime_handle: OnceLock::new(),
statements_url: OnceLock::new(),
}
}

/// `{base_url}/statements` followed by `segments` (e.g. a statement id).
fn statements_url(&self, segments: &[&str]) -> Result<reqwest::Url> {
let parse = || {
reqwest::Url::parse(&format!("{}/statements", self.base_url())).map_err(|e| {
Error::invalid_argument().message(format!("Failed to build request URL: {e}"))
})
};
// The base URL is fixed once the session route is set; before that
// (session creation) it is parsed per call.
let mut url = match (self.session_route.get(), self.statements_url.get()) {
(Some(_), Some(url)) => url.clone(),
(Some(_), None) => {
let url = parse()?;
self.statements_url.get_or_init(|| url).clone()
}
(None, _) => parse()?,
};
if !segments.is_empty() {
url.path_segments_mut()
.map_err(|()| Error::invalid_argument().message("statement URL cannot be a base"))?
.extend(segments);
}
Ok(url)
}

/// Set the reader factory and runtime handle. Must be called once after wrapping in `Arc`.
pub fn set_reader_factory(
&self,
Expand Down Expand Up @@ -656,7 +685,7 @@ impl SeaRestTransport {
request_type: RequestType,
metadata_operation: Option<MetadataOperation>,
) -> Result<ExecuteResponse> {
let url = format!("{}/statements", self.base_url());
let url = self.statements_url(&[])?;

let (warehouse_id, session_id) = match self.session_route.get() {
Some(SessionRoute::Standard { .. }) | None => (
Expand Down Expand Up @@ -687,7 +716,7 @@ impl SeaRestTransport {
let mut builder = self
.http_client
.inner()
.request(Method::POST, &url)
.request(Method::POST, url)
.header("Content-Type", "application/json");

// Signal that a metadata query may be served fully synchronously, so
Expand Down Expand Up @@ -874,16 +903,38 @@ impl SeaRestTransport {
// GetStatementStatus round-trip.
let telemetry_result_format = Self::telemetry_result_format(&response);
let telemetry_is_compressed = Self::telemetry_is_compressed(&response);
let result_complete = Self::result_is_complete_inline(&response);
Ok(ExecuteResult {
statement_id: response.statement_id,
reader,
manifest: response.manifest,
telemetry_result_format,
telemetry_is_compressed,
status: response.status,
result_complete,
})
}

/// Whether a `Succeeded` response carries its whole result inline: an
/// attachment, no next chunk or links, and at most one chunk in total.
fn result_is_complete_inline(response: &ExecuteResponse) -> bool {
if response.status.state != StatementState::Succeeded {
return false;
}
let Some(result) = response.result.as_ref() else {
return false;
};
result.inline_arrow_data.is_some()
&& result.inline_next_chunk_index.is_none()
&& result.next_chunk_internal_link.is_none()
&& result.external_links.as_ref().is_none_or(Vec::is_empty)
&& response
.manifest
.as_ref()
.and_then(|m| m.total_chunk_count)
.is_none_or(|n| n <= 1)
}

/// Execute a SQL statement end-to-end: raw execution → result materialisation.
///
/// Shared implementation for both user queries (`ExecuteStatement`) and
Expand Down Expand Up @@ -1067,7 +1118,13 @@ const DISCARD_BODY_TIMEOUT: std::time::Duration = std::time::Duration::from_mill
/// ignored, and a stalled body is dropped after [`DISCARD_BODY_TIMEOUT`]
/// (losing only the connection) rather than delaying the caller.
async fn discard_body(response: reqwest::Response) {
let _ = tokio::time::timeout(DISCARD_BODY_TIMEOUT, response.bytes()).await;
let mut body = std::pin::pin!(response.bytes());
// The reply is a few bytes that usually arrive with the headers: take it
// without arming a timer.
if futures::poll!(body.as_mut()).is_ready() {
return;
}
let _ = tokio::time::timeout(DISCARD_BODY_TIMEOUT, body).await;
}

/// Convert a parsed `GetChunksResponse` into the kernel-facing
Expand Down Expand Up @@ -1369,6 +1426,7 @@ impl Transport for SeaRestTransport {
telemetry_result_format,
telemetry_is_compressed,
status: r.status,
result_complete: false,
}))
}
// Still running after the inline wait → hand back a running handle;
Expand Down Expand Up @@ -1406,6 +1464,7 @@ impl Transport for SeaRestTransport {
telemetry_result_format,
telemetry_is_compressed,
status: r.status,
result_complete: false,
}))
}
StatementOutcome::NotReady(_) => Ok(None),
Expand Down Expand Up @@ -1590,14 +1649,14 @@ impl Transport for SeaRestTransport {
}

async fn cancel_statement(&self, statement_id: &str) -> Result<()> {
let url = format!("{}/statements/{}/cancel", self.base_url(), statement_id);
let url = self.statements_url(&[statement_id, "cancel"])?;

debug!("Canceling statement at {}", url);

let request = self
.http_client
.inner()
.request(Method::POST, &url)
.request(Method::POST, url)
.build()
.map_err(|e| {
Error::invalid_argument().message(format!("Failed to build request: {e}"))
Expand All @@ -1615,14 +1674,14 @@ impl Transport for SeaRestTransport {
}

async fn close_statement(&self, statement_id: &str) -> Result<()> {
let url = format!("{}/statements/{}", self.base_url(), statement_id);
let url = self.statements_url(&[statement_id])?;

debug!("Closing statement at {}", url);

let request = self
.http_client
.inner()
.request(Method::DELETE, &url)
.request(Method::DELETE, url)
.build()
.map_err(|e| {
Error::invalid_argument().message(format!("Failed to build request: {e}"))
Expand Down Expand Up @@ -2366,6 +2425,136 @@ mod tests {
assert_eq!(client.base_url(), "https://test.databricks.com/api/2.0/sql");
}

#[test]
fn statements_url_matches_the_formatted_url_before_and_after_the_route_is_set() {
let retry_configs = build_retry_configs(&RetryConfig::default(), &HashMap::new());
let http_client = Arc::new(
DatabricksHttpClient::new(HttpClientConfig::default(), retry_configs).unwrap(),
);
let client = SeaRestTransport::new(
http_client,
"https://test.databricks.com/",
"warehouse-123",
TransportConfig::default(),
);
let base = "https://test.databricks.com/api/2.0/sql/statements";
assert_eq!(client.statements_url(&[]).unwrap().as_str(), base);
assert_eq!(
client.statements_url(&["s-1"]).unwrap().as_str(),
format!("{base}/s-1")
);

let session_url = "https://test.databricks.com/api/2.0/sql/warehouses/w/sessions/abc";
client
.session_route
.set(SessionRoute::Realtime {
session_url: session_url.to_string(),
})
.unwrap();
for _ in 0..2 {
// The second round reads the cached URL.
assert_eq!(
client.statements_url(&[]).unwrap().as_str(),
format!("{session_url}/statements")
);
assert_eq!(
client.statements_url(&["s-2", "cancel"]).unwrap().as_str(),
format!("{session_url}/statements/s-2/cancel")
);
}
}

#[test]
fn result_is_complete_inline_only_for_a_single_inline_chunk() {
use crate::types::sea::{ResultManifest, ResultSchema, StatementState, StatementStatus};
let response =
|state: StatementState, chunks: Option<i64>, data: ExecuteResultData| ExecuteResponse {
statement_id: "s".to_string(),
status: StatementStatus {
state,
error: None,
sql_state: None,
session_version: None,
num_modified_rows: None,
display_message: None,
diagnostic_info: None,
error_details_json: None,
},
manifest: Some(ResultManifest {
format: "ARROW_STREAM".to_string(),
schema: ResultSchema {
column_count: 0,
columns: vec![],
},
arrow_schema: None,
total_chunk_count: chunks,
total_row_count: None,
total_byte_count: None,
truncated: false,
chunks: None,
result_compression: None,
is_native_metadata_result: None,
}),
result: Some(data),
};
let inline = || ExecuteResultData {
next_chunk_internal_link: None,
external_links: None,
inline_arrow_data: Some(vec![1, 2, 3]),
inline_chunk_index: Some(0),
inline_row_offset: Some(0),
inline_row_count: Some(1),
inline_next_chunk_index: None,
};
let complete = |r: &ExecuteResponse| SeaRestTransport::result_is_complete_inline(r);

assert!(complete(&response(
StatementState::Succeeded,
Some(1),
inline()
)));
assert!(complete(&response(
StatementState::Succeeded,
None,
inline()
)));
// Already closed by the server, or more chunks / links to fetch.
assert!(!complete(&response(
StatementState::Closed,
Some(1),
inline()
)));
assert!(!complete(&response(
StatementState::Succeeded,
Some(2),
inline()
)));
let mut paged = inline();
paged.inline_next_chunk_index = Some(1);
assert!(!complete(&response(
StatementState::Succeeded,
Some(1),
paged
)));
let mut linked = inline();
linked.next_chunk_internal_link = Some("/next".to_string());
assert!(!complete(&response(
StatementState::Succeeded,
Some(1),
linked
)));
let mut no_data = inline();
no_data.inline_arrow_data = None;
assert!(!complete(&response(
StatementState::Succeeded,
Some(1),
no_data
)));
let mut no_result = response(StatementState::Succeeded, Some(1), inline());
no_result.result = None;
assert!(!complete(&no_result));
}

#[test]
fn test_convert_result_data_without_external_links() {
let result = crate::types::sea::ResultData {
Expand Down
5 changes: 5 additions & 0 deletions src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub(crate) struct ExecuteResult {
/// `display_message` / etc. without a second GetStatementStatus
/// round-trip.
pub status: StatementStatus,
/// The `Succeeded` response carried the whole result inline (one chunk,
/// nothing left to fetch), so the server statement can be closed before
/// the result is read.
pub result_complete: bool,
}

impl std::fmt::Debug for ExecuteResult {
Expand All @@ -140,6 +144,7 @@ impl std::fmt::Debug for ExecuteResult {
.field("telemetry_result_format", &self.telemetry_result_format)
.field("telemetry_is_compressed", &self.telemetry_is_compressed)
.field("status", &self.status)
.field("result_complete", &self.result_complete)
.finish()
}
}
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 @@ -1177,6 +1177,7 @@ mod tests {
diagnostic_info: None,
error_details_json: None,
},
result_complete: false,
})
}

Expand Down
22 changes: 22 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,28 @@ impl SessionInner {
&self.runtime_handle
}

/// `CloseStatement` for `statement_id` as a task on the session runtime.
/// A failure is logged and returned through the handle; dropping the
/// handle leaves the close running.
pub(crate) fn spawn_close_statement(
&self,
statement_id: String,
) -> tokio::task::JoinHandle<Result<()>> {
let transport = Arc::clone(&self.transport);
self.runtime_handle.spawn(async move {
let result = transport.close_statement(&statement_id).await;
if let Err(e) = &result {
Comment thread
oss-reviewer-bot[bot] marked this conversation as resolved.
Resolved
crate::klog::warn!(
statement_id = %statement_id,
error = %e,
"background close_statement failed; an explicit close() sends it \
again, otherwise the server reclaims the statement when the session closes"
);
}
result
})
}

pub(crate) fn dispatch_timeout_cleanup(&self, statement_id: Option<String>) {
self.timeout_cleanup.dispatch(
&self.runtime_handle,
Expand Down
Loading
Loading