Skip to content

Commit 97f0be6

Browse files
authored
Treat COLLATE like any other column option (#1731)
1 parent b482562 commit 97f0be6

13 files changed

+60
-151
lines changed

src/ast/ddl.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,6 @@ impl fmt::Display for ProcedureParam {
12061206
pub struct ColumnDef {
12071207
pub name: Ident,
12081208
pub data_type: DataType,
1209-
pub collation: Option<ObjectName>,
12101209
pub options: Vec<ColumnOptionDef>,
12111210
}
12121211

@@ -1217,9 +1216,6 @@ impl fmt::Display for ColumnDef {
12171216
} else {
12181217
write!(f, "{} {}", self.name, self.data_type)?;
12191218
}
1220-
if let Some(collation) = &self.collation {
1221-
write!(f, " COLLATE {collation}")?;
1222-
}
12231219
for option in &self.options {
12241220
write!(f, " {option}")?;
12251221
}
@@ -1566,6 +1562,7 @@ pub enum ColumnOption {
15661562
/// - ...
15671563
DialectSpecific(Vec<Token>),
15681564
CharacterSet(ObjectName),
1565+
Collation(ObjectName),
15691566
Comment(String),
15701567
OnUpdate(Expr),
15711568
/// `Generated`s are modifiers that follow a column definition in a `CREATE
@@ -1665,6 +1662,7 @@ impl fmt::Display for ColumnOption {
16651662
Check(expr) => write!(f, "CHECK ({expr})"),
16661663
DialectSpecific(val) => write!(f, "{}", display_separated(val, " ")),
16671664
CharacterSet(n) => write!(f, "CHARACTER SET {n}"),
1665+
Collation(n) => write!(f, "COLLATE {n}"),
16681666
Comment(v) => write!(f, "COMMENT '{}'", escape_single_quote_string(v)),
16691667
OnUpdate(expr) => write!(f, "ON UPDATE {expr}"),
16701668
Generated {

src/ast/helpers/stmt_create_table.rs

-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ use crate::parser::ParserError;
4747
/// .columns(vec![ColumnDef {
4848
/// name: Ident::new("c1"),
4949
/// data_type: DataType::Int(None),
50-
/// collation: None,
5150
/// options: vec![],
5251
/// }]);
5352
/// // You can access internal elements with ease

src/ast/spans.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -604,15 +604,10 @@ impl Spanned for ColumnDef {
604604
let ColumnDef {
605605
name,
606606
data_type: _, // enum
607-
collation,
608607
options,
609608
} = self;
610609

611-
union_spans(
612-
core::iter::once(name.span)
613-
.chain(collation.iter().map(|i| i.span()))
614-
.chain(options.iter().map(|i| i.span())),
615-
)
610+
union_spans(core::iter::once(name.span).chain(options.iter().map(|i| i.span())))
616611
}
617612
}
618613

@@ -767,6 +762,7 @@ impl Spanned for ColumnOption {
767762
ColumnOption::Check(expr) => expr.span(),
768763
ColumnOption::DialectSpecific(_) => Span::empty(),
769764
ColumnOption::CharacterSet(object_name) => object_name.span(),
765+
ColumnOption::Collation(object_name) => object_name.span(),
770766
ColumnOption::Comment(_) => Span::empty(),
771767
ColumnOption::OnUpdate(expr) => expr.span(),
772768
ColumnOption::Generated { .. } => Span::empty(),

src/parser/mod.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -6889,11 +6889,6 @@ impl<'a> Parser<'a> {
68896889
} else {
68906890
self.parse_data_type()?
68916891
};
6892-
let mut collation = if self.parse_keyword(Keyword::COLLATE) {
6893-
Some(self.parse_object_name(false)?)
6894-
} else {
6895-
None
6896-
};
68976892
let mut options = vec![];
68986893
loop {
68996894
if self.parse_keyword(Keyword::CONSTRAINT) {
@@ -6908,18 +6903,13 @@ impl<'a> Parser<'a> {
69086903
}
69096904
} else if let Some(option) = self.parse_optional_column_option()? {
69106905
options.push(ColumnOptionDef { name: None, option });
6911-
} else if dialect_of!(self is MySqlDialect | SnowflakeDialect | GenericDialect)
6912-
&& self.parse_keyword(Keyword::COLLATE)
6913-
{
6914-
collation = Some(self.parse_object_name(false)?);
69156906
} else {
69166907
break;
69176908
};
69186909
}
69196910
Ok(ColumnDef {
69206911
name,
69216912
data_type,
6922-
collation,
69236913
options,
69246914
})
69256915
}
@@ -6956,6 +6946,10 @@ impl<'a> Parser<'a> {
69566946
Ok(Some(ColumnOption::CharacterSet(
69576947
self.parse_object_name(false)?,
69586948
)))
6949+
} else if self.parse_keywords(&[Keyword::COLLATE]) {
6950+
Ok(Some(ColumnOption::Collation(
6951+
self.parse_object_name(false)?,
6952+
)))
69596953
} else if self.parse_keywords(&[Keyword::NOT, Keyword::NULL]) {
69606954
Ok(Some(ColumnOption::NotNull))
69616955
} else if self.parse_keywords(&[Keyword::COMMENT]) {
@@ -9047,7 +9041,6 @@ impl<'a> Parser<'a> {
90479041
Ok(ColumnDef {
90489042
name,
90499043
data_type,
9050-
collation: None,
90519044
options: Vec::new(), // No constraints expected here
90529045
})
90539046
}

tests/sqlparser_bigquery.rs

-5
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,6 @@ fn parse_create_table_with_unquoted_hyphen() {
389389
vec![ColumnDef {
390390
name: Ident::new("x"),
391391
data_type: DataType::Int64,
392-
collation: None,
393392
options: vec![]
394393
},],
395394
columns
@@ -427,7 +426,6 @@ fn parse_create_table_with_options() {
427426
ColumnDef {
428427
name: Ident::new("x"),
429428
data_type: DataType::Int64,
430-
collation: None,
431429
options: vec![
432430
ColumnOptionDef {
433431
name: None,
@@ -447,7 +445,6 @@ fn parse_create_table_with_options() {
447445
ColumnDef {
448446
name: Ident::new("y"),
449447
data_type: DataType::Bool,
450-
collation: None,
451448
options: vec![ColumnOptionDef {
452449
name: None,
453450
option: ColumnOption::Options(vec![SqlOption::KeyValue {
@@ -530,7 +527,6 @@ fn parse_nested_data_types() {
530527
],
531528
StructBracketKind::AngleBrackets
532529
),
533-
collation: None,
534530
options: vec![],
535531
},
536532
ColumnDef {
@@ -544,7 +540,6 @@ fn parse_nested_data_types() {
544540
StructBracketKind::AngleBrackets
545541
),
546542
))),
547-
collation: None,
548543
options: vec![],
549544
},
550545
]

tests/sqlparser_clickhouse.rs

-12
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,6 @@ fn column_def(name: Ident, data_type: DataType) -> ColumnDef {
528528
ColumnDef {
529529
name,
530530
data_type,
531-
collation: None,
532531
options: vec![],
533532
}
534533
}
@@ -617,7 +616,6 @@ fn parse_create_table_with_nullable() {
617616
ColumnDef {
618617
name: "d".into(),
619618
data_type: DataType::Date32,
620-
collation: None,
621619
options: vec![ColumnOptionDef {
622620
name: None,
623621
option: ColumnOption::Null
@@ -661,7 +659,6 @@ fn parse_create_table_with_nested_data_types() {
661659
DataType::LowCardinality(Box::new(DataType::String(None)))
662660
)
663661
]),
664-
collation: None,
665662
options: vec![],
666663
},
667664
ColumnDef {
@@ -678,7 +675,6 @@ fn parse_create_table_with_nested_data_types() {
678675
}
679676
])
680677
))),
681-
collation: None,
682678
options: vec![],
683679
},
684680
ColumnDef {
@@ -695,7 +691,6 @@ fn parse_create_table_with_nested_data_types() {
695691
))
696692
},
697693
]),
698-
collation: None,
699694
options: vec![],
700695
},
701696
ColumnDef {
@@ -704,7 +699,6 @@ fn parse_create_table_with_nested_data_types() {
704699
Box::new(DataType::String(None)),
705700
Box::new(DataType::UInt16)
706701
),
707-
collation: None,
708702
options: vec![],
709703
},
710704
]
@@ -736,13 +730,11 @@ fn parse_create_table_with_primary_key() {
736730
ColumnDef {
737731
name: Ident::with_quote('`', "i"),
738732
data_type: DataType::Int(None),
739-
collation: None,
740733
options: vec![],
741734
},
742735
ColumnDef {
743736
name: Ident::with_quote('`', "k"),
744737
data_type: DataType::Int(None),
745-
collation: None,
746738
options: vec![],
747739
},
748740
],
@@ -814,7 +806,6 @@ fn parse_create_table_with_variant_default_expressions() {
814806
ColumnDef {
815807
name: Ident::new("a"),
816808
data_type: DataType::Datetime(None),
817-
collation: None,
818809
options: vec![ColumnOptionDef {
819810
name: None,
820811
option: ColumnOption::Materialized(Expr::Function(Function {
@@ -836,7 +827,6 @@ fn parse_create_table_with_variant_default_expressions() {
836827
ColumnDef {
837828
name: Ident::new("b"),
838829
data_type: DataType::Datetime(None),
839-
collation: None,
840830
options: vec![ColumnOptionDef {
841831
name: None,
842832
option: ColumnOption::Ephemeral(Some(Expr::Function(Function {
@@ -858,7 +848,6 @@ fn parse_create_table_with_variant_default_expressions() {
858848
ColumnDef {
859849
name: Ident::new("c"),
860850
data_type: DataType::Datetime(None),
861-
collation: None,
862851
options: vec![ColumnOptionDef {
863852
name: None,
864853
option: ColumnOption::Ephemeral(None)
@@ -867,7 +856,6 @@ fn parse_create_table_with_variant_default_expressions() {
867856
ColumnDef {
868857
name: Ident::new("d"),
869858
data_type: DataType::String(None),
870-
collation: None,
871859
options: vec![ColumnOptionDef {
872860
name: None,
873861
option: ColumnOption::Alias(Expr::Function(Function {

0 commit comments

Comments
 (0)