Skip to content

Commit de8f0c3

Browse files
Add psql alter table operation REPLICA IDENTITY
1 parent ac1c339 commit de8f0c3

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

src/ast/ddl.rs

+30
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ use crate::ast::{
3939
use crate::keywords::Keyword;
4040
use crate::tokenizer::Token;
4141

42+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
43+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
44+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
45+
pub enum ReplicaIdentity {
46+
None,
47+
Full,
48+
Default,
49+
Index(Ident),
50+
}
51+
52+
impl fmt::Display for ReplicaIdentity {
53+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
54+
match self {
55+
ReplicaIdentity::None => f.write_str("NONE"),
56+
ReplicaIdentity::Full => f.write_str("FULL"),
57+
ReplicaIdentity::Default => f.write_str("DEFAULT"),
58+
ReplicaIdentity::Index(idx) => write!(f, "USING INDEX {}", idx),
59+
}
60+
}
61+
}
62+
4263
/// An `ALTER TABLE` (`Statement::AlterTable`) operation
4364
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4465
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
@@ -208,6 +229,12 @@ pub enum AlterTableOperation {
208229
old_partitions: Vec<Expr>,
209230
new_partitions: Vec<Expr>,
210231
},
232+
/// REPLICA IDENTITY { DEFAULT | USING INDEX index_name | FULL | NOTHING }
233+
///
234+
/// Note: this is a PostgreSQL-specific operation.
235+
ReplicaIdentity {
236+
identity: ReplicaIdentity,
237+
},
211238
/// Add Partitions
212239
AddPartitions {
213240
if_not_exists: bool,
@@ -729,6 +756,9 @@ impl fmt::Display for AlterTableOperation {
729756
AlterTableOperation::Lock { equals, lock } => {
730757
write!(f, "LOCK {}{}", if *equals { "= " } else { "" }, lock)
731758
}
759+
AlterTableOperation::ReplicaIdentity { identity } => {
760+
write!(f, "REPLICA IDENTITY {identity}")
761+
}
732762
}
733763
}
734764
}

src/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub use self::ddl::{
5959
DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
6060
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
6161
IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition,
62-
ProcedureParam, ReferentialAction, TableConstraint, TagsColumnOption,
62+
ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint, TagsColumnOption,
6363
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
6464
};
6565
pub use self::dml::{CreateIndex, CreateTable, Delete, IndexColumn, Insert};

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ impl Spanned for AlterTableOperation {
11741174
AlterTableOperation::Algorithm { .. } => Span::empty(),
11751175
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
11761176
AlterTableOperation::Lock { .. } => Span::empty(),
1177+
AlterTableOperation::ReplicaIdentity { .. } => Span::empty(),
11771178
}
11781179
}
11791180
}

src/parser/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -8770,6 +8770,23 @@ impl<'a> Parser<'a> {
87708770
let equals = self.consume_token(&Token::Eq);
87718771
let value = self.parse_number_value()?;
87728772
AlterTableOperation::AutoIncrement { equals, value }
8773+
} else if self.parse_keywords(&[Keyword::REPLICA, Keyword::IDENTITY]) {
8774+
let identity = if self.parse_keyword(Keyword::NONE) {
8775+
ReplicaIdentity::None
8776+
} else if self.parse_keyword(Keyword::FULL) {
8777+
ReplicaIdentity::Full
8778+
} else if self.parse_keyword(Keyword::DEFAULT) {
8779+
ReplicaIdentity::Default
8780+
} else if self.parse_keywords(&[Keyword::USING, Keyword::INDEX]) {
8781+
ReplicaIdentity::Index(self.parse_identifier()?)
8782+
} else {
8783+
return self.expected(
8784+
"NONE, FULL, DEFAULT, or USING INDEX index_name after REPLICA IDENTITY",
8785+
self.peek_token(),
8786+
);
8787+
};
8788+
8789+
AlterTableOperation::ReplicaIdentity { identity }
87738790
} else {
87748791
let options: Vec<SqlOption> =
87758792
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
@@ -8779,7 +8796,7 @@ impl<'a> Parser<'a> {
87798796
}
87808797
} else {
87818798
return self.expected(
8782-
"ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE",
8799+
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, or SET TBLPROPERTIES after ALTER TABLE",
87838800
self.peek_token(),
87848801
);
87858802
}

0 commit comments

Comments
 (0)