Skip to content
Open
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_2744_alter_table_reset.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE t RESET (fillfactor, toast.autovacuum_enabled)
13 changes: 13 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,16 @@ pub enum AlterTableOperation {
/// Parenthesized options supplied to `SET (...)`.
options: Vec<SqlOption>,
},
/// Parenthesized `RESET` of storage parameters.
///
/// ```sql
/// RESET ( storage_parameter [, ... ] )
/// ```
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
ResetOptions {
/// Parameters to reset, optionally namespaced as in `toast.autovacuum_enabled`.
options: Vec<ObjectName>,
},
}

/// An `ALTER Policy` (`Statement::AlterPolicy`) operation
Expand Down Expand Up @@ -1122,6 +1132,9 @@ impl fmt::Display for AlterTableOperation {
AlterTableOperation::SetOptionsParens { options } => {
write!(f, "SET ({})", display_comma_separated(options))
}
AlterTableOperation::ResetOptions { options } => {
write!(f, "RESET ({})", display_comma_separated(options))
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9141,7 +9141,8 @@ pub enum SqlOption {
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#TableOptions>
Clustered(TableOptionsClustered),
/// Single identifier options, e.g. `HEAP` for MSSQL.
/// Single identifier options, e.g. `HEAP` for MSSQL or a PostgreSQL storage parameter
/// named without a value.
///
/// <https://learn.microsoft.com/en-us/sql/t-sql/statements/create-table-azure-sql-data-warehouse?view=aps-pdw-2016-au7#TableOptions>
Ident(Ident),
Expand Down
3 changes: 3 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,9 @@ impl Spanned for AlterTableOperation {
AlterTableOperation::SetOptionsParens { options } => {
union_spans(options.iter().map(|i| i.span()))
}
AlterTableOperation::ResetOptions { options } => {
union_spans(options.iter().map(|i| i.span()))
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl Dialect for GenericDialect {
true
}

fn supports_sql_option_without_value(&self) -> bool {
true
}

fn supports_explain_with_utility_options(&self) -> bool {
true
}
Expand Down
8 changes: 8 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,6 +1348,14 @@ pub trait Dialect: Debug + Any {
false
}

/// Does the dialect accept an option named without a value,
/// e.g. `CREATE VIEW v WITH (security_barrier) AS SELECT 1`?
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-createtable.html#SQL-CREATETABLE-STORAGE-PARAMETERS)
fn supports_sql_option_without_value(&self) -> bool {
false
}

/// Whether `INTERVAL` expressions require units (called "qualifiers" in the ANSI SQL spec) to be specified,
/// e.g. `INTERVAL 1 DAY` vs `INTERVAL 1`.
///
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ impl Dialect for PostgreSqlDialect {
true
}

fn supports_sql_option_without_value(&self) -> bool {
true
}

/// see <https://www.postgresql.org/docs/current/sql-explain.html>
fn supports_explain_with_utility_options(&self) -> bool {
true
Expand Down
10 changes: 10 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11128,6 +11128,11 @@ impl<'a> Parser<'a> {
}
_ => {
let name = self.parse_identifier()?;
if self.dialect.supports_sql_option_without_value()
&& matches!(self.peek_token_ref().token, Token::Comma | Token::RParen)
{
return Ok(SqlOption::Ident(name));
}
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;

Expand Down Expand Up @@ -11735,6 +11740,11 @@ impl<'a> Parser<'a> {
AlterTableOperation::SetLogged
} else if self.parse_keywords(&[Keyword::SET, Keyword::UNLOGGED]) {
AlterTableOperation::SetUnlogged
} else if self.parse_keyword(Keyword::RESET) {
self.expect_token(&Token::LParen)?;
let options = self.parse_comma_separated(|p| p.parse_object_name(false))?;
self.expect_token(&Token::RParen)?;
AlterTableOperation::ResetOptions { options }
} else {
let mut options =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
Expand Down
34 changes: 34 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22848,3 +22848,37 @@ fn parse_grant_on_tables_named_after_object_types() {
verified_stmt(&format!("GRANT SELECT ON {name}.t TO r"));
}
}

#[test]
fn parse_alter_table_reset_options() {
match alter_table_op(verified_stmt(
"ALTER TABLE tab RESET (fillfactor, toast.autovacuum_enabled)",
)) {
AlterTableOperation::ResetOptions { options } => {
assert_eq!(
options,
[
ObjectName::from(vec![Ident::new("fillfactor")]),
ObjectName::from(vec![Ident::new("toast"), Ident::new("autovacuum_enabled")]),
]
);
}
_ => unreachable!(),
}

for (sql, expected) in [
("ALTER TABLE tab RESET ()", "Expected: identifier, found: )"),
("ALTER TABLE tab RESET (a = 1)", "Expected: ), found: ="),
("ALTER TABLE tab RESET a", "Expected: (, found: a"),
(
"ALTER TABLE tab RESET (toast.)",
"Expected: identifier, found: )",
),
] {
assert_eq!(
all_dialects().parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(expected.to_string()),
"{sql}"
);
}
}
65 changes: 63 additions & 2 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9122,9 +9122,9 @@ fn parse_create_text_search() {
)
);

// CREATE options are key-value pairs in PostgreSQL syntax.
assert_eq!(
pg().parse_sql_statements("CREATE TEXT SEARCH DICTIONARY d (template)")
all_dialects_where(|d| !d.supports_sql_option_without_value())
.parse_sql_statements("CREATE TEXT SEARCH DICTIONARY d (template)")
.unwrap_err(),
ParserError::ParserError("Expected: =, found: )".to_string())
);
Expand Down Expand Up @@ -11671,3 +11671,64 @@ fn parse_drop_group_is_dialect_gated() {
))
);
}

#[test]
fn parse_storage_parameters_without_value() {
let dialects = all_dialects_where(|d| d.supports_sql_option_without_value());
let bare = SqlOption::Ident(Ident::new("security_barrier"));
let key_value = SqlOption::KeyValue {
key: Ident::new("security_invoker"),
value: Expr::Value(Value::Boolean(true).with_empty_span()),
};

match dialects
.verified_stmt("CREATE VIEW v WITH (security_invoker = true, security_barrier) AS SELECT 1")
{
Statement::CreateView(CreateView { options, .. }) => assert_eq!(
options,
CreateTableOptions::With(vec![key_value.clone(), bare.clone()])
),
_ => unreachable!(),
}
match dialects.verified_stmt("CREATE TABLE t (a INT) WITH (security_barrier)") {
Statement::CreateTable(CreateTable { table_options, .. }) => {
assert_eq!(table_options, CreateTableOptions::With(vec![bare.clone()]))
}
_ => unreachable!(),
}
match dialects.verified_stmt("ALTER TABLE t SET (security_barrier, security_invoker = true)") {
Statement::AlterTable(AlterTable { operations, .. }) => assert_eq!(
operations,
[AlterTableOperation::SetOptionsParens {
options: vec![bare, key_value],
}]
),
_ => unreachable!(),
}
dialects.verified_stmt("CREATE MATERIALIZED VIEW m WITH (autovacuum_enabled) AS SELECT 1");

assert_eq!(
dialects
.parse_sql_statements("CREATE TABLE t (a INT) WITH (fillfactor 70)")
.unwrap_err(),
ParserError::ParserError("Expected: =, found: 70".to_string())
);
assert_eq!(
all_dialects_where(|d| !d.supports_sql_option_without_value())
.parse_sql_statements("ALTER TABLE t SET (autovacuum_enabled)")
.unwrap_err(),
ParserError::ParserError("Expected: =, found: )".to_string())
);
assert_eq!(
dialects
.parse_sql_statements("CREATE TABLE t (a INT) WITH (fillfactor =)")
.unwrap_err(),
ParserError::ParserError("Expected: an expression, found: )".to_string())
);
assert_eq!(
dialects
.parse_sql_statements("ALTER TABLE t SET TBLPROPERTIES (fillfactor 70)")
.unwrap_err(),
ParserError::ParserError("Expected: =, found: 70".to_string())
);
}
Loading