Skip to content

Commit c4dcf91

Browse files
feat: add show views & desc view (#14926)
* add show views & desc view * replace useless use of `format!` * fix error & add test case * rust fmt * add logic tests * add more logic tests * move desc view test case to package 05_ddl --------- Co-authored-by: TCeason <33082201+TCeason@users.noreply.github.com>
1 parent 83c68ae commit c4dcf91

File tree

23 files changed

+799
-1
lines changed

23 files changed

+799
-1
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,33 @@ impl<'ast> Visitor<'ast> for AstFormatVisitor {
17231723
self.children.push(node);
17241724
}
17251725

1726+
fn visit_show_views(&mut self, stmt: &'ast ShowViewsStmt) {
1727+
let mut children = Vec::new();
1728+
if let Some(database) = &stmt.database {
1729+
let database_name = format!("Database {}", database);
1730+
let database_format_ctx = AstFormatContext::new(database_name);
1731+
let database_node = FormatTreeNode::new(database_format_ctx);
1732+
children.push(database_node);
1733+
}
1734+
if let Some(limit) = &stmt.limit {
1735+
self.visit_show_limit(limit);
1736+
children.push(self.children.pop().unwrap());
1737+
}
1738+
let name = "ShowViews".to_string();
1739+
let format_ctx = AstFormatContext::with_children(name, children.len());
1740+
let node = FormatTreeNode::with_children(format_ctx, children);
1741+
self.children.push(node);
1742+
}
1743+
1744+
fn visit_describe_view(&mut self, stmt: &'ast DescribeViewStmt) {
1745+
self.visit_table_ref(&stmt.catalog, &stmt.database, &stmt.view);
1746+
let child = self.children.pop().unwrap();
1747+
let name = "DescribeView".to_string();
1748+
let format_ctx = AstFormatContext::with_children(name, 1);
1749+
let node = FormatTreeNode::with_children(format_ctx, vec![child]);
1750+
self.children.push(node);
1751+
}
1752+
17261753
fn visit_create_stream(&mut self, stmt: &'ast CreateStreamStmt) {
17271754
let mut children = Vec::new();
17281755
self.visit_table_ref(&stmt.catalog, &stmt.database, &stmt.stream);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ pub enum Statement {
154154
CreateView(CreateViewStmt),
155155
AlterView(AlterViewStmt),
156156
DropView(DropViewStmt),
157+
ShowViews(ShowViewsStmt),
158+
DescribeView(DescribeViewStmt),
157159

158160
// Streams
159161
CreateStream(CreateStreamStmt),
@@ -549,6 +551,8 @@ impl Display for Statement {
549551
Statement::CreateView(stmt) => write!(f, "{stmt}")?,
550552
Statement::AlterView(stmt) => write!(f, "{stmt}")?,
551553
Statement::DropView(stmt) => write!(f, "{stmt}")?,
554+
Statement::ShowViews(stmt) => write!(f, "{stmt}")?,
555+
Statement::DescribeView(stmt) => write!(f, "{stmt}")?,
552556
Statement::CreateStream(stmt) => write!(f, "{stmt}")?,
553557
Statement::DropStream(stmt) => write!(f, "{stmt}")?,
554558
Statement::ShowStreams(stmt) => write!(f, "{stmt}")?,

src/query/ast/src/ast/statements/view.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use crate::ast::write_comma_separated_list;
2323
use crate::ast::write_dot_separated_list;
2424
use crate::ast::Identifier;
2525
use crate::ast::Query;
26+
use crate::ast::ShowLimit;
2627

2728
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
2829
pub struct CreateViewStmt {
@@ -113,3 +114,58 @@ impl Display for DropViewStmt {
113114
)
114115
}
115116
}
117+
118+
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
119+
pub struct ShowViewsStmt {
120+
pub catalog: Option<Identifier>,
121+
pub database: Option<Identifier>,
122+
#[drive(skip)]
123+
pub full: bool,
124+
pub limit: Option<ShowLimit>,
125+
#[drive(skip)]
126+
pub with_history: bool,
127+
}
128+
129+
impl Display for ShowViewsStmt {
130+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
131+
write!(f, "SHOW")?;
132+
if self.full {
133+
write!(f, " FULL")?;
134+
}
135+
write!(f, " VIEWS")?;
136+
if self.with_history {
137+
write!(f, " HISTORY")?;
138+
}
139+
if let Some(database) = &self.database {
140+
write!(f, " FROM ")?;
141+
if let Some(catalog) = &self.catalog {
142+
write!(f, "{catalog}.",)?;
143+
}
144+
write!(f, "{database}")?;
145+
}
146+
if let Some(limit) = &self.limit {
147+
write!(f, " {limit}")?;
148+
}
149+
150+
Ok(())
151+
}
152+
}
153+
154+
#[derive(Debug, Clone, PartialEq, Eq, Drive, DriveMut)]
155+
pub struct DescribeViewStmt {
156+
pub catalog: Option<Identifier>,
157+
pub database: Option<Identifier>,
158+
pub view: Identifier,
159+
}
160+
161+
impl Display for DescribeViewStmt {
162+
fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
163+
write!(f, "DESCRIBE ")?;
164+
write_dot_separated_list(
165+
f,
166+
self.catalog
167+
.iter()
168+
.chain(self.database.iter().chain(Some(&self.view))),
169+
)
170+
}
171+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,10 @@ pub trait Visitor<'ast>: Sized {
572572

573573
fn visit_drop_view(&mut self, _stmt: &'ast DropViewStmt) {}
574574

575+
fn visit_show_views(&mut self, _stmt: &'ast ShowViewsStmt) {}
576+
577+
fn visit_describe_view(&mut self, _stmt: &'ast DescribeViewStmt) {}
578+
575579
fn visit_create_stream(&mut self, _stmt: &'ast CreateStreamStmt) {}
576580

577581
fn visit_drop_stream(&mut self, _stmt: &'ast DropStreamStmt) {}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,10 @@ pub trait VisitorMut: Sized {
585585

586586
fn visit_drop_view(&mut self, _stmt: &mut DropViewStmt) {}
587587

588+
fn visit_show_views(&mut self, _stmt: &mut ShowViewsStmt) {}
589+
590+
fn visit_describe_view(&mut self, _stmt: &mut DescribeViewStmt) {}
591+
588592
fn visit_create_stream(&mut self, _stmt: &mut CreateStreamStmt) {}
589593

590594
fn visit_drop_stream(&mut self, _stmt: &mut DropStreamStmt) {}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ pub fn walk_statement<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Statem
451451
Statement::CreateView(stmt) => visitor.visit_create_view(stmt),
452452
Statement::AlterView(stmt) => visitor.visit_alter_view(stmt),
453453
Statement::DropView(stmt) => visitor.visit_drop_view(stmt),
454+
Statement::ShowViews(stmt) => visitor.visit_show_views(stmt),
455+
Statement::DescribeView(stmt) => visitor.visit_describe_view(stmt),
454456
Statement::CreateStream(stmt) => visitor.visit_create_stream(stmt),
455457
Statement::DropStream(stmt) => visitor.visit_drop_stream(stmt),
456458
Statement::ShowStreams(stmt) => visitor.visit_show_streams(stmt),

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,8 @@ pub fn walk_statement_mut<V: VisitorMut>(visitor: &mut V, statement: &mut Statem
446446
Statement::CreateView(stmt) => visitor.visit_create_view(stmt),
447447
Statement::AlterView(stmt) => visitor.visit_alter_view(stmt),
448448
Statement::DropView(stmt) => visitor.visit_drop_view(stmt),
449+
Statement::ShowViews(stmt) => visitor.visit_show_views(stmt),
450+
Statement::DescribeView(stmt) => visitor.visit_describe_view(stmt),
449451
Statement::CreateStream(stmt) => visitor.visit_create_stream(stmt),
450452
Statement::DropStream(stmt) => visitor.visit_drop_stream(stmt),
451453
Statement::ShowStreams(stmt) => visitor.visit_show_streams(stmt),

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,37 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
918918
})
919919
},
920920
);
921+
let show_views = map(
922+
rule! {
923+
SHOW ~ FULL? ~ VIEWS ~ HISTORY? ~ ( ( FROM | IN ) ~ #dot_separated_idents_1_to_2 )? ~ #show_limit?
924+
},
925+
|(_, opt_full, _, opt_history, ctl_db, limit)| {
926+
let (catalog, database) = match ctl_db {
927+
Some((_, (Some(c), d))) => (Some(c), Some(d)),
928+
Some((_, (None, d))) => (None, Some(d)),
929+
_ => (None, None),
930+
};
931+
Statement::ShowViews(ShowViewsStmt {
932+
catalog,
933+
database,
934+
full: opt_full.is_some(),
935+
limit,
936+
with_history: opt_history.is_some(),
937+
})
938+
},
939+
);
940+
let describe_view = map(
941+
rule! {
942+
( DESC | DESCRIBE ) ~ VIEW ~ #dot_separated_idents_1_to_3
943+
},
944+
|(_, _, (catalog, database, view))| {
945+
Statement::DescribeView(DescribeViewStmt {
946+
catalog,
947+
database,
948+
view,
949+
})
950+
},
951+
);
921952

922953
let create_index = map_res(
923954
rule! {
@@ -2048,6 +2079,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
20482079
#show_tables : "`SHOW [FULL] TABLES [FROM <database>] [<show_limit>]`"
20492080
| #show_columns : "`SHOW [FULL] COLUMNS FROM <table> [FROM|IN <catalog>.<database>] [<show_limit>]`"
20502081
| #show_create_table : "`SHOW CREATE TABLE [<database>.]<table>`"
2082+
| #describe_view : "`DESCRIBE VIEW [<database>.]<view>`"
20512083
| #describe_table : "`DESCRIBE [<database>.]<table>`"
20522084
| #show_fields : "`SHOW FIELDS FROM [<database>.]<table>`"
20532085
| #show_tables_status : "`SHOW TABLES STATUS [FROM <database>] [<show_limit>]`"
@@ -2071,6 +2103,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
20712103
#create_view : "`CREATE [OR REPLACE] VIEW [IF NOT EXISTS] [<database>.]<view> [(<column>, ...)] AS SELECT ...`"
20722104
| #drop_view : "`DROP VIEW [IF EXISTS] [<database>.]<view>`"
20732105
| #alter_view : "`ALTER VIEW [<database>.]<view> [(<column>, ...)] AS SELECT ...`"
2106+
| #show_views : "`SHOW [FULL] VIEWS [FROM <database>] [<show_limit>]`"
20742107
| #stream_table
20752108
| #create_index: "`CREATE [OR REPLACE] AGGREGATING INDEX [IF NOT EXISTS] <index> AS SELECT ...`"
20762109
| #drop_index: "`DROP <index_type> INDEX [IF EXISTS] <index>`"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,8 @@ pub enum TokenKind {
11091109
VERBOSE,
11101110
#[token("VIEW", ignore(ascii_case))]
11111111
VIEW,
1112+
#[token("VIEWS", ignore(ascii_case))]
1113+
VIEWS,
11121114
#[token("VIRTUAL", ignore(ascii_case))]
11131115
VIRTUAL,
11141116
#[token("WHEN", ignore(ascii_case))]

src/query/ast/tests/it/parser.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ fn test_statement() {
142142
r#"create view v1(c1) as select number % 3 as a from numbers(1000);"#,
143143
r#"create or replace view v1(c1) as select number % 3 as a from numbers(1000);"#,
144144
r#"alter view v1(c2) as select number % 3 as a from numbers(1000);"#,
145+
r#"show views"#,
146+
r#"show views format TabSeparatedWithNamesAndTypes;"#,
147+
r#"show full views"#,
148+
r#"show full views from db"#,
149+
r#"show full views from ctl.db"#,
145150
r#"create stream test2.s1 on table test.t append_only = false;"#,
146151
r#"create stream if not exists test2.s2 on table test.t at (stream => test1.s1) comment = 'this is a stream';"#,
147152
r#"create or replace stream test2.s1 on table test.t append_only = false;"#,

0 commit comments

Comments
 (0)