From 3ef3ee958ee20002326d56c4a1a22cbd47cbab22 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sat, 3 Oct 2026 08:35:31 +0200 Subject: [PATCH 1/7] Parse SQLite's `IS` and `IS NOT` operators --- src/ast/operator.rs | 6 +++ src/dialect/mod.rs | 5 +++ src/dialect/sqlite.rs | 4 ++ src/parser/mod.rs | 49 ++++++++++++++++++---- tests/sqlparser_common.rs | 3 +- tests/sqlparser_sqlite.rs | 87 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 145 insertions(+), 9 deletions(-) diff --git a/src/ast/operator.rs b/src/ast/operator.rs index 669470cdaa..f2c8454f34 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 c867b76f61..f46d6bc995 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1793,6 +1793,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 44dcc9d267..b983f02b9c 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -163,6 +163,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 3efb22a349..d18588ecec 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4204,6 +4204,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 { @@ -13166,8 +13191,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, @@ -13182,13 +13218,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 f71869e820..67485b1fc3 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -11634,7 +11634,8 @@ fn parse_is_boolean() { ("SELECT s, s IS TRIM(' NFKC ') FROM foo", "TRIM"), ] { assert_eq!( - parse_sql_statements(sql), + all_dialects_except(|dialect| dialect.supports_is_operator()) + .parse_sql_statements(sql), Err(ParserError::ParserError(format!( "Expected: [NOT] NULL | TRUE | FALSE | DISTINCT | [NOT] JSON [VALUE | SCALAR | ARRAY | OBJECT] [WITH | WITHOUT UNIQUE [KEYS]] | [form] NORMALIZED FROM after IS, found: {found}" ))), diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index ad80e95a93..b6608d88bd 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1026,6 +1026,93 @@ fn test_non_bmp_identifiers() { ); } +#[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')"); From fbad2cab9a18dc9e90ac2ce849191bb3cd3f2880 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Fri, 16 Oct 2026 23:51:50 +0200 Subject: [PATCH 2/7] Track the new InList.global field in the SQLite IS tests --- tests/sqlparser_sqlite.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index b6608d88bd..1af245d8c0 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1090,8 +1090,10 @@ fn sqlite_is_operators() { expr, list, negated, + global, } => { assert!(!negated); + assert!(!global); assert_eq!( *expr, Expr::BinaryOp { From d61d9a851654f4da7e84c729173f0b2d612d1f84 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Tue, 20 Oct 2026 12:51:23 +0200 Subject: [PATCH 3/7] Cover missing `IS` operands and simplify normalization backtracking --- src/parser/mod.rs | 4 ++-- tests/sqlparser_sqlite.rs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d18588ecec..b813629e2c 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4205,8 +4205,8 @@ impl<'a> Parser<'a> { } 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())? + if let Ok((form, negated)) = + self.try_parse(|parser| parser.parse_unicode_is_normalized_suffix()) { Ok(Expr::IsNormalized { expr: Box::new(expr), diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index 1af245d8c0..20f8d05472 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1113,6 +1113,16 @@ fn sqlite_is_operators() { } other => panic!("expected InList on top, got {other:?}"), } + + // A dangling right-hand side is rejected + for sql in ["SELECT a IS", "SELECT a IS NOT"] { + assert_eq!( + sqlite().parse_sql_statements(sql), + Err(ParserError::ParserError( + "Expected: an expression, found: EOF".to_string() + )) + ); + } } #[test] From 2aeb534f2ff942a9cb7018608e2ef221233566e0 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Tue, 20 Oct 2026 17:55:57 +0200 Subject: [PATCH 4/7] Clarify SQLite `IS` precedence --- src/parser/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b813629e2c..6eb42a4e7a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4219,8 +4219,7 @@ impl<'a> Parser<'a> { } else { BinaryOperator::Is }; - // SQLite parses `IS` at the same left-associative level as the - // comparison operators, see + // SQLite gives `IS` the same precedence as `=` (https://www.sqlite.org/lang_expr.html). Ok(Expr::BinaryOp { left: Box::new(expr), op, From f1baa01f60ddfd544fb2bd4db565442b4e530b25 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Wed, 21 Oct 2026 09:34:12 +0200 Subject: [PATCH 5/7] Adapt SQLite `IS` assertion to the unified `IN` AST --- tests/sqlparser_sqlite.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index 20f8d05472..6e39c35a5b 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1086,9 +1086,9 @@ fn sqlite_is_operators() { ); match sqlite().verified_expr("a IS b IN (1)") { - Expr::InList { + Expr::In { expr, - list, + target: InTarget::List(list), negated, global, } => { From 705e5c9306c935ca6b64c12a3ac6b06591c9aa88 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sat, 24 Oct 2026 00:23:37 +0200 Subject: [PATCH 6/7] Place regression tests beside related syntax cases --- tests/sqlparser_sqlite.rs | 198 +++++++++++++++++++------------------- 1 file changed, 99 insertions(+), 99 deletions(-) diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index 6e39c35a5b..c5efee3965 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -659,6 +659,105 @@ fn test_glob_operator() { ); } +#[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::In { + expr, + target: InTarget::List(list), + negated, + global, + } => { + assert!(!negated); + assert!(!global); + 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:?}"), + } + + // A dangling right-hand side is rejected + for sql in ["SELECT a IS", "SELECT a IS NOT"] { + assert_eq!( + sqlite().parse_sql_statements(sql), + Err(ParserError::ParserError( + "Expected: an expression, found: EOF".to_string() + )) + ); + } +} + #[test] fn test_update_delete_limit() { match sqlite().verified_stmt("UPDATE foo SET bar = 1 LIMIT 99") { @@ -1026,105 +1125,6 @@ fn test_non_bmp_identifiers() { ); } -#[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::In { - expr, - target: InTarget::List(list), - negated, - global, - } => { - assert!(!negated); - assert!(!global); - 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:?}"), - } - - // A dangling right-hand side is rejected - for sql in ["SELECT a IS", "SELECT a IS NOT"] { - assert_eq!( - sqlite().parse_sql_statements(sql), - Err(ParserError::ParserError( - "Expected: an expression, found: EOF".to_string() - )) - ); - } -} - #[test] fn parse_create_table_string_column_names() { sqlite().verified_stmt("CREATE TABLE t ('a')"); From c4a4c07f99a26455aa6932c01840b4ac30492771 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Sat, 24 Oct 2026 13:43:11 +0200 Subject: [PATCH 7/7] Add fuzz seeds for sqlite is operator --- fuzz/fuzz_seeds/sqlite_2470_is_operators.sql | 1 + 1 file changed, 1 insertion(+) create mode 100644 fuzz/fuzz_seeds/sqlite_2470_is_operators.sql diff --git a/fuzz/fuzz_seeds/sqlite_2470_is_operators.sql b/fuzz/fuzz_seeds/sqlite_2470_is_operators.sql new file mode 100644 index 0000000000..290c48ce6d --- /dev/null +++ b/fuzz/fuzz_seeds/sqlite_2470_is_operators.sql @@ -0,0 +1 @@ +SELECT a IS b, a IS NOT b, a IS b + c, a IS b AND c IS NOT d, a IS b = c, a IS b IN (1) FROM t