diff --git a/fuzz/fuzz_seeds/sqlite_2840_not_null_display.sql b/fuzz/fuzz_seeds/sqlite_2840_not_null_display.sql new file mode 100644 index 0000000000..8651b3465b --- /dev/null +++ b/fuzz/fuzz_seeds/sqlite_2840_not_null_display.sql @@ -0,0 +1 @@ +SELECT a NOT NULL + 1; diff --git a/src/ast/mod.rs b/src/ast/mod.rs index a84ffb3508..242a118096 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -807,6 +807,52 @@ pub enum CastKind { DoubleColon, } +/// The spelling of an [`Expr::IsNull`] test. +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] +pub enum IsNullSyntax { + /// ` IS NULL` + IsNull, + /// ` ISNULL` + Isnull, +} + +impl fmt::Display for IsNullSyntax { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + IsNullSyntax::IsNull => f.write_str("IS NULL"), + IsNullSyntax::Isnull => f.write_str("ISNULL"), + } + } +} + +/// The spelling of an [`Expr::IsNotNull`] test. +/// +/// In SQLite `IS NOT` is a binary operator, so `a NOT NULL + 1` means +/// `(a NOT NULL) + 1` while `a IS NOT NULL + 1` means `a IS NOT (NULL + 1)`. +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] +pub enum IsNotNullSyntax { + /// ` IS NOT NULL` + IsNotNull, + /// ` NOT NULL` + NotNull, + /// ` NOTNULL` + Notnull, +} + +impl fmt::Display for IsNotNullSyntax { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + IsNotNullSyntax::IsNotNull => f.write_str("IS NOT NULL"), + IsNotNullSyntax::NotNull => f.write_str("NOT NULL"), + IsNotNullSyntax::Notnull => f.write_str("NOTNULL"), + } + } +} + /// `MATCH` type for constraint references /// /// See: @@ -1002,9 +1048,19 @@ pub enum Expr { /// `IS NOT TRUE` operator IsNotTrue(Box), /// `IS NULL` operator - IsNull(Box), + IsNull { + /// The tested expression. + expr: Box, + /// How the test was written. + syntax: IsNullSyntax, + }, /// `IS NOT NULL` operator - IsNotNull(Box), + IsNotNull { + /// The tested expression. + expr: Box, + /// How the test was written. + syntax: IsNotNullSyntax, + }, /// `IS UNKNOWN` operator IsUnknown(Box), /// `IS NOT UNKNOWN` operator @@ -1816,8 +1872,8 @@ impl fmt::Display for Expr { Expr::IsNotTrue(ast) => write!(f, "{ast} IS NOT TRUE"), Expr::IsFalse(ast) => write!(f, "{ast} IS FALSE"), Expr::IsNotFalse(ast) => write!(f, "{ast} IS NOT FALSE"), - Expr::IsNull(ast) => write!(f, "{ast} IS NULL"), - Expr::IsNotNull(ast) => write!(f, "{ast} IS NOT NULL"), + Expr::IsNull { expr, syntax } => write!(f, "{expr} {syntax}"), + Expr::IsNotNull { expr, syntax } => write!(f, "{expr} {syntax}"), Expr::IsUnknown(ast) => write!(f, "{ast} IS UNKNOWN"), Expr::IsNotUnknown(ast) => write!(f, "{ast} IS NOT UNKNOWN"), Expr::IsJson { diff --git a/src/ast/spans.rs b/src/ast/spans.rs index 0bc9e5b026..3887e4022c 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -1656,8 +1656,8 @@ impl Spanned for Expr { Expr::IsNotFalse(expr) => expr.span(), Expr::IsTrue(expr) => expr.span(), Expr::IsNotTrue(expr) => expr.span(), - Expr::IsNull(expr) => expr.span(), - Expr::IsNotNull(expr) => expr.span(), + Expr::IsNull { expr, syntax: _ } => expr.span(), + Expr::IsNotNull { expr, syntax: _ } => expr.span(), Expr::IsUnknown(expr) => expr.span(), Expr::IsNotUnknown(expr) => expr.span(), Expr::IsJson { diff --git a/src/parser/mod.rs b/src/parser/mod.rs index ff200f3e18..d64337f7e2 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4190,9 +4190,15 @@ impl<'a> Parser<'a> { match w.keyword { Keyword::IS => { if self.parse_keyword(Keyword::NULL) { - Ok(Expr::IsNull(Box::new(expr))) + Ok(Expr::IsNull { + expr: Box::new(expr), + syntax: IsNullSyntax::IsNull, + }) } else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) { - Ok(Expr::IsNotNull(Box::new(expr))) + Ok(Expr::IsNotNull { + expr: Box::new(expr), + syntax: IsNotNullSyntax::IsNotNull, + }) } else if self.parse_keywords(&[Keyword::TRUE]) { Ok(Expr::IsTrue(Box::new(expr))) } else if self.parse_keywords(&[Keyword::NOT, Keyword::TRUE]) { @@ -4281,7 +4287,10 @@ impl<'a> Parser<'a> { regexp, }) } else if negated && null { - Ok(Expr::IsNotNull(Box::new(expr))) + Ok(Expr::IsNotNull { + expr: Box::new(expr), + syntax: IsNotNullSyntax::NotNull, + }) } else if self.parse_keyword(Keyword::IN) { self.parse_in(expr, negated, false) } else if self.parse_keyword(Keyword::BETWEEN) { @@ -4319,12 +4328,14 @@ impl<'a> Parser<'a> { self.expected_ref("IN or BETWEEN after NOT", self.peek_token_ref()) } } - Keyword::NOTNULL if dialect.supports_notnull_operator() => { - Ok(Expr::IsNotNull(Box::new(expr))) - } - Keyword::ISNULL if dialect.supports_isnull_operator() => { - Ok(Expr::IsNull(Box::new(expr))) - } + Keyword::NOTNULL if dialect.supports_notnull_operator() => Ok(Expr::IsNotNull { + expr: Box::new(expr), + syntax: IsNotNullSyntax::Notnull, + }), + Keyword::ISNULL if dialect.supports_isnull_operator() => Ok(Expr::IsNull { + expr: Box::new(expr), + syntax: IsNullSyntax::Isnull, + }), Keyword::MEMBER => { if self.parse_keyword(Keyword::OF) { self.expect_token(&Token::LParen)?; diff --git a/tests/sqlparser_clickhouse.rs b/tests/sqlparser_clickhouse.rs index f4d637be69..d567324456 100644 --- a/tests/sqlparser_clickhouse.rs +++ b/tests/sqlparser_clickhouse.rs @@ -1728,24 +1728,20 @@ fn parse_table_sample() { fn test_parse_not_null_in_column_options() { // In addition to DEFAULT and CHECK ClickHouse also supports MATERIALIZED, all of which // can contain `IS NOT NULL` and thus `NOT NULL` as an alias. - let canonical = concat!( + clickhouse().verified_stmt(concat!( "CREATE TABLE foo (", "abc INT DEFAULT (42 IS NOT NULL) NOT NULL,", " not_null BOOL MATERIALIZED (abc IS NOT NULL),", " CHECK (abc IS NOT NULL)", ")", - ); - clickhouse().verified_stmt(canonical); - clickhouse().one_statement_parses_to( - concat!( - "CREATE TABLE foo (", - "abc INT DEFAULT (42 NOT NULL) NOT NULL,", - " not_null BOOL MATERIALIZED (abc NOT NULL),", - " CHECK (abc NOT NULL)", - ")", - ), - canonical, - ); + )); + clickhouse().verified_stmt(concat!( + "CREATE TABLE foo (", + "abc INT DEFAULT (42 NOT NULL) NOT NULL,", + " not_null BOOL MATERIALIZED (abc NOT NULL),", + " CHECK (abc NOT NULL)", + ")", + )); } #[test] diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 729fbf5798..54c3e24341 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -2013,7 +2013,10 @@ fn parse_is_null() { use self::Expr::*; let sql = "a IS NULL"; assert_eq!( - IsNull(Box::new(Identifier(Ident::new("a")))), + IsNull { + expr: Box::new(Identifier(Ident::new("a"))), + syntax: IsNullSyntax::IsNull, + }, verified_expr(sql) ); } @@ -2023,7 +2026,10 @@ fn parse_is_not_null() { use self::Expr::*; let sql = "a IS NOT NULL"; assert_eq!( - IsNotNull(Box::new(Identifier(Ident::new("a")))), + IsNotNull { + expr: Box::new(Identifier(Ident::new("a"))), + syntax: IsNotNullSyntax::IsNotNull, + }, verified_expr(sql) ); } @@ -2141,10 +2147,13 @@ fn parse_is_distinct_from_precedence() { // `a IS NULL IS NULL` already parses. Deliberately more permissive than // PostgreSQL, which declares IS as %nonassoc and rejects the chain. assert_eq!( - IsNull(Box::new(IsDistinctFrom( - Box::new(Identifier(Ident::new("a"))), - Box::new(Identifier(Ident::new("b"))), - ))), + IsNull { + expr: Box::new(IsDistinctFrom( + Box::new(Identifier(Ident::new("a"))), + Box::new(Identifier(Ident::new("b"))), + )), + syntax: IsNullSyntax::IsNull, + }, verified_expr("a IS DISTINCT FROM b IS NULL") ); @@ -2413,15 +2422,18 @@ fn parse_ilike() { ); let select = verified_only_select(sql); assert_eq!( - Expr::IsNull(Box::new(Expr::ILike { - expr: Box::new(Expr::Identifier(Ident::new("name"))), - negated, - pattern: Box::new(Expr::Value( - (Value::SingleQuotedString("%a".to_string())).with_empty_span() - )), - escape_char: None, - any: false, - })), + Expr::IsNull { + expr: Box::new(Expr::ILike { + expr: Box::new(Expr::Identifier(Ident::new("name"))), + negated, + pattern: Box::new(Expr::Value( + (Value::SingleQuotedString("%a".to_string())).with_empty_span() + )), + escape_char: None, + any: false, + }), + syntax: IsNullSyntax::IsNull, + }, select.selection.unwrap() ); } @@ -2510,15 +2522,18 @@ fn parse_like() { ); let select = verified_only_select(sql); assert_eq!( - Expr::IsNull(Box::new(Expr::Like { - expr: Box::new(Expr::Identifier(Ident::new("name"))), - negated, - pattern: Box::new(Expr::Value( - (Value::SingleQuotedString("%a".to_string())).with_empty_span() - )), - escape_char: None, - any: false, - })), + Expr::IsNull { + expr: Box::new(Expr::Like { + expr: Box::new(Expr::Identifier(Ident::new("name"))), + negated, + pattern: Box::new(Expr::Value( + (Value::SingleQuotedString("%a".to_string())).with_empty_span() + )), + escape_char: None, + any: false, + }), + syntax: IsNullSyntax::IsNull, + }, select.selection.unwrap() ); } @@ -2595,16 +2610,19 @@ fn parse_similar_to() { ); let select = verified_only_select(sql); assert_eq!( - Expr::IsNull(Box::new(Expr::SimilarTo { - expr: Box::new(Expr::Identifier(Ident::new("name"))), - negated, - pattern: Box::new(Expr::Value( - (Value::SingleQuotedString("%a".to_string())).with_empty_span() - )), - escape_char: Some(Box::new(Expr::value( - Value::SingleQuotedString('^'.to_string()).with_empty_span(), - ))), - })), + Expr::IsNull { + expr: Box::new(Expr::SimilarTo { + expr: Box::new(Expr::Identifier(Ident::new("name"))), + negated, + pattern: Box::new(Expr::Value( + (Value::SingleQuotedString("%a".to_string())).with_empty_span() + )), + escape_char: Some(Box::new(Expr::value( + Value::SingleQuotedString('^'.to_string()).with_empty_span(), + ))), + }), + syntax: IsNullSyntax::IsNull, + }, select.selection.unwrap() ); } @@ -2967,20 +2985,23 @@ fn parse_between_with_expr() { let sql = "SELECT * FROM t WHERE 1 BETWEEN 1 + 2 AND 3 + 4 IS NULL"; let select = verified_only_select(sql); assert_eq!( - Expr::IsNull(Box::new(Expr::Between { - expr: Box::new(Expr::value(number("1"))), - low: Box::new(Expr::BinaryOp { - left: Box::new(Expr::value(number("1"))), - op: Plus, - right: Box::new(Expr::value(number("2"))), - }), - high: Box::new(Expr::BinaryOp { - left: Box::new(Expr::value(number("3"))), - op: Plus, - right: Box::new(Expr::value(number("4"))), + Expr::IsNull { + expr: Box::new(Expr::Between { + expr: Box::new(Expr::value(number("1"))), + low: Box::new(Expr::BinaryOp { + left: Box::new(Expr::value(number("1"))), + op: Plus, + right: Box::new(Expr::value(number("2"))), + }), + high: Box::new(Expr::BinaryOp { + left: Box::new(Expr::value(number("3"))), + op: Plus, + right: Box::new(Expr::value(number("4"))), + }), + negated: false, }), - negated: false, - })), + syntax: IsNullSyntax::IsNull, + }, select.selection.unwrap() ); @@ -8259,7 +8280,10 @@ fn parse_searched_case_expr() { operand: None, conditions: vec![ CaseWhen { - condition: IsNull(Box::new(Identifier(Ident::new("bar")))), + condition: IsNull { + expr: Box::new(Identifier(Ident::new("bar"))), + syntax: IsNullSyntax::IsNull, + }, result: Expr::value(Value::SingleQuotedString("null".to_string())), }, CaseWhen { @@ -14495,9 +14519,10 @@ fn test_selective_aggregation() { ))], clauses: vec![], }), - filter: Some(Box::new(Expr::IsNotNull(Box::new(Expr::Identifier( - Ident::new("name") - ))))), + filter: Some(Box::new(Expr::IsNotNull { + expr: Box::new(Expr::Identifier(Ident::new("name"))), + syntax: IsNotNullSyntax::IsNotNull, + })), over: None, within_group: vec![], null_treatment: None @@ -17489,7 +17514,13 @@ fn parse_composite_access_expr() { }; assert_eq!(stmt.projection[0], SelectItem::UnnamedExpr(expr.clone())); - assert_eq!(stmt.selection.unwrap(), Expr::IsNotNull(Box::new(expr))); + assert_eq!( + stmt.selection.unwrap(), + Expr::IsNotNull { + expr: Box::new(expr), + syntax: IsNotNullSyntax::IsNotNull, + } + ); // Compound access with quoted identifier. all_dialects_where(|d| d.is_delimited_identifier_start('"')) @@ -19821,18 +19852,19 @@ fn parse_create_procedure_with_parameter_modes() { #[test] fn parse_not_null() { - let _ = all_dialects().expr_parses_to("x NOT NULL", "x IS NOT NULL"); - let _ = all_dialects().expr_parses_to("NULL NOT NULL", "NULL IS NOT NULL"); + all_dialects().verified_expr("x NOT NULL"); + all_dialects().verified_expr("x NOT NULL + 1"); + all_dialects().verified_expr("NULL NOT NULL"); assert_matches!( - all_dialects().expr_parses_to("NOT NULL NOT NULL", "NOT NULL IS NOT NULL"), + all_dialects().verified_expr("NOT NULL NOT NULL"), Expr::UnaryOp { op: UnaryOperator::Not, .. } ); assert_matches!( - all_dialects().expr_parses_to("NOT x NOT NULL", "NOT x IS NOT NULL"), + all_dialects().verified_expr("NOT x NOT NULL"), Expr::UnaryOp { op: UnaryOperator::Not, .. @@ -20028,12 +20060,13 @@ fn parse_notnull() { // Supported dialects consider `x NOTNULL` as an alias for `x IS NOT NULL` let notnull_supported_dialects = all_dialects_where(|d| d.supports_notnull_operator()); - let _ = notnull_supported_dialects.expr_parses_to("x NOTNULL", "x IS NOT NULL"); + notnull_supported_dialects.verified_expr("x NOTNULL"); + notnull_supported_dialects.verified_expr("x NOTNULL + 1"); // For dialects which support it, `NOT NULL NOTNULL` should // parse as `(NOT (NULL IS NOT NULL))` assert_matches!( - notnull_supported_dialects.expr_parses_to("NOT NULL NOTNULL", "NOT NULL IS NOT NULL"), + notnull_supported_dialects.verified_expr("NOT NULL NOTNULL"), Expr::UnaryOp { op: UnaryOperator::Not, .. @@ -20055,26 +20088,17 @@ fn parse_isnull() { // Supported dialects consider `x ISNULL` as an alias for `x IS NULL` let isnull_supported_dialects = all_dialects_where(|d| d.supports_isnull_operator()); - let _ = isnull_supported_dialects.expr_parses_to("x ISNULL", "x IS NULL"); - isnull_supported_dialects - .one_statement_parses_to("SELECT a ISNULL FROM t", "SELECT a IS NULL FROM t"); - isnull_supported_dialects.one_statement_parses_to( - "SELECT a ISNULL AND b ISNULL FROM t", - "SELECT a IS NULL AND b IS NULL FROM t", - ); - isnull_supported_dialects.one_statement_parses_to( - "SELECT * FROM t WHERE a ISNULL", - "SELECT * FROM t WHERE a IS NULL", - ); - isnull_supported_dialects.one_statement_parses_to( - "SELECT CASE WHEN a ISNULL THEN 1 ELSE 2 END FROM t", - "SELECT CASE WHEN a IS NULL THEN 1 ELSE 2 END FROM t", - ); + isnull_supported_dialects.verified_expr("x ISNULL"); + isnull_supported_dialects.verified_expr("x ISNULL + 1"); + isnull_supported_dialects.verified_stmt("SELECT a ISNULL FROM t"); + isnull_supported_dialects.verified_stmt("SELECT a ISNULL AND b ISNULL FROM t"); + isnull_supported_dialects.verified_stmt("SELECT * FROM t WHERE a ISNULL"); + isnull_supported_dialects.verified_stmt("SELECT CASE WHEN a ISNULL THEN 1 ELSE 2 END FROM t"); // For dialects which support it, `NOT NULL ISNULL` should // parse as `(NOT (NULL IS NULL))` assert_matches!( - isnull_supported_dialects.expr_parses_to("NOT NULL ISNULL", "NOT NULL IS NULL"), + isnull_supported_dialects.verified_expr("NOT NULL ISNULL"), Expr::UnaryOp { op: UnaryOperator::Not, .. @@ -20249,26 +20273,22 @@ fn parse_create_view_if_not_exists() { #[test] fn test_parse_not_null_in_column_options() { - let canonical = concat!( + all_dialects().verified_stmt(concat!( "CREATE TABLE foo (", "abc INT DEFAULT (42 IS NOT NULL) NOT NULL,", " def INT,", " def_null BOOL GENERATED ALWAYS AS (def IS NOT NULL) STORED,", " CHECK (abc IS NOT NULL)", ")" - ); - all_dialects().verified_stmt(canonical); - all_dialects().one_statement_parses_to( - concat!( - "CREATE TABLE foo (", - "abc INT DEFAULT (42 NOT NULL) NOT NULL,", - " def INT,", - " def_null BOOL GENERATED ALWAYS AS (def NOT NULL) STORED,", - " CHECK (abc NOT NULL)", - ")" - ), - canonical, - ); + )); + all_dialects().verified_stmt(concat!( + "CREATE TABLE foo (", + "abc INT DEFAULT (42 NOT NULL) NOT NULL,", + " def INT,", + " def_null BOOL GENERATED ALWAYS AS (def NOT NULL) STORED,", + " CHECK (abc NOT NULL)", + ")" + )); } #[test] diff --git a/tests/sqlparser_databricks.rs b/tests/sqlparser_databricks.rs index ce26acc621..2717f2b69b 100644 --- a/tests/sqlparser_databricks.rs +++ b/tests/sqlparser_databricks.rs @@ -89,7 +89,10 @@ fn test_databricks_exists() { name: Ident::new("x"), data_type: None }), - body: Box::new(Expr::IsNull(Box::new(Expr::Identifier(Ident::new("x"))))), + body: Box::new(Expr::IsNull { + expr: Box::new(Expr::Identifier(Ident::new("x"))), + syntax: IsNullSyntax::IsNull, + }), syntax: LambdaSyntax::Arrow, }) ] diff --git a/tests/sqlparser_sqlite.rs b/tests/sqlparser_sqlite.rs index cef2b554a7..b17ad59056 100644 --- a/tests/sqlparser_sqlite.rs +++ b/tests/sqlparser_sqlite.rs @@ -1161,7 +1161,7 @@ fn parse_between_low_bound_precedence() { } sqlite().verified_expr("a BETWEEN b LIKE 'x' AND c"); sqlite().verified_expr("a BETWEEN b IS NOT NULL AND c"); - sqlite().expr_parses_to("a BETWEEN b NOTNULL AND c", "a BETWEEN b IS NOT NULL AND c"); + sqlite().verified_expr("a BETWEEN b NOTNULL AND c"); sqlite().verified_expr("a BETWEEN b BETWEEN 1 AND 2 AND c"); // Operators after the high bound apply to the whole BETWEEN.