use serde_json::json;
use crate::databricks_api::_helpers::requests::json_with_retries;
use crate::databricks_api::_helpers::statements::{chunk_rows, column_names, run_statement};
use crate::databricks_api::constants::WAIT_TIMEOUT;
use crate::databricks_api::models::DatabricksClient;
use crate::http::errors::ApiError;
use crate::sql_api::models::SqlRows;
pub async fn execute_statement(
client: &DatabricksClient,
statement: &str,
) -> Result<SqlRows, ApiError> {
let body = json!({
"warehouse_id": client.warehouse_id,
"statement": statement,
"wait_timeout": WAIT_TIMEOUT,
"on_wait_timeout": "CONTINUE",
"disposition": "INLINE",
"format": "JSON_ARRAY",
});
let response = run_statement(client, &body).await?;
let mut rows = SqlRows {
columns: column_names(&response),
rows: Vec::new(),
};
let mut chunk = response["result"].clone();
loop {
rows.rows.extend(chunk_rows(&chunk));
let Some(next) = chunk["next_chunk_internal_link"].as_str() else {
return Ok(rows);
};
chunk = json_with_retries(client, &format!("{}{next}", client.host)).await?;
}
}