Skip to content

Commit

Permalink
feat: implement show create table operation
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Dec 11, 2024
1 parent 5ebbe1a commit 4ebfd5c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 12 deletions.
45 changes: 45 additions & 0 deletions src/datatypes/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,51 @@ impl ConcreteDataType {
_ => None,
}
}

/// Return the datatype name in postgres type system
pub fn postgres_datatype_name(&self) -> &'static str {
match self {
&ConcreteDataType::Null(_) => "UNKNOWN",
&ConcreteDataType::Boolean(_) => "BOOL",
&ConcreteDataType::Int8(_) | &ConcreteDataType::UInt8(_) => "CHAR",
&ConcreteDataType::Int16(_) | &ConcreteDataType::UInt16(_) => "INT2",
&ConcreteDataType::Int32(_) | &ConcreteDataType::UInt32(_) => "INT4",
&ConcreteDataType::Int64(_) | &ConcreteDataType::UInt64(_) => "INT8",
&ConcreteDataType::Float32(_) => "FLOAT4",
&ConcreteDataType::Float64(_) => "FLOAT8",
&ConcreteDataType::Binary(_) | &ConcreteDataType::Vector(_) => "BYTEA",
&ConcreteDataType::String(_) => "VARCHAR",
&ConcreteDataType::Date(_) => "DATE",
&ConcreteDataType::DateTime(_) | &ConcreteDataType::Timestamp(_) => "TIMESTAMP",
&ConcreteDataType::Time(_) => "TIME",
&ConcreteDataType::Interval(_) => "INTERVAL",
&ConcreteDataType::Decimal128(_) => "NUMERIC",
&ConcreteDataType::Json(_) => "JSON",
ConcreteDataType::List(list) => match list.item_type() {
&ConcreteDataType::Null(_) => "UNKNOWN",
&ConcreteDataType::Boolean(_) => "_BOOL",
&ConcreteDataType::Int8(_) | &ConcreteDataType::UInt8(_) => "_CHAR",
&ConcreteDataType::Int16(_) | &ConcreteDataType::UInt16(_) => "_INT2",
&ConcreteDataType::Int32(_) | &ConcreteDataType::UInt32(_) => "_INT4",
&ConcreteDataType::Int64(_) | &ConcreteDataType::UInt64(_) => "_INT8",
&ConcreteDataType::Float32(_) => "_FLOAT4",
&ConcreteDataType::Float64(_) => "_FLOAT8",
&ConcreteDataType::Binary(_) => "_BYTEA",
&ConcreteDataType::String(_) => "_VARCHAR",
&ConcreteDataType::Date(_) => "_DATE",
&ConcreteDataType::DateTime(_) | &ConcreteDataType::Timestamp(_) => "_TIMESTAMP",
&ConcreteDataType::Time(_) => "_TIME",
&ConcreteDataType::Interval(_) => "_INTERVAL",
&ConcreteDataType::Decimal128(_) => "_NUMERIC",
&ConcreteDataType::Json(_) => "_JSON",
&ConcreteDataType::Duration(_)
| &ConcreteDataType::Dictionary(_)
| &ConcreteDataType::Vector(_)
| &ConcreteDataType::List(_) => "UNKNOWN",
},
&ConcreteDataType::Duration(_) | &ConcreteDataType::Dictionary(_) => "UNKNOWN",
}
}
}

impl From<&ConcreteDataType> for ConcreteDataType {
Expand Down
13 changes: 1 addition & 12 deletions src/operator/src/statement/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,7 @@ impl StatementExecutor {
.fail();
}

let schema_options = self
.table_metadata_manager
.schema_manager()
.get(SchemaNameKey {
catalog: &table_name.catalog_name,
schema: &table_name.schema_name,
})
.await
.context(TableMetadataManagerSnafu)?
.map(|v| v.into_inner());

query::sql::show_create_table(table, schema_options, None, query_ctx)
query::sql::show_create_foreign_table_for_pg(table, query_ctx)
.context(ExecuteStatementSnafu)
}

