Skip to content

Commit 95f0086

Browse files
Merge branch 'main' into unique-unique-constraint
2 parents 961b1be + f861566 commit 95f0086

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

src/ast/ddl.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
3333
display_comma_separated, display_separated,
3434
table_constraints::{
35-
ForeignKeyConstraint, PrimaryKeyConstraint, TableConstraint, UniqueConstraint,
35+
CheckConstraint, ForeignKeyConstraint, PrimaryKeyConstraint, TableConstraint,
36+
UniqueConstraint,
3637
},
3738
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
3839
CreateFunctionUsing, CreateTableLikeKind, CreateTableOptions, CreateViewParams, DataType, Expr,
@@ -1586,7 +1587,7 @@ pub enum ColumnOption {
15861587
/// `).
15871588
ForeignKey(ForeignKeyConstraint),
15881589
/// `CHECK (<expr>)`
1589-
Check(Expr),
1590+
Check(CheckConstraint),
15901591
/// Dialect-specific options, such as:
15911592
/// - MySQL's `AUTO_INCREMENT` or SQLite's `AUTOINCREMENT`
15921593
/// - ...
@@ -1667,6 +1668,11 @@ impl From<PrimaryKeyConstraint> for ColumnOption {
16671668
}
16681669
}
16691670

1671+
impl From<CheckConstraint> for ColumnOption {
1672+
fn from(c: CheckConstraint) -> Self {
1673+
ColumnOption::Check(c)
1674+
}
1675+
}
16701676
impl From<ForeignKeyConstraint> for ColumnOption {
16711677
fn from(fk: ForeignKeyConstraint) -> Self {
16721678
ColumnOption::ForeignKey(fk)
@@ -1726,7 +1732,7 @@ impl fmt::Display for ColumnOption {
17261732
}
17271733
Ok(())
17281734
}
1729-
Check(expr) => write!(f, "CHECK ({expr})"),
1735+
Check(constraint) => write!(f, "{constraint}"),
17301736
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
17311737
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
17321738
Collation(n) => write!(f, "COLLATE {n}"),

src/ast/spans.rs

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

src/ast/visitor.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,10 @@ visit_noop!(bigdecimal::BigDecimal);
182182
/// ```
183183
pub trait Visitor {
184184
/// Type returned when the recursion returns early.
185+
///
186+
/// Important note: The `Break` type should be kept as small as possible to prevent
187+
/// stack overflow during recursion. If you need to return an error, consider
188+
/// boxing it with `Box` to minimize stack usage.
185189
type Break;
186190

187191
/// Invoked for any queries that appear in the AST before visiting children
@@ -290,6 +294,10 @@ pub trait Visitor {
290294
/// ```
291295
pub trait VisitorMut {
292296
/// Type returned when the recursion returns early.
297+
///
298+
/// Important note: The `Break` type should be kept as small as possible to prevent
299+
/// stack overflow during recursion. If you need to return an error, consider
300+
/// boxing it with `Box` to minimize stack usage.
293301
type Break;
294302

295303
/// Invoked for any queries that appear in the AST before visiting children

src/parser/mod.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8120,7 +8120,14 @@ impl<'a> Parser<'a> {
81208120
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
81218121
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
81228122
self.expect_token(&Token::RParen)?;
8123-
Ok(Some(ColumnOption::Check(expr)))
8123+
Ok(Some(
8124+
CheckConstraint {
8125+
name: None, // Column-level check constraints don't have names
8126+
expr: Box::new(expr),
8127+
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
8128+
}
8129+
.into(),
8130+
))
81248131
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
81258132
&& dialect_of!(self is MySqlDialect | GenericDialect)
81268133
{

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)