Skip to content

Treat COLLATE like any other column option #1731

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 20, 2025
Merged
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
6 changes: 2 additions & 4 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
}

Expand All @@ -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}")?;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())))
}
}

Expand Down Expand Up @@ -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(),
Expand Down
15 changes: 4 additions & 11 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -6908,18 +6903,13 @@ 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;
};
}
Ok(ColumnDef {
name,
data_type,
collation,
options,
})
}
Expand Down Expand Up @@ -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]) {
Expand Down Expand Up @@ -9047,7 +9041,6 @@ impl<'a> Parser<'a> {
Ok(ColumnDef {
name,
data_type,
collation: None,
options: Vec::new(), // No constraints expected here
})
}
Expand Down
5 changes: 0 additions & 5 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ fn parse_create_table_with_unquoted_hyphen() {
vec![ColumnDef {
name: Ident::new("x"),
data_type: DataType::Int64,
collation: None,
options: vec![]
},],
columns
Expand Down Expand Up @@ -427,7 +426,6 @@ fn parse_create_table_with_options() {
ColumnDef {
name: Ident::new("x"),
data_type: DataType::Int64,
collation: None,
options: vec![
ColumnOptionDef {
name: None,
Expand All @@ -447,7 +445,6 @@ fn parse_create_table_with_options() {
ColumnDef {
name: Ident::new("y"),
data_type: DataType::Bool,
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Options(vec![SqlOption::KeyValue {
Expand Down Expand Up @@ -530,7 +527,6 @@ fn parse_nested_data_types() {
],
StructBracketKind::AngleBrackets
),
collation: None,
options: vec![],
},
ColumnDef {
Expand All @@ -544,7 +540,6 @@ fn parse_nested_data_types() {
StructBracketKind::AngleBrackets
),
))),
collation: None,
options: vec![],
},
]
Expand Down
12 changes: 0 additions & 12 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,6 @@ fn column_def(name: Ident, data_type: DataType) -> ColumnDef {
ColumnDef {
name,
data_type,
collation: None,
options: vec![],
}
}
Expand Down Expand Up @@ -617,7 +616,6 @@ fn parse_create_table_with_nullable() {
ColumnDef {
name: "d".into(),
data_type: DataType::Date32,
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Null
Expand Down Expand Up @@ -661,7 +659,6 @@ fn parse_create_table_with_nested_data_types() {
DataType::LowCardinality(Box::new(DataType::String(None)))
)
]),
collation: None,
options: vec![],
},
ColumnDef {
Expand All @@ -678,7 +675,6 @@ fn parse_create_table_with_nested_data_types() {
}
])
))),
collation: None,
options: vec![],
},
ColumnDef {
Expand All @@ -695,7 +691,6 @@ fn parse_create_table_with_nested_data_types() {
))
},
]),
collation: None,
options: vec![],
},
ColumnDef {
Expand All @@ -704,7 +699,6 @@ fn parse_create_table_with_nested_data_types() {
Box::new(DataType::String(None)),
Box::new(DataType::UInt16)
),
collation: None,
options: vec![],
},
]
Expand Down Expand Up @@ -736,13 +730,11 @@ fn parse_create_table_with_primary_key() {
ColumnDef {
name: Ident::with_quote('`', "i"),
data_type: DataType::Int(None),
collation: None,
options: vec![],
},
ColumnDef {
name: Ident::with_quote('`', "k"),
data_type: DataType::Int(None),
collation: None,
options: vec![],
},
],
Expand Down Expand Up @@ -814,7 +806,6 @@ fn parse_create_table_with_variant_default_expressions() {
ColumnDef {
name: Ident::new("a"),
data_type: DataType::Datetime(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Materialized(Expr::Function(Function {
Expand All @@ -836,7 +827,6 @@ fn parse_create_table_with_variant_default_expressions() {
ColumnDef {
name: Ident::new("b"),
data_type: DataType::Datetime(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Ephemeral(Some(Expr::Function(Function {
Expand All @@ -858,7 +848,6 @@ fn parse_create_table_with_variant_default_expressions() {
ColumnDef {
name: Ident::new("c"),
data_type: DataType::Datetime(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Ephemeral(None)
Expand All @@ -867,7 +856,6 @@ fn parse_create_table_with_variant_default_expressions() {
ColumnDef {
name: Ident::new("d"),
data_type: DataType::String(None),
collation: None,
options: vec![ColumnOptionDef {
name: None,
option: ColumnOption::Alias(Expr::Function(Function {
Expand Down
Loading