This is an automated email from the ASF dual-hosted git repository.

iffyio pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion-sqlparser-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 84c3a1b3 MySQL: `[[NOT] ENFORCED]` in CHECK constraint (#1870)
84c3a1b3 is described below

commit 84c3a1b325c39c879b68ab712e3b9b3e3e40ed56
Author: Mohamed Abdeen <83442793+mohamedabdee...@users.noreply.github.com>
AuthorDate: Sat Jun 7 05:48:40 2025 +0100

    MySQL: `[[NOT] ENFORCED]` in CHECK constraint (#1870)
---
 src/ast/ddl.rs              | 18 +++++++++++++++---
 src/ast/spans.rs            |  8 +++++---
 src/parser/mod.rs           | 15 ++++++++++++++-
 tests/sqlparser_common.rs   |  7 +++++++
 tests/sqlparser_postgres.rs |  5 +++++
 5 files changed, 46 insertions(+), 7 deletions(-)

diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs
index bbc15704..b0a3708c 100644
--- a/src/ast/ddl.rs
+++ b/src/ast/ddl.rs
@@ -1029,10 +1029,13 @@ pub enum TableConstraint {
         on_update: Option<ReferentialAction>,
         characteristics: Option<ConstraintCharacteristics>,
     },
-    /// `[ CONSTRAINT <name> ] CHECK (<expr>)`
+    /// `[ CONSTRAINT <name> ] CHECK (<expr>) [[NOT] ENFORCED]`
     Check {
         name: Option<Ident>,
         expr: Box<Expr>,
+        /// MySQL-specific syntax
+        /// <https://dev.mysql.com/doc/refman/8.4/en/create-table.html>
+        enforced: Option<bool>,
     },
     /// MySQLs [index definition][1] for index creation. Not present on ANSI 
so, for now, the usage
     /// is restricted to MySQL, as no other dialects that support this syntax 
were found.
@@ -1162,8 +1165,17 @@ impl fmt::Display for TableConstraint {
                 }
                 Ok(())
             }
-            TableConstraint::Check { name, expr } => {
-                write!(f, "{}CHECK ({})", display_constraint_name(name), expr)
+            TableConstraint::Check {
+                name,
+                expr,
+                enforced,
+            } => {
+                write!(f, "{}CHECK ({})", display_constraint_name(name), 
expr)?;
+                if let Some(b) = enforced {
+                    write!(f, " {}", if *b { "ENFORCED" } else { "NOT 
ENFORCED" })
+                } else {
+                    Ok(())
+                }
             }
             TableConstraint::Index {
                 display_as_key,
diff --git a/src/ast/spans.rs b/src/ast/spans.rs
index f957194a..39d30df9 100644
--- a/src/ast/spans.rs
+++ b/src/ast/spans.rs
@@ -687,9 +687,11 @@ impl Spanned for TableConstraint {
                     .chain(on_update.iter().map(|i| i.span()))
                     .chain(characteristics.iter().map(|i| i.span())),
             ),
-            TableConstraint::Check { name, expr } => {
-                expr.span().union_opt(&name.as_ref().map(|i| i.span))
-            }
+            TableConstraint::Check {
+                name,
+                expr,
+                enforced: _,
+            } => expr.span().union_opt(&name.as_ref().map(|i| i.span)),
             TableConstraint::Index {
                 display_as_key: _,
                 name,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs
index f8c307dc..9cef22ed 100644
--- a/src/parser/mod.rs
+++ b/src/parser/mod.rs
@@ -8139,7 +8139,20 @@ impl<'a> Parser<'a> {
                 self.expect_token(&Token::LParen)?;
                 let expr = Box::new(self.parse_expr()?);
                 self.expect_token(&Token::RParen)?;
-                Ok(Some(TableConstraint::Check { name, expr }))
+
+                let enforced = if self.parse_keyword(Keyword::ENFORCED) {
+                    Some(true)
+                } else if self.parse_keywords(&[Keyword::NOT, 
Keyword::ENFORCED]) {
+                    Some(false)
+                } else {
+                    None
+                };
+
+                Ok(Some(TableConstraint::Check {
+                    name,
+                    expr,
+                    enforced,
+                }))
             }
             Token::Word(w)
                 if (w.keyword == Keyword::INDEX || w.keyword == Keyword::KEY)
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs
index 5b96dcd7..399fdb3d 100644
--- a/tests/sqlparser_common.rs
+++ b/tests/sqlparser_common.rs
@@ -15336,3 +15336,10 @@ fn parse_truncate_only() {
         truncate
     );
 }
+
+#[test]
+fn check_enforced() {
+    all_dialects().verified_stmt(
+        "CREATE TABLE t (a INT, b INT, c INT, CHECK (a > 0) NOT ENFORCED, 
CHECK (b > 0) ENFORCED, CHECK (c > 0))",
+    );
+}
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs
index c50f066a..6f0ba9c6 100644
--- a/tests/sqlparser_postgres.rs
+++ b/tests/sqlparser_postgres.rs
@@ -5378,6 +5378,7 @@ fn parse_create_domain() {
                 op: BinaryOperator::Gt,
                 right: Box::new(Expr::Value(test_utils::number("0").into())),
             }),
+            enforced: None,
         }],
     });
 
@@ -5396,6 +5397,7 @@ fn parse_create_domain() {
                 op: BinaryOperator::Gt,
                 right: Box::new(Expr::Value(test_utils::number("0").into())),
             }),
+            enforced: None,
         }],
     });
 
@@ -5414,6 +5416,7 @@ fn parse_create_domain() {
                 op: BinaryOperator::Gt,
                 right: Box::new(Expr::Value(test_utils::number("0").into())),
             }),
+            enforced: None,
         }],
     });
 
@@ -5432,6 +5435,7 @@ fn parse_create_domain() {
                 op: BinaryOperator::Gt,
                 right: Box::new(Expr::Value(test_utils::number("0").into())),
             }),
+            enforced: None,
         }],
     });
 
@@ -5450,6 +5454,7 @@ fn parse_create_domain() {
                 op: BinaryOperator::Gt,
                 right: Box::new(Expr::Value(test_utils::number("0").into())),
             }),
+            enforced: None,
         }],
     });
 


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@datafusion.apache.org
For additional commands, e-mail: commits-h...@datafusion.apache.org

Reply via email to