Skip to content

Commit c8b7f7c

Browse files
authored
Add ENFORCED/NOT ENFORCED support for column-level CHECK constraints (apache#2180)
1 parent ed983e0 commit c8b7f7c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/parser/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8922,11 +8922,20 @@ impl<'a> Parser<'a> {
89228922
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
89238923
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
89248924
self.expect_token(&Token::RParen)?;
8925+
8926+
let enforced = if self.parse_keyword(Keyword::ENFORCED) {
8927+
Some(true)
8928+
} else if self.parse_keywords(&[Keyword::NOT, Keyword::ENFORCED]) {
8929+
Some(false)
8930+
} else {
8931+
None
8932+
};
8933+
89258934
Ok(Some(
89268935
CheckConstraint {
89278936
name: None, // Column-level check constraints don't have names
89288937
expr: Box::new(expr),
8929-
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
8938+
enforced,
89308939
}
89318940
.into(),
89328941
))

tests/sqlparser_common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16853,6 +16853,15 @@ fn check_enforced() {
1685316853
);
1685416854
}
1685516855

16856+
#[test]
16857+
fn column_check_enforced() {
16858+
all_dialects().verified_stmt("CREATE TABLE t (x INT CHECK (x > 1) NOT ENFORCED)");
16859+
all_dialects().verified_stmt("CREATE TABLE t (x INT CHECK (x > 1) ENFORCED)");
16860+
all_dialects().verified_stmt(
16861+
"CREATE TABLE t (a INT CHECK (a > 0) NOT ENFORCED, b INT CHECK (b > 0) ENFORCED, c INT CHECK (c > 0))",
16862+
);
16863+
}
16864+
1685616865
#[test]
1685716866
fn join_precedence() {
1685816867
all_dialects_except(|d| !d.supports_left_associative_joins_without_parens())

0 commit comments

Comments
 (0)