Skip to content

Commit 0daa672

Browse files
Merge unify-check-constraint into future-main
2 parents 63d7153 + fd69a92 commit 0daa672

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/ast/ddl.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ use crate::ast::table_constraints::TableConstraint;
3333
use crate::ast::value::escape_single_quote_string;
3434
use crate::ast::{
3535
display_comma_separated, display_separated,
36-
table_constraints::{ForeignKeyConstraint, PrimaryKeyConstraint, UniqueConstraint},
36+
table_constraints::{
37+
CheckConstraint, ForeignKeyConstraint, PrimaryKeyConstraint, UniqueConstraint,
38+
},
3739
ArgMode, CommentDef, ConditionalStatements, CreateFunctionBody, CreateFunctionUsing,
3840
CreateTableLikeKind, CreateTableOptions, DataType, Expr, FileFormat, FunctionBehavior,
3941
FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, HiveDistributionStyle,
@@ -1585,7 +1587,7 @@ pub enum ColumnOption {
15851587
/// `).
15861588
ForeignKey(ForeignKeyConstraint),
15871589
/// `CHECK (<expr>)`
1588-
Check(Expr),
1590+
Check(CheckConstraint),
15891591
/// Dialect-specific options, such as:
15901592
/// - MySQL's `AUTO_INCREMENT` or SQLite's `AUTOINCREMENT`
15911593
/// - ...
@@ -1670,6 +1672,11 @@ impl From<PrimaryKeyConstraint> for ColumnOption {
16701672
ColumnOption::PrimaryKey(c)
16711673
}
16721674
}
1675+
impl From<CheckConstraint> for ColumnOption {
1676+
fn from(c: CheckConstraint) -> Self {
1677+
ColumnOption::Check(c)
1678+
}
1679+
}
16731680

16741681
impl fmt::Display for ColumnOption {
16751682
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -1724,7 +1731,7 @@ impl fmt::Display for ColumnOption {
17241731
}
17251732
Ok(())
17261733
}
1727-
Check(expr) => write!(f, "CHECK ({expr})"),
1734+
Check(constraint) => write!(f, "{constraint}"),
17281735
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
17291736
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
17301737
Collation(n) => write!(f, "COLLATE {n}"),

src/ast/spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl Spanned for ColumnOption {
744744
ColumnOption::PrimaryKey(constraint) => constraint.span(),
745745
ColumnOption::Unique(constraint) => constraint.span(),
746746
ColumnOption::ForeignKey(constraint) => constraint.span(),
747-
ColumnOption::Check(expr) => expr.span(),
747+
ColumnOption::Check(constraint) => constraint.span(),
748748
ColumnOption::DialectSpecific(_) => Span::empty(),
749749
ColumnOption::CharacterSet(object_name) => object_name.span(),
750750
ColumnOption::Collation(object_name) => object_name.span(),

src/parser/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8128,7 +8128,14 @@ impl<'a> Parser<'a> {
81288128
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
81298129
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
81308130
self.expect_token(&Token::RParen)?;
8131-
Ok(Some(ColumnOption::Check(expr)))
8131+
Ok(Some(
8132+
CheckConstraint {
8133+
name: None, // Column-level check constraints don't have names
8134+
expr: Box::new(expr),
8135+
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
8136+
}
8137+
.into(),
8138+
))
81328139
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
81338140
&& dialect_of!(self is MySqlDialect | GenericDialect)
81348141
{

tests/sqlparser_common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3791,7 +3791,11 @@ fn parse_create_table() {
37913791
},
37923792
ColumnOptionDef {
37933793
name: None,
3794-
option: ColumnOption::Check(verified_expr("constrained > 0")),
3794+
option: ColumnOption::Check(CheckConstraint {
3795+
name: None,
3796+
expr: Box::new(verified_expr("constrained > 0")),
3797+
enforced: None,
3798+
}),
37953799
},
37963800
],
37973801
},

0 commit comments

Comments
 (0)