From 59ec47b974aecc53ad2b834dc1352a5b9f96d078 Mon Sep 17 00:00:00 2001 From: WenyXu Date: Wed, 15 May 2024 13:41:18 +0000 Subject: [PATCH] feat: ignore internal columns --- src/query/src/sql/show_create_table.rs | 23 +++++++- .../standalone/common/show/show_create.result | 54 +++++++++++++++++++ .../standalone/common/show/show_create.sql | 12 +++++ 3 files changed, 87 insertions(+), 2 deletions(-) diff --git a/src/query/src/sql/show_create_table.rs b/src/query/src/sql/show_create_table.rs index 8cafdc2b0291..82626cc526ff 100644 --- a/src/query/src/sql/show_create_table.rs +++ b/src/query/src/sql/show_create_table.rs @@ -24,6 +24,9 @@ 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::{ + DATA_SCHEMA_TABLE_ID_COLUMN_NAME, DATA_SCHEMA_TSID_COLUMN_NAME, +}; use table::metadata::{TableInfoRef, TableMeta}; use table::requests::{FILE_TABLE_META_KEY, TTL_KEY, WRITE_BUFFER_SIZE_KEY}; @@ -113,7 +116,13 @@ fn create_table_constraints( if !table_meta.primary_key_indices.is_empty() { let columns = table_meta .row_key_column_names() - .map(|name| Ident::with_quote(quote_style, name)) + .flat_map(|name| { + if !is_internal_column(name) { + Some(Ident::with_quote(quote_style, name)) + } else { + None + } + }) .collect(); constraints.push(TableConstraint::Unique { name: None, @@ -126,6 +135,10 @@ fn create_table_constraints( constraints } +fn is_internal_column(name: &str) -> bool { + name == DATA_SCHEMA_TABLE_ID_COLUMN_NAME || name == DATA_SCHEMA_TSID_COLUMN_NAME +} + /// Create a CreateTable statement from table info. pub fn create_table_stmt(table_info: &TableInfoRef, quote_style: char) -> Result { let table_meta = &table_info.meta; @@ -135,7 +148,13 @@ pub fn create_table_stmt(table_info: &TableInfoRef, quote_style: char) -> Result let columns = schema .column_schemas() .iter() - .map(|c| create_column_def(c, quote_style)) + .filter_map(|c| { + if !is_internal_column(&c.name) { + Some(create_column_def(c, quote_style)) + } else { + None + } + }) .collect::>>()?; let constraints = create_table_constraints(schema, table_meta, quote_style); diff --git a/tests/cases/standalone/common/show/show_create.result b/tests/cases/standalone/common/show/show_create.result index 90c99d77f0fa..400c3d5a15d6 100644 --- a/tests/cases/standalone/common/show/show_create.result +++ b/tests/cases/standalone/common/show/show_create.result @@ -95,3 +95,57 @@ 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 + diff --git a/tests/cases/standalone/common/show/show_create.sql b/tests/cases/standalone/common/show/show_create.sql index ccc3333f5150..839a71e4a563 100644 --- a/tests/cases/standalone/common/show/show_create.sql +++ b/tests/cases/standalone/common/show/show_create.sql @@ -48,3 +48,15 @@ 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; \ No newline at end of file