Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sink): support shared tree for clickhouse #17919

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
87 changes: 82 additions & 5 deletions src/connector/src/sink/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,16 @@ enum ClickHouseEngine {
ReplicatedReplacingMergeTree(Option<String>),
ReplicatedSummingMergeTree,
ReplicatedAggregatingMergeTree,
#[expect(dead_code)]
ReplicatedCollapsingMergeTree(String),
#[expect(dead_code)]
ReplicatedVersionedCollapsingMergeTree(String),
ReplicatedGraphiteMergeTree,
SharedMergeTree,
SharedReplacingMergeTree(Option<String>),
SharedSummingMergeTree,
SharedAggregatingMergeTree,
SharedCollapsingMergeTree(String),
SharedVersionedCollapsingMergeTree(String),
SharedGraphiteMergeTree,
}
impl ClickHouseEngine {
pub fn is_collapsing_engine(&self) -> bool {
Expand All @@ -99,13 +104,16 @@ impl ClickHouseEngine {
| ClickHouseEngine::VersionedCollapsingMergeTree(_)
| ClickHouseEngine::ReplicatedCollapsingMergeTree(_)
| ClickHouseEngine::ReplicatedVersionedCollapsingMergeTree(_)
| ClickHouseEngine::SharedCollapsingMergeTree(_)
| ClickHouseEngine::SharedVersionedCollapsingMergeTree(_)
)
}

pub fn is_delete_replacing_engine(&self) -> bool {
match self {
ClickHouseEngine::ReplacingMergeTree(delete_col) => delete_col.is_some(),
ClickHouseEngine::ReplicatedReplacingMergeTree(delete_col) => delete_col.is_some(),
ClickHouseEngine::SharedReplacingMergeTree(delete_col) => delete_col.is_some(),
_ => false,
}
}
Expand All @@ -116,6 +124,9 @@ impl ClickHouseEngine {
ClickHouseEngine::ReplicatedReplacingMergeTree(Some(delete_col)) => {
Some(delete_col.to_string())
}
ClickHouseEngine::SharedReplacingMergeTree(Some(delete_col)) => {
Some(delete_col.to_string())
}
_ => None,
}
}
Expand All @@ -132,10 +143,27 @@ impl ClickHouseEngine {
ClickHouseEngine::ReplicatedVersionedCollapsingMergeTree(sign_name) => {
Some(sign_name.to_string())
}
ClickHouseEngine::SharedCollapsingMergeTree(sign_name) => Some(sign_name.to_string()),
ClickHouseEngine::SharedVersionedCollapsingMergeTree(sign_name) => {
Some(sign_name.to_string())
}
_ => None,
}
}

pub fn is_shared_tree(&self) -> bool {
matches!(
self,
ClickHouseEngine::SharedMergeTree
| ClickHouseEngine::SharedReplacingMergeTree(_)
| ClickHouseEngine::SharedSummingMergeTree
| ClickHouseEngine::SharedAggregatingMergeTree
| ClickHouseEngine::SharedCollapsingMergeTree(_)
| ClickHouseEngine::SharedVersionedCollapsingMergeTree(_)
| ClickHouseEngine::SharedGraphiteMergeTree
)
}

