Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER MATERIALIZED VIEW IF EXISTS v ALTER c SET (n_distinct = -0.5, n_distinct_inherited = 3);
ALTER MATERIALIZED VIEW v ALTER COLUMN c RESET (n_distinct, n_distinct_inherited);
ALTER MATERIALIZED VIEW v ALTER c SET STORAGE DEFAULT;
41 changes: 31 additions & 10 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,7 +1415,7 @@ impl fmt::Display for OperatorOption {
}
}

/// An `ALTER COLUMN` (`Statement::AlterTable`) operation
/// An `ALTER COLUMN` operation in `ALTER TABLE` or `ALTER [ MATERIALIZED ] VIEW`.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
Expand Down Expand Up @@ -1461,12 +1461,12 @@ pub enum AlterColumnOperation {
/// Whether `IF EXISTS` was specified.
if_exists: bool,
},
/// `SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }`
/// `SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN | DEFAULT }`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
SetStorage {
/// The type of storage.
storage: UserDefinedTypeStorage,
/// The storage mode, or `None` for `DEFAULT`.
storage: Option<UserDefinedTypeStorage>,
},
/// `OPTIONS ( [ ADD | SET | DROP ] option ['value'] [, ...] )`, changing the
/// options of a foreign table column.
Expand All @@ -1476,6 +1476,20 @@ pub enum AlterColumnOperation {
/// The option changes, in source order.
options: Vec<AlterGenericOption>,
},
/// `SET ( attribute_option [ = value ] [, ... ] )`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
SetOptions {
/// Options to set, using [`SqlOption::Ident`] when no value is given.
options: Vec<SqlOption>,
},
/// `RESET ( attribute_option [, ... ] )`
///
/// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
ResetOptions {
/// Option names to reset, possibly qualified.
options: Vec<ObjectName>,
},
}

