Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/sqlite_2840_not_null_display.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT a NOT NULL + 1;
64 changes: 60 additions & 4 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/// `<expr> IS NULL`
IsNull,
/// `<expr> 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 {
/// `<expr> IS NOT NULL`
IsNotNull,
/// `<expr> NOT NULL`
NotNull,
/// `<expr> 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: <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES>
Expand Down Expand Up @@ -1002,9 +1048,19 @@ pub enum Expr {
/// `IS NOT TRUE` operator
IsNotTrue(Box<Expr>),
/// `IS NULL` operator
IsNull(Box<Expr>),
IsNull {
/// The tested expression.
expr: Box<Expr>,
/// How the test was written.
syntax: IsNullSyntax,
},
/// `IS NOT NULL` operator
IsNotNull(Box<Expr>),
IsNotNull {
/// The tested expression.
expr: Box<Expr>,
/// How the test was written.
syntax: IsNotNullSyntax,
},
/// `IS UNKNOWN` operator
IsUnknown(Box<Expr>),
/// `IS NOT UNKNOWN` operator
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 20 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)?;
Expand Down
22 changes: 9 additions & 13 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Loading
Loading