pub fn from_query_engine(
engine_name: &ClickhouseQueryEngine,
config: &ClickHouseConfig,
Expand Down Expand Up @@ -201,7 +229,9 @@ impl ClickHouseEngine {
.ok_or_else(|| SinkError::ClickHouse("must have index 1".to_string()))?
.trim()
.to_string();
Ok(ClickHouseEngine::VersionedCollapsingMergeTree(sign_name))
Ok(ClickHouseEngine::ReplicatedVersionedCollapsingMergeTree(
sign_name,
))
}
// ReplicatedCollapsingMergeTree("a","b",sign_name)
"ReplicatedCollapsingMergeTree" => {
Expand All @@ -218,9 +248,51 @@ impl ClickHouseEngine {
.ok_or_else(|| SinkError::ClickHouse("must have last".to_string()))?
.trim()
.to_string();
Ok(ClickHouseEngine::CollapsingMergeTree(sign_name))
Ok(ClickHouseEngine::ReplicatedCollapsingMergeTree(sign_name))
}
"ReplicatedGraphiteMergeTree" => Ok(ClickHouseEngine::ReplicatedGraphiteMergeTree),
"SharedMergeTree" => Ok(ClickHouseEngine::SharedMergeTree),
"SharedReplacingMergeTree" => {
let delete_column = config.common.delete_column.clone();
Ok(ClickHouseEngine::SharedReplacingMergeTree(delete_column))
}
"SharedSummingMergeTree" => Ok(ClickHouseEngine::SharedSummingMergeTree),
"SharedAggregatingMergeTree" => Ok(ClickHouseEngine::SharedAggregatingMergeTree),
// SharedVersionedCollapsingMergeTree("a","b",sign_name,"c")
"SharedVersionedCollapsingMergeTree" => {
let sign_name = engine_name
.create_table_query
.split("SharedVersionedCollapsingMergeTree(")
.last()
.ok_or_else(|| SinkError::ClickHouse("must have last".to_string()))?
.split(',')
.rev()
.nth(1)
.ok_or_else(|| SinkError::ClickHouse("must have index 1".to_string()))?
.trim()
.to_string();
Ok(ClickHouseEngine::SharedVersionedCollapsingMergeTree(
sign_name,
))
}
// SharedCollapsingMergeTree("a","b",sign_name)
"SharedCollapsingMergeTree" => {
let sign_name = engine_name
.create_table_query
.split("SharedCollapsingMergeTree(")
.last()
.ok_or_else(|| SinkError::ClickHouse("must have last".to_string()))?
.split(')')
.next()
.ok_or_else(|| SinkError::ClickHouse("must have next".to_string()))?
.split(',')
.last()
.ok_or_else(|| SinkError::ClickHouse("must have last".to_string()))?
.trim()
.to_string();
Ok(ClickHouseEngine::SharedCollapsingMergeTree(sign_name))
}
"SharedGraphiteMergeTree" => Ok(ClickHouseEngine::SharedGraphiteMergeTree),
_ => Err(SinkError::ClickHouse(format!(
"Cannot find clickhouse engine {:?}",
engine_name.engine
Expand Down Expand Up @@ -452,13 +524,18 @@ impl Sink for ClickHouseSink {

let (clickhouse_column, clickhouse_engine) =
query_column_engine_from_ck(client, &self.config).await?;
if clickhouse_engine.is_shared_tree() {
risingwave_common::license::Feature::ClickHouseSharedEngine
.check_available()
.map_err(|e| anyhow::anyhow!(e))?;
}

if !self.is_append_only
&& !clickhouse_engine.is_collapsing_engine()
&& !clickhouse_engine.is_delete_replacing_engine()
{
return match clickhouse_engine {
ClickHouseEngine::ReplicatedReplacingMergeTree(None) | ClickHouseEngine::ReplacingMergeTree(None) => {
ClickHouseEngine::ReplicatedReplacingMergeTree(None) | ClickHouseEngine::ReplacingMergeTree(None) | ClickHouseEngine::SharedReplacingMergeTree(None) => {
Err(SinkError::ClickHouse("To enable upsert with a `ReplacingMergeTree`, you must set a `clickhouse.delete.column` to the UInt8 column in ClickHouse used to signify deletes. See https://clickhouse.com/docs/en/engines/table-engines/mergetree-family/replacingmergetree#is_deleted for more information".to_owned()))
}
_ => Err(SinkError::ClickHouse("If you want to use upsert, please use either `VersionedCollapsingMergeTree`, `CollapsingMergeTree` or the `ReplacingMergeTree` in ClickHouse".to_owned()))
Expand Down
1 change: 1 addition & 0 deletions src/license/src/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ macro_rules! for_all_features {
{ TestPaid, Paid, "A dummy feature that's only available on paid tier for testing purposes." },
{ TimeTravel, Paid, "Query historical data within the retention period."},
{ GlueSchemaRegistry, Paid, "Use Schema Registry from AWS Glue rather than Confluent." },
{ ClickHouseSharedEngine,Paid, "Delivering data to Shared tree on clickhouse cloud"},
{ SecretManagement, Paid, "Secret management." },
{ CdcTableSchemaMap, Paid, "Automatically map upstream schema to CDC Table."},
{ SqlServerSink, Paid, "Sink data from RisingWave to SQL Server." },
Expand Down