diff --git a/src/query/src/sql/show_create_table.rs b/src/query/src/sql/show_create_table.rs index 8cafdc2b0291..8002ef0d8e1b 100644 --- a/src/query/src/sql/show_create_table.rs +++ b/src/query/src/sql/show_create_table.rs @@ -24,6 +24,7 @@ use sql::dialect::GreptimeDbDialect; use sql::parser::ParserContext; use sql::statements::create::{CreateTable, TIME_INDEX}; use sql::statements::{self, OptionMap}; +use store_api::metric_engine_consts::{is_metric_engine, is_metric_engine_internal_column}; use table::metadata::{TableInfoRef, TableMeta}; use table::requests::{FILE_TABLE_META_KEY, TTL_KEY, WRITE_BUFFER_SIZE_KEY}; @@ -96,6 +97,7 @@ fn create_column_def(column_schema: &ColumnSchema, quote_style: char) -> Result< } fn create_table_constraints( + engine: &str, schema: &SchemaRef, table_meta: &TableMeta, quote_style: char, @@ -111,9 +113,16 @@ fn create_table_constraints( }); } if !table_meta.primary_key_indices.is_empty() { + let is_metric_engine = is_metric_engine(engine); let columns = table_meta .row_key_column_names() - .map(|name| Ident::with_quote(quote_style, name)) + .flat_map(|name| { + if is_metric_engine && is_metric_engine_internal_column(name) { + None + } else { + Some(Ident::with_quote(quote_style, name)) + } + }) .collect(); constraints.push(TableConstraint::Unique { name: None, @@ -131,14 +140,20 @@ pub fn create_table_stmt(table_info: &TableInfoRef, quote_style: char) -> Result let table_meta = &table_info.meta; let table_name = &table_info.name; let schema = &table_info.meta.schema; - + let is_metric_engine = is_metric_engine(&table_meta.engine); let columns = schema .column_schemas() .iter() - .map(|c| create_column_def(c, quote_style)) + .filter_map(|c| { + if is_metric_engine && is_metric_engine_internal_column(&c.name) { + None + } else { + Some(create_column_def(c, quote_style)) + } + }) .collect::>>()?; - let constraints = create_table_constraints(schema, table_meta, quote_style); + let constraints = create_table_constraints(&table_meta.engine, schema, table_meta, quote_style); Ok(CreateTable { if_not_exists: true, diff --git a/src/store-api/src/metric_engine_consts.rs b/src/store-api/src/metric_engine_consts.rs index 1f0343f1b0d2..16666167e457 100644 --- a/src/store-api/src/metric_engine_consts.rs +++ b/src/store-api/src/metric_engine_consts.rs @@ -70,3 +70,13 @@ pub const LOGICAL_TABLE_METADATA_KEY: &str = "on_physical_table"; /// HashMap key to be used in the region server's extension response. /// Represent a list of column metadata that are added to physical table. pub const ALTER_PHYSICAL_EXTENSION_KEY: &str = "ALTER_PHYSICAL"; + +/// Returns true if it's a internal column of the metric engine. +pub fn is_metric_engine_internal_column(name: &str) -> bool { + name == DATA_SCHEMA_TABLE_ID_COLUMN_NAME || name == DATA_SCHEMA_TSID_COLUMN_NAME +} + +/// Returns true if it's metric engine +pub fn is_metric_engine(name: &str) -> bool { + name == METRIC_ENGINE_NAME +} diff --git a/tests/cases/standalone/common/show/show_create.result b/tests/cases/standalone/common/show/show_create.result index 90c99d77f0fa..6071b5cf9a4d 100644 --- a/tests/cases/standalone/common/show/show_create.result +++ b/tests/cases/standalone/common/show/show_create.result @@ -95,3 +95,100 @@ WITH( Error: 1004(InvalidArguments), Object store not found: S3 +CREATE TABLE phy (ts timestamp time index, val double) engine=metric with ("physical_metric_table" = ""); + +Affected Rows: 0 + +CREATE TABLE t1 (ts timestamp time index, val double, host string primary key) engine = metric with ("on_physical_table" = "phy"); + +Affected Rows: 0 + +show create table phy; + ++-------+------------------------------------+ +| Table | Create Table | ++-------+------------------------------------+ +| phy | CREATE TABLE IF NOT EXISTS "phy" ( | +| | "ts" TIMESTAMP(3) NOT NULL, | +| | "val" DOUBLE NULL, | +| | "host" STRING NULL, | +| | TIME INDEX ("ts"), | +| | PRIMARY KEY ("host") | +| | ) | +| | | +| | ENGINE=metric | +| | WITH( | +| | physical_metric_table = '' | +| | ) | ++-------+------------------------------------+ + +show create table t1; + ++-------+-----------------------------------+ +| Table | Create Table | ++-------+-----------------------------------+ +| t1 | CREATE TABLE IF NOT EXISTS "t1" ( | +| | "host" STRING NULL, | +| | "ts" TIMESTAMP(3) NOT NULL, | +| | "val" DOUBLE NULL, | +| | TIME INDEX ("ts"), | +| | PRIMARY KEY ("host") | +| | ) | +| | | +| | ENGINE=metric | +| | WITH( | +| | on_physical_table = 'phy' | +| | ) | ++-------+-----------------------------------+ + +drop table t1; + +Affected Rows: 0 + +drop table phy; + +Affected Rows: 0 + +CREATE TABLE IF NOT EXISTS "phy" ( + "ts" TIMESTAMP(3) NOT NULL, + "val" DOUBLE NULL, + "__table_id" INT UNSIGNED NOT NULL, + "__tsid" BIGINT UNSIGNED NOT NULL, + "host" STRING NULL, + "job" STRING NULL, + TIME INDEX ("ts"), + PRIMARY KEY ("__table_id", "__tsid", "host", "job") +) +ENGINE=mito +WITH( + physical_metric_table = '', +); + +Affected Rows: 0 + +show create table phy; + ++-------+-------------------------------------------------------+ +| Table | Create Table | ++-------+-------------------------------------------------------+ +| phy | CREATE TABLE IF NOT EXISTS "phy" ( | +| | "ts" TIMESTAMP(3) NOT NULL, | +| | "val" DOUBLE NULL, | +| | "__table_id" INT UNSIGNED NOT NULL, | +| | "__tsid" BIGINT UNSIGNED NOT NULL, | +| | "host" STRING NULL, | +| | "job" STRING NULL, | +| | TIME INDEX ("ts"), | +| | PRIMARY KEY ("__table_id", "__tsid", "host", "job") | +| | ) | +| | | +| | ENGINE=mito | +| | WITH( | +| | physical_metric_table = '' | +| | ) | ++-------+-------------------------------------------------------+ + +drop table phy; + +Affected Rows: 0 + diff --git a/tests/cases/standalone/common/show/show_create.sql b/tests/cases/standalone/common/show/show_create.sql index ccc3333f5150..f59b72971a93 100644 --- a/tests/cases/standalone/common/show/show_create.sql +++ b/tests/cases/standalone/common/show/show_create.sql @@ -48,3 +48,34 @@ ENGINE=mito WITH( storage = 'S3' ); + +CREATE TABLE phy (ts timestamp time index, val double) engine=metric with ("physical_metric_table" = ""); + +CREATE TABLE t1 (ts timestamp time index, val double, host string primary key) engine = metric with ("on_physical_table" = "phy"); + +show create table phy; + +show create table t1; + +drop table t1; + +drop table phy; + +CREATE TABLE IF NOT EXISTS "phy" ( + "ts" TIMESTAMP(3) NOT NULL, + "val" DOUBLE NULL, + "__table_id" INT UNSIGNED NOT NULL, + "__tsid" BIGINT UNSIGNED NOT NULL, + "host" STRING NULL, + "job" STRING NULL, + TIME INDEX ("ts"), + PRIMARY KEY ("__table_id", "__tsid", "host", "job") +) +ENGINE=mito +WITH( + physical_metric_table = '', +); + +show create table phy; + +drop table phy;