Skip to content

Commit 1654c63

Browse files
MohamedAbdeen21ayman-sigma
authored andcommitted
Add LOCK operation for ALTER TABLE (apache#1768)
1 parent d753af7 commit 1654c63

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

src/ast/ddl.rs

+37
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,16 @@ pub enum AlterTableOperation {
288288
equals: bool,
289289
algorithm: AlterTableAlgorithm,
290290
},
291+
292+
/// `LOCK [=] { DEFAULT | NONE | SHARED | EXCLUSIVE }`
293+
///
294+
/// [MySQL]-specific table alter lock.
295+
///
296+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
297+
Lock {
298+
equals: bool,
299+
lock: AlterTableLock,
300+
},
291301
/// `AUTO_INCREMENT [=] <value>`
292302
///
293303
/// [MySQL]-specific table option for raising current auto increment value.
@@ -366,6 +376,30 @@ impl fmt::Display for AlterTableAlgorithm {
366376
}
367377
}
368378

379+
/// [MySQL] `ALTER TABLE` lock.
380+
///
381+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
382+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
383+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
384+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
385+
pub enum AlterTableLock {
386+
Default,
387+
None,
388+
Shared,
389+
Exclusive,
390+
}
391+
392+
impl fmt::Display for AlterTableLock {
393+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
394+
f.write_str(match self {
395+
Self::Default => "DEFAULT",
396+
Self::None => "NONE",
397+
Self::Shared => "SHARED",
398+
Self::Exclusive => "EXCLUSIVE",
399+
})
400+
}
401+
}
402+
369403
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
370404
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
371405
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
@@ -692,6 +726,9 @@ impl fmt::Display for AlterTableOperation {
692726
value
693727
)
694728
}
729+
AlterTableOperation::Lock { equals, lock } => {
730+
write!(f, "LOCK {}{}", if *equals { "= " } else { "" }, lock)
731+
}
695732
}
696733
}
697734
}

src/ast/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use self::dcl::{
4848
};
4949
pub use self::ddl::{
5050
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
51-
AlterTableAlgorithm, AlterTableOperation, AlterType, AlterTypeAddValue,
51+
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType, AlterTypeAddValue,
5252
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
5353
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty,
5454
ConstraintCharacteristics, CreateConnector, CreateFunction, Deduplicate, DeferrableInitial,

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ impl Spanned for AlterTableOperation {
11191119
AlterTableOperation::ResumeRecluster => Span::empty(),
11201120
AlterTableOperation::Algorithm { .. } => Span::empty(),
11211121
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
1122+
AlterTableOperation::Lock { .. } => Span::empty(),
11221123
}
11231124
}
11241125
}

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,7 @@ define_keywords!(
797797
SETS,
798798
SETTINGS,
799799
SHARE,
800+
SHARED,
800801
SHARING,
801802
SHOW,
802803
SIGNED,

src/parser/mod.rs

+18
Original file line numberDiff line numberDiff line change
@@ -8321,6 +8321,24 @@ impl<'a> Parser<'a> {
83218321
AlterTableOperation::SuspendRecluster
83228322
} else if self.parse_keywords(&[Keyword::RESUME, Keyword::RECLUSTER]) {
83238323
AlterTableOperation::ResumeRecluster
8324+
} else if self.parse_keyword(Keyword::LOCK) {
8325+
let equals = self.consume_token(&Token::Eq);
8326+
let lock = match self.parse_one_of_keywords(&[
8327+
Keyword::DEFAULT,
8328+
Keyword::EXCLUSIVE,
8329+
Keyword::NONE,
8330+
Keyword::SHARED,
8331+
]) {
8332+
Some(Keyword::DEFAULT) => AlterTableLock::Default,
8333+
Some(Keyword::EXCLUSIVE) => AlterTableLock::Exclusive,
8334+
Some(Keyword::NONE) => AlterTableLock::None,
8335+
Some(Keyword::SHARED) => AlterTableLock::Shared,
8336+
_ => self.expected(
8337+
"DEFAULT, EXCLUSIVE, NONE or SHARED after LOCK [=]",
8338+
self.peek_token(),
8339+
)?,
8340+
};
8341+
AlterTableOperation::Lock { equals, lock }
83248342
} else if self.parse_keyword(Keyword::ALGORITHM) {
83258343
let equals = self.consume_token(&Token::Eq);
83268344
let algorithm = match self.parse_one_of_keywords(&[

tests/sqlparser_mysql.rs

+46
Original file line numberDiff line numberDiff line change
@@ -2454,6 +2454,52 @@ fn parse_alter_table_with_algorithm() {
24542454
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = COPY");
24552455
}
24562456

2457+
#[test]
2458+
fn parse_alter_table_with_lock() {
2459+
let sql = "ALTER TABLE tab LOCK = SHARED";
2460+
let expected_operation = AlterTableOperation::Lock {
2461+
equals: true,
2462+
lock: AlterTableLock::Shared,
2463+
};
2464+
let operation = alter_table_op(mysql_and_generic().verified_stmt(sql));
2465+
assert_eq!(expected_operation, operation);
2466+
2467+
let sql =
2468+
"ALTER TABLE users DROP COLUMN password_digest, LOCK = EXCLUSIVE, RENAME COLUMN name TO username";
2469+
let stmt = mysql_and_generic().verified_stmt(sql);
2470+
match stmt {
2471+
Statement::AlterTable { operations, .. } => {
2472+
assert_eq!(
2473+
operations,
2474+
vec![
2475+
AlterTableOperation::DropColumn {
2476+
column_name: Ident::new("password_digest"),
2477+
if_exists: false,
2478+
drop_behavior: None,
2479+
},
2480+
AlterTableOperation::Lock {
2481+
equals: true,
2482+
lock: AlterTableLock::Exclusive,
2483+
},
2484+
AlterTableOperation::RenameColumn {
2485+
old_column_name: Ident::new("name"),
2486+
new_column_name: Ident::new("username")
2487+
},
2488+
]
2489+
)
2490+
}
2491+
_ => panic!("Unexpected statement {stmt}"),
2492+
}
2493+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK DEFAULT");
2494+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK SHARED");
2495+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK NONE");
2496+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK EXCLUSIVE");
2497+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK = DEFAULT");
2498+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK = SHARED");
2499+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK = NONE");
2500+
mysql_and_generic().verified_stmt("ALTER TABLE `users` LOCK = EXCLUSIVE");
2501+
}
2502+
24572503
#[test]
24582504
fn parse_alter_table_auto_increment() {
24592505
let sql = "ALTER TABLE tab AUTO_INCREMENT = 42";

0 commit comments

Comments
 (0)