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
5 changes: 5 additions & 0 deletions src/dialect/bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,9 @@ impl Dialect for BigQueryDialect {
fn supports_comma_separated_trim(&self) -> bool {
true
}

/// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_table_add_column_statement>
fn supports_add_column_if_not_exists(&self) -> bool {
true
}
}
5 changes: 5 additions & 0 deletions src/dialect/duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,9 @@ impl Dialect for DuckDbDialect {
fn supports_nested_comments(&self) -> bool {
true
}

/// See <https://duckdb.org/docs/stable/sql/statements/alter_table>
fn supports_add_column_if_not_exists(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,4 +332,8 @@ impl Dialect for GenericDialect {
fn supports_alter_column_position(&self) -> bool {
true
}

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

/// Returns true if the dialect supports `ALTER TABLE ... ADD [COLUMN] IF NOT EXISTS <col>`.
/// Example:
/// ```sql
/// ALTER TABLE tbl ADD COLUMN IF NOT EXISTS c INT
/// ```
fn supports_add_column_if_not_exists(&self) -> bool {
false
}

/// Returns true if the dialect considers the specified ident as a function
/// that returns an identifier. Typically used to generate identifiers
/// programmatically.
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,9 @@ impl Dialect for PostgreSqlDialect {
fn supports_comment_optimizer_hint(&self) -> bool {
true
}

/// See <https://www.postgresql.org/docs/current/sql-altertable.html>
fn supports_add_column_if_not_exists(&self) -> bool {
true
}
}
5 changes: 5 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ impl Dialect for SnowflakeDialect {
fn supports_comma_separated_trim(&self) -> bool {
true
}

/// See <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
fn supports_add_column_if_not_exists(&self) -> bool {
true
}
}

// Peeks ahead to identify tokens that are expected after
Expand Down
3 changes: 1 addition & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10818,8 +10818,7 @@ impl<'a> Parser<'a> {
} else {
let column_keyword = self.parse_keyword(Keyword::COLUMN);

let if_not_exists = if dialect_of!(self is PostgreSqlDialect | BigQueryDialect | DuckDbDialect | GenericDialect)
{
let if_not_exists = if self.dialect.supports_add_column_if_not_exists() {
self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS])
|| if_not_exists
} else {
Expand Down
7 changes: 1 addition & 6 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5453,12 +5453,7 @@ fn parse_alter_table_add_column() {

#[test]
fn parse_alter_table_add_column_if_not_exists() {
let dialects = TestedDialects::new(vec![
Box::new(PostgreSqlDialect {}),
Box::new(BigQueryDialect {}),
Box::new(GenericDialect {}),
Box::new(DuckDbDialect {}),
]);
let dialects = all_dialects_where(|d| d.supports_add_column_if_not_exists());

match alter_table_op(dialects.verified_stmt("ALTER TABLE tab ADD IF NOT EXISTS foo TEXT")) {
AlterTableOperation::AddColumn { if_not_exists, .. } => {
Expand Down
Loading