Skip to content

Commit 6b9925b

Browse files
committed
Add dialect options
1 parent f73a330 commit 6b9925b

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

src/dialect/mod.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,22 @@ pub trait Dialect: Debug + Any {
10601060
fn supports_space_separated_column_options(&self) -> bool {
10611061
false
10621062
}
1063+
1064+
/// Returns true if the dialect supports `ALTER TABLE tbl ALTER COLUMN col TYPE <type>`
1065+
/// without specifying `SET DATA` before `TYPE`.
1066+
///
1067+
/// - [Redshift](https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html#r_ALTER_TABLE-synopsis)
1068+
/// - [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
1069+
fn supports_alter_column_type_without_set(&self) -> bool {
1070+
false
1071+
}
1072+
1073+
/// Returns true if the dialect supports `ALTER TABLE tbl ALTER COLUMN col SET DATA TYPE <type> USING <exp>`
1074+
///
1075+
/// - [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertable.html)
1076+
fn supports_alter_column_type_using(&self) -> bool {
1077+
false
1078+
}
10631079
}
10641080

10651081
/// This represents the operators for which precedence must be defined

src/dialect/postgresql.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,4 +258,12 @@ impl Dialect for PostgreSqlDialect {
258258
fn supports_set_names(&self) -> bool {
259259
true
260260
}
261+
262+
fn supports_alter_column_type_without_set(&self) -> bool {
263+
true
264+
}
265+
266+
fn supports_alter_column_type_using(&self) -> bool {
267+
true
268+
}
261269
}

src/dialect/redshift.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,8 @@ impl Dialect for RedshiftSqlDialect {
129129
fn supports_string_literal_backslash_escape(&self) -> bool {
130130
true
131131
}
132+
133+
fn supports_alter_column_type_without_set(&self) -> bool {
134+
true
135+
}
132136
}

src/parser/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8736,7 +8736,9 @@ impl<'a> Parser<'a> {
87368736
AlterColumnOperation::DropDefault {}
87378737
} else if self.parse_keywords(&[Keyword::SET, Keyword::DATA, Keyword::TYPE]) {
87388738
self.parse_set_data_type(true)?
8739-
} else if self.parse_keyword(Keyword::TYPE) {
8739+
} else if self.dialect.supports_alter_column_type_without_set()
8740+
&& self.parse_keyword(Keyword::TYPE)
8741+
{
87408742
self.parse_set_data_type(false)?
87418743
} else if self.parse_keywords(&[Keyword::ADD, Keyword::GENERATED]) {
87428744
let generated_as = if self.parse_keyword(Keyword::ALWAYS) {
@@ -8905,7 +8907,9 @@ impl<'a> Parser<'a> {
89058907

89068908
fn parse_set_data_type(&mut self, had_set: bool) -> Result<AlterColumnOperation, ParserError> {
89078909
let data_type = self.parse_data_type()?;
8908-
let using = if self.parse_keyword(Keyword::USING) {
8910+
let using = if self.dialect.supports_alter_column_type_using()
8911+
&& self.parse_keyword(Keyword::USING)
8912+
{
89098913
Some(self.parse_expr()?)
89108914
} else {
89118915
None

tests/sqlparser_common.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5046,6 +5046,7 @@ fn parse_alter_table_alter_column() {
50465046

50475047
#[test]
50485048
fn parse_alter_table_alter_column_type() {
5049+
let alter_stmt = "ALTER TABLE tab";
50495050
match alter_table_op(verified_stmt(
50505051
"ALTER TABLE tab ALTER COLUMN is_active SET DATA TYPE TEXT",
50515052
)) {
@@ -5062,6 +5063,23 @@ fn parse_alter_table_alter_column_type() {
50625063
}
50635064
_ => unreachable!(),
50645065
}
5066+
5067+
let dialects = all_dialects_except(|d| d.supports_alter_column_type_without_set());
5068+
let res =
5069+
dialects.parse_sql_statements(&format!("{alter_stmt} ALTER COLUMN is_active TYPE TEXT"));
5070+
assert_eq!(
5071+
ParserError::ParserError("Expected: SET/DROP NOT NULL, SET DEFAULT, or SET DATA TYPE after ALTER COLUMN, found: TYPE".to_string()),
5072+
res.unwrap_err()
5073+
);
5074+
5075+
let dialects = all_dialects_except(|d| d.supports_alter_column_type_using());
5076+
let res = dialects.parse_sql_statements(&format!(
5077+
"{alter_stmt} ALTER COLUMN is_active SET DATA TYPE TEXT USING 'text'"
5078+
));
5079+
assert_eq!(
5080+
ParserError::ParserError("Expected: end of statement, found: USING".to_string()),
5081+
res.unwrap_err()
5082+
);
50655083
}
50665084

50675085
#[test]

0 commit comments

Comments
 (0)