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
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.
options: Vec<Ident>,
},
}

/// 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 @@ -9104,7 +9104,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 @@ -1334,6 +1334,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 @@ -398,6 +398,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 @@ -11089,6 +11089,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 @@ -11696,6 +11701,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(Parser::parse_identifier)?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ALTER TABLE t RESET (toast.autovacuum_enabled) fails with Expected: ), found: .; PostgreSQL 16 accepts it (ALTER TABLE). Vec<ObjectName> via parse_object_name(false) would cover the toast. prefix; SET (toast.autovacuum_enabled = false) has the same limitation through parse_sql_option, which is pre-existing.

self.expect_token(&Token::RParen)?;
AlterTableOperation::ResetOptions { options }
} else {
let mut options =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
Expand Down
27 changes: 27 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22744,3 +22744,30 @@ fn display_create_procedure_parameter_list_propagates_write_errors() {
let mut writer = RejectingWriter(" (");
assert_eq!(write!(writer, "{statement}"), Err(fmt::Error));
}

#[test]
fn parse_alter_table_reset_options() {
match alter_table_op(verified_stmt(
"ALTER TABLE tab RESET (fillfactor, autovacuum_enabled)",
)) {
AlterTableOperation::ResetOptions { options } => {
assert_eq!(
options,
[Ident::new("fillfactor"), 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"),
] {
assert_eq!(
all_dialects().parse_sql_statements(sql).unwrap_err(),
ParserError::ParserError(expected.to_string()),
"{sql}"
);
}
}
53 changes: 51 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 @@ -11351,3 +11351,52 @@ fn parse_drop_foreign_table_rejects_modifiers() {
);
}
}

#[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())
);
}
Loading