Skip to content

Commit 2b53303

Browse files
authored
Merge branch 'main' into sql_comments
2 parents 2d6ab2e + f861566 commit 2b53303

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

src/ast/ddl.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use sqlparser_derive::{Visit, VisitMut};
3131
use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
3333
display_comma_separated, display_separated,
34-
table_constraints::{ForeignKeyConstraint, TableConstraint},
34+
table_constraints::{CheckConstraint, ForeignKeyConstraint, TableConstraint},
3535
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
3636
CreateFunctionUsing, CreateTableLikeKind, CreateTableOptions, CreateViewParams, DataType, Expr,
3737
FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier,
@@ -1571,7 +1571,7 @@ pub enum ColumnOption {
15711571
/// `).
15721572
ForeignKey(ForeignKeyConstraint),
15731573
/// `CHECK (<expr>)`
1574-
Check(Expr),
1574+
Check(CheckConstraint),
15751575
/// Dialect-specific options, such as:
15761576
/// - MySQL's `AUTO_INCREMENT` or SQLite's `AUTOINCREMENT`
15771577
/// - ...
@@ -1640,6 +1640,11 @@ pub enum ColumnOption {
16401640
Invisible,
16411641
}
16421642

1643+
impl From<CheckConstraint> for ColumnOption {
1644+
fn from(c: CheckConstraint) -> Self {
1645+
ColumnOption::Check(c)
1646+
}
1647+
}
16431648
impl From<ForeignKeyConstraint> for ColumnOption {
16441649
fn from(fk: ForeignKeyConstraint) -> Self {
16451650
ColumnOption::ForeignKey(fk)
@@ -1695,7 +1700,7 @@ impl fmt::Display for ColumnOption {
16951700
}
16961701
Ok(())
16971702
}
1698-
Check(expr) => write!(f, "CHECK ({expr})"),
1703+
Check(constraint) => write!(f, "{constraint}"),
16991704
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
17001705
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
17011706
Collation(n) => write!(f, "COLLATE {n}"),

src/ast/spans.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@ impl Spanned for ColumnOption {
741741
ColumnOption::Ephemeral(expr) => expr.as_ref().map_or(Span::empty(), |e| e.span()),
742742
ColumnOption::Alias(expr) => expr.span(),
743743
ColumnOption::Unique { .. } => Span::empty(),
744+
ColumnOption::Check(constraint) => constraint.span(),
744745
ColumnOption::ForeignKey(constraint) => constraint.span(),
745-
ColumnOption::Check(expr) => expr.span(),
746746
ColumnOption::DialectSpecific(_) => Span::empty(),
747747
ColumnOption::CharacterSet(object_name) => object_name.span(),
748748
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
@@ -8116,7 +8116,14 @@ impl<'a> Parser<'a> {
81168116
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
81178117
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
81188118
self.expect_token(&Token::RParen)?;
8119-
Ok(Some(ColumnOption::Check(expr)))
8119+
Ok(Some(
8120+
CheckConstraint {
8121+
name: None, // Column-level check constraints don't have names
8122+
expr: Box::new(expr),
8123+
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
8124+
}
8125+
.into(),
8126+
))
81208127
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
81218128
&& dialect_of!(self is MySqlDialect | GenericDialect)
81228129
{

tests/sqlparser_common.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3781,7 +3781,11 @@ fn parse_create_table() {
37813781
},
37823782
ColumnOptionDef {
37833783
name: None,
3784-
option: ColumnOption::Check(verified_expr("constrained > 0")),
3784+
option: ColumnOption::Check(CheckConstraint {
3785+
name: None,
3786+
expr: Box::new(verified_expr("constrained > 0")),
3787+
enforced: None,
3788+
}),
37853789
},
37863790
],
37873791
},

0 commit comments

Comments
 (0)