-
Notifications
You must be signed in to change notification settings - Fork 666
Add support for SHOW DATABASES/SCHEMAS/TABLES/VIEWS in Hive #1487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
4e67a52
a5edbb7
9249aa3
febab0b
407003a
07a651a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,8 +73,9 @@ pub use self::value::{ | |
TrimWhereField, Value, | ||
}; | ||
|
||
use crate::ast::helpers::stmt_data_loading::{ | ||
DataLoadingOptions, StageLoadSelectItem, StageParamsObject, | ||
use crate::{ | ||
ast::helpers::stmt_data_loading::{DataLoadingOptions, StageLoadSelectItem, StageParamsObject}, | ||
keywords::Keyword, | ||
}; | ||
#[cfg(feature = "visitor")] | ||
pub use visitor::*; | ||
|
@@ -2782,12 +2783,30 @@ pub enum Statement { | |
filter: Option<ShowStatementFilter>, | ||
}, | ||
/// ```sql | ||
/// SHOW DATABASES [LIKE 'pattern'] | ||
/// ``` | ||
ShowDatabases { pattern: Option<String> }, | ||
/// ```sql | ||
/// SHOW SCHEMAS [LIKE 'pattern'] | ||
/// ``` | ||
ShowSchemas { pattern: Option<String> }, | ||
/// ```sql | ||
/// SHOW TABLES | ||
/// ``` | ||
/// Note: this is a MySQL-specific statement. | ||
ShowTables { | ||
extended: bool, | ||
full: bool, | ||
// The keyword used to indicate the DB name (IN/FROM) | ||
db_name_keyword: Option<Keyword>, | ||
db_name: Option<Ident>, | ||
filter: Option<ShowStatementFilter>, | ||
}, | ||
/// ```sql | ||
/// SHOW VIEWS | ||
/// ``` | ||
ShowViews { | ||
// The keyword used to indicate the DB name (IN/FROM) | ||
db_name_keyword: Option<Keyword>, | ||
db_name: Option<Ident>, | ||
filter: Option<ShowStatementFilter>, | ||
}, | ||
|
@@ -4363,9 +4382,24 @@ impl fmt::Display for Statement { | |
} | ||
Ok(()) | ||
} | ||
Statement::ShowDatabases { pattern } => { | ||
write!(f, "SHOW DATABASES")?; | ||
if let Some(pattern) = pattern { | ||
write!(f, " LIKE '{}'", pattern)?; | ||
} | ||
Ok(()) | ||
} | ||
Statement::ShowSchemas { pattern } => { | ||
write!(f, "SHOW SCHEMAS")?; | ||
if let Some(pattern) = pattern { | ||
write!(f, " LIKE '{}'", pattern)?; | ||
} | ||
Ok(()) | ||
} | ||
Statement::ShowTables { | ||
extended, | ||
full, | ||
db_name_keyword, | ||
db_name, | ||
filter, | ||
} => { | ||
|
@@ -4376,7 +4410,31 @@ impl fmt::Display for Statement { | |
full = if *full { "FULL " } else { "" }, | ||
)?; | ||
if let Some(db_name) = db_name { | ||
write!(f, " FROM {db_name}")?; | ||
let keyword = match &db_name_keyword { | ||
Some(Keyword::FROM) => "FROM", | ||
Some(Keyword::IN) => "IN", | ||
_ => unreachable!(), | ||
}; | ||
write!(f, " {} {db_name}", keyword)?; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think representation wise we can represent the IN/FROM explicitly? e.g. enum ShowClause {
IN,
FROM
}
Statement::ShowViews{ clause: Option<ShowClause>, db_name: Option<Ident> } The unreachable panic I don't think fits well here since there's no invariant guarding that panic (the parser codepath is quite far away from this and could possibly change subtly to trigger it), I think it'd be reasonable as in the previous version to display an incorrect sql (which would be less consequential given we have a bug vs than crash the user's service). Though in this case likely we can rely on the compiler to make such a state unavoidable via the enum route. Also would it make more sense to skip the nested if let Some(db_name_keyword) = db_name_keyword {
write(f, " {db_name_keyword}")?;
}
if let Some(db_name) = db_name {
write(f, " {db_name})?;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good comments, with the ShowClause the unreachable dilemma is irrelevant |
||
if let Some(filter) = filter { | ||
write!(f, " {filter}")?; | ||
} | ||
Ok(()) | ||
} | ||
Statement::ShowViews { | ||
db_name_keyword, | ||
db_name, | ||
filter, | ||
} => { | ||
write!(f, "SHOW VIEWS")?; | ||
if let Some(db_name) = db_name { | ||
let keyword = match &db_name_keyword { | ||
Some(Keyword::FROM) => "FROM", | ||
Some(Keyword::IN) => "IN", | ||
_ => unreachable!(), | ||
}; | ||
write!(f, " {} {db_name}", keyword)?; | ||
} | ||
if let Some(filter) = filter { | ||
write!(f, " {filter}")?; | ||
|
@@ -6057,6 +6115,7 @@ pub enum ShowStatementFilter { | |
Like(String), | ||
ILike(String), | ||
Where(Expr), | ||
NoKeyword(String), | ||
} | ||
|
||
impl fmt::Display for ShowStatementFilter { | ||
|
@@ -6066,6 +6125,7 @@ impl fmt::Display for ShowStatementFilter { | |
Like(pattern) => write!(f, "LIKE '{}'", value::escape_single_quote_string(pattern)), | ||
ILike(pattern) => write!(f, "ILIKE {}", value::escape_single_quote_string(pattern)), | ||
Where(expr) => write!(f, "WHERE {expr}"), | ||
NoKeyword(pattern) => write!(f, "'{}'", value::escape_single_quote_string(pattern)), | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -534,6 +534,20 @@ fn parse_use() { | |
); | ||
} | ||
|
||
#[test] | ||
|
||
fn test_show() { | ||
hive_and_generic().verified_stmt("SHOW DATABASES"); | ||
hive_and_generic().verified_stmt("SHOW DATABASES LIKE '%abc'"); | ||
hive_and_generic().verified_stmt("SHOW SCHEMAS"); | ||
hive_and_generic().verified_stmt("SHOW SCHEMAS LIKE '%abc'"); | ||
hive_and_generic().verified_stmt("SHOW TABLES"); | ||
hive_and_generic().verified_stmt("SHOW TABLES IN db1"); | ||
hive_and_generic().verified_stmt("SHOW TABLES IN db1 'abc'"); | ||
|
||
hive_and_generic().verified_stmt("SHOW VIEWS"); | ||
hive_and_generic().verified_stmt("SHOW VIEWS IN db1"); | ||
hive_and_generic().verified_stmt("SHOW VIEWS IN db1 'abc'"); | ||
} | ||
|
||
fn hive() -> TestedDialects { | ||
TestedDialects::new(vec![Box::new(HiveDialect {})]) | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to also use
ShowStatementFilter
to represent this (if there's a chance that the syntax can be extended)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea