Skip to content

Parse MySQL ALTER TABLE ALGORITHM option #1745

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 26, 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
45 changes: 41 additions & 4 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ pub enum AlterTableOperation {
name: Ident,
select: ProjectionSelect,
},

/// `DROP PROJECTION [IF EXISTS] name`
///
/// Note: this is a ClickHouse-specific operation.
Expand All @@ -74,7 +73,6 @@ pub enum AlterTableOperation {
if_exists: bool,
name: Ident,
},

/// `MATERIALIZE PROJECTION [IF EXISTS] name [IN PARTITION partition_name]`
///
/// Note: this is a ClickHouse-specific operation.
Expand All @@ -84,7 +82,6 @@ pub enum AlterTableOperation {
name: Ident,
partition: Option<Ident>,
},

/// `CLEAR PROJECTION [IF EXISTS] name [IN PARTITION partition_name]`
///
/// Note: this is a ClickHouse-specific operation.
Expand All @@ -94,7 +91,6 @@ pub enum AlterTableOperation {
name: Ident,
partition: Option<Ident>,
},

/// `DISABLE ROW LEVEL SECURITY`
///
/// Note: this is a PostgreSQL-specific operation.
Expand Down Expand Up @@ -272,6 +268,15 @@ pub enum AlterTableOperation {
DropClusteringKey,
SuspendRecluster,
ResumeRecluster,
/// `ALGORITHM [=] { DEFAULT | INSTANT | INPLACE | COPY }`
///
/// [MySQL]-specific table alter algorithm.
///
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
Algorithm {
equals: bool,
algorithm: AlterTableAlgorithm,
},
}

/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
Expand Down Expand Up @@ -317,6 +322,30 @@ impl fmt::Display for AlterPolicyOperation {
}
}

/// [MySQL] `ALTER TABLE` algorithm.
///
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AlterTableAlgorithm {
Default,
Instant,
Inplace,
Copy,
}

impl fmt::Display for AlterTableAlgorithm {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(match self {
Self::Default => "DEFAULT",
Self::Instant => "INSTANT",
Self::Inplace => "INPLACE",
Self::Copy => "COPY",
})
}
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down Expand Up @@ -407,6 +436,14 @@ impl fmt::Display for AlterTableOperation {
}
write!(f, " {} ({})", name, query)
}
AlterTableOperation::Algorithm { equals, algorithm } => {
write!(
f,
"ALGORITHM {}{}",
if *equals { "= " } else { "" },
algorithm
)
}
AlterTableOperation::DropProjection { if_exists, name } => {
write!(f, "DROP PROJECTION")?;
if *if_exists {
Expand Down
18 changes: 9 additions & 9 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ pub use self::dcl::{
};
pub use self::ddl::{
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
AlterTableOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, ClusteredBy, ColumnDef,
ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics,
CreateConnector, CreateFunction, Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs,
GeneratedExpressionMode, IdentityParameters, IdentityProperty, IdentityPropertyFormatKind,
IdentityPropertyKind, IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay,
NullsDistinctOption, Owner, Partition, ProcedureParam, ReferentialAction, TableConstraint,
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
ViewColumnDef,
AlterTableAlgorithm, AlterTableOperation, AlterType, AlterTypeAddValue,
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty,
ConstraintCharacteristics, CreateConnector, CreateFunction, Deduplicate, DeferrableInitial,
DropBehavior, GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexOption,
IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition, ProcedureParam,
ReferentialAction, TableConstraint, TagsColumnOption, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeRepresentation, ViewColumnDef,
};
pub use self::dml::{CreateIndex, CreateTable, Delete, Insert};
pub use self::operator::{BinaryOperator, UnaryOperator};
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1062,6 +1062,7 @@ impl Spanned for AlterTableOperation {
AlterTableOperation::DropClusteringKey => Span::empty(),
AlterTableOperation::SuspendRecluster => Span::empty(),
AlterTableOperation::ResumeRecluster => Span::empty(),
AlterTableOperation::Algorithm { .. } => Span::empty(),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,13 @@ define_keywords!(
INNER,
INOUT,
INPATH,
INPLACE,
INPUT,
INPUTFORMAT,
INSENSITIVE,
INSERT,
INSTALL,
INSTANT,
INSTEAD,
INT,
INT128,
Expand Down
18 changes: 18 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8163,6 +8163,24 @@ impl<'a> Parser<'a> {
AlterTableOperation::SuspendRecluster
} else if self.parse_keywords(&[Keyword::RESUME, Keyword::RECLUSTER]) {
AlterTableOperation::ResumeRecluster
} else if self.parse_keyword(Keyword::ALGORITHM) {
let equals = self.consume_token(&Token::Eq);
let algorithm = match self.parse_one_of_keywords(&[
Keyword::DEFAULT,
Keyword::INSTANT,
Keyword::INPLACE,
Keyword::COPY,
]) {
Some(Keyword::DEFAULT) => AlterTableAlgorithm::Default,
Some(Keyword::INSTANT) => AlterTableAlgorithm::Instant,
Some(Keyword::INPLACE) => AlterTableAlgorithm::Inplace,
Some(Keyword::COPY) => AlterTableAlgorithm::Copy,
_ => self.expected(
"DEFAULT, INSTANT, INPLACE, or COPY after ALGORITHM [=]",
self.peek_token(),
)?,
};
AlterTableOperation::Algorithm { equals, algorithm }
} else {
let options: Vec<SqlOption> =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
Expand Down
48 changes: 48 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,54 @@ fn parse_alter_table_modify_column() {
assert_eq!(expected_operation, operation);
}

#[test]
fn parse_alter_table_with_algorithm() {
let sql = "ALTER TABLE tab ALGORITHM = COPY";
let expected_operation = AlterTableOperation::Algorithm {
equals: true,
algorithm: AlterTableAlgorithm::Copy,
};
let operation = alter_table_op(mysql_and_generic().verified_stmt(sql));
assert_eq!(expected_operation, operation);

// Check order doesn't matter
let sql =
"ALTER TABLE users DROP COLUMN password_digest, ALGORITHM = COPY, RENAME COLUMN name TO username";
let stmt = mysql_and_generic().verified_stmt(sql);
match stmt {
Statement::AlterTable { operations, .. } => {
assert_eq!(
operations,
vec![
AlterTableOperation::DropColumn {
column_name: Ident::new("password_digest"),
if_exists: false,
drop_behavior: None,
},
AlterTableOperation::Algorithm {
equals: true,
algorithm: AlterTableAlgorithm::Copy,
},
AlterTableOperation::RenameColumn {
old_column_name: Ident::new("name"),
new_column_name: Ident::new("username")
},
]
)
}
_ => panic!("Unexpected statement {stmt}"),
}

mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM DEFAULT");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM INSTANT");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM INPLACE");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM COPY");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = DEFAULT");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = INSTANT");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = INPLACE");
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = COPY");
}

#[test]
fn parse_alter_table_modify_column_with_column_position() {
let expected_name = ObjectName::from(vec![Ident::new("orders")]);
Expand Down