fix: skip partition clause in show create table (#2200)

* fix: skip partition clause in show create table

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

* update test results

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>

---------

Signed-off-by: Ruihang Xia <waynestxia@gmail.com>
This commit is contained in:
Ruihang Xia
2023-08-18 17:10:31 +08:00
committed by GitHub
parent 8d6a2d0b59
commit 1bbec75f5b
4 changed files with 121 additions and 48 deletions

View File

@@ -149,14 +149,18 @@ impl Display for PartitionEntry {
impl Display for Partitions { impl Display for Partitions {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!( if !self.column_list.is_empty() {
f, write!(
r#"PARTITION BY RANGE COLUMNS ({}) ( f,
{} r#"PARTITION BY RANGE COLUMNS ({}) (
)"#, {}
format_list_comma!(self.column_list), )"#,
format_list_indent!(self.entries), format_list_comma!(self.column_list),
) format_list_indent!(self.entries),
)
} else {
write!(f, "")
}
} }
} }
@@ -247,10 +251,10 @@ CREATE TABLE IF NOT EXISTS demo (
PRIMARY KEY (ts, host) PRIMARY KEY (ts, host)
) )
PARTITION BY RANGE COLUMNS (ts) ( PARTITION BY RANGE COLUMNS (ts) (
PARTITION r0 VALUES LESS THAN (5), PARTITION r0 VALUES LESS THAN (5),
PARTITION r1 VALUES LESS THAN (9), PARTITION r1 VALUES LESS THAN (9),
PARTITION r2 VALUES LESS THAN (MAXVALUE) PARTITION r2 VALUES LESS THAN (MAXVALUE)
) )
ENGINE=mito ENGINE=mito
WITH( WITH(
regions = 1, regions = 1,
@@ -266,4 +270,44 @@ WITH(
_ => unreachable!(), _ => unreachable!(),
} }
} }
#[test]
fn test_display_empty_partition_column() {
let sql = r"create table if not exists demo(
host string,
ts bigint,
cpu double default 0,
memory double,
TIME INDEX (ts),
PRIMARY KEY(ts, host)
);
";
let result = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap();
assert_eq!(1, result.len());
match &result[0] {
Statement::CreateTable(c) => {
let new_sql = format!("\n{}", c);
assert_eq!(
r#"
CREATE TABLE IF NOT EXISTS demo (
host STRING,
ts BIGINT,
cpu DOUBLE DEFAULT 0,
memory DOUBLE,
TIME INDEX (ts),
PRIMARY KEY (ts, host)
)
ENGINE=mito
"#,
&new_sql
);
let new_result =
ParserContext::create_with_dialect(&new_sql, &GreptimeDbDialect {}).unwrap();
assert_eq!(result, new_result);
}
_ => unreachable!(),
}
}
} }

View File

@@ -98,22 +98,20 @@ async fn test_show_create_table(instance: Arc<dyn MockInstance>) {
let expected = if instance.is_distributed_mode() { let expected = if instance.is_distributed_mode() {
"\ "\
+-------+--------------------------------------------+ +-------+-----------------------------------+
| Table | Create Table | | Table | Create Table |
+-------+--------------------------------------------+ +-------+-----------------------------------+
| demo | CREATE TABLE IF NOT EXISTS demo ( | | demo | CREATE TABLE IF NOT EXISTS demo ( |
| | host STRING NULL, | | | host STRING NULL, |
| | cpu DOUBLE NULL, | | | cpu DOUBLE NULL, |
| | memory DOUBLE NULL, | | | memory DOUBLE NULL, |
| | ts BIGINT NOT NULL, | | | ts BIGINT NOT NULL, |
| | TIME INDEX (ts) | | | TIME INDEX (ts) |
| | ) | | | ) |
| | PARTITION BY RANGE COLUMNS () ( | | | |
| | PARTITION r0 VALUES LESS THAN (MAXVALUE) | | | ENGINE=mito |
| | ) | | | |
| | ENGINE=mito | +-------+-----------------------------------+"
| | |
+-------+--------------------------------------------+"
} else { } else {
"\ "\
+-------+-----------------------------------+ +-------+-----------------------------------+

View File

@@ -19,29 +19,53 @@ Affected Rows: 0
SHOW CREATE TABLE system_metrics; SHOW CREATE TABLE system_metrics;
+----------------+---------------------------------------------------------+ +----------------+----------------------------------------------------------+
| Table | Create Table | | Table | Create Table |
+----------------+---------------------------------------------------------+ +----------------+----------------------------------------------------------+
| system_metrics | CREATE TABLE IF NOT EXISTS system_metrics ( | | system_metrics | CREATE TABLE IF NOT EXISTS system_metrics ( |
| | id INT UNSIGNED NULL, | | | id INT UNSIGNED NULL, |
| | host STRING NULL, | | | host STRING NULL, |
| | cpu DOUBLE NULL, | | | cpu DOUBLE NULL, |
| | disk FLOAT NULL, | | | disk FLOAT NULL, |
| | n INT NULL, | | | n INT NULL, |
| | ts TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), | | | ts TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), |
| | TIME INDEX (ts), | | | TIME INDEX (ts), |
| | PRIMARY KEY (id, host) | | | PRIMARY KEY (id, host) |
| | ) | | | ) |
| | PARTITION BY RANGE COLUMNS (n) ( | | | PARTITION BY RANGE COLUMNS (n) ( |
| | PARTITION r0 VALUES LESS THAN (5), | | | PARTITION r0 VALUES LESS THAN (5), |
| | PARTITION r1 VALUES LESS THAN (9), | | | PARTITION r1 VALUES LESS THAN (9), |
| | PARTITION r2 VALUES LESS THAN (MAXVALUE) | | | PARTITION r2 VALUES LESS THAN (MAXVALUE) |
| | ) | | | ) |
| | ENGINE=mito | | | ENGINE=mito |
| | | | | |
+----------------+---------------------------------------------------------+ +----------------+----------------------------------------------------------+
DROP TABLE system_metrics; DROP TABLE system_metrics;
Affected Rows: 1 Affected Rows: 1
create table table_without_partition (
ts TIMESTAMP TIME INDEX NOT NULL DEFAULT current_timestamp()
);
Affected Rows: 0
show create table table_without_partition;
+-------------------------+---------------------------------------------------------+
| Table | Create Table |
+-------------------------+---------------------------------------------------------+
| table_without_partition | CREATE TABLE IF NOT EXISTS table_without_partition ( |
| | ts TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), |
| | TIME INDEX (ts) |
| | ) |
| | |
| | ENGINE=mito |
| | |
+-------------------------+---------------------------------------------------------+
drop table table_without_partition;
Affected Rows: 1

View File

@@ -15,7 +15,14 @@ PARTITION BY RANGE COLUMNS (n) (
) )
ENGINE=mito; ENGINE=mito;
SHOW CREATE TABLE system_metrics; SHOW CREATE TABLE system_metrics;
DROP TABLE system_metrics; DROP TABLE system_metrics;
create table table_without_partition (
ts TIMESTAMP TIME INDEX NOT NULL DEFAULT current_timestamp()
);
show create table table_without_partition;
drop table table_without_partition;