Skip to content

Commit c2914f8

Browse files
authored
Parse ALTER TABLE AUTO_INCREMENT operation for MySQL (#1748)
1 parent 5b35001 commit c2914f8

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/ast/ddl.rs

+18
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::ast::{
3434
CreateFunctionUsing, DataType, Expr, FunctionBehavior, FunctionCalledOnNull,
3535
FunctionDeterminismSpecifier, FunctionParallel, Ident, MySQLColumnPosition, ObjectName,
3636
OperateFunctionArg, OrderByExpr, ProjectionSelect, SequenceOptions, SqlOption, Tag, Value,
37+
ValueWithSpan,
3738
};
3839
use crate::keywords::Keyword;
3940
use crate::tokenizer::Token;
@@ -277,6 +278,15 @@ pub enum AlterTableOperation {
277278
equals: bool,
278279
algorithm: AlterTableAlgorithm,
279280
},
281+
/// `AUTO_INCREMENT [=] <value>`
282+
///
283+
/// [MySQL]-specific table option for raising current auto increment value.
284+
///
285+
/// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/alter-table.html
286+
AutoIncrement {
287+
equals: bool,
288+
value: ValueWithSpan,
289+
},
280290
}
281291

282292
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
@@ -663,6 +673,14 @@ impl fmt::Display for AlterTableOperation {
663673
write!(f, "RESUME RECLUSTER")?;
664674
Ok(())
665675
}
676+
AlterTableOperation::AutoIncrement { equals, value } => {
677+
write!(
678+
f,
679+
"AUTO_INCREMENT {}{}",
680+
if *equals { "= " } else { "" },
681+
value
682+
)
683+
}
666684
}
667685
}
668686
}

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ impl Spanned for AlterTableOperation {
10631063
AlterTableOperation::SuspendRecluster => Span::empty(),
10641064
AlterTableOperation::ResumeRecluster => Span::empty(),
10651065
AlterTableOperation::Algorithm { .. } => Span::empty(),
1066+
AlterTableOperation::AutoIncrement { value, .. } => value.span(),
10661067
}
10671068
}
10681069
}

src/parser/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -8181,6 +8181,10 @@ impl<'a> Parser<'a> {
81818181
)?,
81828182
};
81838183
AlterTableOperation::Algorithm { equals, algorithm }
8184+
} else if self.parse_keyword(Keyword::AUTO_INCREMENT) {
8185+
let equals = self.consume_token(&Token::Eq);
8186+
let value = self.parse_number_value()?;
8187+
AlterTableOperation::AutoIncrement { equals, value }
81848188
} else {
81858189
let options: Vec<SqlOption> =
81868190
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;

tests/sqlparser_mysql.rs

+13
Original file line numberDiff line numberDiff line change
@@ -2470,6 +2470,19 @@ fn parse_alter_table_with_algorithm() {
24702470
mysql_and_generic().verified_stmt("ALTER TABLE `users` ALGORITHM = COPY");
24712471
}
24722472

2473+
#[test]
2474+
fn parse_alter_table_auto_increment() {
2475+
let sql = "ALTER TABLE tab AUTO_INCREMENT = 42";
2476+
let expected_operation = AlterTableOperation::AutoIncrement {
2477+
equals: true,
2478+
value: number("42").with_empty_span(),
2479+
};
2480+
let operation = alter_table_op(mysql().verified_stmt(sql));
2481+
assert_eq!(expected_operation, operation);
2482+
2483+
mysql_and_generic().verified_stmt("ALTER TABLE `users` AUTO_INCREMENT 42");
2484+
}
2485+
24732486
#[test]
24742487
fn parse_alter_table_modify_column_with_column_position() {
24752488
let expected_name = ObjectName::from(vec![Ident::new("orders")]);

0 commit comments

Comments
 (0)