Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ignore internal columns in SHOW CREATE TABLE #3950

Merged
merged 4 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/query/src/sql/show_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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::<Result<Vec<_>>>()?;

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,
Expand Down
10 changes: 10 additions & 0 deletions src/store-api/src/metric_engine_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
97 changes: 97 additions & 0 deletions tests/cases/standalone/common/show/show_create.result
Original file line number Diff line number Diff line change
Expand Up @@ -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

31 changes: 31 additions & 0 deletions tests/cases/standalone/common/show/show_create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;