Skip to content

Commit ae2fd4a

Browse files
committed
Add ColumnDef::clear_constraint_characteristics
Counterpart to TableConstraint::clear_characteristics for inline column constraints, so a consumer targeting a dialect without the constraint characteristics grammar can drop them from a column list in one call instead of hand-matching the constraint-bearing ColumnOption variants.
1 parent 0e5e26e commit ae2fd4a

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/ast/ddl.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,42 @@ pub struct ColumnDef {
16251625
pub options: Vec<ColumnOptionDef>,
16261626
}
16271627

1628+
impl ColumnDef {
1629+
/// Drop any [`ConstraintCharacteristics`] the column's inline constraints
1630+
/// carry, so they render as bare constraints. Counterpart to
1631+
/// [`TableConstraint::clear_characteristics`] for dialects whose grammar has
1632+
/// no equivalent of the characteristics the source dialect accepts.
1633+
pub fn clear_constraint_characteristics(&mut self) {
1634+
for option in &mut self.options {
1635+
match &mut option.option {
1636+
ColumnOption::PrimaryKey(constraint) => constraint.characteristics = None,
1637+
ColumnOption::Unique(constraint) => constraint.characteristics = None,
1638+
ColumnOption::ForeignKey(constraint) => constraint.characteristics = None,
1639+
ColumnOption::Null
1640+
| ColumnOption::NotNull
1641+
| ColumnOption::Default(_)
1642+
| ColumnOption::Materialized(_)
1643+
| ColumnOption::Ephemeral(_)
1644+
| ColumnOption::Alias(_)
1645+
| ColumnOption::Check(_)
1646+
| ColumnOption::DialectSpecific(_)
1647+
| ColumnOption::CharacterSet(_)
1648+
| ColumnOption::Collation(_)
1649+
| ColumnOption::Comment(_)
1650+
| ColumnOption::OnUpdate(_)
1651+
| ColumnOption::Generated { .. }
1652+
| ColumnOption::Options(_)
1653+
| ColumnOption::Identity(_)
1654+
| ColumnOption::OnConflict(_)
1655+
| ColumnOption::Policy(_)
1656+
| ColumnOption::Tags(_)
1657+
| ColumnOption::Srid(_)
1658+
| ColumnOption::Invisible => {}
1659+
}
1660+
}
1661+
}
1662+
}
1663+
16281664
impl fmt::Display for ColumnDef {
16291665
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16301666
if self.data_type == DataType::Unspecified {

tests/sqlparser_snowflake.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,17 @@ fn parse_sf_informational_constraint_properties() {
142142

143143
#[test]
144144
fn parse_sf_clear_constraint_characteristics() {
145-
let sql = "CREATE TABLE t (id INT, CONSTRAINT u UNIQUE (id) NOT ENFORCED RELY)";
145+
let sql = "CREATE TABLE t (id INT UNIQUE ENABLE VALIDATE RELY, CONSTRAINT u UNIQUE (id) NOT ENFORCED RELY)";
146146
match snowflake().verified_stmt(sql) {
147147
Statement::CreateTable(CreateTable {
148-
mut constraints, ..
148+
mut columns,
149+
mut constraints,
150+
..
149151
}) => {
150152
constraints[0].clear_characteristics();
151153
assert_eq!(constraints[0].to_string(), "CONSTRAINT u UNIQUE (id)");
154+
columns[0].clear_constraint_characteristics();
155+
assert_eq!(columns[0].to_string(), "id INT UNIQUE");
152156
}
153157
other => panic!("unexpected statement: {other}"),
154158
}

0 commit comments

Comments
 (0)