Skip to content

Commit f49c30f

Browse files
Postgres: Support parenthesized SET options for ALTER TABLE (#1947)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
1 parent 2ed2cbe commit f49c30f

File tree

4 files changed

+53
-4
lines changed

4 files changed

+53
-4
lines changed

src/ast/ddl.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,16 @@ pub enum AlterTableOperation {
351351
ValidateConstraint {
352352
name: Ident,
353353
},
354+
/// Arbitrary parenthesized `SET` options.
355+
///
356+
/// Example:
357+
/// ```sql
358+
/// SET (scale_factor = 0.01, threshold = 500)`
359+
/// ```
360+
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
361+
SetOptionsParens {
362+
options: Vec<SqlOption>,
363+
},
354364
}
355365

356366
/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
@@ -791,6 +801,9 @@ impl fmt::Display for AlterTableOperation {
791801
AlterTableOperation::ValidateConstraint { name } => {
792802
write!(f, "VALIDATE CONSTRAINT {name}")
793803
}
804+
AlterTableOperation::SetOptionsParens { options } => {
805+
write!(f, "SET ({})", display_comma_separated(options))
806+
}
794807
}
795808
}
796809
}

src/ast/spans.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,9 @@ impl Spanned for AlterTableOperation {
12021202
AlterTableOperation::Lock { .. } => Span::empty(),
12031203
AlterTableOperation::ReplicaIdentity { .. } => Span::empty(),
12041204
AlterTableOperation::ValidateConstraint { name } => name.span,
1205+
AlterTableOperation::SetOptionsParens { options } => {
1206+
union_spans(options.iter().map(|i| i.span()))
1207+
}
12051208
}
12061209
}
12071210
}

src/parser/mod.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9045,17 +9045,22 @@ impl<'a> Parser<'a> {
90459045
let name = self.parse_identifier()?;
90469046
AlterTableOperation::ValidateConstraint { name }
90479047
} else {
9048-
let options: Vec<SqlOption> =
9048+
let mut options =
90499049
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
90509050
if !options.is_empty() {
90519051
AlterTableOperation::SetTblProperties {
90529052
table_properties: options,
90539053
}
90549054
} else {
9055-
return self.expected(
9056-
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, or SET TBLPROPERTIES after ALTER TABLE",
9055+
options = self.parse_options(Keyword::SET)?;
9056+
if !options.is_empty() {
9057+
AlterTableOperation::SetOptionsParens { options }
9058+
} else {
9059+
return self.expected(
9060+
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, SET, or SET TBLPROPERTIES after ALTER TABLE",
90579061
self.peek_token(),
9058-
);
9062+
);
9063+
}
90599064
}
90609065
};
90619066
Ok(operation)

tests/sqlparser_common.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4728,6 +4728,34 @@ fn parse_alter_table() {
47284728
}
47294729
_ => unreachable!(),
47304730
}
4731+
4732+
let set_storage_parameters = "ALTER TABLE tab SET (autovacuum_vacuum_scale_factor = 0.01, autovacuum_vacuum_threshold = 500)";
4733+
match alter_table_op(verified_stmt(set_storage_parameters)) {
4734+
AlterTableOperation::SetOptionsParens { options } => {
4735+
assert_eq!(
4736+
options,
4737+
[
4738+
SqlOption::KeyValue {
4739+
key: Ident {
4740+
value: "autovacuum_vacuum_scale_factor".to_string(),
4741+
quote_style: None,
4742+
span: Span::empty(),
4743+
},
4744+
value: Expr::Value(test_utils::number("0.01").with_empty_span()),
4745+
},
4746+
SqlOption::KeyValue {
4747+
key: Ident {
4748+
value: "autovacuum_vacuum_threshold".to_string(),
4749+
quote_style: None,
4750+
span: Span::empty(),
4751+
},
4752+
value: Expr::Value(test_utils::number("500").with_empty_span()),
4753+
}
4754+
],
4755+
);
4756+
}
4757+
_ => unreachable!(),
4758+
}
47314759
}
47324760

47334761
#[test]

0 commit comments

Comments
 (0)