impl fmt::Display for AlterColumnOperation {
Expand Down Expand Up @@ -1536,12 +1550,19 @@ impl fmt::Display for AlterColumnOperation {
}
Ok(())
}
AlterColumnOperation::SetStorage { storage } => {
write!(f, "SET STORAGE {storage}")
}
AlterColumnOperation::SetStorage { storage } => match storage {
Some(storage) => write!(f, "SET STORAGE {storage}"),
None => write!(f, "SET STORAGE DEFAULT"),
},
AlterColumnOperation::GenericOptions { options } => {
write!(f, "OPTIONS ({})", display_comma_separated(options))
}
AlterColumnOperation::SetOptions { options } => {
write!(f, "SET ({})", display_comma_separated(options))
}
AlterColumnOperation::ResetOptions { options } => {
write!(f, "RESET ({})", display_comma_separated(options))
}
}
}
}
Expand Down Expand Up @@ -5392,8 +5413,7 @@ impl Spanned for AlterCollation {
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AlterView {
/// Whether `MATERIALIZED` was specified. A materialized view's operation is never
/// [`AlterViewOperation::As`] or [`AlterViewOperation::AlterColumn`].
/// Whether `MATERIALIZED` was specified.
pub materialized: bool,
/// View name being altered.
#[cfg_attr(feature = "visitor", visit(with = "visit_relation"))]
Expand Down Expand Up @@ -5428,11 +5448,12 @@ pub enum AlterViewOperation {
},
/// ```sql
/// ALTER VIEW name ALTER [ COLUMN ] column_name { SET DEFAULT expression | DROP DEFAULT }
/// ALTER MATERIALIZED VIEW name ALTER [ COLUMN ] column_name { SET (...) | RESET (...) | SET STORAGE mode }
/// ```
AlterColumn {
/// The column to alter.
column_name: Ident,
/// Either [`AlterColumnOperation::SetDefault`] or [`AlterColumnOperation::DropDefault`].
/// A column default operation, or an attribute option or storage change on a materialized view.
op: AlterColumnOperation,
},
/// ```sql
Expand Down
6 changes: 6 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,12 @@ impl Spanned for AlterColumnOperation {
AlterColumnOperation::GenericOptions { options } => {
union_spans(options.iter().map(|option| option.span()))
}
AlterColumnOperation::SetOptions { options } => {
union_spans(options.iter().map(|option| option.span()))
}
AlterColumnOperation::ResetOptions { options } => {
union_spans(options.iter().map(|option| option.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 @@ -145,6 +145,10 @@ impl Dialect for GenericDialect {
true
}

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

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

/// Whether `ALTER COLUMN` supports `SET (...)`, `RESET (...)`, and `SET STORAGE DEFAULT`,
/// as well as explicit `SET STORAGE` modes on materialized views.
fn supports_alter_column_options(&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 @@ -409,6 +409,10 @@ impl Dialect for PostgreSqlDialect {
true
}

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

/// see <https://www.postgresql.org/docs/current/sql-explain.html>
fn supports_explain_with_utility_options(&self) -> bool {
true
Expand Down
115 changes: 83 additions & 32 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11473,19 +11473,25 @@ impl<'a> Parser<'a> {
Token::Word(w) if w.keyword == Keyword::CLUSTERED && is_mssql => {
self.parse_option_clustered()
}
_ => {
let name = ObjectName::from(self.parse_period_separated(|p| p.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()?;
_ => self.parse_simple_sql_option(),
}
}

Ok(SqlOption::KeyValue { key: name, value })
}
fn parse_sql_option_name(&mut self) -> Result<ObjectName, ParserError> {
self.parse_period_separated(Self::parse_identifier)
.map(ObjectName::from)
}

fn parse_simple_sql_option(&mut self) -> Result<SqlOption, ParserError> {
let key = self.parse_sql_option_name()?;
if self.dialect.supports_sql_option_without_value()
&& matches!(self.peek_token_ref().token, Token::Comma | Token::RParen)
{
return Ok(SqlOption::Ident(key));
}
self.expect_token(&Token::Eq)?;
let value = self.parse_expr()?;
Ok(SqlOption::KeyValue { key, value })
}

/// Parse a `CLUSTERED` table option (MSSQL-specific syntaxes supported).
Expand Down Expand Up @@ -11956,10 +11962,8 @@ impl<'a> Parser<'a> {
AlterColumnOperation::GenericOptions {
options: self.parse_alter_generic_options()?,
}
} else if self.parse_keywords(&[Keyword::SET, Keyword::STORAGE]) {
AlterColumnOperation::SetStorage {
storage: self.parse_user_defined_type_storage()?,
}
} else if let Some(op) = self.parse_optional_alter_column_options(true)? {
op
} else {
let message = if is_postgresql {
"SET/DROP NOT NULL, SET DEFAULT, SET DATA TYPE, ADD GENERATED, or OPTIONS after ALTER COLUMN"
Expand Down Expand Up @@ -12154,6 +12158,43 @@ impl<'a> Parser<'a> {
Ok(operation)
}

fn parse_optional_alter_column_options(
&mut self,
is_alter_table: bool,
) -> Result<Option<AlterColumnOperation>, ParserError> {
let supports_options = self.dialect.supports_alter_column_options();
let op = if (is_alter_table || supports_options)
&& self.parse_keywords(&[Keyword::SET, Keyword::STORAGE])
{
let storage = if let Some(storage) = self.parse_optional_user_defined_type_storage() {
Some(storage)
} else if supports_options && self.parse_keyword(Keyword::DEFAULT) {
None
} else {
let expected = if supports_options {
"storage value (plain, external, extended, main, or default)"
} else {
"storage value (plain, external, extended, or main)"
};
return self.expected_ref(expected, self.peek_token_ref());
};
AlterColumnOperation::SetStorage { storage }
} else if !supports_options {
return Ok(None);
} else if self.parse_keyword_with_tokens(Keyword::SET, &[Token::LParen]) {
let options = self.parse_comma_separated(Self::parse_simple_sql_option)?;
self.expect_token(&Token::RParen)?;
AlterColumnOperation::SetOptions { options }
} else if self.parse_keyword(Keyword::RESET) {
let options =
self.parse_parenthesized(|p| p.parse_comma_separated(Self::parse_sql_option_name))?;
AlterColumnOperation::ResetOptions { options }
} else {
return Ok(None);
};
Ok(Some(op))
}

fn parse_set_data_type(&mut self, had_set: bool) -> Result<AlterColumnOperation, ParserError> {
let data_type = self.parse_data_type()?;
let collation = if self.parse_keyword(Keyword::COLLATE) {
Expand Down Expand Up @@ -12589,17 +12630,26 @@ impl<'a> Parser<'a> {
} else if self.parse_keyword(Keyword::ALTER) {
let _ = self.parse_keyword(Keyword::COLUMN);
let column_name = self.parse_identifier()?;
let op = if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
let column_options = if materialized {
self.parse_optional_alter_column_options(false)?
} else {
None
};
let op = if let Some(op) = column_options {
op
} else if self.parse_keywords(&[Keyword::SET, Keyword::DEFAULT]) {
AlterColumnOperation::SetDefault {
value: self.parse_expr()?,
}
} else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
AlterColumnOperation::DropDefault
} else {
return self.expected_ref(
"SET DEFAULT or DROP DEFAULT after ALTER COLUMN",
self.peek_token_ref(),
);
let expected = if materialized && self.dialect.supports_alter_column_options() {
"SET DEFAULT, DROP DEFAULT, SET (...), RESET (...), or SET STORAGE after ALTER COLUMN"
} else {
"SET DEFAULT or DROP DEFAULT after ALTER COLUMN"
};
return self.expected_ref(expected, self.peek_token_ref());
};
AlterViewOperation::AlterColumn { column_name, op }
} else {
Expand Down Expand Up @@ -22752,9 +22802,13 @@ impl<'a> Parser<'a> {
}
Some(Keyword::STORAGE) => {
self.expect_token(&Token::Eq)?;
Ok(UserDefinedTypeSqlDefinitionOption::Storage(
self.parse_user_defined_type_storage()?,
))
let Some(storage) = self.parse_optional_user_defined_type_storage() else {
return self.expected_ref(
"storage value (plain, external, extended, or main)",
self.peek_token_ref(),
);
};
Ok(UserDefinedTypeSqlDefinitionOption::Storage(storage))
}
Some(Keyword::LIKE) => {
self.expect_token(&Token::Eq)?;
Expand Down Expand Up @@ -22803,22 +22857,19 @@ impl<'a> Parser<'a> {
}

/// Parses `PLAIN`, `EXTERNAL`, `EXTENDED` or `MAIN`.
fn parse_user_defined_type_storage(&mut self) -> Result<UserDefinedTypeStorage, ParserError> {
fn parse_optional_user_defined_type_storage(&mut self) -> Option<UserDefinedTypeStorage> {
let storage_keyword = self.parse_one_of_keywords(&[
Keyword::PLAIN,
Keyword::EXTERNAL,
Keyword::EXTENDED,
Keyword::MAIN,
]);
match storage_keyword {
Some(Keyword::PLAIN) => Ok(UserDefinedTypeStorage::Plain),
Some(Keyword::EXTERNAL) => Ok(UserDefinedTypeStorage::External),
Some(Keyword::EXTENDED) => Ok(UserDefinedTypeStorage::Extended),
Some(Keyword::MAIN) => Ok(UserDefinedTypeStorage::Main),
_ => self.expected_ref(
"storage value (plain, external, extended, or main)",
self.peek_token_ref(),
),
Some(Keyword::PLAIN) => Some(UserDefinedTypeStorage::Plain),
Some(Keyword::EXTERNAL) => Some(UserDefinedTypeStorage::External),
Some(Keyword::EXTENDED) => Some(UserDefinedTypeStorage::Extended),
Some(Keyword::MAIN) => Some(UserDefinedTypeStorage::Main),
_ => None,
}
}

Expand Down
Loading
Loading