Skip to content

Commit 0e68cee

Browse files
authored
Support SHOW $foo (where foo is TABLES, SOURCES, VIEWS, or SINKS)
1 parent 9aac761 commit 0e68cee

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

src/ast/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,15 @@ pub enum Statement {
457457
/// ```
458458
/// Limitations: doesn't support `LIKE`, `WHERE` or `SHOW COLUMNS FROM mytable FROM mydb;`
459459
ShowColumns { table_name: ObjectName },
460+
/// SHOW <object>S
461+
///
462+
/// ```sql
463+
/// SHOW TABLES;
464+
/// SHOW VIEWS;
465+
/// SHOW SOURCES;
466+
/// SHOW SINKS;
467+
/// ```
468+
Show { object_type: ObjectType },
460469
}
461470

462471
impl fmt::Display for Statement {
@@ -662,6 +671,19 @@ impl fmt::Display for Statement {
662671
write!(f, "ROLLBACK{}", if *chain { " AND CHAIN" } else { "" },)
663672
}
664673
Statement::Peek { name } => write!(f, "PEEK {}", name),
674+
Statement::Show { object_type } => {
675+
use ObjectType::*;
676+
write!(
677+
f,
678+
"SHOW {}",
679+
match object_type {
680+
Table => "TABLES",
681+
View => "VIEWS",
682+
Source => "SOURCES",
683+
Sink => "SINKS",
684+
}
685+
)
686+
}
665687
Statement::ShowColumns { table_name } => write!(f, "SHOW COLUMNS FROM {}", table_name),
666688
Statement::Tail { name } => write!(f, "TAIL {}", name),
667689
}
@@ -781,7 +803,7 @@ impl FromStr for FileFormat {
781803
}
782804
}
783805

784-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
806+
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy)]
785807
pub enum ObjectType {
786808
Table,
787809
View,

src/ast/visit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,8 @@ pub trait Visit<'ast> {
469469
visit_show_columns(self, name)
470470
}
471471

472+
fn visit_show(&mut self, _object_type: ObjectType) {}
473+
472474
fn visit_tail(&mut self, name: &'ast ObjectName) {
473475
visit_tail(self, name)
474476
}
@@ -546,6 +548,7 @@ pub fn visit_statement<'ast, V: Visit<'ast> + ?Sized>(visitor: &mut V, statement
546548
Statement::Peek { name } => {
547549
visitor.visit_peek(name);
548550
}
551+
Statement::Show { object_type } => visitor.visit_show(*object_type),
549552
Statement::ShowColumns { table_name } => visitor.visit_show_columns(table_name),
550553
Statement::Tail { name } => {
551554
visitor.visit_tail(name);

src/dialect/keywords.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,11 @@ define_keywords!(
341341
SHOW,
342342
SIMILAR,
343343
SINK,
344+
SINKS,
344345
SMALLINT,
345346
SOME,
346347
SOURCE,
348+
SOURCES,
347349
SPECIFIC,
348350
SPECIFICTYPE,
349351
SQL,
@@ -367,6 +369,7 @@ define_keywords!(
367369
SYSTEM_TIME,
368370
SYSTEM_USER,
369371
TABLE,
372+
TABLES,
370373
TABLESAMPLE,
371374
TAIL,
372375
TEXT,
@@ -410,6 +413,7 @@ define_keywords!(
410413
VARYING,
411414
VERSIONING,
412415
VIEW,
416+
VIEWS,
413417
WHEN,
414418
WHENEVER,
415419
WHERE,

src/parser.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,13 @@ impl Parser {
16681668
if self.parse_keyword("COLUMNS") {
16691669
self.parse_show_columns()
16701670
} else {
1671-
self.expected("COLUMNS", self.peek_token())
1671+
self.parse_show_objects().ok_or_else(|| {
1672+
self.expected::<()>(
1673+
"One of {COLUMNS, TABLES, VIEWS, SOURCES, SINKS}",
1674+
self.peek_token(),
1675+
)
1676+
.unwrap_err()
1677+
})
16721678
}
16731679
}
16741680

@@ -1683,6 +1689,22 @@ impl Parser {
16831689
})
16841690
}
16851691

1692+
fn parse_show_objects(&mut self) -> Option<Statement> {
1693+
let object_type = self
1694+
.parse_one_of_keywords(&["SOURCES", "VIEWS", "SINKS", "TABLES"])
1695+
.map(|s| match s {
1696+
"SOURCES" => ObjectType::Source,
1697+
"VIEWS" => ObjectType::View,
1698+
"SINKS" => ObjectType::Sink,
1699+
"TABLES" => ObjectType::Table,
1700+
val => panic!(
1701+
"`parse_one_of_keywords` returned an impossible value: {}",
1702+
val
1703+
),
1704+
})?;
1705+
Some(Statement::Show { object_type })
1706+
}
1707+
16861708
pub fn parse_table_and_joins(&mut self) -> Result<TableWithJoins, ParserError> {
16871709
let relation = self.parse_table_factor()?;
16881710

tests/sqlparser_common.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,21 @@ fn parse_show_columns() {
14811481
}
14821482
}
14831483

1484+
#[test]
1485+
fn parse_show_objects() {
1486+
let trials = [
1487+
("SOURCES", ObjectType::Source),
1488+
("VIEWS", ObjectType::View),
1489+
("TABLES", ObjectType::Table),
1490+
("SINKS", ObjectType::Sink),
1491+
];
1492+
1493+
for (s, ot) in &trials {
1494+
let sql = format!("SHOW {}", s);
1495+
assert_eq!(verified_stmt(&sql), Statement::Show { object_type: *ot })
1496+
}
1497+
}
1498+
14841499
#[test]
14851500
fn parse_simple_case_expr() {
14861501
// ANSI calls a CASE expression with an operand "<simple case>"

0 commit comments

Comments
 (0)