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(query): support show columns query #10726

Merged
merged 2 commits into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/query/ast/src/ast/format/ast_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,30 @@ impl<'ast> Visitor<'ast> for AstFormatVisitor {
self.children.push(node);
}

fn visit_show_columns(&mut self, stmt: &'ast ShowColumnsStmt) {
let mut children = Vec::new();
if let Some(database) = &stmt.database {
let database_name = format!("Database {}", database);
let database_format_ctx = AstFormatContext::new(database_name);
let database_node = FormatTreeNode::new(database_format_ctx);
children.push(database_node);
}

let table_name = format!("Table {}", &stmt.table);
let table_format_ctx = AstFormatContext::new(table_name);
let table_node = FormatTreeNode::new(table_format_ctx);
children.push(table_node);

if let Some(limit) = &stmt.limit {
self.visit_show_limit(limit);
children.push(self.children.pop().unwrap());
}
let name = "ShowColumns".to_string();
let format_ctx = AstFormatContext::with_children(name, children.len());
let node = FormatTreeNode::with_children(format_ctx, children);
self.children.push(node);
}

fn visit_show_create_table(&mut self, stmt: &'ast ShowCreateTableStmt) {
self.visit_table_ref(&stmt.catalog, &stmt.database, &stmt.table);
let child = self.children.pop().unwrap();
Expand Down
52 changes: 52 additions & 0 deletions src/query/ast/src/ast/statements/columns.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2023 Datafuse Labs.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fmt::Display;
use std::fmt::Formatter;

use crate::ast::Identifier;
use crate::ast::ShowLimit;

#[derive(Debug, Clone, PartialEq)] // Columns
pub struct ShowColumnsStmt {
pub catalog: Option<Identifier>,
pub database: Option<Identifier>,
pub table: Identifier,
pub full: bool,
pub limit: Option<ShowLimit>,
}

impl Display for ShowColumnsStmt {
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
write!(f, "SHOW")?;
if self.full {
write!(f, " FULL")?;
}
write!(f, " COLUMNS FROM {}", self.table)?;

if let Some(database) = &self.database {
write!(f, " FROM ")?;
if let Some(catalog) = &self.catalog {
write!(f, "{catalog}.",)?;
}
write!(f, "{database}")?;
}

if let Some(limit) = &self.limit {
write!(f, " {limit}")?;
}

Ok(())
}
}
2 changes: 2 additions & 0 deletions src/query/ast/src/ast/statements/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

mod call;
mod catalog;
mod columns;
mod copy;
mod database;
mod explain;
Expand All @@ -33,6 +34,7 @@ mod view;

