Skip to content

Commit

Permalink
feat: support parsing multiple show variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-du committed Feb 9, 2021
1 parent f40955e commit 492c6a4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 25 deletions.
10 changes: 8 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ pub enum Statement {
/// SHOW <variable>
///
/// Note: this is a PostgreSQL-specific statement.
ShowVariable { variable: Ident },
ShowVariable { variable: Vec<Ident> },
/// SHOW COLUMNS
///
/// Note: this is a MySQL-specific statement.
Expand Down Expand Up @@ -1136,7 +1136,13 @@ impl fmt::Display for Statement {
value = display_comma_separated(value)
)
}
Statement::ShowVariable { variable } => write!(f, "SHOW {}", variable),
Statement::ShowVariable { variable } => {
write!(f, "SHOW")?;
if !variable.is_empty() {
write!(f, " {}", display_separated(variable, " "))?;
}
Ok(())
}
Statement::ShowColumns {
extended,
full,
Expand Down
15 changes: 14 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2043,6 +2043,19 @@ impl<'a> Parser<'a> {
Ok(ObjectName(idents))
}

/// Parse identifiers
pub fn parse_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
let mut idents = vec![];
loop {
match self.next_token() {
Token::Word(w) => idents.push(w.to_ident()),
Token::EOF => break,
_ => {}
}
}
Ok(idents)
}

/// Parse a simple one-word identifier (possibly quoted, possibly a keyword)
pub fn parse_identifier(&mut self) -> Result<Ident, ParserError> {
match self.next_token() {
Expand Down Expand Up @@ -2439,7 +2452,7 @@ impl<'a> Parser<'a> {
self.parse_show_columns()
} else {
Ok(Statement::ShowVariable {
variable: self.parse_identifier()?,
variable: self.parse_identifiers()?,
})
}
}
Expand Down
28 changes: 10 additions & 18 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,23 +543,17 @@ fn parse_is_not_null() {
fn parse_not_precedence() {
// NOT has higher precedence than OR/AND, so the following must parse as (NOT true) OR true
let sql = "NOT true OR true";
assert_matches!(
verified_expr(sql),
Expr::BinaryOp {
op: BinaryOperator::Or,
..
}
);
assert_matches!(verified_expr(sql), Expr::BinaryOp {
op: BinaryOperator::Or,
..
});

// But NOT has lower precedence than comparison operators, so the following parses as NOT (a IS NULL)
let sql = "NOT a IS NULL";
assert_matches!(
verified_expr(sql),
Expr::UnaryOp {
op: UnaryOperator::Not,
..
}
);
assert_matches!(verified_expr(sql), Expr::UnaryOp {
op: UnaryOperator::Not,
..
});

// NOT has lower precedence than BETWEEN, so the following parses as NOT (1 NOT BETWEEN 1 AND 2)
let sql = "NOT 1 NOT BETWEEN 1 AND 2";
Expand Down Expand Up @@ -1469,7 +1463,7 @@ fn parse_create_external_table_lowercase() {
lng DOUBLE) \
STORED AS PARQUET LOCATION '/tmp/example.csv'",
);
assert_matches!(ast, Statement::CreateTable { .. });
assert_matches!(ast, Statement::CreateTable{..});
}

#[test]
Expand Down Expand Up @@ -2588,9 +2582,7 @@ fn parse_multiple_statements() {
#[test]
fn parse_scalar_subqueries() {
let sql = "(SELECT 1) + (SELECT 2)";
assert_matches!(
verified_expr(sql),
Expr::BinaryOp {
assert_matches!(verified_expr(sql), Expr::BinaryOp {
op: BinaryOperator::Plus, ..
//left: box Subquery { .. },
//right: box Subquery { .. },
Expand Down
8 changes: 4 additions & 4 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,19 +443,19 @@ fn parse_set() {

#[test]
fn parse_show() {
let stmt = pg_and_generic().verified_stmt("SHOW a");
let stmt = pg_and_generic().verified_stmt("SHOW a a");
assert_eq!(
stmt,
Statement::ShowVariable {
variable: "a".into()
variable: vec!["a".into(), "a".into()]
}
);

let stmt = pg_and_generic().verified_stmt("SHOW ALL");
let stmt = pg_and_generic().verified_stmt("SHOW ALL ALL");
assert_eq!(
stmt,
Statement::ShowVariable {
variable: "ALL".into()
variable: vec!["ALL".into(), "ALL".into()]
}
)
}
Expand Down

0 comments on commit 492c6a4

Please sign in to comment.