Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zhyass committed Dec 27, 2024
1 parent 698c4f9 commit 2673c1e
Show file tree
Hide file tree
Showing 24 changed files with 40 additions and 57 deletions.
19 changes: 8 additions & 11 deletions src/meta/app/src/schema/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,8 @@ pub struct TableMeta {
pub options: BTreeMap<String, String>,
// The default cluster key.
pub default_cluster_key: Option<String>,
// All cluster keys that have been defined.
pub cluster_keys: Vec<String>,
// The sequence number of default_cluster_key in cluster_keys.
pub default_cluster_key_id: Option<u32>,
// The sequence number of default_cluster_key.
pub default_cluster_key_id: u32,
pub created_on: DateTime<Utc>,
pub updated_on: DateTime<Utc>,
pub comment: String,
Expand Down Expand Up @@ -419,8 +417,7 @@ impl Default for TableMeta {
part_prefix: "".to_string(),
options: BTreeMap::new(),
default_cluster_key: None,
cluster_keys: vec![],
default_cluster_key_id: None,
default_cluster_key_id: 0,
created_on: Utc::now(),
updated_on: Utc::now(),
comment: "".to_string(),
Expand All @@ -435,16 +432,16 @@ impl Default for TableMeta {
}

impl TableMeta {
pub fn push_cluster_key(mut self, cluster_key: String) -> Self {
self.cluster_keys.push(cluster_key.clone());
pub fn set_cluster_key(mut self, cluster_key: String) -> Self {
self.default_cluster_key = Some(cluster_key);
self.default_cluster_key_id = Some(self.cluster_keys.len() as u32 - 1);
self.default_cluster_key_id += 1;
self
}

pub fn cluster_key(&self) -> Option<(u32, String)> {
self.default_cluster_key_id
.zip(self.default_cluster_key.clone())
self.default_cluster_key
.clone()
.map(|k| (self.default_cluster_key_id, k))
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/meta/proto-conv/src/table_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ impl FromToProto for mt::TableMeta {
indexes.insert(name, mt::TableIndex::from_pb(index)?);
}

let default_cluster_key_id = if let Some(cluster_key_id) = p.default_cluster_key_id {
cluster_key_id
} else {
p.cluster_keys.len() as u32 - 1
};

let v = Self {
schema: Arc::new(ex::TableSchema::from_pb(schema)?),
engine: p.engine,
Expand All @@ -203,8 +209,7 @@ impl FromToProto for mt::TableMeta {
part_prefix: p.part_prefix.unwrap_or("".to_string()),
options: p.options,
default_cluster_key: p.default_cluster_key,
cluster_keys: p.cluster_keys,
default_cluster_key_id: p.default_cluster_key_id,
default_cluster_key_id,
created_on: DateTime::<Utc>::from_pb(p.created_on)?,
updated_on: DateTime::<Utc>::from_pb(p.updated_on)?,
drop_on: match p.drop_on {
Expand Down Expand Up @@ -251,8 +256,9 @@ impl FromToProto for mt::TableMeta {
},
options: self.options.clone(),
default_cluster_key: self.default_cluster_key.clone(),
cluster_keys: self.cluster_keys.clone(),
default_cluster_key_id: self.default_cluster_key_id,
// cluster_keys is deprecated.
cluster_keys: vec![],
default_cluster_key_id: Some(self.default_cluster_key_id),
created_on: self.created_on.to_pb()?,
updated_on: self.updated_on.to_pb()?,
drop_on: match self.drop_on {
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/proto_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,7 @@ fn new_table_meta() -> mt::TableMeta {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v002_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ fn test_decode_v2_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v010_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,7 @@ fn test_decode_v10_table_meta() -> anyhow::Result<()> {
part_prefix: "".to_string(),
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v012_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ fn test_decode_v12_table_meta() -> anyhow::Result<()> {
part_prefix: "".to_string(),
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v023_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ fn test_decode_v23_table_meta() -> anyhow::Result<()> {
part_prefix: "lulu_".to_string(),
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v024_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,7 @@ fn test_decode_v24_table_meta() -> anyhow::Result<()> {
part_prefix: "lulu_".to_string(),
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v033_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ fn test_decode_v33_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v040_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ fn test_decode_v40_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v044_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ fn test_decode_v44_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v055_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ fn test_decode_v55_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v074_table_db_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ fn test_decode_v74_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v080_geometry_datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,7 @@ fn test_decode_v80_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v082_table_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ fn test_decode_v82_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v085_table_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ fn test_decode_v85_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v086_table_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ fn test_decode_v86_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v094_table_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ fn test_decode_v94_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v107_geography_datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ fn test_decode_v107_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
3 changes: 1 addition & 2 deletions src/meta/proto-conv/tests/it/v114_interval_datatype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,7 @@ fn test_decode_v114_table_meta() -> anyhow::Result<()> {
engine_options: btreemap! {s("abc") => s("def")},
options: btreemap! {s("xyz") => s("foo")},
default_cluster_key: Some("(a + 2, b)".to_string()),
cluster_keys: vec!["(a + 2, b)".to_string()],
default_cluster_key_id: Some(0),
default_cluster_key_id: 0,
created_on: Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(),
updated_on: Utc.with_ymd_and_hms(2014, 11, 29, 12, 0, 10).unwrap(),
comment: s("table_comment"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl Interpreter for AlterTableClusterKeyInterpreter {
new_table_meta
.options
.insert(OPT_KEY_CLUSTER_TYPE.to_owned(), plan.cluster_type.clone());
new_table_meta = new_table_meta.push_cluster_key(cluster_key_str);
new_table_meta = new_table_meta.set_cluster_key(cluster_key_str);

let req = UpdateTableMetaReq {
table_id: table_info.ident.table_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ impl Interpreter for DropTableClusterKeyInterpreter {
let table_info = fuse_table.get_table_info();
let mut new_table_meta = table_info.meta.clone();
new_table_meta.default_cluster_key = None;
new_table_meta.default_cluster_key_id = None;
new_table_meta.options.remove(OPT_KEY_CLUSTER_TYPE);

let req = UpdateTableMetaReq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ impl CreateTableInterpreter {
}

if let Some(cluster_key) = &self.plan.cluster_key {
table_meta = table_meta.push_cluster_key(cluster_key.clone());
table_meta = table_meta.set_cluster_key(cluster_key.clone());
}

let req = CreateTableReq {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ async fn test_fuse_alter_table_cluster_key() -> databend_common_exception::Resul

let table = fixture.latest_default_table().await?;
let table_info = table.get_table_info();
assert_eq!(table_info.meta.cluster_keys, vec!["(id)".to_string()]);
assert_eq!(table_info.meta.default_cluster_key_id, Some(0));
assert_eq!(table_info.meta.default_cluster_key_id, 1);
assert_eq!(
table_info.meta.options.get(OPT_KEY_CLUSTER_TYPE).unwrap(),
LINEAR_CLUSTER_TYPE
Expand All @@ -95,7 +94,7 @@ async fn test_fuse_alter_table_cluster_key() -> databend_common_exception::Resul
let table = fixture.latest_default_table().await?;
let table_info = table.get_table_info();
assert_eq!(table_info.meta.default_cluster_key, None);
assert_eq!(table_info.meta.default_cluster_key_id, None);
assert_eq!(table_info.meta.default_cluster_key_id, 1);

Ok(())
}

0 comments on commit 2673c1e

Please sign in to comment.