mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-19 22:40:40 +00:00
feat: impl show table status (#4303)
* feat: impl show table status * chore: style and comment * test: revert lz4 compression
This commit is contained in:
@@ -22,7 +22,7 @@ use crate::error::{
|
||||
use crate::parser::ParserContext;
|
||||
use crate::statements::show::{
|
||||
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowDatabases, ShowIndex, ShowKind, ShowStatus,
|
||||
ShowTables, ShowVariables,
|
||||
ShowTableStatus, ShowTables, ShowVariables,
|
||||
};
|
||||
use crate::statements::statement::Statement;
|
||||
|
||||
@@ -36,6 +36,14 @@ impl<'a> ParserContext<'a> {
|
||||
} else if self.matches_keyword(Keyword::TABLES) {
|
||||
self.parser.next_token();
|
||||
self.parse_show_tables(false)
|
||||
} else if self.matches_keyword(Keyword::TABLE) {
|
||||
self.parser.next_token();
|
||||
if self.matches_keyword(Keyword::STATUS) {
|
||||
self.parser.next_token();
|
||||
self.parse_show_table_status()
|
||||
} else {
|
||||
self.unsupported(self.peek_token_as_string())
|
||||
}
|
||||
} else if self.matches_keyword(Keyword::CHARSET) {
|
||||
self.parser.next_token();
|
||||
Ok(Statement::ShowCharset(self.parse_show_kind()?))
|
||||
@@ -342,6 +350,32 @@ impl<'a> ParserContext<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
fn parse_show_table_status(&mut self) -> Result<Statement> {
|
||||
let database = match self.parser.peek_token().token {
|
||||
Token::EOF | Token::SemiColon => {
|
||||
return Ok(Statement::ShowTableStatus(ShowTableStatus {
|
||||
kind: ShowKind::All,
|
||||
database: None,
|
||||
}));
|
||||
}
|
||||
|
||||
// SHOW TABLE STATUS [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::ShowTableStatus(ShowTableStatus {
|
||||
kind,
|
||||
database,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Parses `SHOW DATABASES` statement.
|
||||
pub fn parse_show_databases(&mut self, full: bool) -> Result<Statement> {
|
||||
let tok = self.parser.next_token().token;
|
||||
@@ -835,4 +869,41 @@ mod tests {
|
||||
Statement::ShowCharset(ShowKind::Like(_))
|
||||
));
|
||||
}
|
||||
|
||||
fn parse_show_table_status(sql: &str) -> ShowTableStatus {
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
let mut stmts = result.unwrap();
|
||||
assert_eq!(1, stmts.len());
|
||||
|
||||
match stmts.remove(0) {
|
||||
Statement::ShowTableStatus(stmt) => stmt,
|
||||
_ => panic!("Failed to parse show table status"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn test_show_table_status() {
|
||||
let sql = "SHOW TABLE STATUS";
|
||||
let stmt = parse_show_table_status(sql);
|
||||
assert!(stmt.database.is_none());
|
||||
assert_eq!(sql, stmt.to_string());
|
||||
|
||||
let sql = "SHOW TABLE STATUS IN test";
|
||||
let stmt = parse_show_table_status(sql);
|
||||
assert_eq!("test", stmt.database.as_ref().unwrap());
|
||||
assert_eq!(sql, stmt.to_string());
|
||||
|
||||
let sql = "SHOW TABLE STATUS LIKE '%monitor'";
|
||||
let stmt = parse_show_table_status(sql);
|
||||
assert!(stmt.database.is_none());
|
||||
assert!(matches!(stmt.kind, ShowKind::Like(_)));
|
||||
assert_eq!(sql, stmt.to_string());
|
||||
|
||||
let sql = "SHOW TABLE STATUS IN test WHERE Name = 'monitor'";
|
||||
let stmt = parse_show_table_status(sql);
|
||||
assert_eq!("test", stmt.database.as_ref().unwrap());
|
||||
assert!(matches!(stmt.kind, ShowKind::Where(_)));
|
||||
assert_eq!(sql, stmt.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +29,22 @@ pub enum ShowKind {
|
||||
impl Display for ShowKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ShowKind::All => write!(f, "ALL"),
|
||||
// The `All` is the default kind placeholder, not a valid statement.
|
||||
ShowKind::All => write!(f, ""),
|
||||
ShowKind::Like(ident) => write!(f, "LIKE {ident}"),
|
||||
ShowKind::Where(expr) => write!(f, "WHERE {expr}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! format_kind {
|
||||
($self: expr, $f: expr) => {
|
||||
if $self.kind != ShowKind::All {
|
||||
write!($f, " {}", &$self.kind)?;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// SQL structure for `SHOW DATABASES`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
|
||||
pub struct ShowDatabases {
|
||||
@@ -58,11 +67,12 @@ impl Display for ShowColumns {
|
||||
if self.full {
|
||||
write!(f, "FULL ")?;
|
||||
}
|
||||
write!(f, "COLUMNS IN {} ", &self.table)?;
|
||||
write!(f, "COLUMNS IN {}", &self.table)?;
|
||||
if let Some(database) = &self.database {
|
||||
write!(f, "IN {database} ")?;
|
||||
write!(f, " IN {database}")?;
|
||||
}
|
||||
write!(f, "{}", &self.kind)
|
||||
format_kind!(self, f);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,11 +86,13 @@ pub struct ShowIndex {
|
||||
|
||||
impl Display for ShowIndex {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "SHOW INDEX IN {} ", &self.table)?;
|
||||
write!(f, "SHOW INDEX IN {}", &self.table)?;
|
||||
if let Some(database) = &self.database {
|
||||
write!(f, "IN {database} ")?;
|
||||
write!(f, " IN {database}")?;
|
||||
}
|
||||
write!(f, "{}", &self.kind)
|
||||
format_kind!(self, f);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,13 +105,15 @@ impl ShowDatabases {
|
||||
|
||||
impl Display for ShowDatabases {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
let kind = &self.kind;
|
||||
|
||||
if self.full {
|
||||
write!(f, r#"SHOW FULL DATABASES {kind}"#)
|
||||
write!(f, "SHOW FULL DATABASES")?;
|
||||
} else {
|
||||
write!(f, r#"SHOW DATABASES {kind}"#)
|
||||
write!(f, "SHOW DATABASES")?;
|
||||
}
|
||||
|
||||
format_kind!(self, f);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,11 +131,33 @@ impl Display for ShowTables {
|
||||
if self.full {
|
||||
write!(f, "FULL ")?;
|
||||
}
|
||||
write!(f, "TABLES ")?;
|
||||
write!(f, "TABLES")?;
|
||||
if let Some(database) = &self.database {
|
||||
write!(f, "IN {database} ")?;
|
||||
write!(f, " IN {database}")?;
|
||||
}
|
||||
write!(f, "{}", &self.kind)
|
||||
format_kind!(self, f);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// SQL structure for `SHOW TABLE STATUS`.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Visit, VisitMut)]
|
||||
pub struct ShowTableStatus {
|
||||
pub kind: ShowKind,
|
||||
pub database: Option<String>,
|
||||
}
|
||||
|
||||
impl Display for ShowTableStatus {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "SHOW TABLE STATUS")?;
|
||||
if let Some(database) = &self.database {
|
||||
write!(f, " IN {database}")?;
|
||||
}
|
||||
|
||||
format_kind!(self, f);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -187,7 +223,7 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_kind_display() {
|
||||
assert_eq!("ALL", format!("{}", ShowKind::All));
|
||||
assert_eq!("", format!("{}", ShowKind::All));
|
||||
assert_eq!(
|
||||
"LIKE test",
|
||||
format!(
|
||||
@@ -348,7 +384,7 @@ SHOW CREATE TABLE monitor"#,
|
||||
let new_sql = format!("\n{}", show);
|
||||
assert_eq!(
|
||||
r#"
|
||||
SHOW INDEX IN t1 IN d1 ALL"#,
|
||||
SHOW INDEX IN t1 IN d1"#,
|
||||
&new_sql
|
||||
);
|
||||
}
|
||||
@@ -371,7 +407,7 @@ SHOW INDEX IN t1 IN d1 ALL"#,
|
||||
let new_sql = format!("\n{}", show);
|
||||
assert_eq!(
|
||||
r#"
|
||||
SHOW FULL COLUMNS IN t1 IN d1 ALL"#,
|
||||
SHOW FULL COLUMNS IN t1 IN d1"#,
|
||||
&new_sql
|
||||
);
|
||||
}
|
||||
@@ -394,7 +430,7 @@ SHOW FULL COLUMNS IN t1 IN d1 ALL"#,
|
||||
let new_sql = format!("\n{}", show);
|
||||
assert_eq!(
|
||||
r#"
|
||||
SHOW FULL TABLES IN d1 ALL"#,
|
||||
SHOW FULL TABLES IN d1"#,
|
||||
&new_sql
|
||||
);
|
||||
}
|
||||
@@ -414,7 +450,7 @@ SHOW FULL TABLES IN d1 ALL"#,
|
||||
let new_sql = format!("\n{}", show);
|
||||
assert_eq!(
|
||||
r#"
|
||||
SHOW FULL TABLES ALL"#,
|
||||
SHOW FULL TABLES"#,
|
||||
&new_sql
|
||||
);
|
||||
}
|
||||
@@ -437,7 +473,7 @@ SHOW FULL TABLES ALL"#,
|
||||
let new_sql = format!("\n{}", show);
|
||||
assert_eq!(
|
||||
r#"
|
||||
SHOW DATABASES ALL"#,
|
||||
SHOW DATABASES"#,
|
||||
&new_sql
|
||||
);
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ use crate::statements::query::Query;
|
||||
use crate::statements::set_variables::SetVariables;
|
||||
use crate::statements::show::{
|
||||
ShowColumns, ShowCreateFlow, ShowCreateTable, ShowDatabases, ShowIndex, ShowKind, ShowStatus,
|
||||
ShowTables, ShowVariables,
|
||||
ShowTableStatus, ShowTables, ShowVariables,
|
||||
};
|
||||
use crate::statements::tql::Tql;
|
||||
use crate::statements::truncate::TruncateTable;
|
||||
@@ -71,6 +71,8 @@ pub enum Statement {
|
||||
ShowDatabases(ShowDatabases),
|
||||
// SHOW TABLES
|
||||
ShowTables(ShowTables),
|
||||
// SHOW TABLE STATUS
|
||||
ShowTableStatus(ShowTableStatus),
|
||||
// SHOW COLUMNS
|
||||
ShowColumns(ShowColumns),
|
||||
// SHOW CHARSET or SHOW CHARACTER SET
|
||||
@@ -119,6 +121,7 @@ impl Display for Statement {
|
||||
Statement::Alter(s) => s.fmt(f),
|
||||
Statement::ShowDatabases(s) => s.fmt(f),
|
||||
Statement::ShowTables(s) => s.fmt(f),
|
||||
Statement::ShowTableStatus(s) => s.fmt(f),
|
||||
Statement::ShowColumns(s) => s.fmt(f),
|
||||
Statement::ShowIndex(s) => s.fmt(f),
|
||||
Statement::ShowCreateTable(s) => s.fmt(f),
|
||||
|
||||
Reference in New Issue
Block a user