Skip to content

Redshift alter column type no set #1912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
18 changes: 14 additions & 4 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,10 @@ pub enum AlterColumnOperation {
data_type: DataType,
/// PostgreSQL specific
using: Option<Expr>,
/// Whether the statement included the optional `SET DATA` keywords
had_set: bool,
},

/// `ADD GENERATED { ALWAYS | BY DEFAULT } AS IDENTITY [ ( sequence_options ) ]`
///
/// Note: this is a PostgreSQL-specific operation.
Expand All @@ -914,12 +917,19 @@ impl fmt::Display for AlterColumnOperation {
AlterColumnOperation::DropDefault => {
write!(f, "DROP DEFAULT")
}
AlterColumnOperation::SetDataType { data_type, using } => {
AlterColumnOperation::SetDataType {
data_type,
using,
had_set,
} => {
if *had_set {
write!(f, "SET DATA ")?;
}
write!(f, "TYPE {data_type}")?;
if let Some(expr) = using {
write!(f, "SET DATA TYPE {data_type} USING {expr}")
} else {
write!(f, "SET DATA TYPE {data_type}")
write!(f, " USING {expr}")?;
}
Ok(())
}
AlterColumnOperation::AddGenerated {
generated_as,
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ impl Spanned for AlterColumnOperation {
AlterColumnOperation::SetDataType {
data_type: _,
using,
had_set: _,
} => using.as_ref().map_or(Span::empty(), |u| u.span()),
AlterColumnOperation::AddGenerated { .. } => Span::empty(),
}
Expand Down
16 changes: 16 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,22 @@ pub trait Dialect: Debug + Any {
fn supports_space_separated_column_options(&self) -> bool {
false
}

/// Returns true if the dialect supports `ALTER TABLE tbl ALTER COLUMN col TYPE <type>`
/// without specifying `SET DATA` before `TYPE`.
///
/// - [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html#r_ALTER_TABLE-synopsis)
/// - [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
fn supports_alter_column_type_without_set(&self) -> bool {
false
}

/// Returns true if the dialect supports `ALTER TABLE tbl ALTER COLUMN col SET DATA TYPE <type> USING <exp>`
///
/// - [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
fn supports_alter_column_type_using(&self) -> bool {
false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
8 changes: 8 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,12 @@ impl Dialect for PostgreSqlDialect {
fn supports_set_names(&self) -> bool {
true
}

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

fn supports_alter_column_type_using(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,8 @@ impl Dialect for RedshiftSqlDialect {
fn supports_string_literal_backslash_escape(&self) -> bool {
true
}

fn supports_alter_column_type_without_set(&self) -> bool {
true
}
}
30 changes: 21 additions & 9 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8734,16 +8734,12 @@ impl<'a> Parser<'a> {
}
} else if self.parse_keywords(&[Keyword::DROP, Keyword::DEFAULT]) {
AlterColumnOperation::DropDefault {}
} else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE])
|| (is_postgresql && self.parse_keyword(Keyword::TYPE))
} else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE]) {
self.parse_set_data_type(true)?
} else if self.dialect.supports_alter_column_type_without_set()
&& self.parse_keyword(Keyword::TYPE)
{
let data_type = self.parse_data_type()?;
let using = if is_postgresql && self.parse_keyword(Keyword::USING) {
Some(self.parse_expr()?)
} else {
None
};
AlterColumnOperation::SetDataType { data_type, using }
self.parse_set_data_type(false)?
} else if self.parse_keywords(&[Keyword::ADD, Keyword::GENERATED]) {
let generated_as = if self.parse_keyword(Keyword::ALWAYS) {
Some(GeneratedAs::Always)
Expand Down Expand Up @@ -8909,6 +8905,22 @@ impl<'a> Parser<'a> {
Ok(operation)
}

fn parse_set_data_type(&mut self, had_set: bool) -> Result<AlterColumnOperation, ParserError> {
let data_type = self.parse_data_type()?;
let using = if self.dialect.supports_alter_column_type_using()
&& self.parse_keyword(Keyword::USING)
{
Some(self.parse_expr()?)
} else {
None
};
Ok(AlterColumnOperation::SetDataType {
data_type,
using,
had_set,
})
}

fn parse_part_or_partition(&mut self) -> Result<Partition, ParserError> {
let keyword = self.expect_one_of_keywords(&[Keyword::PART, Keyword::PARTITION])?;
match keyword {
Expand Down
15 changes: 12 additions & 3 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5057,22 +5057,31 @@ fn parse_alter_table_alter_column_type() {
AlterColumnOperation::SetDataType {
data_type: DataType::Text,
using: None,
had_set: true,
}
);
}
_ => unreachable!(),
}

let dialect = TestedDialects::new(vec![Box::new(GenericDialect {})]);
let dialects = all_dialects_where(|d| d.supports_alter_column_type_without_set());
dialects.verified_stmt(&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"));

let dialects = all_dialects_except(|d| d.supports_alter_column_type_without_set());
let res =
dialect.parse_sql_statements(&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"));
dialects.parse_sql_statements(&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"));
assert_eq!(
ParserError::ParserError("Expected: SET/DROP NOT NULL, SET DEFAULT, or SET DATA TYPE after ALTER COLUMN, found: TYPE".to_string()),
res.unwrap_err()
);

let res = dialect.parse_sql_statements(&format!(
let dialects = all_dialects_where(|d| d.supports_alter_column_type_using());
dialects.verified_stmt(&format!(
"{alter_stmt} ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'"
));

let dialects = all_dialects_except(|d| d.supports_alter_column_type_using());
let res = dialects.parse_sql_statements(&format!(
"{alter_stmt} ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'"
));
assert_eq!(
Expand Down
6 changes: 2 additions & 4 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,10 +764,7 @@ fn parse_drop_extension() {

#[test]
fn parse_alter_table_alter_column() {
pg().one_statement_parses_to(
"ALTER TABLE tab ALTER COLUMN is_active TYPE TEXT USING 'text'",
"ALTER TABLE tab ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'",
);
pg().verified_stmt("ALTER TABLE tab ALTER COLUMN is_active TYPE TEXT USING 'text'");

match alter_table_op(
pg().verified_stmt(
Expand All @@ -783,6 +780,7 @@ fn parse_alter_table_alter_column() {
AlterColumnOperation::SetDataType {
data_type: DataType::Text,
using: Some(using_expr),
had_set: true,
}
);
}
Expand Down
Loading