feat: ignore internal columns in SHOW CREATE TABLE (#3950)

* feat: ignore internal columns

* chore: add new line

* chore: apply suggestions from CR

* chore: apply suggestions from CR
This commit is contained in:
Weny Xu
2024-05-16 15:28:48 +09:00
committed by GitHub
parent fe34ebf770
commit dff7ba7598
4 changed files with 157 additions and 4 deletions

View File

@@ -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::<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,

View File

@@ -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
}