From 7732c34b195e5fffeeb8d1fb145b1aad082c345a Mon Sep 17 00:00:00 2001 From: yuval-illumex <85674443+yuval-illumex@users.noreply.github.com> Date: Mon, 2 May 2022 21:02:28 +0300 Subject: [PATCH] Add support in IS boolean filter (#474) * Add support in IS TRUE IS FALSE * Fix lint * Add test for is false --- src/parser.rs | 14 +++++++++++++- tests/sqlparser_common.rs | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/parser.rs b/src/parser.rs index e855bdf4..5ab68006 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1130,9 +1130,21 @@ impl<'a> Parser<'a> { { let expr2 = self.parse_expr()?; Ok(Expr::IsNotDistinctFrom(Box::new(expr), Box::new(expr2))) + } else if let Some(right) = + self.parse_one_of_keywords(&[Keyword::TRUE, Keyword::FALSE]) + { + let mut val = Value::Boolean(true); + if right == Keyword::FALSE { + val = Value::Boolean(false); + } + Ok(Expr::BinaryOp { + left: Box::new(expr), + op: BinaryOperator::Eq, + right: Box::new(Expr::Value(val)), + }) } else { self.expected( - "[NOT] NULL or [NOT] DISTINCT FROM after IS", + "[NOT] NULL or [NOT] DISTINCT FROM TRUE FALSE after IS", self.peek_token(), ) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index e0c7f34b..43db2f76 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -4658,3 +4658,25 @@ fn parse_position_negative() { res.unwrap_err() ); } + +#[test] +fn parse_is_boolean() { + one_statement_parses_to( + "SELECT f from foo where field is true", + "SELECT f FROM foo WHERE field = true", + ); + + one_statement_parses_to( + "SELECT f from foo where field is false", + "SELECT f FROM foo WHERE field = false", + ); + + let sql = "SELECT f from foo where field is 0"; + let res = parse_sql_statements(sql); + assert_eq!( + ParserError::ParserError( + "Expected [NOT] NULL or [NOT] DISTINCT FROM TRUE FALSE after IS, found: 0".to_string() + ), + res.unwrap_err() + ); +}