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: support for show full tables #2410

Merged
merged 11 commits into from
Sep 22, 2023
39 changes: 37 additions & 2 deletions src/query/src/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ pub async fn show_databases(
.context(error::CreateRecordBatchSnafu)?;
Ok(Output::RecordBatches(records))
}
ShowKind::Full => todo!(),
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -143,14 +144,14 @@ pub async fn show_tables(
catalog_manager: CatalogManagerRef,
query_ctx: QueryContextRef,
) -> Result<Output> {
let schema = if let Some(database) = stmt.database {
let schema_name = if let Some(database) = stmt.database {
database
} else {
query_ctx.current_schema().to_owned()
};
// TODO(sunng87): move this function into query_ctx
let mut tables = catalog_manager
.table_names(query_ctx.current_catalog(), &schema)
.table_names(query_ctx.current_catalog(), &schema_name)
.await
.context(error::CatalogSnafu)?;

Expand Down Expand Up @@ -182,6 +183,40 @@ pub async fn show_tables(
.context(error::CreateRecordBatchSnafu)?;
Ok(Output::RecordBatches(records))
}
ShowKind::Full => {
let mut table_types = Vec::new();
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
for table_name in &tables {
let table_type = catalog_manager
.table(query_ctx.current_catalog(), &schema_name, table_name)
.await
.context(error::CatalogSnafu)?
.unwrap()
.table_type();
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved

let table_type = match table_type {
table::metadata::TableType::Base => "BASE TABLE",
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
table::metadata::TableType::Temporary => "TEMPORARY",
table::metadata::TableType::View => "VIEW",
};
table_types.push(table_type);
}

let table_types = Arc::new(StringVector::from(table_types)) as _;
let tables = Arc::new(StringVector::from(tables)) as _;

let schema = Arc::new(Schema::new(vec![
ColumnSchema::new(
format!("Tables_in_{schema_name}"),
ConcreteDataType::string_datatype(),
false,
),
ColumnSchema::new("Table_type", ConcreteDataType::string_datatype(), false),
]));

let records = RecordBatches::try_from_columns(schema, vec![tables, table_types])
.context(error::CreateRecordBatchSnafu)?;
Ok(Output::RecordBatches(records))
}
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/sql/src/parsers/show_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,27 @@ impl<'a> ParserContext<'a> {
} else {
self.unsupported(self.peek_token_as_string())
}
} else if self.consume_token("FULL") {
if self.consume_token("TABLES") {
self.parse_show_full_tables()
} else {
self.unsupported(self.peek_token_as_string())
}
} else {
self.unsupported(self.peek_token_as_string())
}
}

fn parse_show_full_tables(&mut self) -> Result<Statement> {
match self.parser.peek_token().token {
Token::EOF | Token::SemiColon => Ok(Statement::ShowTables(ShowTables {
kind: ShowKind::Full,
database: None,
})),
_ => self.unsupported(self.peek_token_as_string()),
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Parse SHOW CREATE TABLE statement
fn parse_show_create_table(&mut self) -> Result<Statement> {
let table_name =
Expand Down Expand Up @@ -301,4 +317,20 @@ mod tests {
})
);
}

#[test]
pub fn test_show_full_tables() {
let sql = "SHOW FULL TABLES";
let stmts = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::ShowDatabases { .. });
match &stmts[0] {
Statement::ShowDatabases(show) => {
assert_eq!(ShowKind::Full, show.kind);
}
_ => {
unreachable!();
}
}
}
}
19 changes: 19 additions & 0 deletions src/sql/src/statements/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::ast::{Expr, Ident, ObjectName};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShowKind {
All,
Full,
Like(Ident),
Where(Expr),
}
Expand All @@ -28,6 +29,7 @@ impl fmt::Display for ShowKind {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ShowKind::All => write!(f, "ALL"),
ShowKind::Full => write!(f, "FULL"),
ShowKind::Like(ident) => write!(f, "LIKE {ident}"),
ShowKind::Where(expr) => write!(f, "WHERE {expr}"),
}
Expand Down Expand Up @@ -74,6 +76,7 @@ mod tests {
#[test]
fn test_kind_display() {
assert_eq!("ALL", format!("{}", ShowKind::All));
assert_eq!("FULL", format!("{}", ShowKind::Full));
assert_eq!(
"LIKE test",
format!(
Expand Down Expand Up @@ -137,4 +140,20 @@ mod tests {
let sql = "SHOW CREATE TABLE";
assert!(ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).is_err());
}

#[test]
pub fn test_show_full_tables() {
let sql = "SHOW FULL TABLES";
let stmts = ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}).unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::ShowDatabases { .. });
match &stmts[0] {
Statement::ShowDatabases(show) => {
assert_eq!(ShowKind::Full, show.kind);
}
_ => {
unreachable!();
}
}
}
}
Loading