Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ use sqlparser_derive::{Visit, VisitMut};

use crate::ast::value::escape_single_quote_string;
use crate::ast::{
display_comma_separated, display_separated, table_constraints::TableConstraint, ArgMode,
CommentDef, ConditionalStatements, CreateFunctionBody, CreateFunctionUsing,
display_comma_separated, display_separated,
table_constraints::{CheckConstraint, TableConstraint},
ArgMode, CommentDef, ConditionalStatements, CreateFunctionBody, CreateFunctionUsing,
CreateTableLikeKind, CreateTableOptions, DataType, Expr, FileFormat, FunctionBehavior,
FunctionCalledOnNull, FunctionDeterminismSpecifier, FunctionParallel, HiveDistributionStyle,
HiveFormat, HiveIOFormat, HiveRowFormat, Ident, InitializeKind, MySQLColumnPosition,
Expand Down Expand Up @@ -1573,7 +1574,7 @@ pub enum ColumnOption {
characteristics: Option<ConstraintCharacteristics>,
},
/// `CHECK (<expr>)`
Check(Expr),
Check(CheckConstraint),
/// Dialect-specific options, such as:
/// - MySQL's `AUTO_INCREMENT` or SQLite's `AUTOINCREMENT`
/// - ...
Expand Down Expand Up @@ -1642,6 +1643,12 @@ pub enum ColumnOption {
Invisible,
}

impl From<CheckConstraint> for ColumnOption {
fn from(c: CheckConstraint) -> Self {
ColumnOption::Check(c)
}
}

impl fmt::Display for ColumnOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use ColumnOption::*;
Expand Down Expand Up @@ -1690,7 +1697,7 @@ impl fmt::Display for ColumnOption {
}
Ok(())
}
Check(expr) => write!(f, "CHECK ({expr})"),
Check(constraint) => write!(f, "{constraint}"),
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
Collation(n) => write!(f, "COLLATE {n}"),
Expand Down
2 changes: 1 addition & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,7 @@ impl Spanned for ColumnOption {
.chain(on_update.iter().map(|i| i.span()))
.chain(characteristics.iter().map(|i| i.span())),
),
ColumnOption::Check(expr) => expr.span(),
ColumnOption::Check(constraint) => constraint.span(),
ColumnOption::DialectSpecific(_) => Span::empty(),
ColumnOption::CharacterSet(object_name) => object_name.span(),
ColumnOption::Collation(object_name) => object_name.span(),
Expand Down
9 changes: 8 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8071,7 +8071,14 @@ impl<'a> Parser<'a> {
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
let expr: Expr = self.with_state(ParserState::Normal, |p| p.parse_expr())?;
self.expect_token(&Token::RParen)?;
Ok(Some(ColumnOption::Check(expr)))
Ok(Some(
CheckConstraint {
name: None, // Column-level check constraints don't have names
expr: Box::new(expr),
enforced: None, // Could be extended later to support MySQL ENFORCED/NOT ENFORCED
}
.into(),
))
} else if self.parse_keyword(Keyword::AUTO_INCREMENT)
&& dialect_of!(self is MySqlDialect | GenericDialect)
{
Expand Down
6 changes: 5 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3781,7 +3781,11 @@ fn parse_create_table() {
},
ColumnOptionDef {
name: None,
option: ColumnOption::Check(verified_expr("constrained > 0")),
option: ColumnOption::Check(CheckConstraint {
name: None,
expr: Box::new(verified_expr("constrained > 0")),
enforced: None,
}),
},
],
},
Expand Down