Skip to content

Commit 76df2b6

Browse files
PostgreSQL: Support ALTER COLUMN ... SET STORAGE (#3032)
1 parent 6ce1bbd commit 76df2b6

5 files changed

Lines changed: 77 additions & 24 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE t ALTER COLUMN b SET STORAGE EXTERNAL

‎src/ast/ddl.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,13 @@ pub enum AlterColumnOperation {
14611461
/// Whether `IF EXISTS` was specified.
14621462
if_exists: bool,
14631463
},
1464+
/// `SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }`
1465+
///
1466+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
1467+
SetStorage {
1468+
/// The type of storage.
1469+
storage: UserDefinedTypeStorage,
1470+
},
14641471
/// `OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ...] )`, changing the
14651472
/// options of a foreign table column.
14661473
///
@@ -1529,6 +1536,9 @@ impl fmt::Display for AlterColumnOperation {
15291536
}
15301537
Ok(())
15311538
}
1539+
AlterColumnOperation::SetStorage { storage } => {
1540+
write!(f, "SET STORAGE {storage}")
1541+
}
15321542
AlterColumnOperation::GenericOptions { options } => {
15331543
write!(f, "OPTIONS ({})", display_comma_separated(options))
15341544
}

‎src/ast/spans.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ impl Spanned for RefreshMaterializedView {
973973
/// - [AlterColumnOperation::DropNotNull]
974974
/// - [AlterColumnOperation::DropDefault]
975975
/// - [AlterColumnOperation::AddGenerated]
976+
/// - [AlterColumnOperation::SetStorage]
976977
impl Spanned for AlterColumnOperation {
977978
fn span(&self) -> Span {
978979
match self {
@@ -993,6 +994,7 @@ impl Spanned for AlterColumnOperation {
993994
),
994995
AlterColumnOperation::AddGenerated { .. } => Span::empty(),
995996
AlterColumnOperation::DropExpression { .. } => Span::empty(),
997+
AlterColumnOperation::SetStorage { .. } => Span::empty(),
996998
AlterColumnOperation::GenericOptions { options } => {
997999
union_spans(options.iter().map(|option| option.span()))
9981000
}

‎src/parser/mod.rs‎

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11874,6 +11874,10 @@ impl<'a> Parser<'a> {
1187411874
AlterColumnOperation::GenericOptions {
1187511875
options: self.parse_alter_generic_options()?,
1187611876
}
11877+
} else if self.parse_keywords(&[Keyword::SET, Keyword::STORAGE]) {
11878+
AlterColumnOperation::SetStorage {
11879+
storage: self.parse_user_defined_type_storage()?,
11880+
}
1187711881
} else {
1187811882
let message = if is_postgresql {
1187911883
"SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE, ADD GENERATED, or OPTIONS after ALTER COLUMN"
@@ -22664,30 +22668,9 @@ impl<'a> Parser<'a> {
2266422668
}
2266522669
Some(Keyword::STORAGE) => {
2266622670
self.expect_token(&Token::Eq)?;
22667-
let storage_keyword = self.parse_one_of_keywords(&[
22668-
Keyword::PLAIN,
22669-
Keyword::EXTERNAL,
22670-
Keyword::EXTENDED,
22671-
Keyword::MAIN,
22672-
]);
22673-
match storage_keyword {
22674-
Some(Keyword::PLAIN) => Ok(UserDefinedTypeSqlDefinitionOption::Storage(
22675-
UserDefinedTypeStorage::Plain,
22676-
)),
22677-
Some(Keyword::EXTERNAL) => Ok(UserDefinedTypeSqlDefinitionOption::Storage(
22678-
UserDefinedTypeStorage::External,
22679-
)),
22680-
Some(Keyword::EXTENDED) => Ok(UserDefinedTypeSqlDefinitionOption::Storage(
22681-
UserDefinedTypeStorage::Extended,
22682-
)),
22683-
Some(Keyword::MAIN) => Ok(UserDefinedTypeSqlDefinitionOption::Storage(
22684-
UserDefinedTypeStorage::Main,
22685-
)),
22686-
_ => self.expected_ref(
22687-
"storage value (plain, external, extended, or main)",
22688-
self.peek_token_ref(),
22689-
),
22690-
}
22671+
Ok(UserDefinedTypeSqlDefinitionOption::Storage(
22672+
self.parse_user_defined_type_storage()?,
22673+
))
2269122674
}
2269222675
Some(Keyword::LIKE) => {
2269322676
self.expect_token(&Token::Eq)?;
@@ -22735,6 +22718,26 @@ impl<'a> Parser<'a> {
2273522718
}
2273622719
}
2273722720

22721+
/// Parses `PLAIN`, `EXTERNAL`, `EXTENDED` or `MAIN`.
22722+
fn parse_user_defined_type_storage(&mut self) -> Result<UserDefinedTypeStorage, ParserError> {
22723+
let storage_keyword = self.parse_one_of_keywords(&[
22724+
Keyword::PLAIN,
22725+
Keyword::EXTERNAL,
22726+
Keyword::EXTENDED,
22727+
Keyword::MAIN,
22728+
]);
22729+
match storage_keyword {
22730+
Some(Keyword::PLAIN) => Ok(UserDefinedTypeStorage::Plain),
22731+
Some(Keyword::EXTERNAL) => Ok(UserDefinedTypeStorage::External),
22732+
Some(Keyword::EXTENDED) => Ok(UserDefinedTypeStorage::Extended),
22733+
Some(Keyword::MAIN) => Ok(UserDefinedTypeStorage::Main),
22734+
_ => self.expected_ref(
22735+
"storage value (plain, external, extended, or main)",
22736+
self.peek_token_ref(),
22737+
),
22738+
}
22739+
}
22740+
2273822741
fn parse_parenthesized_identifiers(&mut self) -> Result<Vec<Ident>, ParserError> {
2273922742
self.expect_token(&Token::LParen)?;
2274022743
let idents = self.parse_comma_separated0(|p| p.parse_identifier(), Token::RParen)?;

‎tests/sqlparser_postgres.rs‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14560,3 +14560,40 @@ fn parse_alter_table_set_schema() {
1456014560
"sql parser error: Expected: identifier, found: EOF"
1456114561
);
1456214562
}
14563+
14564+
#[test]
14565+
fn parse_alter_column_set_storage() {
14566+
for (input, output, storage) in [
14567+
("PLAIN", "plain", UserDefinedTypeStorage::Plain),
14568+
("EXTERNAL", "external", UserDefinedTypeStorage::External),
14569+
("EXTENDED", "extended", UserDefinedTypeStorage::Extended),
14570+
("MAIN", "main", UserDefinedTypeStorage::Main),
14571+
] {
14572+
let statement = pg_and_generic().one_statement_parses_to(
14573+
&format!("ALTER TABLE t ALTER COLUMN b SET STORAGE {input}"),
14574+
&format!("ALTER TABLE t ALTER COLUMN b SET STORAGE {output}"),
14575+
);
14576+
14577+
let Statement::AlterTable(AlterTable { operations, .. }) = statement else {
14578+
panic!("expected ALTER TABLE, got {statement:?}")
14579+
};
14580+
assert_eq!(
14581+
operations,
14582+
vec![AlterTableOperation::AlterColumn {
14583+
column_name: Ident::new("b"),
14584+
op: AlterColumnOperation::SetStorage { storage }
14585+
}]
14586+
)
14587+
}
14588+
14589+
for (sql, found) in [
14590+
("ALTER TABLE t ALTER COLUMN b SET STORAGE", "EOF"),
14591+
("ALTER TABLE t ALTER COLUMN b SET STORAGE fast", "fast"),
14592+
] {
14593+
assert_eq!(
14594+
pg_and_generic().parse_sql_statements(sql).unwrap_err().to_string(),
14595+
format!("sql parser error: Expected: storage value (plain, external, extended, or main), found: {found}"),
14596+
"{sql}"
14597+
);
14598+
}
14599+
}

0 commit comments

Comments
 (0)