Add dialect options

This commit is contained in:
Yoav Cohen 2025-06-29 18:19:59 +02:00
parent f73a330bd3
commit 6b9925b713
5 changed files with 52 additions and 2 deletions

View file

@ -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

View file

@ -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
}
}

View file

@ -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
}
}

View file

@ -8736,7 +8736,9 @@ impl<'a> Parser<'a> {
AlterColumnOperation::DropDefault {}
} else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE]) {
self.parse_set_data_type(true)?
} else if self.parse_keyword(Keyword::TYPE) {
} else if self.dialect.supports_alter_column_type_without_set()
&& self.parse_keyword(Keyword::TYPE)
{
self.parse_set_data_type(false)?
} else if self.parse_keywords(&[Keyword::ADD, Keyword::GENERATED]) {
let generated_as = if self.parse_keyword(Keyword::ALWAYS) {
@ -8905,7 +8907,9 @@ impl<'a> Parser<'a> {
fn parse_set_data_type(&mut self, had_set: bool) -> Result<AlterColumnOperation, ParserError> {
let data_type = self.parse_data_type()?;
let using = if self.parse_keyword(Keyword::USING) {
let using = if self.dialect.supports_alter_column_type_using()
&& self.parse_keyword(Keyword::USING)
{
Some(self.parse_expr()?)
} else {
None

View file

@ -5046,6 +5046,7 @@ fn parse_alter_table_alter_column() {
#[test]
fn parse_alter_table_alter_column_type() {
let alter_stmt = "ALTER TABLE tab";
match alter_table_op(verified_stmt(
"ALTER TABLE tab ALTER COLUMN is_active SET DATA TYPE TEXT",
)) {
@ -5062,6 +5063,23 @@ fn parse_alter_table_alter_column_type() {
}
_ => unreachable!(),
}
let dialects = all_dialects_except(|d| d.supports_alter_column_type_without_set());
let res =
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 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!(
ParserError::ParserError("Expected: end of statement, found: USING".to_string()),
res.unwrap_err()
);
}
#[test]