Skip to content

Commit 569653b

Browse files
parse NOT VALID, update tests
1 parent 0e3263a commit 569653b

File tree

6 files changed

+74
-22
lines changed

6 files changed

+74
-22
lines changed

src/ast/ddl.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,11 @@ impl fmt::Display for ReplicaIdentity {
6767
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
6868
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
6969
pub enum AlterTableOperation {
70-
/// `ADD <table_constraint>`
71-
AddConstraint(TableConstraint),
70+
/// `ADD <table_constraint> [NOT VALID]`
71+
AddConstraint {
72+
constraint: TableConstraint,
73+
not_valid: bool,
74+
},
7275
/// `ADD [COLUMN] [IF NOT EXISTS] <column_def>`
7376
AddColumn {
7477
/// `[COLUMN]`.
@@ -494,7 +497,16 @@ impl fmt::Display for AlterTableOperation {
494497
display_separated(new_partitions, " "),
495498
ine = if *if_not_exists { " IF NOT EXISTS" } else { "" }
496499
),
497-
AlterTableOperation::AddConstraint(c) => write!(f, "ADD {c}"),
500+
AlterTableOperation::AddConstraint {
501+
not_valid,
502+
constraint,
503+
} => {
504+
write!(f, "ADD {constraint}")?;
505+
if *not_valid {
506+
write!(f, " NOT VALID")?;
507+
}
508+
Ok(())
509+
}
498510
AlterTableOperation::AddColumn {
499511
column_keyword,
500512
if_not_exists,

src/ast/spans.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,10 @@ impl Spanned for CreateTableOptions {
10741074
impl Spanned for AlterTableOperation {
10751075
fn span(&self) -> Span {
10761076
match self {
1077-
AlterTableOperation::AddConstraint(table_constraint) => table_constraint.span(),
1077+
AlterTableOperation::AddConstraint {
1078+
constraint,
1079+
not_valid: _,
1080+
} => constraint.span(),
10781081
AlterTableOperation::AddColumn {
10791082
column_keyword: _,
10801083
if_not_exists: _,

src/parser/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8477,7 +8477,11 @@ impl<'a> Parser<'a> {
84778477
pub fn parse_alter_table_operation(&mut self) -> Result<AlterTableOperation, ParserError> {
84788478
let operation = if self.parse_keyword(Keyword::ADD) {
84798479
if let Some(constraint) = self.parse_optional_table_constraint()? {
8480-
AlterTableOperation::AddConstraint(constraint)
8480+
let not_valid = self.parse_keywords(&[Keyword::NOT, Keyword::VALID]);
8481+
AlterTableOperation::AddConstraint {
8482+
constraint,
8483+
not_valid,
8484+
}
84818485
} else if dialect_of!(self is ClickHouseDialect|GenericDialect)
84828486
&& self.parse_keyword(Keyword::PROJECTION)
84838487
{

src/test_utils.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -474,20 +474,25 @@ pub fn index_column(stmt: Statement) -> Expr {
474474
}
475475
}
476476
Statement::AlterTable { operations, .. } => match operations.first().unwrap() {
477-
AlterTableOperation::AddConstraint(TableConstraint::Index { columns, .. }) => {
478-
columns.first().unwrap().column.expr.clone()
479-
}
480-
AlterTableOperation::AddConstraint(TableConstraint::Unique { columns, .. }) => {
481-
columns.first().unwrap().column.expr.clone()
482-
}
483-
AlterTableOperation::AddConstraint(TableConstraint::PrimaryKey { columns, .. }) => {
484-
columns.first().unwrap().column.expr.clone()
477+
AlterTableOperation::AddConstraint { constraint, .. } => {
478+
match constraint {
479+
TableConstraint::Index { columns, .. } => {
480+
columns.first().unwrap().column.expr.clone()
481+
}
482+
TableConstraint::Unique { columns, .. } => {
483+
columns.first().unwrap().column.expr.clone()
484+
}
485+
TableConstraint::PrimaryKey { columns, .. } => {
486+
columns.first().unwrap().column.expr.clone()
487+
}
488+
TableConstraint::FulltextOrSpatial {
489+
columns,
490+
..
491+
} => columns.first().unwrap().column.expr.clone(),
492+
_ => panic!("Expected an index, unique, primary, full text, or spatial constraint (foreign key does not support general key part expressions)"),
493+
}
485494
}
486-
AlterTableOperation::AddConstraint(TableConstraint::FulltextOrSpatial {
487-
columns,
488-
..
489-
}) => columns.first().unwrap().column.expr.clone(),
490-
_ => panic!("Expected an index, unique, primary, full text, or spatial constraint (foreign key does not support general key part expressions)"),
495+
_ => panic!("Expected a constraint"),
491496
},
492497
_ => panic!("Expected CREATE INDEX, ALTER TABLE, or CREATE TABLE, got: {stmt:?}"),
493498
}

tests/sqlparser_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4956,7 +4956,7 @@ fn parse_alter_table_constraints() {
49564956
match alter_table_op(verified_stmt(&format!(
49574957
"ALTER TABLE tab ADD {constraint_text}"
49584958
))) {
4959-
AlterTableOperation::AddConstraint(constraint) => {
4959+
AlterTableOperation::AddConstraint { constraint, .. } => {
49604960
assert_eq!(constraint_text, constraint.to_string());
49614961
}
49624962
_ => unreachable!(),

tests/sqlparser_postgres.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -606,9 +606,10 @@ fn parse_alter_table_constraints_unique_nulls_distinct() {
606606
.verified_stmt("ALTER TABLE t ADD CONSTRAINT b UNIQUE NULLS NOT DISTINCT (c)")
607607
{
608608
Statement::AlterTable { operations, .. } => match &operations[0] {
609-
AlterTableOperation::AddConstraint(TableConstraint::Unique {
610-
nulls_distinct, ..
611-
}) => {
609+
AlterTableOperation::AddConstraint {
610+
constraint: TableConstraint::Unique { nulls_distinct, .. },
611+
..
612+
} => {
612613
assert_eq!(nulls_distinct, &NullsDistinctOption::NotDistinct)
613614
}
614615
_ => unreachable!(),
@@ -6232,3 +6233,30 @@ fn parse_ts_datatypes() {
62326233
_ => unreachable!(),
62336234
}
62346235
}
6236+
6237+
#[test]
6238+
fn parse_alter_table_constraint_not_valid() {
6239+
match pg_and_generic().verified_stmt(
6240+
"ALTER TABLE foo ADD CONSTRAINT bar FOREIGN KEY (baz) REFERENCES other(ref) NOT VALID",
6241+
) {
6242+
Statement::AlterTable { operations, .. } => {
6243+
assert_eq!(
6244+
operations,
6245+
vec![AlterTableOperation::AddConstraint {
6246+
constraint: TableConstraint::ForeignKey {
6247+
name: Some("bar".into()),
6248+
index_name: None,
6249+
columns: vec!["baz".into()],
6250+
foreign_table: ObjectName::from(vec!["other".into()]),
6251+
referred_columns: vec!["ref".into()],
6252+
on_delete: None,
6253+
on_update: None,
6254+
characteristics: None,
6255+
},
6256+
not_valid: true,
6257+
}]
6258+
);
6259+
}
6260+
_ => unreachable!(),
6261+
}
6262+
}

0 commit comments

Comments
 (0)