pub use call::*;
pub use catalog::*;
pub use columns::*;
pub use copy::*;
pub use database::*;
pub use explain::*;
Expand Down
3 changes: 3 additions & 0 deletions src/query/ast/src/ast/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ pub enum Statement {
OptimizeTable(OptimizeTableStmt),
AnalyzeTable(AnalyzeTableStmt),
ExistsTable(ExistsTableStmt),
// Columns
ShowColumns(ShowColumnsStmt),

// Views
CreateView(CreateViewStmt),
Expand Down Expand Up @@ -343,6 +345,7 @@ impl Display for Statement {
Statement::AlterDatabase(stmt) => write!(f, "{stmt}")?,
Statement::UseDatabase { database } => write!(f, "USE {database}")?,
Statement::ShowTables(stmt) => write!(f, "{stmt}")?,
Statement::ShowColumns(stmt) => write!(f, "{stmt}")?,
Statement::ShowCreateTable(stmt) => write!(f, "{stmt}")?,
Statement::DescribeTable(stmt) => write!(f, "{stmt}")?,
Statement::ShowTablesStatus(stmt) => write!(f, "{stmt}")?,
Expand Down
20 changes: 20 additions & 0 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,25 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
})
},
);
let show_columns = map(
rule! {
SHOW ~ FULL? ~ COLUMNS ~ ( FROM | IN ) ~ #ident ~ ((FROM | IN) ~ #period_separated_idents_1_to_2)? ~ #show_limit?
},
|(_, opt_full, _, _, table, ctl_db, limit)| {
let (catalog, database) = match ctl_db {
Some((_, (Some(c), d))) => (Some(c), Some(d)),
Some((_, (None, d))) => (None, Some(d)),
_ => (None, None),
};
Statement::ShowColumns(ShowColumnsStmt {
catalog,
database,
table,
full: opt_full.is_some(),
limit,
})
},
);
let show_create_table = map(
rule! {
SHOW ~ CREATE ~ TABLE ~ #period_separated_idents_1_to_3
Expand Down Expand Up @@ -1094,6 +1113,7 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
),
rule!(
#show_tables : "`SHOW [FULL] TABLES [FROM <database>] [<show_limit>]`"
| #show_columns : "`SHOW [FULL] COLUMNS FROM <table> [FROM|IN <catalog>.<database>] [<show_limit>]`"
| #show_create_table : "`SHOW CREATE TABLE [<database>.]<table>`"
| #describe_table : "`DESCRIBE [<database>.]<table>`"
| #show_fields : "`SHOW FIELDS FROM [<database>.]<table>`"
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ pub enum TokenKind {
CHAR,
#[token("COLUMN", ignore(ascii_case))]
COLUMN,
#[token("COLUMNS", ignore(ascii_case))]
COLUMNS,
#[token("CHARACTER", ignore(ascii_case))]
CHARACTER,
#[token("CONFLICT", ignore(ascii_case))]
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/visitors/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,8 @@ pub trait Visitor<'ast>: Sized {

fn visit_show_tables(&mut self, _stmt: &'ast ShowTablesStmt) {}

fn visit_show_columns(&mut self, _stmt: &'ast ShowColumnsStmt) {}

fn visit_show_create_table(&mut self, _stmt: &'ast ShowCreateTableStmt) {}

fn visit_describe_table(&mut self, _stmt: &'ast DescribeTableStmt) {}
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/visitors/visitor_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ pub trait VisitorMut: Sized {

fn visit_show_tables(&mut self, _stmt: &mut ShowTablesStmt) {}

fn visit_show_columns(&mut self, _stmt: &mut ShowColumnsStmt) {}

fn visit_show_create_table(&mut self, _stmt: &mut ShowCreateTableStmt) {}

fn visit_describe_table(&mut self, _stmt: &mut DescribeTableStmt) {}
Expand Down
1 change: 1 addition & 0 deletions src/query/ast/src/visitors/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ pub fn walk_statement<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Statem
Statement::AlterDatabase(stmt) => visitor.visit_alter_database(stmt),
Statement::UseDatabase { database } => visitor.visit_use_database(database),
Statement::ShowTables(stmt) => visitor.visit_show_tables(stmt),
Statement::ShowColumns(stmt) => visitor.visit_show_columns(stmt),
Statement::ShowCreateTable(stmt) => visitor.visit_show_create_table(stmt),
Statement::DescribeTable(stmt) => visitor.visit_describe_table(stmt),
Statement::ShowTablesStatus(stmt) => visitor.visit_show_tables_status(stmt),
Expand Down
1 change: 1 addition & 0 deletions src/query/ast/src/visitors/walk_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ pub fn walk_statement_mut<V: VisitorMut>(visitor: &mut V, statement: &mut Statem
Statement::AlterDatabase(stmt) => visitor.visit_alter_database(stmt),
Statement::UseDatabase { database } => visitor.visit_use_database(database),
Statement::ShowTables(stmt) => visitor.visit_show_tables(stmt),
Statement::ShowColumns(stmt) => visitor.visit_show_columns(stmt),
Statement::ShowCreateTable(stmt) => visitor.visit_show_create_table(stmt),
Statement::DescribeTable(stmt) => visitor.visit_describe_table(stmt),
Statement::ShowTablesStatus(stmt) => visitor.visit_show_tables_status(stmt),
Expand Down
3 changes: 3 additions & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ fn test_statement() {
r#"show full tables"#,
r#"show full tables from db"#,
r#"show full tables from ctl.db"#,
r#"show full columns in t in db"#,
r#"show columns in t from ctl.db"#,
r#"show full columns from t from db like 'id%'"#,
r#"show processlist;"#,
r#"show create table a.b;"#,
r#"show create table a.b format TabSeparatedWithNamesAndTypes;"#,
Expand Down
2 changes: 1 addition & 1 deletion src/query/ast/tests/it/testdata/statement-error.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ error:
--> SQL:1:6
|
1 | SHOW GRANT FOR ROLE role1;
| ^^^^^ expected `SETTINGS`, `STAGES`, `ENGINES`, `PROCESSLIST`, `METRICS`, `FUNCTIONS`, or 14 more ...
| ^^^^^ expected `SETTINGS`, `STAGES`, `ENGINES`, `PROCESSLIST`, `METRICS`, `FUNCTIONS`, or 15 more ...


---------- Input ----------
Expand Down
102 changes: 102 additions & 0 deletions src/query/ast/tests/it/testdata/statement.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,108 @@ ShowTables(
)


---------- Input ----------
show full columns in t in db
---------- Output ---------
SHOW FULL COLUMNS FROM t FROM db
---------- AST ------------
ShowColumns(
ShowColumnsStmt {
catalog: None,
database: Some(
Identifier {
name: "db",
quote: None,
span: Some(
26..28,
),
},
),
table: Identifier {
name: "t",
quote: None,
span: Some(
21..22,
),
},
full: true,
limit: None,
},
)


---------- Input ----------
show columns in t from ctl.db
---------- Output ---------
SHOW COLUMNS FROM t FROM ctl.db
---------- AST ------------
ShowColumns(
ShowColumnsStmt {
catalog: Some(
Identifier {
name: "ctl",
quote: None,
span: Some(
23..26,
),
},
),
database: Some(
Identifier {
name: "db",
quote: None,
span: Some(
27..29,
),
},
),
table: Identifier {
name: "t",
quote: None,
span: Some(
16..17,
),
},
full: false,
limit: None,
},
)


---------- Input ----------
show full columns from t from db like 'id%'
---------- Output ---------
SHOW FULL COLUMNS FROM t FROM db LIKE 'id%'
---------- AST ------------
ShowColumns(
ShowColumnsStmt {
catalog: None,
database: Some(
Identifier {
name: "db",
quote: None,
span: Some(
30..32,
),
},
),
table: Identifier {
name: "t",
quote: None,
span: Some(
23..24,
),
},
full: true,
limit: Some(
Like {
pattern: "id%",
},
),
},
)


---------- Input ----------
show processlist;
---------- Output ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ impl AccessChecker for ManagementModeAccess {
Some(ref v) => matches!(v,
RewriteKind::ShowDatabases
| RewriteKind::ShowTables
| RewriteKind::ShowColumns
| RewriteKind::ShowEngines
| RewriteKind::ShowSettings
| RewriteKind::ShowFunctions
Expand Down
2 changes: 2 additions & 0 deletions src/query/sql/src/planner/binder/binder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ impl<'a> Binder {
database: database.name.clone(),
}))
}
// Columns
Statement::ShowColumns(stmt) => self.bind_show_columns(bind_context, stmt).await?,
// Tables
Statement::ShowTables(stmt) => self.bind_show_tables(bind_context, stmt).await?,
Statement::ShowCreateTable(stmt) => self.bind_show_create_table(stmt).await?,
Expand Down
Loading