use crate::adapters::clickhouse::_helpers::dialect::publish::{
partition_rows_query, replace_partition,
};
use crate::adapters::clickhouse::_helpers::session::http::send;
use crate::adapters::clickhouse::constants::{
EXCHANGE_UNSUPPORTED_CODES, LIGHTWEIGHT_DELETE_MODE, LIGHTWEIGHT_DELETE_REFUSED_CODE,
PUBLISH_PREFIX,
};
use crate::adapters::clickhouse::models::{ClickHouseDestination, ClickHouseSql, PublishStep};
use crate::adapters::contract::types::SqlAdapter;
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::ReplacePartition {
target,
copy,
partition,
}) => replace_or_drop(destination, (&target, ©, &partition)).await,
Some(PublishStep::DeleteRows { table, condition }) => {
delete_rows(destination, (&table, &condition)).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),
}
}
async fn replace_or_drop(
destination: &ClickHouseDestination,
(target, copy, partition): (&str, &str, &str),
) -> Result<(), ApiError> {
let counted = send(
destination,
None,
partition_rows_query(copy, partition).into_bytes(),
)
.await?;
let statement = match counted.trim() {
"0" => format!(
"ALTER TABLE {target} DROP PARTITION ID {}",
ClickHouseSql.render_loader_value_literal(partition)
),
_ => replace_partition(target, copy, partition),
};
send(destination, None, statement.into_bytes())
.await
.map(|_| ())
}
async fn delete_rows(
destination: &ClickHouseDestination,
(table, condition): (&str, &str),
) -> Result<(), ApiError> {
let deleted = send(
destination,
None,
format!(
"DELETE FROM {table} WHERE {condition} SETTINGS {} = '{}', lightweight_deletes_sync = 2",
LIGHTWEIGHT_DELETE_MODE.0, LIGHTWEIGHT_DELETE_MODE.1
)
.into_bytes(),
)
.await;
match deleted {
Ok(_) => Ok(()),
Err(error) if error.code() == Some(LIGHTWEIGHT_DELETE_REFUSED_CODE) => {
tracing::debug!(%error, "a lightweight DELETE is refused here; rewriting the parts");
send(
destination,
None,
format!("ALTER TABLE {table} DELETE WHERE {condition}").into_bytes(),
)
.await
.map(|_| ())
}
Err(error) => Err(error),
}
}