mirror of
https://github.com/GreptimeTeam/greptimedb.git
synced 2026-05-23 16:30:39 +00:00
feat: impl show collation and show charset statements (#3753)
* feat: impl show collation and show charset statements * docs: add api docs
This commit is contained in:
@@ -31,11 +31,26 @@ impl<'a> ParserContext<'a> {
|
||||
if self.consume_token("DATABASES") || self.consume_token("SCHEMAS") {
|
||||
self.parse_show_databases()
|
||||
} else if self.matches_keyword(Keyword::TABLES) {
|
||||
let _ = self.parser.next_token();
|
||||
self.parser.next_token();
|
||||
self.parse_show_tables(false)
|
||||
} else if self.matches_keyword(Keyword::CHARSET) {
|
||||
self.parser.next_token();
|
||||
Ok(Statement::ShowCharset(self.parse_show_kind()?))
|
||||
} else if self.matches_keyword(Keyword::CHARACTER) {
|
||||
self.parser.next_token();
|
||||
|
||||
if self.matches_keyword(Keyword::SET) {
|
||||
self.parser.next_token();
|
||||
Ok(Statement::ShowCharset(self.parse_show_kind()?))
|
||||
} else {
|
||||
self.unsupported(self.peek_token_as_string())
|
||||
}
|
||||
} else if self.matches_keyword(Keyword::COLLATION) {
|
||||
self.parser.next_token();
|
||||
Ok(Statement::ShowCollation(self.parse_show_kind()?))
|
||||
} else if self.matches_keyword(Keyword::COLUMNS) || self.matches_keyword(Keyword::FIELDS) {
|
||||
// SHOW {COLUMNS | FIELDS}
|
||||
let _ = self.parser.next_token();
|
||||
self.parser.next_token();
|
||||
self.parse_show_columns(false)
|
||||
} else if self.consume_token("INDEX")
|
||||
|| self.consume_token("INDEXES")
|
||||
@@ -92,7 +107,7 @@ impl<'a> ParserContext<'a> {
|
||||
}
|
||||
|
||||
fn parse_show_table_name(&mut self) -> Result<String> {
|
||||
let _ = self.parser.next_token();
|
||||
self.parser.next_token();
|
||||
let table_name = self
|
||||
.parse_object_name()
|
||||
.with_context(|_| error::UnexpectedSnafu {
|
||||
@@ -115,7 +130,7 @@ impl<'a> ParserContext<'a> {
|
||||
}
|
||||
|
||||
fn parse_db_name(&mut self) -> Result<Option<String>> {
|
||||
let _ = self.parser.next_token();
|
||||
self.parser.next_token();
|
||||
let db_name = self
|
||||
.parse_object_name()
|
||||
.with_context(|_| error::UnexpectedSnafu {
|
||||
@@ -172,34 +187,7 @@ impl<'a> ParserContext<'a> {
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let kind = match self.parser.peek_token().token {
|
||||
Token::EOF | Token::SemiColon => ShowKind::All,
|
||||
// SHOW COLUMNS [WHERE | LIKE] [EXPR]
|
||||
Token::Word(w) => match w.keyword {
|
||||
Keyword::LIKE => {
|
||||
let _ = self.parser.next_token();
|
||||
ShowKind::Like(self.parse_identifier().with_context(|_| {
|
||||
error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "LIKE",
|
||||
actual: self.peek_token_as_string(),
|
||||
}
|
||||
})?)
|
||||
}
|
||||
Keyword::WHERE => {
|
||||
let _ = self.parser.next_token();
|
||||
ShowKind::Where(self.parser.parse_expr().with_context(|_| {
|
||||
error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "some valid expression",
|
||||
actual: self.peek_token_as_string(),
|
||||
}
|
||||
})?)
|
||||
}
|
||||
_ => return self.unsupported(self.peek_token_as_string()),
|
||||
},
|
||||
_ => return self.unsupported(self.peek_token_as_string()),
|
||||
};
|
||||
let kind = self.parse_show_kind()?;
|
||||
|
||||
Ok(Statement::ShowColumns(ShowColumns {
|
||||
kind,
|
||||
@@ -209,6 +197,36 @@ impl<'a> ParserContext<'a> {
|
||||
}))
|
||||
}
|
||||
|
||||
fn parse_show_kind(&mut self) -> Result<ShowKind> {
|
||||
match self.parser.peek_token().token {
|
||||
Token::EOF | Token::SemiColon => Ok(ShowKind::All),
|
||||
Token::Word(w) => match w.keyword {
|
||||
Keyword::LIKE => {
|
||||
self.parser.next_token();
|
||||
Ok(ShowKind::Like(self.parse_identifier().with_context(
|
||||
|_| error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "LIKE",
|
||||
actual: self.peek_token_as_string(),
|
||||
},
|
||||
)?))
|
||||
}
|
||||
Keyword::WHERE => {
|
||||
self.parser.next_token();
|
||||
Ok(ShowKind::Where(self.parser.parse_expr().with_context(
|
||||
|_| error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "some valid expression",
|
||||
actual: self.peek_token_as_string(),
|
||||
},
|
||||
)?))
|
||||
}
|
||||
_ => self.unsupported(self.peek_token_as_string()),
|
||||
},
|
||||
_ => self.unsupported(self.peek_token_as_string()),
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_show_index(&mut self) -> Result<Statement> {
|
||||
let table = match self.parser.peek_token().token {
|
||||
// SHOW INDEX {in | FROM} TABLE
|
||||
@@ -248,7 +266,7 @@ impl<'a> ParserContext<'a> {
|
||||
// SHOW INDEX [WHERE] [EXPR]
|
||||
Token::Word(w) => match w.keyword {
|
||||
Keyword::WHERE => {
|
||||
let _ = self.parser.next_token();
|
||||
self.parser.next_token();
|
||||
ShowKind::Where(self.parser.parse_expr().with_context(|_| {
|
||||
error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
@@ -288,34 +306,7 @@ impl<'a> ParserContext<'a> {
|
||||
_ => None,
|
||||
};
|
||||
|
||||
let kind = match self.parser.peek_token().token {
|
||||
Token::EOF | Token::SemiColon => ShowKind::All,
|
||||
// SHOW TABLES [WHERE | LIKE] [EXPR]
|
||||
Token::Word(w) => match w.keyword {
|
||||
Keyword::LIKE => {
|
||||
let _ = self.parser.next_token();
|
||||
ShowKind::Like(self.parse_identifier().with_context(|_| {
|
||||
error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "LIKE",
|
||||
actual: self.peek_token_as_string(),
|
||||
}
|
||||
})?)
|
||||
}
|
||||
Keyword::WHERE => {
|
||||
let _ = self.parser.next_token();
|
||||
ShowKind::Where(self.parser.parse_expr().with_context(|_| {
|
||||
error::UnexpectedSnafu {
|
||||
sql: self.sql,
|
||||
expected: "some valid expression",
|
||||
actual: self.peek_token_as_string(),
|
||||
}
|
||||
})?)
|
||||
}
|
||||
_ => return self.unsupported(self.peek_token_as_string()),
|
||||
},
|
||||
_ => return self.unsupported(self.peek_token_as_string()),
|
||||
};
|
||||
let kind = self.parse_show_kind()?;
|
||||
|
||||
Ok(Statement::ShowTables(ShowTables {
|
||||
kind,
|
||||
@@ -718,4 +709,66 @@ mod tests {
|
||||
..
|
||||
}) if table == "test" && expr.to_string() == "Field = 'disk'"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_show_collation() {
|
||||
let sql = "SHOW COLLATION";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCollation(ShowKind::All)
|
||||
));
|
||||
|
||||
let sql = "SHOW COLLATION WHERE Charset = 'latin1'";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCollation(ShowKind::Where(_))
|
||||
));
|
||||
|
||||
let sql = "SHOW COLLATION LIKE 'latin1'";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCollation(ShowKind::Like(_))
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_show_charset() {
|
||||
let sql = "SHOW CHARSET";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCharset(ShowKind::All)
|
||||
));
|
||||
|
||||
let sql = "SHOW CHARACTER SET";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCharset(ShowKind::All)
|
||||
));
|
||||
|
||||
let sql = "SHOW CHARSET WHERE Charset = 'latin1'";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCharset(ShowKind::Where(_))
|
||||
));
|
||||
|
||||
let sql = "SHOW CHARACTER SET LIKE 'latin1'";
|
||||
let result =
|
||||
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default());
|
||||
assert!(matches!(
|
||||
result.unwrap()[0],
|
||||
Statement::ShowCharset(ShowKind::Like(_))
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,7 +30,9 @@ use crate::statements::explain::Explain;
|
||||
use crate::statements::insert::Insert;
|
||||
use crate::statements::query::Query;
|
||||
use crate::statements::set_variables::SetVariables;
|
||||
use crate::statements::show::{ShowColumns, ShowCreateTable, ShowDatabases, ShowIndex, ShowTables};
|
||||
use crate::statements::show::{
|
||||
ShowColumns, ShowCreateTable, ShowDatabases, ShowIndex, ShowKind, ShowTables,
|
||||
};
|
||||
use crate::statements::tql::Tql;
|
||||
use crate::statements::truncate::TruncateTable;
|
||||
|
||||
@@ -64,6 +66,10 @@ pub enum Statement {
|
||||
ShowTables(ShowTables),
|
||||
// SHOW COLUMNS
|
||||
ShowColumns(ShowColumns),
|
||||
// SHOW CHARSET or SHOW CHARACTER SET
|
||||
ShowCharset(ShowKind),
|
||||
// SHOW COLLATION
|
||||
ShowCollation(ShowKind),
|
||||
// SHOW INDEX
|
||||
ShowIndex(ShowIndex),
|
||||
// SHOW CREATE TABLE
|
||||
|
||||
Reference in New Issue
Block a user