-
Notifications
You must be signed in to change notification settings - Fork 621
DuckDB, Postgres, SQLite: NOT NULL and NOTNULL expressions #1927
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
d6f51a1
f022dfa
2bf08db
431fca3
637fb69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -608,6 +608,7 @@ define_keywords!( | |
NOT, | ||
NOTHING, | ||
NOTIFY, | ||
NOTNULL, | ||
NOWAIT, | ||
NO_WRITE_TO_BINLOG, | ||
NTH_VALUE, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15974,3 +15974,135 @@ fn parse_create_procedure_with_parameter_modes() { | |
_ => unreachable!(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn parse_not_null_unsupported() { | ||
// Only DuckDB and SQLite support `x NOT NULL` as an expression | ||
// All other dialects fail to parse. | ||
let sql = r#"WITH t AS (SELECT NULL AS x) SELECT x NOT NULL FROM t"#; | ||
let dialects = all_dialects_except(|d| d.supports_not_null()); | ||
let res = dialects.parse_sql_statements(sql); | ||
assert_eq!( | ||
ParserError::ParserError("Expected: end of statement, found: NULL".to_string()), | ||
res.unwrap_err() | ||
); | ||
} | ||
|
||
#[test] | ||
fn parse_not_null_supported() { | ||
// DuckDB and SQLite support `x NOT NULL` as an expression | ||
let sql = r#"WITH t AS (SELECT NULL AS x) SELECT x NOT NULL FROM t"#; | ||
let dialects = all_dialects_where(|d| d.supports_not_null()); | ||
let stmt = dialects.one_statement_parses_to(sql, sql); | ||
match stmt { | ||
Statement::Query(qry) => match *qry.body { | ||
SetExpr::Select(select) => { | ||
assert_eq!(select.projection.len(), 1); | ||
match select.projection.first().unwrap() { | ||
UnnamedExpr(expr) => { | ||
let fake_span = Span { | ||
start: Location { line: 0, column: 0 }, | ||
end: Location { line: 0, column: 0 }, | ||
}; | ||
assert_eq!( | ||
*expr, | ||
Expr::NotNull { | ||
expr: Box::new(Identifier(Ident { | ||
value: "x".to_string(), | ||
quote_style: None, | ||
span: fake_span, | ||
})), | ||
one_word: false, | ||
}, | ||
); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
_ => unreachable!(), | ||
}, | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn parse_notnull_unsupported() { | ||
// Only Postgres, DuckDB, and SQLite support `x NOTNULL` as an expression | ||
// All other dialects consider `x NOTNULL` like `x AS NOTNULL` and thus | ||
// consider `NOTNULL` an alias for x. | ||
let sql = r#"WITH t AS (SELECT NULL AS x) SELECT x NOTNULL FROM t"#; | ||
let canonical = r#"WITH t AS (SELECT NULL AS x) SELECT x AS NOTNULL FROM t"#; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can simplify this test, since we're only interested in testing the expression, doing so via a CTE makes the test verbose and harder to maintain going forward without providing additional coverage (note we also have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! I simplified the tests in 431fca3, thanks for the pointers they are much more readable now! |
||
let dialects = all_dialects_except(|d| d.supports_notnull()); | ||
let stmt = dialects.one_statement_parses_to(sql, canonical); | ||
match stmt { | ||
Statement::Query(qry) => match *qry.body { | ||
SetExpr::Select(select) => { | ||
assert_eq!(select.projection.len(), 1); | ||
match select.projection.first().unwrap() { | ||
SelectItem::ExprWithAlias { expr, alias } => { | ||
let fake_span = Span { | ||
start: Location { line: 0, column: 0 }, | ||
end: Location { line: 0, column: 0 }, | ||
}; | ||
assert_eq!( | ||
*expr, | ||
Identifier(Ident { | ||
value: "x".to_string(), | ||
quote_style: None, | ||
span: fake_span, | ||
}) | ||
); | ||
assert_eq!( | ||
*alias, | ||
Ident { | ||
value: "NOTNULL".to_string(), | ||
quote_style: None, | ||
span: fake_span, | ||
} | ||
); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
_ => unreachable!(), | ||
}, | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
#[test] | ||
fn parse_notnull_supported() { | ||
// DuckDB and SQLite support `x NOT NULL` as an expression | ||
let sql = r#"WITH t AS (SELECT NULL AS x) SELECT x NOTNULL FROM t"#; | ||
let dialects = all_dialects_where(|d| d.supports_notnull()); | ||
let stmt = dialects.one_statement_parses_to(sql, ""); | ||
match stmt { | ||
Statement::Query(qry) => match *qry.body { | ||
SetExpr::Select(select) => { | ||
assert_eq!(select.projection.len(), 1); | ||
match select.projection.first().unwrap() { | ||
UnnamedExpr(expr) => { | ||
let fake_span = Span { | ||
start: Location { line: 0, column: 0 }, | ||
end: Location { line: 0, column: 0 }, | ||
}; | ||
assert_eq!( | ||
*expr, | ||
Expr::NotNull { | ||
expr: Box::new(Identifier(Ident { | ||
value: "x".to_string(), | ||
quote_style: None, | ||
span: fake_span, | ||
})), | ||
one_word: true, | ||
}, | ||
); | ||
} | ||
_ => unreachable!(), | ||
} | ||
} | ||
_ => unreachable!(), | ||
}, | ||
_ => unreachable!(), | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.