Skip to content

Commit

Permalink
fix: partition number disappear after altering table options
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Nov 6, 2024
1 parent b33894c commit 996167b
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 26 deletions.
24 changes: 8 additions & 16 deletions src/table/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,15 +201,7 @@ impl TableMeta {
self.change_column_types(table_name, columns)
}
// No need to rebuild table meta when renaming tables.
AlterKind::RenameTable { .. } => {
let mut meta_builder = TableMetaBuilder::default();
let _ = meta_builder
.schema(self.schema.clone())
.primary_key_indices(self.primary_key_indices.clone())
.engine(self.engine.clone())
.next_column_id(self.next_column_id);
Ok(meta_builder)
}
AlterKind::RenameTable { .. } => Ok(self.new_meta_builder()),
AlterKind::ChangeTableOptions { options } => self.change_table_options(options),
}
}
Expand All @@ -229,13 +221,9 @@ impl TableMeta {
}
}
}
let mut builder = TableMetaBuilder::default();
builder
.options(new_options)
.schema(self.schema.clone())
.primary_key_indices(self.primary_key_indices.clone())
.engine(self.engine.clone())
.next_column_id(self.next_column_id);
let mut builder = self.new_meta_builder();
builder.options(new_options);

Ok(builder)
}

Expand Down Expand Up @@ -266,9 +254,13 @@ impl TableMeta {
Ok(desc)
}

/// Create a `[TableMetaBuilder]`
/// Note: please always use this function to create the builder.
fn new_meta_builder(&self) -> TableMetaBuilder {
let mut builder = TableMetaBuilder::default();
let _ = builder
.schema(self.schema.clone())
.primary_key_indices(self.primary_key_indices.clone())
.engine(&self.engine)
.options(self.options.clone())
.created_on(self.created_on)
Expand Down
69 changes: 62 additions & 7 deletions tests/cases/standalone/common/alter/alter_table_options.result
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
CREATE TABLE ato(i INTEGER, j TIMESTAMP TIME INDEX);
CREATE TABLE ato(i INTEGER, j TIMESTAMP TIME INDEX, PRIMARY KEY(i));

Affected Rows: 0

INSERT INTO ato VALUES(1, now()), (2, now());

Affected Rows: 2

SELECT i FROM ato;

+---+
| i |
+---+
| 1 |
| 2 |
+---+

ALTER TABLE ato SET 'ttl'='1d';

Affected Rows: 0

SELECT i FROM ato;

+---+
| i |
+---+
| 1 |
| 2 |
+---+

SHOW CREATE TABLE ato;

+-------+------------------------------------+
Expand All @@ -14,7 +36,8 @@ SHOW CREATE TABLE ato;
| ato | CREATE TABLE IF NOT EXISTS "ato" ( |
| | "i" INT NULL, |
| | "j" TIMESTAMP(3) NOT NULL, |
| | TIME INDEX ("j") |
| | TIME INDEX ("j"), |
| | PRIMARY KEY ("i") |
| | ) |
| | |
| | ENGINE=mito |
Expand All @@ -27,6 +50,15 @@ ALTER TABLE ato SET 'ttl'='2d';

Affected Rows: 0

SELECT i FROM ato;

+---+
| i |
+---+
| 1 |
| 2 |
+---+

SHOW CREATE TABLE ato;

+-------+------------------------------------+
Expand All @@ -35,7 +67,8 @@ SHOW CREATE TABLE ato;
| ato | CREATE TABLE IF NOT EXISTS "ato" ( |
| | "i" INT NULL, |
| | "j" TIMESTAMP(3) NOT NULL, |
| | TIME INDEX ("j") |
| | TIME INDEX ("j"), |
| | PRIMARY KEY ("i") |
| | ) |
| | |
| | ENGINE=mito |
Expand All @@ -48,6 +81,15 @@ ALTER TABLE ato SET 'ttl'=NULL;

Affected Rows: 0

SELECT i FROM ato;

+---+
| i |
+---+
| 1 |
| 2 |
+---+

SHOW CREATE TABLE ato;

+-------+------------------------------------+
Expand All @@ -56,14 +98,15 @@ SHOW CREATE TABLE ato;
| ato | CREATE TABLE IF NOT EXISTS "ato" ( |
| | "i" INT NULL, |
| | "j" TIMESTAMP(3) NOT NULL, |
| | TIME INDEX ("j") |
| | TIME INDEX ("j"), |
| | PRIMARY KEY ("i") |
| | ) |
| | |
| | ENGINE=mito |
| | |
+-------+------------------------------------+

ALTER TABLE ato SET 'ttl'='0d';
ALTER TABLE ato SET 'ttl'='1s';

Affected Rows: 0

Expand All @@ -75,13 +118,25 @@ SHOW CREATE TABLE ato;
| ato | CREATE TABLE IF NOT EXISTS "ato" ( |
| | "i" INT NULL, |
| | "j" TIMESTAMP(3) NOT NULL, |
| | TIME INDEX ("j") |
| | TIME INDEX ("j"), |
| | PRIMARY KEY ("i") |
| | ) |
| | |
| | ENGINE=mito |
| | |
| | WITH( |
| | ttl = '1s' |
| | ) |
+-------+------------------------------------+

SELECT i FROM ato;

+---+
| i |
+---+
| 1 |
| 2 |
+---+

DROP TABLE ato;

Affected Rows: 0
Expand Down
18 changes: 15 additions & 3 deletions tests/cases/standalone/common/alter/alter_table_options.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
CREATE TABLE ato(i INTEGER, j TIMESTAMP TIME INDEX);
CREATE TABLE ato(i INTEGER, j TIMESTAMP TIME INDEX, PRIMARY KEY(i));

INSERT INTO ato VALUES(1, now()), (2, now());

SELECT i FROM ato;

ALTER TABLE ato SET 'ttl'='1d';

SELECT i FROM ato;

SHOW CREATE TABLE ato;

ALTER TABLE ato SET 'ttl'='2d';

SELECT i FROM ato;

SHOW CREATE TABLE ato;

ALTER TABLE ato SET 'ttl'=NULL;

SELECT i FROM ato;

SHOW CREATE TABLE ato;

ALTER TABLE ato SET 'ttl'='0d';
ALTER TABLE ato SET 'ttl'='1s';

SHOW CREATE TABLE ato;

DROP TABLE ato;
SELECT i FROM ato;

DROP TABLE ato;

0 comments on commit 996167b

Please sign in to comment.