Support CASE operand WHEN expected_value THEN ..

Another part of #15
This commit is contained in:
Nickolay Ponomarev 2019-04-27 20:09:53 +03:00
parent 2aa4c267e7
commit e5e3d71354
3 changed files with 62 additions and 37 deletions

View file

@ -106,8 +106,11 @@ pub enum ASTNode {
over: Option<SQLWindowSpec>,
},
/// CASE [<operand>] WHEN <condition> THEN <result> ... [ELSE <result>] END
/// Note we only recognize a complete single expression as <condition>, not
/// `< 0` nor `1, 2, 3` as allowed in a <simple when clause> per
/// https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-when-clause
SQLCase {
// TODO: support optional operand for "simple case"
operand: Option<Box<ASTNode>>,
conditions: Vec<ASTNode>,
results: Vec<ASTNode>,
else_result: Option<Box<ASTNode>>,
@ -182,19 +185,21 @@ impl ToString for ASTNode {
s
}
ASTNode::SQLCase {
operand,
conditions,
results,
else_result,
} => {
let mut s = format!(
"CASE {}",
conditions
.iter()
.zip(results)
.map(|(c, r)| format!("WHEN {} THEN {}", c.to_string(), r.to_string()))
.collect::<Vec<String>>()
.join(" ")
);
let mut s = "CASE".to_string();
if let Some(operand) = operand {
s += &format!(" {}", operand.to_string());
}
s += &conditions
.iter()
.zip(results)
.map(|(c, r)| format!(" WHEN {} THEN {}", c.to_string(), r.to_string()))
.collect::<Vec<String>>()
.join("");
if let Some(else_result) = else_result {
s += &format!(" ELSE {}", else_result.to_string())
}

View file

@ -336,33 +336,33 @@ impl Parser {
}
pub fn parse_case_expression(&mut self) -> Result<ASTNode, ParserError> {
if self.parse_keyword("WHEN") {
let mut conditions = vec![];
let mut results = vec![];
loop {
conditions.push(self.parse_expr()?);
self.expect_keyword("THEN")?;
results.push(self.parse_expr()?);
if !self.parse_keyword("WHEN") {
break;
}
}
let else_result = if self.parse_keyword("ELSE") {
Some(Box::new(self.parse_expr()?))
} else {
None
};
self.expect_keyword("END")?;
Ok(ASTNode::SQLCase {
conditions,
results,
else_result,
})
} else {
// TODO: implement "simple" case
// https://jakewheat.github.io/sql-overview/sql-2011-foundation-grammar.html#simple-case
parser_err!("Simple case not implemented")
let mut operand = None;
if !self.parse_keyword("WHEN") {
operand = Some(Box::new(self.parse_expr()?));
self.expect_keyword("WHEN")?;
}
let mut conditions = vec![];
let mut results = vec![];
loop {
conditions.push(self.parse_expr()?);
self.expect_keyword("THEN")?;
results.push(self.parse_expr()?);
if !self.parse_keyword("WHEN") {
break;
}
}
let else_result = if self.parse_keyword("ELSE") {
Some(Box::new(self.parse_expr()?))
} else {
None
};
self.expect_keyword("END")?;
Ok(ASTNode::SQLCase {
operand,
conditions,
results,
else_result,
})
}
/// Parse a SQL CAST function e.g. `CAST(expr AS FLOAT)`

View file

@ -698,13 +698,14 @@ fn parse_parens() {
}
#[test]
fn parse_case_expression() {
fn parse_searched_case_expression() {
let sql = "SELECT CASE WHEN bar IS NULL THEN 'null' WHEN bar = 0 THEN '=0' WHEN bar >= 0 THEN '>=0' ELSE '<0' END FROM foo";
use self::ASTNode::{SQLBinaryExpr, SQLCase, SQLIdentifier, SQLIsNull, SQLValue};
use self::SQLOperator::*;
let select = verified_only_select(sql);
assert_eq!(
&SQLCase {
operand: None,
conditions: vec![
SQLIsNull(Box::new(SQLIdentifier("bar".to_string()))),
SQLBinaryExpr {
@ -731,6 +732,25 @@ fn parse_case_expression() {
);
}
#[test]
fn parse_simple_case_expression() {
// ANSI calls a CASE expression with an operand "<simple case>"
let sql = "SELECT CASE foo WHEN 1 THEN 'Y' ELSE 'N' END";
let select = verified_only_select(sql);
use self::ASTNode::{SQLCase, SQLIdentifier, SQLValue};
assert_eq!(
&SQLCase {
operand: Some(Box::new(SQLIdentifier("foo".to_string()))),
conditions: vec![SQLValue(Value::Long(1))],
results: vec![SQLValue(Value::SingleQuotedString("Y".to_string())),],
else_result: Some(Box::new(SQLValue(Value::SingleQuotedString(
"N".to_string()
))))
},
expr_from_projection(only(&select.projection)),
);
}
#[test]
fn parse_implicit_join() {
let sql = "SELECT * FROM t1, t2";