Skip to content
This repository was archived by the owner on Dec 25, 2019. It is now read-only.

Commit b55f4c5

Browse files
authored
Merge pull request #19 from MaterializeInc/parse_show_objects_regex
Parse LIKE regex for Statement::ShowObjects
2 parents 5f22011 + 08f2cf3 commit b55f4c5

4 files changed

Lines changed: 29 additions & 6 deletions

File tree

src/ast/mod.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,10 @@ pub enum Statement {
620620
/// SHOW SOURCES;
621621
/// SHOW SINKS;
622622
/// ```
623-
ShowObjects { object_type: ObjectType },
623+
ShowObjects {
624+
object_type: ObjectType,
625+
like: Option<String>,
626+
},
624627
/// `SHOW COLUMNS`
625628
///
626629
/// Note: this is a MySQL-specific statement.
@@ -880,7 +883,7 @@ impl fmt::Display for Statement {
880883
write!(f, "{} = {}", variable, value)
881884
}
882885
Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable),
883-
Statement::ShowObjects { object_type } => {
886+
Statement::ShowObjects { object_type, like } => {
884887
use ObjectType::*;
885888
write!(
886889
f,
@@ -892,7 +895,11 @@ impl fmt::Display for Statement {
892895
Sink => "SINKS",
893896
Index => unreachable!(),
894897
}
895-
)
898+
)?;
899+
if let Some(like) = like {
900+
write!(f, " LIKE '{}'", value::escape_single_quote_string(like))?;
901+
}
902+
Ok(())
896903
}
897904
Statement::ShowColumns {
898905
extended,

src/ast/visit_macro.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ macro_rules! make_visitor {
505505
visit_show_variable(self, variable)
506506
}
507507

508-
fn visit_show_objects(&mut self, _object_type: ObjectType) {}
508+
fn visit_show_objects(&mut self, _object_type: ObjectType, _like: &Option<String>) {}
509509

510510
fn visit_show_columns(
511511
&mut self,
@@ -654,7 +654,7 @@ macro_rules! make_visitor {
654654
value,
655655
} => visitor.visit_set_variable(*local, variable, value),
656656
Statement::ShowVariable { variable } => visitor.visit_show_variable(variable),
657-
Statement::ShowObjects { object_type } => visitor.visit_show_objects(*object_type),
657+
Statement::ShowObjects { object_type, like } => visitor.visit_show_objects(*object_type, like),
658658
Statement::ShowColumns {
659659
extended,
660660
full,

src/parser.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1940,6 +1940,7 @@ impl Parser {
19401940
val
19411941
),
19421942
},
1943+
like: self.parse_like_filter()?,
19431944
})
19441945
} else if self.parse_keywords(vec!["CREATE", "VIEW"]) {
19451946
Ok(Statement::ShowCreateView {

tests/sqlparser_common.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,10 @@ fn parse_show_objects() {
19121912
let sql = format!("SHOW {}", s);
19131913
assert_eq!(
19141914
verified_stmt(&sql),
1915-
Statement::ShowObjects { object_type: *ot }
1915+
Statement::ShowObjects {
1916+
object_type: *ot,
1917+
like: None
1918+
}
19161919
)
19171920
}
19181921
}
@@ -2744,6 +2747,18 @@ fn parse_create_sources_with_like_regex() {
27442747
}
27452748
}
27462749

2750+
#[test]
2751+
fn parse_show_objects_with_like_regex() {
2752+
let sql = "SHOW TABLES LIKE '%foo%'";
2753+
match verified_stmt(sql) {
2754+
Statement::ShowObjects { object_type, like } => {
2755+
assert_eq!(like.unwrap(), "%foo%");
2756+
assert_eq!(ObjectType::Table, object_type);
2757+
}
2758+
_ => panic!("invalid SHOW OBJECTS statement"),
2759+
}
2760+
}
2761+
27472762
#[test]
27482763
fn parse_create_sink() {
27492764
let sql = "CREATE SINK foo FROM bar INTO 'baz' WITH (name = 'val')";

0 commit comments

Comments
 (0)