Skip to content

Commit

Permalink
Add support for SHOW DATABASES/SCHEMAS/TABLES/VIEWS in Hive (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavcloud authored Nov 3, 2024
1 parent 8de3cb0 commit e2197ee
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 7 deletions.
81 changes: 79 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2782,12 +2782,29 @@ pub enum Statement {
filter: Option<ShowStatementFilter>,
},
/// ```sql
/// SHOW DATABASES [LIKE 'pattern']
/// ```
ShowDatabases { filter: Option<ShowStatementFilter> },
/// ```sql
/// SHOW SCHEMAS [LIKE 'pattern']
/// ```
ShowSchemas { filter: Option<ShowStatementFilter> },
/// ```sql
/// SHOW TABLES
/// ```
/// Note: this is a MySQL-specific statement.
ShowTables {
extended: bool,
full: bool,
clause: Option<ShowClause>,
db_name: Option<Ident>,
filter: Option<ShowStatementFilter>,
},
/// ```sql
/// SHOW VIEWS
/// ```
ShowViews {
materialized: bool,
clause: Option<ShowClause>,
db_name: Option<Ident>,
filter: Option<ShowStatementFilter>,
},
Expand Down Expand Up @@ -4363,9 +4380,24 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::ShowDatabases { filter } => {
write!(f, "SHOW DATABASES")?;
if let Some(filter) = filter {
write!(f, " {filter}")?;
}
Ok(())
}
Statement::ShowSchemas { filter } => {
write!(f, "SHOW SCHEMAS")?;
if let Some(filter) = filter {
write!(f, " {filter}")?;
}
Ok(())
}
Statement::ShowTables {
extended,
full,
clause: show_clause,
db_name,
filter,
} => {
Expand All @@ -4375,8 +4407,33 @@ impl fmt::Display for Statement {
extended = if *extended { "EXTENDED " } else { "" },
full = if *full { "FULL " } else { "" },
)?;
if let Some(show_clause) = show_clause {
write!(f, " {show_clause}")?;
}
if let Some(db_name) = db_name {
write!(f, " {db_name}")?;
}
if let Some(filter) = filter {
write!(f, " {filter}")?;
}
Ok(())
}
Statement::ShowViews {
materialized,
clause: show_clause,
db_name,
filter,
} => {
write!(
f,
"SHOW {}VIEWS",
if *materialized { "MATERIALIZED " } else { "" }
)?;
if let Some(show_clause) = show_clause {
write!(f, " {show_clause}")?;
}
if let Some(db_name) = db_name {
write!(f, " FROM {db_name}")?;
write!(f, " {db_name}")?;
}
if let Some(filter) = filter {
write!(f, " {filter}")?;
Expand Down Expand Up @@ -6057,6 +6114,7 @@ pub enum ShowStatementFilter {
Like(String),
ILike(String),
Where(Expr),
NoKeyword(String),
}

impl fmt::Display for ShowStatementFilter {
Expand All @@ -6066,6 +6124,25 @@ 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)),
}
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum ShowClause {
IN,
FROM,
}

impl fmt::Display for ShowClause {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ShowClause::*;
match self {
FROM => write!(f, "FROM"),
IN => write!(f, "IN"),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ define_keywords!(
CYCLE,
DATA,
DATABASE,
DATABASES,
DATA_RETENTION_TIME_IN_DAYS,
DATE,
DATE32,
Expand Down Expand Up @@ -662,6 +663,7 @@ define_keywords!(
SAFE_CAST,
SAVEPOINT,
SCHEMA,
SCHEMAS,
SCOPE,
SCROLL,
SEARCH,
Expand Down Expand Up @@ -822,6 +824,7 @@ define_keywords!(
VERSION,
VERSIONING,
VIEW,
VIEWS,
VIRTUAL,
VOLATILE,
WAREHOUSE,
Expand Down
50 changes: 46 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9579,6 +9579,10 @@ impl<'a> Parser<'a> {
Ok(self.parse_show_columns(extended, full)?)
} else if self.parse_keyword(Keyword::TABLES) {
Ok(self.parse_show_tables(extended, full)?)
} else if self.parse_keywords(&[Keyword::MATERIALIZED, Keyword::VIEWS]) {
Ok(self.parse_show_views(true)?)
} else if self.parse_keyword(Keyword::VIEWS) {
Ok(self.parse_show_views(false)?)
} else if self.parse_keyword(Keyword::FUNCTIONS) {
Ok(self.parse_show_functions()?)
} else if extended || full {
Expand All @@ -9605,13 +9609,29 @@ impl<'a> Parser<'a> {
session,
global,
})
} else if self.parse_keyword(Keyword::DATABASES) {
self.parse_show_databases()
} else if self.parse_keyword(Keyword::SCHEMAS) {
self.parse_show_schemas()
} else {
Ok(Statement::ShowVariable {
variable: self.parse_identifiers()?,
})
}
}

fn parse_show_databases(&mut self) -> Result<Statement, ParserError> {
Ok(Statement::ShowDatabases {
filter: self.parse_show_statement_filter()?,
})
}

fn parse_show_schemas(&mut self) -> Result<Statement, ParserError> {
Ok(Statement::ShowSchemas {
filter: self.parse_show_statement_filter()?,
})
}

pub fn parse_show_create(&mut self) -> Result<Statement, ParserError> {
let obj_type = match self.expect_one_of_keywords(&[
Keyword::TABLE,
Expand Down Expand Up @@ -9667,14 +9687,31 @@ impl<'a> Parser<'a> {
extended: bool,
full: bool,
) -> Result<Statement, ParserError> {
let db_name = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
Some(_) => Some(self.parse_identifier(false)?),
None => None,
let (clause, db_name) = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
Some(Keyword::FROM) => (Some(ShowClause::FROM), Some(self.parse_identifier(false)?)),
Some(Keyword::IN) => (Some(ShowClause::IN), Some(self.parse_identifier(false)?)),
_ => (None, None),
};
let filter = self.parse_show_statement_filter()?;
Ok(Statement::ShowTables {
extended,
full,
clause,
db_name,
filter,
})
}

fn parse_show_views(&mut self, materialized: bool) -> Result<Statement, ParserError> {
let (clause, db_name) = match self.parse_one_of_keywords(&[Keyword::FROM, Keyword::IN]) {
Some(Keyword::FROM) => (Some(ShowClause::FROM), Some(self.parse_identifier(false)?)),
Some(Keyword::IN) => (Some(ShowClause::IN), Some(self.parse_identifier(false)?)),
_ => (None, None),
};
let filter = self.parse_show_statement_filter()?;
Ok(Statement::ShowViews {
materialized,
clause,
db_name,
filter,
})
Expand Down Expand Up @@ -9704,7 +9741,12 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::WHERE) {
Ok(Some(ShowStatementFilter::Where(self.parse_expr()?)))
} else {
Ok(None)
self.maybe_parse(|parser| -> Result<String, ParserError> {
parser.parse_literal_string()
})?
.map_or(Ok(None), |filter| {
Ok(Some(ShowStatementFilter::NoKeyword(filter)))
})
}
}

Expand Down
21 changes: 21 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11378,3 +11378,24 @@ fn test_try_convert() {
all_dialects_where(|d| d.supports_try_convert() && !d.convert_type_before_value());
dialects.verified_expr("TRY_CONVERT('foo', VARCHAR(MAX))");
}

#[test]
fn test_show_dbs_schemas_tables_views() {
verified_stmt("SHOW DATABASES");
verified_stmt("SHOW DATABASES LIKE '%abc'");
verified_stmt("SHOW SCHEMAS");
verified_stmt("SHOW SCHEMAS LIKE '%abc'");
verified_stmt("SHOW TABLES");
verified_stmt("SHOW TABLES IN db1");
verified_stmt("SHOW TABLES IN db1 'abc'");
verified_stmt("SHOW VIEWS");
verified_stmt("SHOW VIEWS IN db1");
verified_stmt("SHOW VIEWS IN db1 'abc'");
verified_stmt("SHOW VIEWS FROM db1");
verified_stmt("SHOW VIEWS FROM db1 'abc'");
verified_stmt("SHOW MATERIALIZED VIEWS");
verified_stmt("SHOW MATERIALIZED VIEWS IN db1");
verified_stmt("SHOW MATERIALIZED VIEWS IN db1 'abc'");
verified_stmt("SHOW MATERIALIZED VIEWS FROM db1");
verified_stmt("SHOW MATERIALIZED VIEWS FROM db1 'abc'");
}
9 changes: 8 additions & 1 deletion tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: false,
full: false,
clause: None,
db_name: None,
filter: None,
}
Expand All @@ -338,6 +339,7 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: false,
full: false,
clause: Some(ShowClause::FROM),
db_name: Some(Ident::new("mydb")),
filter: None,
}
Expand All @@ -347,6 +349,7 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: true,
full: false,
clause: None,
db_name: None,
filter: None,
}
Expand All @@ -356,6 +359,7 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: false,
full: true,
clause: None,
db_name: None,
filter: None,
}
Expand All @@ -365,6 +369,7 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: false,
full: false,
clause: None,
db_name: None,
filter: Some(ShowStatementFilter::Like("pattern".into())),
}
Expand All @@ -374,13 +379,15 @@ fn parse_show_tables() {
Statement::ShowTables {
extended: false,
full: false,
clause: None,
db_name: None,
filter: Some(ShowStatementFilter::Where(
mysql_and_generic().verified_expr("1 = 2")
)),
}
);
mysql_and_generic().one_statement_parses_to("SHOW TABLES IN mydb", "SHOW TABLES FROM mydb");
mysql_and_generic().verified_stmt("SHOW TABLES IN mydb");
mysql_and_generic().verified_stmt("SHOW TABLES FROM mydb");
}

#[test]
Expand Down

0 comments on commit e2197ee

Please sign in to comment.