Treat COLLATE like any other column option (#1731)

This commit is contained in:
Michael Victor Zink 2025-02-19 21:26:20 -08:00 committed by GitHub
parent b482562618
commit 97f0be6991
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 60 additions and 151 deletions

View file

@ -1206,7 +1206,6 @@ impl fmt::Display for ProcedureParam {
pub struct ColumnDef {
pub name: Ident,
pub data_type: DataType,
pub collation: Option<ObjectName>,
pub options: Vec<ColumnOptionDef>,
}
@ -1217,9 +1216,6 @@ impl fmt::Display for ColumnDef {
} else {
write!(f, "{} {}", self.name, self.data_type)?;
}
if let Some(collation) = &self.collation {
write!(f, " COLLATE {collation}")?;
}
for option in &self.options {
write!(f, " {option}")?;
}
@ -1566,6 +1562,7 @@ pub enum ColumnOption {
/// - ...
DialectSpecific(Vec<Token>),
CharacterSet(ObjectName),
Collation(ObjectName),
Comment(String),
OnUpdate(Expr),
/// `Generated`s are modifiers that follow a column definition in a `CREATE
@ -1665,6 +1662,7 @@ impl fmt::Display for ColumnOption {
Check(expr) => write!(f, "CHECK ({expr})"),
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
Collation(n) => write!(f, "COLLATE {n}"),
Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
OnUpdate(expr) => write!(f, "ON UPDATE {expr}"),
Generated {

View file

@ -47,7 +47,6 @@ use crate::parser::ParserError;
/// .columns(vec![ColumnDef {
/// name: Ident::new("c1"),
/// data_type: DataType::Int(None),
/// collation: None,
/// options: vec![],
/// }]);
/// // You can access internal elements with ease

View file

@ -604,15 +604,10 @@ impl Spanned for ColumnDef {
let ColumnDef {
name,
data_type: _, // enum
collation,
options,
} = self;
union_spans(
core::iter::once(name.span)
.chain(collation.iter().map(|i| i.span()))
.chain(options.iter().map(|i| i.span())),
)
union_spans(core::iter::once(name.span).chain(options.iter().map(|i| i.span())))
}
}
@ -767,6 +762,7 @@ impl Spanned for ColumnOption {
ColumnOption::Check(expr) => expr.span(),
ColumnOption::DialectSpecific(_) => Span::empty(),
ColumnOption::CharacterSet(object_name) => object_name.span(),
ColumnOption::Collation(object_name) => object_name.span(),
ColumnOption::Comment(_) => Span::empty(),
ColumnOption::OnUpdate(expr) => expr.span(),
ColumnOption::Generated { .. } => Span::empty(),

View file

@ -6889,11 +6889,6 @@ impl<'a> Parser<'a> {
} else {
self.parse_data_type()?
};
let mut collation = if self.parse_keyword(Keyword::COLLATE) {
Some(self.parse_object_name(false)?)
} else {
None
};
let mut options = vec![];
loop {
if self.parse_keyword(Keyword::CONSTRAINT) {
@ -6908,10 +6903,6 @@ impl<'a> Parser<'a> {
}
} else if let Some(option) = self.parse_optional_column_option()? {
options.push(ColumnOptionDef { name: None, option });
} else if dialect_of!(self is MySqlDialect | SnowflakeDialect | GenericDialect)
&& self.parse_keyword(Keyword::COLLATE)
{
collation = Some(self.parse_object_name(false)?);
} else {
break;
};
@ -6919,7 +6910,6 @@ impl<'a> Parser<'a> {
Ok(ColumnDef {
name,
data_type,
collation,
options,
})
}
@ -6956,6 +6946,10 @@ impl<'a> Parser<'a> {
Ok(Some(ColumnOption::CharacterSet(
self.parse_object_name(false)?,
)))
} else if self.parse_keywords(&[Keyword::COLLATE]) {
Ok(Some(ColumnOption::Collation(
self.parse_object_name(false)?,
)))
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
Ok(Some(ColumnOption::NotNull))
} else if self.parse_keywords(&[Keyword::COMMENT]) {
@ -9047,7 +9041,6 @@ impl<'a> Parser<'a> {
Ok(ColumnDef {
name,
data_type,
collation: None,
options: Vec::new(), // No constraints expected here
})
}