Parse ALTER TABLE AUTO_INCREMENT operation for MySQL (#1748)

This commit is contained in:
Michael Victor Zink 2025-02-26 21:40:30 -08:00 committed by GitHub
parent 5b3500139a
commit c2914f82e1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 36 additions and 0 deletions

View file

@ -34,6 +34,7 @@ use crate::ast::{
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName,
OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag, Value,
ValueWithSpan,
};
use crate::keywords::Keyword;
use crate::tokenizer::Token;
@ -277,6 +278,15 @@ pub enum AlterTableOperation {
equals: bool,
algorithm: AlterTableAlgorithm,
},
/// `AUTO_INCREMENT [=] <value>`
///
/// [MySQL]-specific table option for raising current auto increment value.
///
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
AutoIncrement {
equals: bool,
value: ValueWithSpan,
},
}
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
@ -663,6 +673,14 @@ impl fmt::Display for AlterTableOperation {
write!(f, "RESUME RECLUSTER")?;
Ok(())
}
AlterTableOperation::AutoIncrement { equals, value } => {
write!(
f,
"AUTO_INCREMENT {}{}",
if *equals { "= " } else { "" },
value
)
}
}
}
}

View file

@ -1063,6 +1063,7 @@ impl Spanned for AlterTableOperation {
AlterTableOperation::SuspendRecluster => Span::empty(),
AlterTableOperation::ResumeRecluster => Span::empty(),
AlterTableOperation::Algorithm { .. } => Span::empty(),
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
}
}
}

View file

@ -8181,6 +8181,10 @@ impl<'a> Parser<'a> {
)?,
};
AlterTableOperation::Algorithm { equals, algorithm }
} else if self.parse_keyword(Keyword::AUTO_INCREMENT) {
let equals = self.consume_token(&Token::Eq);
let value = self.parse_number_value()?;
AlterTableOperation::AutoIncrement { equals, value }
} else {
let options: Vec<SqlOption> =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;

View file

@ -2470,6 +2470,19 @@ fn parse_alter_table_with_algorithm() {
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = COPY");
}
#[test]
fn parse_alter_table_auto_increment() {
let sql = "ALTER TABLE tab AUTO_INCREMENT = 42";
let expected_operation = AlterTableOperation::AutoIncrement {
equals: true,
value: number("42").with_empty_span(),
};
let operation = alter_table_op(mysql().verified_stmt(sql));
assert_eq!(expected_operation, operation);
mysql_and_generic().verified_stmt("ALTER TABLE `users` AUTO_INCREMENT 42");
}
#[test]
fn parse_alter_table_modify_column_with_column_position() {
let expected_name = ObjectName::from(vec![Ident::new("orders")]);