use crate::adapters::clickhouse::_helpers::session::http::send;
use crate::adapters::clickhouse::constants::{EXCHANGE_UNSUPPORTED_CODES, PUBLISH_PREFIX};
use crate::adapters::clickhouse::models::{ClickHouseDestination, PublishStep};
use crate::http::errors::ApiError;
pub(crate) fn publish_step(statement: &str) -> Result<Option<PublishStep>, ApiError> {
let Some(json) = statement.strip_prefix(PUBLISH_PREFIX) else {
return Ok(None);
};
serde_json::from_str(json)
.map(Some)
.map_err(|error| ApiError::Protocol(format!("a malformed publish step: {error}")))
}
pub(crate) async fn run_step(
destination: &ClickHouseDestination,
statement: &str,
) -> Result<(), ApiError> {
match publish_step(statement)? {
None => send(destination, None, statement.as_bytes().to_vec())
.await
.map(|_| ()),
Some(PublishStep::Exchange {
target,
build,
aside,
}) => exchange(destination, (&target, &build, &aside)).await,
Some(PublishStep::Partitions { .. }) => Err(ApiError::Protocol(
"a partition publish cannot run inside another".to_owned(),
)),
}
}
pub(crate) async fn exchange(
destination: &ClickHouseDestination,
(target, build, aside): (&str, &str, &str),
) -> Result<(), ApiError> {
let exchanged = send(
destination,
None,
format!("EXCHANGE TABLES {target} AND {build}").into_bytes(),
)
.await;
match exchanged {
Ok(_) => Ok(()),
Err(error)
if error
.code()
.is_some_and(|code| EXCHANGE_UNSUPPORTED_CODES.contains(&code)) =>
{
tracing::debug!(%error, "EXCHANGE TABLES is unsupported here; swapping by renames");
send(
destination,
None,
format!(
"RENAME TABLE {target} TO {aside}, {build} TO {target}, {aside} TO {build}"
)
.into_bytes(),
)
.await
.map(|_| ())
}
Err(error) => Err(error),
}
}