feat: FLOWS table in information_schema&SHOW FLOWS (#4386)

* feat(WIP): flow info table

refactor: better err handling&log

feat: add flow metadata to info schema provider

feat(WIP): info_schema.flows

feat: info_schema.flows table

* fix: err after rebase

* fix: wrong comparsion op

* feat: SHOW FLOWS&tests

* refactor: per review

* chore: unused

* refactor: json error

* chore: per review

* test: sqlness

* chore: rm inline error

* refactor: per review
This commit is contained in:
discord9
2024-07-19 17:29:36 +08:00
committed by GitHub
parent ce900e850a
commit 9fa9156bde
24 changed files with 690 additions and 21 deletions

View File

@@ -21,8 +21,8 @@ use crate::error::{
};
use crate::parser::ParserContext;
use crate::statements::show::{
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowIndex,
ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowFlows,
ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
};
use crate::statements::statement::Statement;
@@ -46,6 +46,8 @@ impl<'a> ParserContext<'a> {
}
} else if self.consume_token("VIEWS") {
self.parse_show_views()
} else if self.consume_token("FLOWS") {
self.parse_show_flows()
} else if self.matches_keyword(Keyword::CHARSET) {
self.parser.next_token();
Ok(Statement::ShowCharset(self.parse_show_kind()?))
@@ -442,7 +444,7 @@ impl<'a> ParserContext<'a> {
}));
}
// SHOW VIEWS [in | FROM] [DATABASE]
// SHOW FLOWS [in | FROM] [DATABASE]
Token::Word(w) => match w.keyword {
Keyword::IN | Keyword::FROM => self.parse_db_name()?,
_ => None,
@@ -454,6 +456,28 @@ impl<'a> ParserContext<'a> {
Ok(Statement::ShowViews(ShowViews { kind, database }))
}
fn parse_show_flows(&mut self) -> Result<Statement> {
let database = match self.parser.peek_token().token {
Token::EOF | Token::SemiColon => {
return Ok(Statement::ShowFlows(ShowFlows {
kind: ShowKind::All,
database: None,
}));
}
// SHOW FLOWS [in | FROM] [DATABASE]
Token::Word(w) => match w.keyword {
Keyword::IN | Keyword::FROM => self.parse_db_name()?,
_ => None,
},
_ => None,
};
let kind = self.parse_show_kind()?;
Ok(Statement::ShowFlows(ShowFlows { kind, database }))
}
}
#[cfg(test)]
@@ -1000,4 +1024,38 @@ mod tests {
);
assert_eq!(sql, stmts[0].to_string());
}
#[test]
pub fn test_show_flows() {
let sql = "SHOW FLOWS";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let stmts = result.unwrap();
assert_eq!(1, stmts.len());
assert_eq!(
stmts[0],
Statement::ShowFlows(ShowFlows {
kind: ShowKind::All,
database: None,
})
);
assert_eq!(sql, stmts[0].to_string());
}
#[test]
pub fn test_show_flows_in_db() {
let sql = "SHOW FLOWS IN d1";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let stmts = result.unwrap();
assert_eq!(1, stmts.len());
assert_eq!(
stmts[0],
Statement::ShowFlows(ShowFlows {
kind: ShowKind::All,
database: Some("d1".to_string()),
})
);
assert_eq!(sql, stmts[0].to_string());
}
}

View File

@@ -187,6 +187,25 @@ impl Display for ShowCreateFlow {
}
}
/// SQL structure for `SHOW FLOWS`.
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct ShowFlows {
pub kind: ShowKind,
pub database: Option<String>,
}
impl Display for ShowFlows {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "SHOW FLOWS")?;
if let Some(database) = &self.database {
write!(f, " IN {database}")?;
}
format_kind!(self, f);
Ok(())
}
}
/// SQL structure for `SHOW CREATE VIEW`.
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct ShowCreateView {
@@ -535,6 +554,49 @@ SHOW VIEWS"#,
}
}
#[test]
fn test_display_show_flows() {
let sql = r"show flows in d1;";
let stmts: Vec<Statement> =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::ShowFlows { .. });
match &stmts[0] {
Statement::ShowFlows(show) => {
let new_sql = format!("\n{}", show);
assert_eq!(
r#"
SHOW FLOWS IN d1"#,
&new_sql
);
}
_ => {
unreachable!();
}
}
let sql = r"show flows;";
let stmts: Vec<Statement> =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(&stmts[0], Statement::ShowFlows { .. });
match &stmts[0] {
Statement::ShowFlows(show) => {
let new_sql = format!("\n{}", show);
assert_eq!(
r#"
SHOW FLOWS"#,
&new_sql
);
}
_ => {
unreachable!("{:?}", &stmts[0]);
}
}
}
#[test]
fn test_display_show_databases() {
let sql = r"show databases;";

View File

@@ -31,8 +31,8 @@ use crate::statements::insert::Insert;
use crate::statements::query::Query;
use crate::statements::set_variables::SetVariables;
use crate::statements::show::{
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowIndex,
ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowCreateView, ShowDatabases, ShowFlows,
ShowIndex, ShowKind, ShowStatus, ShowTableStatus, ShowTables, ShowVariables, ShowViews,
};
use crate::statements::tql::Tql;
use crate::statements::truncate::TruncateTable;
@@ -87,6 +87,8 @@ pub enum Statement {
ShowCreateTable(ShowCreateTable),
// SHOW CREATE FLOW
ShowCreateFlow(ShowCreateFlow),
/// SHOW FLOWS
ShowFlows(ShowFlows),
// SHOW CREATE VIEW
ShowCreateView(ShowCreateView),
// SHOW STATUS
@@ -133,6 +135,7 @@ impl Display for Statement {
Statement::ShowIndex(s) => s.fmt(f),
Statement::ShowCreateTable(s) => s.fmt(f),
Statement::ShowCreateFlow(s) => s.fmt(f),
Statement::ShowFlows(s) => s.fmt(f),
Statement::ShowCreateView(s) => s.fmt(f),
Statement::ShowViews(s) => s.fmt(f),
Statement::ShowStatus(s) => s.fmt(f),