diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 9c74cc62af..8826c3f9d8 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1019,6 +1019,9 @@ pub trait Dialect: Debug + Any { Token::Word(w) if w.keyword == Keyword::NOTNULL && self.supports_notnull_operator() => { Ok(p!(Is)) } + Token::Word(w) if w.keyword == Keyword::ISNULL && self.supports_isnull_operator() => { + Ok(p!(Is)) + } Token::Word(w) if w.keyword == Keyword::IS => Ok(p!(Is)), Token::Word(w) if w.keyword == Keyword::GLOBAL && self.supports_global_in() => { match ( @@ -1950,6 +1953,12 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports the `x ISNULL` + /// operator expression. + fn supports_isnull_operator(&self) -> bool { + false + } + /// Returns true if this dialect allows an optional `SIGNED` suffix after integer data types. /// /// Example: diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index 361396b5fe..7dd6febefd 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -187,6 +187,7 @@ const TYPE_FUNC_NAME_KEYWORDS: &[Keyword] = &[ Keyword::ILIKE, Keyword::INNER, Keyword::IS, + Keyword::ISNULL, Keyword::JOIN, Keyword::LEFT, Keyword::LIKE, @@ -550,6 +551,12 @@ impl Dialect for PostgreSqlDialect { true } + /// Postgres supports `ISNULL` as an alias for `IS NULL` + /// See: + fn supports_isnull_operator(&self) -> bool { + true + } + /// [Postgres] supports optional field and precision options for `INTERVAL` data type. /// /// [Postgres]: https://www.postgresql.org/docs/17/datatype-datetime.html diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index 30ea60c58a..78a9a616d4 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -169,6 +169,12 @@ impl Dialect for SQLiteDialect { true } + /// SQLite supports `ISNULL` as an alias for `IS NULL` + /// See: + fn supports_isnull_operator(&self) -> bool { + true + } + fn supports_comma_separated_trim(&self) -> bool { true } @@ -298,6 +304,7 @@ impl Dialect for SQLiteDialect { | Keyword::INTERSECT | Keyword::INTO | Keyword::IS + | Keyword::ISNULL | Keyword::JOIN | Keyword::LIMIT | Keyword::NOT diff --git a/src/keywords.rs b/src/keywords.rs index 1040701815..73d6a74982 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -571,6 +571,7 @@ define_keywords!( INVOKER, IO, IS, + ISNULL, ISODOW, ISOLATION, ISOWEEK, diff --git a/src/parser/mod.rs b/src/parser/mod.rs index e1d8fb53ce..ff200f3e18 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4322,6 +4322,9 @@ impl<'a> Parser<'a> { 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::MEMBER => { if self.parse_keyword(Keyword::OF) { self.expect_token(&Token::LParen)?; diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 38a4a159d2..729fbf5798 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -20044,6 +20044,47 @@ fn parse_notnull() { notnull_unsupported_dialects.expr_parses_to("NOT NULL NOTNULL", "NOT NULL"); } +#[test] +fn parse_isnull() { + // Some dialects support the postfix `x ISNULL` operator while others + // consider `x ISNULL` like `x AS ISNULL` and thus consider `ISNULL` an alias for x. + let isnull_unsupported_dialects = all_dialects_except(|d| d.supports_isnull_operator()); + let _ = isnull_unsupported_dialects + .verified_only_select_with_canonical("SELECT NULL ISNULL", "SELECT NULL AS ISNULL"); + isnull_unsupported_dialects.verified_stmt("SELECT isnull FROM t"); + + // 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", + ); + + // 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"), + Expr::UnaryOp { + op: UnaryOperator::Not, + .. + } + ); + + // for unsupported dialects, parsing should stop at `NOT NULL` + isnull_unsupported_dialects.expr_parses_to("NOT NULL ISNULL", "NOT NULL"); +} + #[test] fn parse_odbc_time_date_timestamp() { // Supported statements diff --git a/tests/sqlparser_mssql.rs b/tests/sqlparser_mssql.rs index cadcabf035..c0b711a560 100644 --- a/tests/sqlparser_mssql.rs +++ b/tests/sqlparser_mssql.rs @@ -2437,6 +2437,15 @@ fn parse_mssql_merge_with_output() { ms_and_generic().verified_stmt(stmt); } +#[test] +fn parse_mssql_isnull_function() { + // The two-argument `ISNULL(a, b)` function must not be read as + // the postfix `x ISNULL` operator in dialects that lack it. + ms_and_generic().verified_stmt("SELECT ISNULL(a, b) FROM t"); + ms_and_generic() + .verified_only_select_with_canonical("SELECT a ISNULL FROM t", "SELECT a AS ISNULL FROM t"); +} + #[test] fn parse_create_trigger() { let create_trigger = "\