feat: show database options (#4174)

* test: test create table with database ttl

* feat: show database options

* fix: comment

* chore: apply suggestion

Co-authored-by: Jeremyhi <jiachun_feng@proton.me>

* chore: fix CR comments and refactor

* chore: style

Co-authored-by: Weny Xu <wenymedia@gmail.com>

---------

Co-authored-by: Jeremyhi <jiachun_feng@proton.me>
Co-authored-by: Weny Xu <wenymedia@gmail.com>
This commit is contained in:
dennis zhuang
2024-06-19 21:21:58 -07:00
committed by GitHub
parent 4c3d4af127
commit 4306cba866
14 changed files with 263 additions and 44 deletions

View File

@@ -32,7 +32,7 @@ impl<'a> ParserContext<'a> {
/// todo(hl) support `show settings`/`show create`/`show users` etc.
pub(crate) fn parse_show(&mut self) -> Result<Statement> {
if self.consume_token("DATABASES") || self.consume_token("SCHEMAS") {
self.parse_show_databases()
self.parse_show_databases(false)
} else if self.matches_keyword(Keyword::TABLES) {
self.parser.next_token();
self.parse_show_tables(false)
@@ -75,6 +75,8 @@ impl<'a> ParserContext<'a> {
} else if self.consume_token("COLUMNS") || self.consume_token("FIELDS") {
// SHOW {COLUMNS | FIELDS}
self.parse_show_columns(true)
} else if self.consume_token("DATABASES") || self.consume_token("SCHEMAS") {
self.parse_show_databases(true)
} else {
self.unsupported(self.peek_token_as_string())
}
@@ -341,12 +343,13 @@ impl<'a> ParserContext<'a> {
}
/// Parses `SHOW DATABASES` statement.
pub fn parse_show_databases(&mut self) -> Result<Statement> {
pub fn parse_show_databases(&mut self, full: bool) -> Result<Statement> {
let tok = self.parser.next_token().token;
match &tok {
Token::EOF | Token::SemiColon => {
Ok(Statement::ShowDatabases(ShowDatabases::new(ShowKind::All)))
}
Token::EOF | Token::SemiColon => Ok(Statement::ShowDatabases(ShowDatabases::new(
ShowKind::All,
full,
))),
Token::Word(w) => match w.keyword {
Keyword::LIKE => Ok(Statement::ShowDatabases(ShowDatabases::new(
ShowKind::Like(self.parse_identifier().with_context(|_| {
@@ -356,6 +359,7 @@ impl<'a> ParserContext<'a> {
actual: tok.to_string(),
}
})?),
full,
))),
Keyword::WHERE => Ok(Statement::ShowDatabases(ShowDatabases::new(
ShowKind::Where(self.parser.parse_expr().with_context(|_| {
@@ -365,6 +369,7 @@ impl<'a> ParserContext<'a> {
actual: self.peek_token_as_string(),
}
})?),
full,
))),
_ => self.unsupported(self.peek_token_as_string()),
},
@@ -395,7 +400,39 @@ mod tests {
assert_matches!(
&stmts[0],
Statement::ShowDatabases(ShowDatabases {
kind: ShowKind::All
kind: ShowKind::All,
full: false,
})
);
}
#[test]
pub fn test_show_full_databases() {
let sql = "SHOW FULL DATABASES";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let stmts = result.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(
&stmts[0],
Statement::ShowDatabases(ShowDatabases {
kind: ShowKind::All,
full: true,
})
);
let sql = "SHOW FULL DATABASES LIKE 'test%'";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
let stmts = result.unwrap();
assert_eq!(1, stmts.len());
assert_matches!(
&stmts[0],
Statement::ShowDatabases(ShowDatabases {
kind: ShowKind::Like(_),
full: true,
})
);
}
@@ -414,7 +451,8 @@ mod tests {
kind: ShowKind::Like(sqlparser::ast::Ident {
value: _,
quote_style: None,
})
}),
..
})
);
}
@@ -434,7 +472,8 @@ mod tests {
left: _,
right: _,
op: sqlparser::ast::BinaryOperator::Or,
})
}),
..
})
);
}

View File

@@ -40,6 +40,7 @@ impl Display for ShowKind {
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
pub struct ShowDatabases {
pub kind: ShowKind,
pub full: bool,
}
/// The SQL `SHOW COLUMNS` statement
@@ -85,15 +86,20 @@ impl Display for ShowIndex {
impl ShowDatabases {
/// Creates a statement for `SHOW DATABASES`
pub fn new(kind: ShowKind) -> Self {
ShowDatabases { kind }
pub fn new(kind: ShowKind, full: bool) -> Self {
ShowDatabases { kind, full }
}
}
impl Display for ShowDatabases {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = &self.kind;
write!(f, r#"SHOW DATABASES {kind}"#)
if self.full {
write!(f, r#"SHOW FULL DATABASES {kind}"#)
} else {
write!(f, r#"SHOW DATABASES {kind}"#)
}
}
}