diff --git a/src/metric-engine/src/engine/create.rs b/src/metric-engine/src/engine/create.rs index b8b39a11ddf6..0ff5bb75f8a2 100644 --- a/src/metric-engine/src/engine/create.rs +++ b/src/metric-engine/src/engine/create.rs @@ -405,6 +405,7 @@ impl MetricEngineInner { request: &RegionCreateRequest, ) -> RegionCreateRequest { let mut data_region_request = request.clone(); + let mut primary_key = vec![ReservedColumnId::table_id(), ReservedColumnId::tsid()]; // concat region dir data_region_request.region_dir = join_dir(&request.region_dir, DATA_REGION_SUBDIR); @@ -416,6 +417,7 @@ impl MetricEngineInner { .for_each(|metadata| { if metadata.semantic_type == SemanticType::Tag { metadata.column_schema.set_nullable(); + primary_key.push(metadata.column_id); } }); @@ -423,8 +425,7 @@ impl MetricEngineInner { let [table_id_col, tsid_col] = Self::internal_column_metadata(); data_region_request.column_metadatas.push(table_id_col); data_region_request.column_metadatas.push(tsid_col); - data_region_request.primary_key = - vec![ReservedColumnId::table_id(), ReservedColumnId::tsid()]; + data_region_request.primary_key = primary_key; // set index options set_index_options_for_data_region(&mut data_region_request.options); @@ -608,7 +609,7 @@ mod test { assert_eq!(data_region_request.column_metadatas.len(), 4); assert_eq!( data_region_request.primary_key, - vec![ReservedColumnId::table_id(), ReservedColumnId::tsid()] + vec![ReservedColumnId::table_id(), ReservedColumnId::tsid(), 1] ); } } diff --git a/tests/cases/standalone/common/create/create_metric_table.result b/tests/cases/standalone/common/create/create_metric_table.result index 0a153ec733fc..5256f9d1b91b 100644 --- a/tests/cases/standalone/common/create/create_metric_table.result +++ b/tests/cases/standalone/common/create/create_metric_table.result @@ -84,3 +84,65 @@ DROP TABLE phy; Affected Rows: 0 +-- create one with other primary keys +CREATE TABLE phy2 (ts timestamp time index, val double, abc string, def string, primary key (abc, def)) engine=metric with ("physical_metric_table" = ""); + +Affected Rows: 0 + +DESC TABLE phy2; + ++--------+----------------------+-----+------+---------+---------------+ +| Column | Type | Key | Null | Default | Semantic Type | ++--------+----------------------+-----+------+---------+---------------+ +| ts | TimestampMillisecond | PRI | NO | | TIMESTAMP | +| val | Float64 | | YES | | FIELD | +| abc | String | PRI | YES | | TAG | +| def | String | PRI | YES | | TAG | ++--------+----------------------+-----+------+---------+---------------+ + +DROP TABLE phy2; + +Affected Rows: 0 + +-- fuzz test case https://github.com/GreptimeTeam/greptimedb/issues/3612 +CREATE TABLE `auT`( + incidunt TIMESTAMP(3) TIME INDEX, + `QuaErAT` BOOLEAN, + `REPREHenDERIt` BOOLEAN DEFAULT true, + `Et` INT NULL, + `AutEM` INT, + esse DOUBLE, + `Tempore` BOOLEAN, + `reruM` BOOLEAN, + `eRrOR` BOOLEAN NULL, + `cOMmodi` BOOLEAN, + `PERfERENdIS` DOUBLE, + `eSt` FLOAT DEFAULT 0.70978713, + PRIMARY KEY(`cOMmodi`, `PERfERENdIS`, esse) +) ENGINE = metric with ("physical_metric_table" = ""); + +Affected Rows: 0 + +DESC TABLE `auT`; + ++---------------+----------------------+-----+------+------------+---------------+ +| Column | Type | Key | Null | Default | Semantic Type | ++---------------+----------------------+-----+------+------------+---------------+ +| incidunt | TimestampMillisecond | PRI | NO | | TIMESTAMP | +| QuaErAT | Boolean | | YES | | FIELD | +| REPREHenDERIt | Boolean | | YES | true | FIELD | +| Et | Int32 | | YES | | FIELD | +| AutEM | Int32 | | YES | | FIELD | +| esse | Float64 | PRI | YES | | TAG | +| Tempore | Boolean | | YES | | FIELD | +| reruM | Boolean | | YES | | FIELD | +| eRrOR | Boolean | | YES | | FIELD | +| cOMmodi | Boolean | PRI | YES | | TAG | +| PERfERENdIS | Float64 | PRI | YES | | TAG | +| eSt | Float32 | | YES | 0.70978713 | FIELD | ++---------------+----------------------+-----+------+------------+---------------+ + +DROP TABLE `auT`; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/create/create_metric_table.sql b/tests/cases/standalone/common/create/create_metric_table.sql index fcc41ff11541..a5be70c95502 100644 --- a/tests/cases/standalone/common/create/create_metric_table.sql +++ b/tests/cases/standalone/common/create/create_metric_table.sql @@ -23,3 +23,31 @@ DROP TABLE t1; DROP TABLE t2; DROP TABLE phy; + +-- create one with other primary keys +CREATE TABLE phy2 (ts timestamp time index, val double, abc string, def string, primary key (abc, def)) engine=metric with ("physical_metric_table" = ""); + +DESC TABLE phy2; + +DROP TABLE phy2; + +-- fuzz test case https://github.com/GreptimeTeam/greptimedb/issues/3612 +CREATE TABLE `auT`( + incidunt TIMESTAMP(3) TIME INDEX, + `QuaErAT` BOOLEAN, + `REPREHenDERIt` BOOLEAN DEFAULT true, + `Et` INT NULL, + `AutEM` INT, + esse DOUBLE, + `Tempore` BOOLEAN, + `reruM` BOOLEAN, + `eRrOR` BOOLEAN NULL, + `cOMmodi` BOOLEAN, + `PERfERENdIS` DOUBLE, + `eSt` FLOAT DEFAULT 0.70978713, + PRIMARY KEY(`cOMmodi`, `PERfERENdIS`, esse) +) ENGINE = metric with ("physical_metric_table" = ""); + +DESC TABLE `auT`; + +DROP TABLE `auT`;