feat: show create postgresql foreign table (#5143)

* feat: add show create table for pg in parser

* feat: implement show create table operation

* fix: adopt upstream changes
This commit is contained in:
Ning Sun
2024-12-18 23:15:55 +08:00
committed by Yingwen
parent 55b0022676
commit 6427682a9a
8 changed files with 219 additions and 7 deletions

View File

@@ -21,9 +21,9 @@ use crate::error::{
};
use crate::parser::ParserContext;
use crate::statements::show::{
ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateView,
ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables,
ShowVariables, ShowViews,
ShowColumns, ShowCreateDatabase, ShowCreateFlow, ShowCreateTable, ShowCreateTableVariant,
ShowCreateView, ShowDatabases, ShowFlows, ShowIndex, ShowKind, ShowStatus, ShowTableStatus,
ShowTables, ShowVariables, ShowViews,
};
use crate::statements::statement::Statement;
@@ -146,7 +146,19 @@ impl ParserContext<'_> {
name: table_name.to_string(),
}
);
Ok(Statement::ShowCreateTable(ShowCreateTable { table_name }))
let mut variant = ShowCreateTableVariant::Original;
if self.consume_token("FOR") {
if self.consume_token("POSTGRES_FOREIGN_TABLE") {
variant = ShowCreateTableVariant::PostgresForeignTable;
} else {
self.unsupported(self.peek_token_as_string())?;
}
}
Ok(Statement::ShowCreateTable(ShowCreateTable {
table_name,
variant,
}))
}
fn parse_show_create_flow(&mut self) -> Result<Statement> {

View File

@@ -179,12 +179,26 @@ impl Display for ShowCreateDatabase {
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
pub struct ShowCreateTable {
pub table_name: ObjectName,
pub variant: ShowCreateTableVariant,
}
/// Variant of a show create table
#[derive(Default, Debug, Clone, PartialEq, Eq, Visit, VisitMut, Serialize)]
pub enum ShowCreateTableVariant {
#[default]
Original,
PostgresForeignTable,
}
impl Display for ShowCreateTable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let table_name = &self.table_name;
write!(f, r#"SHOW CREATE TABLE {table_name}"#)
write!(f, r#"SHOW CREATE TABLE {table_name}"#)?;
if let ShowCreateTableVariant::PostgresForeignTable = self.variant {
write!(f, " FOR POSTGRES_FOREIGN_TABLE")?;
}
Ok(())
}
}
@@ -344,12 +358,31 @@ mod tests {
Statement::ShowCreateTable(show) => {
let table_name = show.table_name.to_string();
assert_eq!(table_name, "test");
assert_eq!(show.variant, ShowCreateTableVariant::Original);
}
_ => {
unreachable!();
}
}
let sql = "SHOW CREATE TABLE test FOR POSTGRES_FOREIGN_TABLE";
let stmts: Vec<Statement> =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::ShowCreateTable { .. });
match &stmts[0] {
Statement::ShowCreateTable(show) => {
let table_name = show.table_name.to_string();
assert_eq!(table_name, "test");
assert_eq!(show.variant, ShowCreateTableVariant::PostgresForeignTable);
}
_ => {
unreachable!();
}
}
}
#[test]
pub fn test_show_create_missing_table_name() {
let sql = "SHOW CREATE TABLE";
@@ -361,6 +394,17 @@ mod tests {
.is_err());
}
#[test]
pub fn test_show_create_unknown_for() {
let sql = "SHOW CREATE TABLE t FOR UNKNOWN";
assert!(ParserContext::create_with_dialect(
sql,
&GreptimeDbDialect {},
ParseOptions::default()
)
.is_err());
}
#[test]
pub fn test_show_create_flow() {
let sql = "SHOW CREATE FLOW test";