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

Commit 55f78f8

Browse files
authored
Merge pull request #15 from umanwizard/flush
Parse `FLUSH` statements
2 parents a5b547a + 726ab5d commit 55f78f8

File tree

5 files changed

+58
-0
lines changed

5 files changed

+58
-0
lines changed

src/ast/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ pub enum Statement {
531531
url: String,
532532
with_options: Vec<SqlOption>,
533533
},
534+
/// `FLUSH SOURCE`
535+
FlushSource {
536+
name: ObjectName
537+
},
538+
/// `FLUSH ALL SOURCES`
539+
FlushAllSources,
534540
/// `CREATE VIEW`
535541
CreateView {
536542
/// View name
@@ -931,6 +937,8 @@ impl fmt::Display for Statement {
931937
}
932938
Statement::Tail { name } => write!(f, "TAIL {}", name),
933939
Statement::Explain { stage, query } => write!(f, "EXPLAIN {} FOR {}", stage, query),
940+
Statement::FlushSource { name } => write!(f, "FLUSH SOURCE {}", name),
941+
Statement::FlushAllSources => write!(f, "FLUSH ALL SOURCES"),
934942
}
935943
}
936944
}

src/ast/visit_macro.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ macro_rules! make_visitor {
562562
fn visit_explain(&mut self, stage: &'ast $($mut)* Stage, query: &'ast $($mut)* Query) {
563563
visit_explain(self, stage, query)
564564
}
565+
fn visit_flush(&mut self, name: &'ast $($mut)* ObjectName) {
566+
visit_flush(self, name)
567+
}
568+
fn visit_flush_all(&mut self) {
569+
visit_flush_all(self)
570+
}
565571
}
566572

567573
pub fn visit_statement<'ast, V: $name<'ast> + ?Sized>(visitor: &mut V, statement: &'ast $($mut)* Statement) {
@@ -665,6 +671,8 @@ macro_rules! make_visitor {
665671
visitor.visit_tail(name);
666672
}
667673
Statement::Explain { stage, query } => visitor.visit_explain(stage, query),
674+
Statement::FlushSource { name } => visitor.visit_flush(name),
675+
Statement::FlushAllSources => visitor.visit_flush_all(),
668676
}
669677
}
670678

@@ -1595,6 +1603,12 @@ macro_rules! make_visitor {
15951603
pub fn visit_explain<'ast, V: $name<'ast> + ?Sized>(visitor: &mut V, _stage: &'ast $($mut)* Stage, query: &'ast $($mut)* Query) {
15961604
visitor.visit_query(query);
15971605
}
1606+
1607+
pub fn visit_flush<'ast, V: $name<'ast> + ?Sized>(visitor: &mut V, name: &'ast $($mut)* ObjectName) {
1608+
visitor.visit_object_name(name);
1609+
}
1610+
1611+
pub fn visit_flush_all<'ast, V: $name<'ast> + ?Sized>(_visitor: &mut V) {}
15981612
}
15991613
}
16001614

src/dialect/keywords.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ define_keywords!(
180180
FIRST_VALUE,
181181
FLOAT,
182182
FLOOR,
183+
FLUSH,
183184
FOLLOWING,
184185
FOR,
185186
FOREIGN,

src/parser.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ impl Parser {
149149
name: self.parse_object_name()?,
150150
}),
151151
"EXPLAIN" => Ok(self.parse_explain()?),
152+
"FLUSH" => Ok(self.parse_flush()?),
152153
_ => parser_err!(format!(
153154
"Unexpected keyword {:?} at the beginning of a statement",
154155
w.to_string()
@@ -2383,6 +2384,23 @@ impl Parser {
23832384
query: Box::new(self.parse_query()?),
23842385
})
23852386
}
2387+
2388+
/// Parse a statement like `FLUSH SOURCE foo` or `FLUSH ALL SOURCES`,
2389+
/// assuming that the `FLUSH` token has already been consumed.
2390+
///
2391+
/// This causes the source (or sources) to downgrade their capability(-ies),
2392+
/// promising not to send any new data for the current timestamp
2393+
pub fn parse_flush(&mut self) -> Result<Statement, ParserError> {
2394+
if self.parse_keywords(vec!["ALL", "SOURCES"]) {
2395+
Ok(Statement::FlushAllSources)
2396+
} else if self.parse_keyword("SOURCE") {
2397+
Ok(Statement::FlushSource {
2398+
name: self.parse_object_name()?
2399+
})
2400+
} else {
2401+
self.expected("ALL SOURCES or SOURCE", self.peek_token())?
2402+
}
2403+
}
23862404
}
23872405

23882406
impl Word {

tests/sqlparser_common.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3320,6 +3320,23 @@ fn parse_explain() {
33203320
);
33213321
}
33223322

3323+
#[test]
3324+
fn parse_flush() {
3325+
let ast = verified_stmt("FLUSH ALL SOURCES");
3326+
assert_eq!(
3327+
ast,
3328+
Statement::FlushAllSources,
3329+
);
3330+
3331+
let ast = verified_stmt("FLUSH SOURCE foo");
3332+
assert_eq!(
3333+
ast,
3334+
Statement::FlushSource {
3335+
name: ObjectName(vec![Ident::new("foo")])
3336+
}
3337+
);
3338+
}
3339+
33233340
fn parse_sql_statements(sql: &str) -> Result<Vec<Statement>, ParserError> {
33243341
all_dialects().parse_sql_statements(sql)
33253342
}

0 commit comments

Comments
 (0)