diff --git a/src/sql/src/statements/create.rs b/src/sql/src/statements/create.rs index 05b479b65f..5ae756ebfb 100644 --- a/src/sql/src/statements/create.rs +++ b/src/sql/src/statements/create.rs @@ -149,14 +149,18 @@ impl Display for PartitionEntry { impl Display for Partitions { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!( - f, - r#"PARTITION BY RANGE COLUMNS ({}) ( -{} -)"#, - format_list_comma!(self.column_list), - format_list_indent!(self.entries), - ) + if !self.column_list.is_empty() { + write!( + f, + r#"PARTITION BY RANGE COLUMNS ({}) ( + {} + )"#, + 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) ) PARTITION BY RANGE COLUMNS (ts) ( - PARTITION r0 VALUES LESS THAN (5), + PARTITION r0 VALUES LESS THAN (5), PARTITION r1 VALUES LESS THAN (9), PARTITION r2 VALUES LESS THAN (MAXVALUE) -) + ) ENGINE=mito WITH( regions = 1, @@ -266,4 +270,44 @@ WITH( _ => 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!(), + } + } } diff --git a/tests-integration/src/tests/instance_test.rs b/tests-integration/src/tests/instance_test.rs index b3cea11fc2..d2c2348333 100644 --- a/tests-integration/src/tests/instance_test.rs +++ b/tests-integration/src/tests/instance_test.rs @@ -98,22 +98,20 @@ async fn test_show_create_table(instance: Arc) { let expected = if instance.is_distributed_mode() { "\ -+-------+--------------------------------------------+ -| Table | Create Table | -+-------+--------------------------------------------+ -| demo | CREATE TABLE IF NOT EXISTS demo ( | -| | host STRING NULL, | -| | cpu DOUBLE NULL, | -| | memory DOUBLE NULL, | -| | ts BIGINT NOT NULL, | -| | TIME INDEX (ts) | -| | ) | -| | PARTITION BY RANGE COLUMNS () ( | -| | PARTITION r0 VALUES LESS THAN (MAXVALUE) | -| | ) | -| | ENGINE=mito | -| | | -+-------+--------------------------------------------+" ++-------+-----------------------------------+ +| Table | Create Table | ++-------+-----------------------------------+ +| demo | CREATE TABLE IF NOT EXISTS demo ( | +| | host STRING NULL, | +| | cpu DOUBLE NULL, | +| | memory DOUBLE NULL, | +| | ts BIGINT NOT NULL, | +| | TIME INDEX (ts) | +| | ) | +| | | +| | ENGINE=mito | +| | | ++-------+-----------------------------------+" } else { "\ +-------+-----------------------------------+ diff --git a/tests/cases/distributed/show/show_create.result b/tests/cases/distributed/show/show_create.result index 785065d85d..93f73a3a20 100644 --- a/tests/cases/distributed/show/show_create.result +++ b/tests/cases/distributed/show/show_create.result @@ -19,29 +19,53 @@ Affected Rows: 0 SHOW CREATE TABLE system_metrics; -+----------------+---------------------------------------------------------+ -| Table | Create Table | -+----------------+---------------------------------------------------------+ -| system_metrics | CREATE TABLE IF NOT EXISTS system_metrics ( | -| | id INT UNSIGNED NULL, | -| | host STRING NULL, | -| | cpu DOUBLE NULL, | -| | disk FLOAT NULL, | -| | n INT NULL, | -| | ts TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), | -| | TIME INDEX (ts), | -| | PRIMARY KEY (id, host) | -| | ) | -| | PARTITION BY RANGE COLUMNS (n) ( | -| | PARTITION r0 VALUES LESS THAN (5), | -| | PARTITION r1 VALUES LESS THAN (9), | -| | PARTITION r2 VALUES LESS THAN (MAXVALUE) | -| | ) | -| | ENGINE=mito | -| | | -+----------------+---------------------------------------------------------+ ++----------------+----------------------------------------------------------+ +| Table | Create Table | ++----------------+----------------------------------------------------------+ +| system_metrics | CREATE TABLE IF NOT EXISTS system_metrics ( | +| | id INT UNSIGNED NULL, | +| | host STRING NULL, | +| | cpu DOUBLE NULL, | +| | disk FLOAT NULL, | +| | n INT NULL, | +| | ts TIMESTAMP(3) NOT NULL DEFAULT current_timestamp(), | +| | TIME INDEX (ts), | +| | PRIMARY KEY (id, host) | +| | ) | +| | PARTITION BY RANGE COLUMNS (n) ( | +| | PARTITION r0 VALUES LESS THAN (5), | +| | PARTITION r1 VALUES LESS THAN (9), | +| | PARTITION r2 VALUES LESS THAN (MAXVALUE) | +| | ) | +| | ENGINE=mito | +| | | ++----------------+----------------------------------------------------------+ DROP TABLE system_metrics; 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 + diff --git a/tests/cases/distributed/show/show_create.sql b/tests/cases/distributed/show/show_create.sql index d67ac0fa8b..cdb6444eee 100644 --- a/tests/cases/distributed/show/show_create.sql +++ b/tests/cases/distributed/show/show_create.sql @@ -15,7 +15,14 @@ PARTITION BY RANGE COLUMNS (n) ( ) ENGINE=mito; - SHOW CREATE 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;