diff --git a/src/ast/operator.rs b/src/ast/operator.rs index 669470cda..f2c8454f3 100644 --- a/src/ast/operator.rs +++ b/src/ast/operator.rs @@ -332,6 +332,10 @@ pub enum BinaryOperator { /// ':=' Assignment Operator /// See Assignment, + /// `IS` operator + Is, + /// `IS NOT` operator + IsNot, } impl fmt::Display for BinaryOperator { @@ -409,6 +413,8 @@ impl fmt::Display for BinaryOperator { BinaryOperator::At => f.write_str("@"), BinaryOperator::TildeEq => f.write_str("~="), BinaryOperator::Assignment => f.write_str(":="), + BinaryOperator::Is => f.write_str("IS"), + BinaryOperator::IsNot => f.write_str("IS NOT"), } } } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 2beb56553..5161d64ab 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1617,6 +1617,11 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports binary `IS` and `IS NOT` operators. + fn supports_is_operator(&self) -> bool { + false + } + /// Returns true if this dialect allows an optional `SIGNED` suffix after integer data types. /// /// Example: diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index 6f9ec0d25..b19045520 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -119,6 +119,10 @@ impl Dialect for SQLiteDialect { true } + fn supports_is_operator(&self) -> bool { + true + } + fn supports_comma_separated_trim(&self) -> bool { true } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 1a46c6e50..802db9249 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4139,6 +4139,31 @@ impl<'a> Parser<'a> { self.parse_is_json_predicate(expr, false) } else if self.parse_keywords(&[Keyword::NOT, Keyword::JSON]) { self.parse_is_json_predicate(expr, true) + } else if self.dialect.supports_is_operator() { + if let Some((form, negated)) = + self.maybe_parse(|parser| parser.parse_unicode_is_normalized_suffix())? + { + Ok(Expr::IsNormalized { + expr: Box::new(expr), + form, + negated, + }) + } else { + let op = if self.parse_keyword(Keyword::NOT) { + BinaryOperator::IsNot + } else { + BinaryOperator::Is + }; + // SQLite parses `IS` at the same left-associative level as the + // comparison operators, see + Ok(Expr::BinaryOp { + left: Box::new(expr), + op, + right: Box::new( + self.parse_subexpr(self.dialect.prec_value(Precedence::Eq))?, + ), + }) + } } else if let Ok(is_normalized) = self.parse_unicode_is_normalized(expr) { Ok(is_normalized) } else { @@ -12841,8 +12866,19 @@ impl<'a> Parser<'a> { /// Parse a literal unicode normalization clause pub fn parse_unicode_is_normalized(&mut self, expr: Expr) -> Result { - let neg = self.parse_keyword(Keyword::NOT); - let normalized_form = self.maybe_parse(|parser| { + let (form, negated) = self.parse_unicode_is_normalized_suffix()?; + Ok(Expr::IsNormalized { + expr: Box::new(expr), + form, + negated, + }) + } + + fn parse_unicode_is_normalized_suffix( + &mut self, + ) -> Result<(Option, bool), ParserError> { + let negated = self.parse_keyword(Keyword::NOT); + let form = self.maybe_parse(|parser| { match parser.parse_one_of_keywords(&[ Keyword::NFC, Keyword::NFD, @@ -12857,13 +12893,10 @@ impl<'a> Parser<'a> { } })?; if self.parse_keyword(Keyword::NORMALIZED) { - return Ok(Expr::IsNormalized { - expr: Box::new(expr), - form: normalized_form, - negated: neg, - }); + Ok((form, negated)) + } else { + self.expected_ref("unicode normalization form", self.peek_token_ref()) } - self.expected_ref("unicode normalization form", self.peek_token_ref()) } /// Parse parenthesized enum members, used with `ENUM(...)` type definitions. diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 8e08c657f..efa1cd6a3 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -11288,7 +11288,9 @@ fn parse_is_boolean() { "SELECT s, s IS TRIM(' NFKC ') FROM foo", ] { assert!( - parse_sql_statements(sql).is_err(), + all_dialects_except(|dialect| dialect.supports_is_operator()) + .parse_sql_statements(sql) + .is_err(), "expected a parse failure for `{sql}`" ); } diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index b8fad7e9d..0df7822a0 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -987,6 +987,93 @@ fn test_non_bmp_identifiers() { assert!(sqlite().parse_sql_statements("SELECT \u{007f}").is_err()); } +#[test] +fn sqlite_is_operators() { + for (sql, op) in [ + ("a IS b", BinaryOperator::Is), + ("a IS NOT b", BinaryOperator::IsNot), + ] { + assert_eq!( + sqlite().verified_expr(sql), + Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op, + right: Box::new(Expr::Identifier(Ident::new("b"))), + } + ); + } + + assert_eq!( + sqlite().verified_expr("a IS b + c"), + Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::Is, + right: Box::new(Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("b"))), + op: BinaryOperator::Plus, + right: Box::new(Expr::Identifier(Ident::new("c"))), + }), + } + ); + + assert_eq!( + sqlite().verified_expr("a IS b AND c IS NOT d"), + Expr::BinaryOp { + left: Box::new(Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::Is, + right: Box::new(Expr::Identifier(Ident::new("b"))), + }), + op: BinaryOperator::And, + right: Box::new(Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("c"))), + op: BinaryOperator::IsNot, + right: Box::new(Expr::Identifier(Ident::new("d"))), + }), + } + ); + + assert_eq!( + sqlite().verified_expr("a IS b = c"), + Expr::BinaryOp { + left: Box::new(Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::Is, + right: Box::new(Expr::Identifier(Ident::new("b"))), + }), + op: BinaryOperator::Eq, + right: Box::new(Expr::Identifier(Ident::new("c"))), + } + ); + + match sqlite().verified_expr("a IS b IN (1)") { + Expr::InList { + expr, + list, + negated, + } => { + assert!(!negated); + assert_eq!( + *expr, + Expr::BinaryOp { + left: Box::new(Expr::Identifier(Ident::new("a"))), + op: BinaryOperator::Is, + right: Box::new(Expr::Identifier(Ident::new("b"))), + } + ); + assert_eq!(list.len(), 1); + assert!(matches!( + list[0], + Expr::Value(ValueWithSpan { + value: Value::Number(_, _), + .. + }) + )); + } + other => panic!("expected InList on top, got {other:?}"), + } +} + #[test] fn parse_create_table_string_column_names() { sqlite().verified_stmt("CREATE TABLE t ('a')");