Skip to content

Commit 2c4ec23

Browse files
authored
Merge pull request #10726 from TCeason/ISSUE-10698
feat(query): support show columns query
2 parents 9cbc23b + 82e4ea2 commit 2c4ec23

File tree

21 files changed

+390
-2
lines changed

21 files changed

+390
-2
lines changed

src/query/ast/src/ast/format/ast_format.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,30 @@ impl<'ast> Visitor<'ast> for AstFormatVisitor {
11301130
self.children.push(node);
11311131
}
11321132

1133+
fn visit_show_columns(&mut self, stmt: &'ast ShowColumnsStmt) {
1134+
let mut children = Vec::new();
1135+
if let Some(database) = &stmt.database {
1136+
let database_name = format!("Database {}", database);
1137+
let database_format_ctx = AstFormatContext::new(database_name);
1138+
let database_node = FormatTreeNode::new(database_format_ctx);
1139+
children.push(database_node);
1140+
}
1141+
1142+
let table_name = format!("Table {}", &stmt.table);
1143+
let table_format_ctx = AstFormatContext::new(table_name);
1144+
let table_node = FormatTreeNode::new(table_format_ctx);
1145+
children.push(table_node);
1146+
1147+
if let Some(limit) = &stmt.limit {
1148+
self.visit_show_limit(limit);
1149+
children.push(self.children.pop().unwrap());
1150+
}
1151+
let name = "ShowColumns".to_string();
1152+
let format_ctx = AstFormatContext::with_children(name, children.len());
1153+
let node = FormatTreeNode::with_children(format_ctx, children);
1154+
self.children.push(node);
1155+
}
1156+
11331157
fn visit_show_create_table(&mut self, stmt: &'ast ShowCreateTableStmt) {
11341158
self.visit_table_ref(&stmt.catalog, &stmt.database, &stmt.table);
11351159
let child = self.children.pop().unwrap();
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::fmt::Display;
16+
use std::fmt::Formatter;
17+
18+
use crate::ast::Identifier;
19+
use crate::ast::ShowLimit;
20+
21+
#[derive(Debug, Clone, PartialEq)] // Columns
22+
pub struct ShowColumnsStmt {
23+
pub catalog: Option<Identifier>,
24+
pub database: Option<Identifier>,
25+
pub table: Identifier,
26+
pub full: bool,
27+
pub limit: Option<ShowLimit>,
28+
}
29+
30+
impl Display for ShowColumnsStmt {
31+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
32+
write!(f, "SHOW")?;
33+
if self.full {
34+
write!(f, " FULL")?;
35+
}
36+
write!(f, " COLUMNS FROM {}", self.table)?;
37+
38+
if let Some(database) = &self.database {
39+
write!(f, " FROM ")?;
40+
if let Some(catalog) = &self.catalog {
41+
write!(f, "{catalog}.",)?;
42+
}
43+
write!(f, "{database}")?;
44+
}
45+
46+
if let Some(limit) = &self.limit {
47+
write!(f, " {limit}")?;
48+
}
49+
50+
Ok(())
51+
}
52+
}

src/query/ast/src/ast/statements/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
mod call;
1616
mod catalog;
17+
mod columns;
1718
mod copy;
1819
mod database;
1920
mod explain;
@@ -33,6 +34,7 @@ mod view;
3334

3435
pub use call::*;
3536
pub use catalog::*;
37+
pub use columns::*;
3638
pub use copy::*;
3739
pub use database::*;
3840
pub use explain::*;

