Skip to content

Commit 4070f3e

Browse files
authored
feat: Convert IS TRUE|FALSE to expression (#499)
1 parent a6d7a35 commit 4070f3e

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/ast/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ pub enum Expr {
233233
},
234234
/// CompositeAccess (postgres) eg: SELECT (information_schema._pg_expandarray(array['i','i'])).n
235235
CompositeAccess { expr: Box<Expr>, key: Ident },
236+
/// `IS FALSE` operator
237+
IsFalse(Box<Expr>),
238+
/// `IS TRUE` operator
239+
IsTrue(Box<Expr>),
236240
/// `IS NULL` operator
237241
IsNull(Box<Expr>),
238242
/// `IS NOT NULL` operator
@@ -379,6 +383,8 @@ impl fmt::Display for Expr {
379383
Ok(())
380384
}
381385
Expr::CompoundIdentifier(s) => write!(f, "{}", display_separated(s, ".")),
386+
Expr::IsTrue(ast) => write!(f, "{} IS TRUE", ast),
387+
Expr::IsFalse(ast) => write!(f, "{} IS FALSE", ast),
382388
Expr::IsNull(ast) => write!(f, "{} IS NULL", ast),
383389
Expr::IsNotNull(ast) => write!(f, "{} IS NOT NULL", ast),
384390
Expr::InList {

src/parser.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,28 +1155,20 @@ impl<'a> Parser<'a> {
11551155
Ok(Expr::IsNull(Box::new(expr)))
11561156
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
11571157
Ok(Expr::IsNotNull(Box::new(expr)))
1158+
} else if self.parse_keywords(&[Keyword::TRUE]) {
1159+
Ok(Expr::IsTrue(Box::new(expr)))
1160+
} else if self.parse_keywords(&[Keyword::FALSE]) {
1161+
Ok(Expr::IsFalse(Box::new(expr)))
11581162
} else if self.parse_keywords(&[Keyword::DISTINCT, Keyword::FROM]) {
11591163
let expr2 = self.parse_expr()?;
11601164
Ok(Expr::IsDistinctFrom(Box::new(expr), Box::new(expr2)))
11611165
} else if self.parse_keywords(&[Keyword::NOT, Keyword::DISTINCT, Keyword::FROM])
11621166
{
11631167
let expr2 = self.parse_expr()?;
11641168
Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2)))
1165-
} else if let Some(right) =
1166-
self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE])
1167-
{
1168-
let mut val = Value::Boolean(true);
1169-
if right == Keyword::FALSE {
1170-
val = Value::Boolean(false);
1171-
}
1172-
Ok(Expr::BinaryOp {
1173-
left: Box::new(expr),
1174-
op: BinaryOperator::Eq,
1175-
right: Box::new(Expr::Value(val)),
1176-
})
11771169
} else {
11781170
self.expected(
1179-
"[NOT] NULL or [NOT] DISTINCT FROM TRUE FALSE after IS",
1171+
"[NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS",
11801172
self.peek_token(),
11811173
)
11821174
}

tests/sqlparser_common.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4768,21 +4768,30 @@ fn parse_position_negative() {
47684768

47694769
#[test]
47704770
fn parse_is_boolean() {
4771-
one_statement_parses_to(
4772-
"SELECT f from foo where field is true",
4773-
"SELECT f FROM foo WHERE field = true",
4771+
use self::Expr::*;
4772+
4773+
let sql = "a IS FALSE";
4774+
assert_eq!(
4775+
IsFalse(Box::new(Identifier(Ident::new("a")))),
4776+
verified_expr(sql)
47744777
);
47754778

4776-
one_statement_parses_to(
4777-
"SELECT f from foo where field is false",
4778-
"SELECT f FROM foo WHERE field = false",
4779+
let sql = "a IS TRUE";
4780+
assert_eq!(
4781+
IsTrue(Box::new(Identifier(Ident::new("a")))),
4782+
verified_expr(sql)
47794783
);
47804784

4785+
verified_stmt("SELECT f FROM foo WHERE field IS TRUE");
4786+
4787+
verified_stmt("SELECT f FROM foo WHERE field IS FALSE");
4788+
47814789
let sql = "SELECT f from foo where field is 0";
47824790
let res = parse_sql_statements(sql);
47834791
assert_eq!(
47844792
ParserError::ParserError(
4785-
"Expected [NOT] NULL or [NOT] DISTINCT FROM TRUE FALSE after IS, found: 0".to_string()
4793+
"Expected [NOT] NULL or TRUE|FALSE or [NOT] DISTINCT FROM after IS, found: 0"
4794+
.to_string()
47864795
),
47874796
res.unwrap_err()
47884797
);

0 commit comments

Comments
 (0)