Skip to content

feat: Include end token in ALTER TABLE statement #1999

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 3 commits into from
Aug 9, 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
3 changes: 3 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3382,6 +3382,8 @@ pub enum Statement {
/// Snowflake "ICEBERG" clause for Iceberg tables
/// <https://docs.snowflake.com/en/sql-reference/sql/alter-iceberg-table>
iceberg: bool,
/// Token that represents the end of the statement (semicolon or EOF)
end_token: AttachedToken,
},
/// ```sql
/// ALTER INDEX
Expand Down Expand Up @@ -5419,6 +5421,7 @@ impl fmt::Display for Statement {
location,
on_cluster,
iceberg,
end_token: _,
} => {
if *iceberg {
write!(f, "ALTER ICEBERG TABLE ")?;
Expand Down
20 changes: 19 additions & 1 deletion src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,12 @@ impl Spanned for Statement {
location: _,
on_cluster,
iceberg: _,
end_token,
} => union_spans(
core::iter::once(name.span())
.chain(operations.iter().map(|i| i.span()))
.chain(on_cluster.iter().map(|i| i.span)),
.chain(on_cluster.iter().map(|i| i.span))
.chain(core::iter::once(end_token.0.span)),
),
Statement::AlterIndex { name, operation } => name.span().union(&operation.span()),
Statement::AlterView {
Expand Down Expand Up @@ -2548,4 +2550,20 @@ pub mod tests {
stmt => panic!("expected query; got {stmt:?}"),
}
}

#[test]
fn test_alter_table_multiline_span() {
let sql = r#"-- foo
ALTER TABLE users
ADD COLUMN foo
varchar; -- hi there"#;

let r = Parser::parse_sql(&crate::dialect::PostgreSqlDialect {}, sql).unwrap();
assert_eq!(1, r.len());

let stmt_span = r[0].span();

assert_eq!(stmt_span.start, (2, 13).into());
assert_eq!(stmt_span.end, (4, 11).into());
}
}
7 changes: 7 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9227,6 +9227,12 @@ impl<'a> Parser<'a> {
});
}

let end_token = if self.peek_token_ref().token == Token::SemiColon {
self.peek_token_ref().clone()
} else {
self.get_current_token().clone()
};

Ok(Statement::AlterTable {
name: table_name,
if_exists,
Expand All @@ -9235,6 +9241,7 @@ impl<'a> Parser<'a> {
location,
on_cluster,
iceberg,
end_token: AttachedToken(end_token),
})
}

Expand Down
1 change: 1 addition & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ pub fn alter_table_op_with_name(stmt: Statement, expected_name: &str) -> AlterTa
on_cluster: _,
location: _,
iceberg,
end_token: _,
} => {
assert_eq!(name.to_string(), expected_name);
assert!(!if_exists);
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2635,6 +2635,7 @@ fn parse_alter_table_add_column() {
iceberg,
location: _,
on_cluster: _,
end_token: _,
} => {
assert_eq!(name.to_string(), "tab");
assert!(!if_exists);
Expand Down
Loading