Skip to content

Commit 80e0da1

Browse files
mvzinkayman-sigma
authored andcommitted
Add ENFORCED/NOT ENFORCED support for column-level CHECK constraints (apache#2180)
1 parent 1930417 commit 80e0da1

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

src/parser/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8953,11 +8953,20 @@ impl<'a> Parser<'a> {
89538953
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
89548954
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
89558955
self.expect_token(&Token::RParen)?;
8956+
8957+
let enforced = if self.parse_keyword(Keyword::ENFORCED) {
8958+
Some(true)
8959+
} else if self.parse_keywords(&[Keyword::NOT, Keyword::ENFORCED]) {
8960+
Some(false)
8961+
} else {
8962+
None
8963+
};
8964+
89568965
Ok(Some(
89578966
CheckConstraint {
89588967
name: None, // Column-level check constraints don't have names
89598968
expr: Box::new(expr),
8960-
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
8969+
enforced,
89618970
}
89628971
.into(),
89638972
))

tests/sqlparser_common.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16879,6 +16879,15 @@ fn check_enforced() {
1687916879
);
1688016880
}
1688116881

16882+
#[test]
16883+
fn column_check_enforced() {
16884+
all_dialects().verified_stmt("CREATE TABLE t (x INT CHECK (x > 1) NOT ENFORCED)");
16885+
all_dialects().verified_stmt("CREATE TABLE t (x INT CHECK (x > 1) ENFORCED)");
16886+
all_dialects().verified_stmt(
16887+
"CREATE TABLE t (a INT CHECK (a > 0) NOT ENFORCED, b INT CHECK (b > 0) ENFORCED, c INT CHECK (c > 0))",
16888+
);
16889+
}
16890+
1688216891
#[test]
1688316892
fn join_precedence() {
1688416893
all_dialects_except(|d| !d.supports_left_associative_joins_without_parens())

0 commit comments

Comments
 (0)