Expand Down
48 changes: 48 additions & 0 deletions src/query/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use datafusion_expr::{case, col, lit, Expr};
use datatypes::prelude::*;
use datatypes::schema::{ColumnDefaultConstraint, ColumnSchema, RawSchema, Schema};
use datatypes::vectors::StringVector;
use itertools::Itertools;
use object_store::ObjectStore;
use once_cell::sync::Lazy;
use regex::Regex;
Expand All @@ -61,6 +62,7 @@ use sql::statements::show::{
use sql::statements::statement::Statement;
use sql::statements::OptionMap;
use sqlparser::ast::ObjectName;
use store_api::metric_engine_consts::{is_metric_engine, is_metric_engine_internal_column};
use table::requests::{FILE_TABLE_LOCATION_KEY, FILE_TABLE_PATTERN_KEY};
use table::TableRef;

Expand Down Expand Up @@ -746,6 +748,52 @@ pub fn show_create_table(
Ok(Output::new_with_record_batches(records))
}

pub fn show_create_foreign_table_for_pg(
table: TableRef,
_query_ctx: QueryContextRef,
) -> Result<Output> {
let table_info = table.table_info();

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()
.filter_map(|c| {
if is_metric_engine && is_metric_engine_internal_column(&c.name) {
None
} else {
Some(format!(
"\"{}\" {}",
c.name,
c.data_type.postgres_datatype_name()
))
}
})
.join(",\n ");

let sql = format!(
r#"CREATE FOREIGN TABLE ft_{} (
{}
)
SERVER greptimedb
OPTIONS (table_name '{}')"#,
table_name, columns, table_name
);

let columns = vec![
Arc::new(StringVector::from(vec![table_name.clone()])) as _,
Arc::new(StringVector::from(vec![sql])) as _,
];
let records = RecordBatches::try_from_columns(SHOW_CREATE_TABLE_OUTPUT_SCHEMA.clone(), columns)
.context(error::CreateRecordBatchSnafu)?;

Ok(Output::new_with_record_batches(records))
}

pub fn show_create_view(
view_name: ObjectName,
definition: &str,
Expand Down
30 changes: 30 additions & 0 deletions tests/cases/standalone/common/show/show_create.result
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ SHOW CREATE TABLE system_metrics;
| | ) |
+----------------+-----------------------------------------------------------+

SHOW CREATE TABLE system_metrics FOR POSTGRES_FOREIGN_TABLE;

+----------------+------------------------------------------+
| Table | Create Table |
+----------------+------------------------------------------+
| system_metrics | CREATE FOREIGN TABLE ft_system_metrics ( |
| | "id" INT4, |
| | "host" VARCHAR, |
| | "cpu" FLOAT8, |
| | "disk" FLOAT4, |
| | "ts" TIMESTAMP |
| | ) |
| | SERVER greptimedb |
| | OPTIONS (table_name 'system_metrics') |
+----------------+------------------------------------------+

DROP TABLE system_metrics;

Affected Rows: 0
Expand Down Expand Up @@ -141,6 +157,20 @@ show create table t1;
| | ) |
+-------+-----------------------------------+

SHOW CREATE TABLE t1 FOR POSTGRES_FOREIGN_TABLE;

+-------+------------------------------+
| Table | Create Table |
+-------+------------------------------+
| t1 | CREATE FOREIGN TABLE ft_t1 ( |
| | "host" VARCHAR, |
| | "ts" TIMESTAMP, |
| | "val" FLOAT8 |
| | ) |
| | SERVER greptimedb |
| | OPTIONS (table_name 't1') |
+-------+------------------------------+

drop table t1;

Affected Rows: 0
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/standalone/common/show/show_create.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ WITH(

SHOW CREATE TABLE system_metrics;

SHOW CREATE TABLE system_metrics FOR POSTGRES_FOREIGN_TABLE;

DROP TABLE system_metrics;

create table table_without_partition (
Expand Down Expand Up @@ -57,6 +59,8 @@ show create table phy;

show create table t1;

SHOW CREATE TABLE t1 FOR POSTGRES_FOREIGN_TABLE;

drop table t1;

drop table phy;
Expand Down

0 comments on commit 4ebfd5c

Please sign in to comment.