Skip to content

Commit c8531d4

Browse files
Added support for MATCH syntax and unified column option ForeignKey (#2062)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
1 parent 4490c8c commit c8531d4

File tree

8 files changed

+179
-63
lines changed

8 files changed

+179
-63
lines changed

src/ast/ddl.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ use sqlparser_derive::{Visit, VisitMut};
3030

3131
use crate::ast::value::escape_single_quote_string;
3232
use crate::ast::{
33-
display_comma_separated, display_separated, ArgMode, AttachedToken, CommentDef,
34-
ConditionalStatements, CreateFunctionBody, CreateFunctionUsing, CreateTableLikeKind,
35-
CreateTableOptions, CreateViewParams, DataType, Expr, FileFormat, FunctionBehavior,
36-
FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier, FunctionParallel,
37-
HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident,
38-
InitializeKind, MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens,
39-
OperateFunctionArg, OrderByExpr, ProjectionSelect, Query, RefreshModeKind, RowAccessPolicy,
40-
SequenceOptions, Spanned, SqlOption, StorageSerializationPolicy, TableConstraint, TableVersion,
33+
display_comma_separated, display_separated,
34+
table_constraints::{ForeignKeyConstraint, TableConstraint},
35+
ArgMode, AttachedToken, CommentDef, ConditionalStatements, CreateFunctionBody,
36+
CreateFunctionUsing, CreateTableLikeKind, CreateTableOptions, CreateViewParams, DataType, Expr,
37+
FileFormat, FunctionBehavior, FunctionCalledOnNull, FunctionDesc, FunctionDeterminismSpecifier,
38+
FunctionParallel, HiveDistributionStyle, HiveFormat, HiveIOFormat, HiveRowFormat,
39+
HiveSetLocation, Ident, InitializeKind, MySQLColumnPosition, ObjectName, OnCommit,
40+
OneOrManyWithParens, OperateFunctionArg, OrderByExpr, ProjectionSelect, Query, RefreshModeKind,
41+
RowAccessPolicy, SequenceOptions, Spanned, SqlOption, StorageSerializationPolicy, TableVersion,
4142
Tag, TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value,
4243
ValueWithSpan, WrappedCollection,
4344
};
@@ -1559,20 +1560,14 @@ pub enum ColumnOption {
15591560
is_primary: bool,
15601561
characteristics: Option<ConstraintCharacteristics>,
15611562
},
1562-
/// A referential integrity constraint (`[FOREIGN KEY REFERENCES
1563-
/// <foreign_table> (<referred_columns>)
1563+
/// A referential integrity constraint (`REFERENCES <foreign_table> (<referred_columns>)
1564+
/// [ MATCH { FULL | PARTIAL | SIMPLE } ]
15641565
/// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
15651566
/// [ON UPDATE <referential_action>] [ON DELETE <referential_action>]
1566-
/// }
1567+
/// }
15671568
/// [<constraint_characteristics>]
15681569
/// `).
1569-
ForeignKey {
1570-
foreign_table: ObjectName,
1571-
referred_columns: Vec<Ident>,
1572-
on_delete: Option<ReferentialAction>,
1573-
on_update: Option<ReferentialAction>,
1574-
characteristics: Option<ConstraintCharacteristics>,
1575-
},
1570+
ForeignKey(ForeignKeyConstraint),
15761571
/// `CHECK (<expr>)`
15771572
Check(Expr),
15781573
/// Dialect-specific options, such as:
@@ -1643,6 +1638,12 @@ pub enum ColumnOption {
16431638
Invisible,
16441639
}
16451640

1641+
impl From<ForeignKeyConstraint> for ColumnOption {
1642+
fn from(fk: ForeignKeyConstraint) -> Self {
1643+
ColumnOption::ForeignKey(fk)
1644+
}
1645+
}
1646+
16461647
impl fmt::Display for ColumnOption {
16471648
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16481649
use ColumnOption::*;
@@ -1669,24 +1670,25 @@ impl fmt::Display for ColumnOption {
16691670
}
16701671
Ok(())
16711672
}
1672-
ForeignKey {
1673-
foreign_table,
1674-
referred_columns,
1675-
on_delete,
1676-
on_update,
1677-
characteristics,
1678-
} => {
1679-
write!(f, "REFERENCES {foreign_table}")?;
1680-
if !referred_columns.is_empty() {
1681-
write!(f, " ({})", display_comma_separated(referred_columns))?;
1673+
ForeignKey(constraint) => {
1674+
write!(f, "REFERENCES {}", constraint.foreign_table)?;
1675+
if !constraint.referred_columns.is_empty() {
1676+
write!(
1677+
f,
1678+
" ({})",
1679+
display_comma_separated(&constraint.referred_columns)
1680+
)?;
16821681
}
1683-
if let Some(action) = on_delete {
1682+
if let Some(match_kind) = &constraint.match_kind {
1683+
write!(f, " {match_kind}")?;
1684+
}
1685+
if let Some(action) = &constraint.on_delete {
16841686
write!(f, " ON DELETE {action}")?;
16851687
}
1686-
if let Some(action) = on_update {
1688+
if let Some(action) = &constraint.on_update {
16871689
write!(f, " ON UPDATE {action}")?;
16881690
}
1689-
if let Some(characteristics) = characteristics {
1691+
if let Some(characteristics) = &constraint.characteristics {
16901692
write!(f, " {characteristics}")?;
16911693
}
16921694
Ok(())

src/ast/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,31 @@ pub enum CastKind {
657657
DoubleColon,
658658
}
659659

660+
/// `MATCH` type for constraint references
661+
///
662+
/// See: <https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-PARMS-REFERENCES>
663+
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
664+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
665+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
666+
pub enum ConstraintReferenceMatchKind {
667+
/// `MATCH FULL`
668+
Full,
669+
/// `MATCH PARTIAL`
670+
Partial,
671+
/// `MATCH SIMPLE`
672+
Simple,
673+
}
674+
675+
impl fmt::Display for ConstraintReferenceMatchKind {
676+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
677+
match self {
678+
Self::Full => write!(f, "MATCH FULL"),
679+
Self::Partial => write!(f, "MATCH PARTIAL"),
680+
Self::Simple => write!(f, "MATCH SIMPLE"),
681+
}
682+
}
683+
}
684+
660685
/// `EXTRACT` syntax variants.
661686
///
662687
/// In Snowflake dialect, the `EXTRACT` expression can support either the `from` syntax

src/ast/spans.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -741,19 +741,7 @@ impl Spanned for ColumnOption {
741741
ColumnOption::Ephemeral(expr) => expr.as_ref().map_or(Span::empty(), |e| e.span()),
742742
ColumnOption::Alias(expr) => expr.span(),
743743
ColumnOption::Unique { .. } => Span::empty(),
744-
ColumnOption::ForeignKey {
745-
foreign_table,
746-
referred_columns,
747-
on_delete,
748-
on_update,
749-
characteristics,
750-
} => union_spans(
751-
core::iter::once(foreign_table.span())
752-
.chain(referred_columns.iter().map(|i| i.span))
753-
.chain(on_delete.iter().map(|i| i.span()))
754-
.chain(on_update.iter().map(|i| i.span()))
755-
.chain(characteristics.iter().map(|i| i.span())),
756-
),
744+
ColumnOption::ForeignKey(constraint) => constraint.span(),
757745
ColumnOption::Check(expr) => expr.span(),
758746
ColumnOption::DialectSpecific(_) => Span::empty(),
759747
ColumnOption::CharacterSet(object_name) => object_name.span(),

src/ast/table_constraints.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
//! SQL Abstract Syntax Tree (AST) types for table constraints
1919
2020
use crate::ast::{
21-
display_comma_separated, display_separated, ConstraintCharacteristics, Expr, Ident,
22-
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, ObjectName,
23-
ReferentialAction,
21+
display_comma_separated, display_separated, ConstraintCharacteristics,
22+
ConstraintReferenceMatchKind, Expr, Ident, IndexColumn, IndexOption, IndexType,
23+
KeyOrIndexDisplay, NullsDistinctOption, ObjectName, ReferentialAction,
2424
};
2525
use crate::tokenizer::Span;
2626
use core::fmt;
@@ -189,7 +189,7 @@ impl crate::ast::Spanned for CheckConstraint {
189189
}
190190

191191
/// A referential integrity constraint (`[ CONSTRAINT <name> ] FOREIGN KEY (<columns>)
192-
/// REFERENCES <foreign_table> (<referred_columns>)
192+
/// REFERENCES <foreign_table> (<referred_columns>) [ MATCH { FULL | PARTIAL | SIMPLE } ]
193193
/// { [ON DELETE <referential_action>] [ON UPDATE <referential_action>] |
194194
/// [ON UPDATE <referential_action>] [ON DELETE <referential_action>]
195195
/// }`).
@@ -206,6 +206,7 @@ pub struct ForeignKeyConstraint {
206206
pub referred_columns: Vec<Ident>,
207207
pub on_delete: Option<ReferentialAction>,
208208
pub on_update: Option<ReferentialAction>,
209+
pub match_kind: Option<ConstraintReferenceMatchKind>,
209210
pub characteristics: Option<ConstraintCharacteristics>,
210211
}
211212

@@ -223,6 +224,9 @@ impl fmt::Display for ForeignKeyConstraint {
223224
if !self.referred_columns.is_empty() {
224225
write!(f, "({})", display_comma_separated(&self.referred_columns))?;
225226
}
227+
if let Some(match_kind) = &self.match_kind {
228+
write!(f, " {match_kind}")?;
229+
}
226230
if let Some(action) = &self.on_delete {
227231
write!(f, " ON DELETE {action}")?;
228232
}

src/keywords.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ define_keywords!(
713713
PARAMETER,
714714
PARQUET,
715715
PART,
716+
PARTIAL,
716717
PARTITION,
717718
PARTITIONED,
718719
PARTITIONS,
@@ -885,6 +886,7 @@ define_keywords!(
885886
SHOW,
886887
SIGNED,
887888
SIMILAR,
889+
SIMPLE,
888890
SKIP,
889891
SLOW,
890892
SMALLINT,

src/parser/mod.rs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7940,7 +7940,7 @@ impl<'a> Parser<'a> {
79407940
}
79417941

79427942
pub fn parse_column_def(&mut self) -> Result<ColumnDef, ParserError> {
7943-
let name = self.parse_identifier()?;
7943+
let col_name = self.parse_identifier()?;
79447944
let data_type = if self.is_column_type_sqlite_unspecified() {
79457945
DataType::Unspecified
79467946
} else {
@@ -7965,7 +7965,7 @@ impl<'a> Parser<'a> {
79657965
};
79667966
}
79677967
Ok(ColumnDef {
7968-
name,
7968+
name: col_name,
79697969
data_type,
79707970
options,
79717971
})
@@ -8065,10 +8065,15 @@ impl<'a> Parser<'a> {
80658065
// PostgreSQL allows omitting the column list and
80668066
// uses the primary key column of the foreign table by default
80678067
let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
8068+
let mut match_kind = None;
80688069
let mut on_delete = None;
80698070
let mut on_update = None;
80708071
loop {
8071-
if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
8072+
if match_kind.is_none() && self.parse_keyword(Keyword::MATCH) {
8073+
match_kind = Some(self.parse_match_kind()?);
8074+
} else if on_delete.is_none()
8075+
&& self.parse_keywords(&[Keyword::ON, Keyword::DELETE])
8076+
{
80728077
on_delete = Some(self.parse_referential_action()?);
80738078
} else if on_update.is_none()
80748079
&& self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
@@ -8080,13 +8085,20 @@ impl<'a> Parser<'a> {
80808085
}
80818086
let characteristics = self.parse_constraint_characteristics()?;
80828087

8083-
Ok(Some(ColumnOption::ForeignKey {
8084-
foreign_table,
8085-
referred_columns,
8086-
on_delete,
8087-
on_update,
8088-
characteristics,
8089-
}))
8088+
Ok(Some(
8089+
ForeignKeyConstraint {
8090+
name: None, // Column-level constraints don't have names
8091+
index_name: None, // Not applicable for column-level constraints
8092+
columns: vec![], // Not applicable for column-level constraints
8093+
foreign_table,
8094+
referred_columns,
8095+
on_delete,
8096+
on_update,
8097+
match_kind,
8098+
characteristics,
8099+
}
8100+
.into(),
8101+
))
80908102
} else if self.parse_keyword(Keyword::CHECK) {
80918103
self.expect_token(&Token::LParen)?;
80928104
// since `CHECK` requires parentheses, we can parse the inner expression in ParserState::Normal
@@ -8360,6 +8372,18 @@ impl<'a> Parser<'a> {
83608372
}
83618373
}
83628374

8375+
pub fn parse_match_kind(&mut self) -> Result<ConstraintReferenceMatchKind, ParserError> {
8376+
if self.parse_keyword(Keyword::FULL) {
8377+
Ok(ConstraintReferenceMatchKind::Full)
8378+
} else if self.parse_keyword(Keyword::PARTIAL) {
8379+
Ok(ConstraintReferenceMatchKind::Partial)
8380+
} else if self.parse_keyword(Keyword::SIMPLE) {
8381+
Ok(ConstraintReferenceMatchKind::Simple)
8382+
} else {
8383+
self.expected("one of FULL, PARTIAL or SIMPLE", self.peek_token())
8384+
}
8385+
}
8386+
83638387
pub fn parse_constraint_characteristics(
83648388
&mut self,
83658389
) -> Result<Option<ConstraintCharacteristics>, ParserError> {
@@ -8470,10 +8494,15 @@ impl<'a> Parser<'a> {
84708494
self.expect_keyword_is(Keyword::REFERENCES)?;
84718495
let foreign_table = self.parse_object_name(false)?;
84728496
let referred_columns = self.parse_parenthesized_column_list(Optional, false)?;
8497+
let mut match_kind = None;
84738498
let mut on_delete = None;
84748499
let mut on_update = None;
84758500
loop {
8476-
if on_delete.is_none() && self.parse_keywords(&[Keyword::ON, Keyword::DELETE]) {
8501+
if match_kind.is_none() && self.parse_keyword(Keyword::MATCH) {
8502+
match_kind = Some(self.parse_match_kind()?);
8503+
} else if on_delete.is_none()
8504+
&& self.parse_keywords(&[Keyword::ON, Keyword::DELETE])
8505+
{
84778506
on_delete = Some(self.parse_referential_action()?);
84788507
} else if on_update.is_none()
84798508
&& self.parse_keywords(&[Keyword::ON, Keyword::UPDATE])
@@ -8495,6 +8524,7 @@ impl<'a> Parser<'a> {
84958524
referred_columns,
84968525
on_delete,
84978526
on_update,
8527+
match_kind,
84988528
characteristics,
84998529
}
85008530
.into(),

0 commit comments

Comments
 (0)