src/query/ast/src/ast/statements/statement.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ pub enum Statement {
114114
OptimizeTable(OptimizeTableStmt),
115115
AnalyzeTable(AnalyzeTableStmt),
116116
ExistsTable(ExistsTableStmt),
117+
// Columns
118+
ShowColumns(ShowColumnsStmt),
117119

118120
// Views
119121
CreateView(CreateViewStmt),
@@ -343,6 +345,7 @@ impl Display for Statement {
343345
Statement::AlterDatabase(stmt) => write!(f, "{stmt}")?,
344346
Statement::UseDatabase { database } => write!(f, "USE {database}")?,
345347
Statement::ShowTables(stmt) => write!(f, "{stmt}")?,
348+
Statement::ShowColumns(stmt) => write!(f, "{stmt}")?,
346349
Statement::ShowCreateTable(stmt) => write!(f, "{stmt}")?,
347350
Statement::DescribeTable(stmt) => write!(f, "{stmt}")?,
348351
Statement::ShowTablesStatus(stmt) => write!(f, "{stmt}")?,

src/query/ast/src/parser/statement.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,25 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
397397
})
398398
},
399399
);
400+
let show_columns = map(
401+
rule! {
402+
SHOW ~ FULL? ~ COLUMNS ~ ( FROM | IN ) ~ #ident ~ ((FROM | IN) ~ #period_separated_idents_1_to_2)? ~ #show_limit?
403+
},
404+
|(_, opt_full, _, _, table, ctl_db, limit)| {
405+
let (catalog, database) = match ctl_db {
406+
Some((_, (Some(c), d))) => (Some(c), Some(d)),
407+
Some((_, (None, d))) => (None, Some(d)),
408+
_ => (None, None),
409+
};
410+
Statement::ShowColumns(ShowColumnsStmt {
411+
catalog,
412+
database,
413+
table,
414+
full: opt_full.is_some(),
415+
limit,
416+
})
417+
},
418+
);
400419
let show_create_table = map(
401420
rule! {
402421
SHOW ~ CREATE ~ TABLE ~ #period_separated_idents_1_to_3
@@ -1094,6 +1113,7 @@ pub fn statement(i: Input) -> IResult<StatementMsg> {
10941113
),
10951114
rule!(
10961115
#show_tables : "`SHOW [FULL] TABLES [FROM <database>] [<show_limit>]`"
1116+
| #show_columns : "`SHOW [FULL] COLUMNS FROM <table> [FROM|IN <catalog>.<database>] [<show_limit>]`"
10971117
| #show_create_table : "`SHOW CREATE TABLE [<database>.]<table>`"
10981118
| #describe_table : "`DESCRIBE [<database>.]<table>`"
10991119
| #show_fields : "`SHOW FIELDS FROM [<database>.]<table>`"

src/query/ast/src/parser/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ pub enum TokenKind {
315315
CHAR,
316316
#[token("COLUMN", ignore(ascii_case))]
317317
COLUMN,
318+
#[token("COLUMNS", ignore(ascii_case))]
319+
COLUMNS,
318320
#[token("CHARACTER", ignore(ascii_case))]
319321
CHARACTER,
320322
#[token("CONFLICT", ignore(ascii_case))]

src/query/ast/src/visitors/visitor.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@ pub trait Visitor<'ast>: Sized {
404404

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

407+
fn visit_show_columns(&mut self, _stmt: &'ast ShowColumnsStmt) {}
408+
407409
fn visit_show_create_table(&mut self, _stmt: &'ast ShowCreateTableStmt) {}
408410

409411
fn visit_describe_table(&mut self, _stmt: &'ast DescribeTableStmt) {}

src/query/ast/src/visitors/visitor_mut.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ pub trait VisitorMut: Sized {
410410

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

413+
fn visit_show_columns(&mut self, _stmt: &mut ShowColumnsStmt) {}
414+
413415
fn visit_show_create_table(&mut self, _stmt: &mut ShowCreateTableStmt) {}
414416

415417
fn visit_describe_table(&mut self, _stmt: &mut DescribeTableStmt) {}

src/query/ast/src/visitors/walk.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ pub fn walk_statement<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Statem
344344
Statement::AlterDatabase(stmt) => visitor.visit_alter_database(stmt),
345345
Statement::UseDatabase { database } => visitor.visit_use_database(database),
346346
Statement::ShowTables(stmt) => visitor.visit_show_tables(stmt),
347+
Statement::ShowColumns(stmt) => visitor.visit_show_columns(stmt),
347348
Statement::ShowCreateTable(stmt) => visitor.visit_show_create_table(stmt),
348349
Statement::DescribeTable(stmt) => visitor.visit_describe_table(stmt),
349350
Statement::ShowTablesStatus(stmt) => visitor.visit_show_tables_status(stmt),

src/query/ast/src/visitors/walk_mut.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ pub fn walk_statement_mut<V: VisitorMut>(visitor: &mut V, statement: &mut Statem
344344
Statement::AlterDatabase(stmt) => visitor.visit_alter_database(stmt),
345345
Statement::UseDatabase { database } => visitor.visit_use_database(database),
346346
Statement::ShowTables(stmt) => visitor.visit_show_tables(stmt),
347+
Statement::ShowColumns(stmt) => visitor.visit_show_columns(stmt),
347348
Statement::ShowCreateTable(stmt) => visitor.visit_show_create_table(stmt),
348349
Statement::DescribeTable(stmt) => visitor.visit_describe_table(stmt),
349350
Statement::ShowTablesStatus(stmt) => visitor.visit_show_tables_status(stmt),

0 commit comments

Comments
 (0)