Skip to content

Commit deb1d6a

Browse files
achristmascarliffyio
authored andcommitted
Postgres: Support parenthesized SET options for ALTER TABLE (apache#1947)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
1 parent 3304f9a commit deb1d6a

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
@@ -9076,17 +9076,22 @@ impl<'a> Parser<'a> {
90769076
let name = self.parse_identifier()?;
90779077
AlterTableOperation::ValidateConstraint { name }
90789078
} else {
9079-
let options: Vec<SqlOption> =
9079+
let mut options =
90809080
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
90819081
if !options.is_empty() {
90829082
AlterTableOperation::SetTblProperties {
90839083
table_properties: options,
90849084
}
90859085
} else {
9086-
return self.expected(
9087-
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, or SET TBLPROPERTIES after ALTER TABLE",
9086+
options = self.parse_options(Keyword::SET)?;
9087+
if !options.is_empty() {
9088+
AlterTableOperation::SetOptionsParens { options }
9089+
} else {
9090+
return self.expected(
9091+
"ADD, RENAME, PARTITION, SWAP, DROP, REPLICA IDENTITY, SET, or SET TBLPROPERTIES after ALTER TABLE",
90889092
self.peek_token(),
9089-
);
9093+
);
9094+
}
90909095
}
90919096
};
90929097
Ok(operation)

tests/sqlparser_common.rs

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

47344762
#[test]

0 commit comments

Comments
 (0)