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
34 changes: 32 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,35 @@ pub async fn show_tables(
.context(error::CreateRecordBatchSnafu)?;
Ok(Output::RecordBatches(records))
}
ShowKind::Full => {
let mut table_types = Vec::with_capacity(tables.len());
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

table_types.push(format!("{table_type}"));
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}

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::ShowTables { .. });
match &stmts[0] {
Statement::ShowTables(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::ShowTables { .. });
match &stmts[0] {
Statement::ShowTables(show) => {
assert_eq!(ShowKind::Full, show.kind);
}
_ => {
unreachable!();
}
}
}
Lilit0x marked this conversation as resolved.
Show resolved Hide resolved
}
10 changes: 10 additions & 0 deletions src/table/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,16 @@ pub enum TableType {
Temporary,
}

impl std::fmt::Display for TableType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TableType::Base => f.write_str("BASE TABLE"),
TableType::Temporary => f.write_str("TEMPORARY"),
TableType::View => f.write_str("VIEW"),
}
}
}

/// Identifier of the table.
#[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Default)]
pub struct TableIdent {
Expand Down
Loading