diff --git a/src/dialect/bigquery.rs b/src/dialect/bigquery.rs index d34be9a40c..b29ac69fa8 100644 --- a/src/dialect/bigquery.rs +++ b/src/dialect/bigquery.rs @@ -179,4 +179,9 @@ impl Dialect for BigQueryDialect { fn supports_comma_separated_trim(&self) -> bool { true } + + /// See + fn supports_add_column_if_not_exists(&self) -> bool { + true + } } diff --git a/src/dialect/duckdb.rs b/src/dialect/duckdb.rs index 28ee240260..f3b44bb03d 100644 --- a/src/dialect/duckdb.rs +++ b/src/dialect/duckdb.rs @@ -146,4 +146,9 @@ impl Dialect for DuckDbDialect { fn supports_nested_comments(&self) -> bool { true } + + /// See + fn supports_add_column_if_not_exists(&self) -> bool { + true + } } diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index 09a5518595..8bb91508bb 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -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 + } } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index f57907e1c1..6ddb239e39 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -1617,6 +1617,15 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports `ALTER TABLE ... ADD [COLUMN] IF NOT EXISTS `. + /// 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. diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index 3bec6ceba3..fce93a85db 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -368,4 +368,9 @@ impl Dialect for PostgreSqlDialect { fn supports_comment_optimizer_hint(&self) -> bool { true } + + /// See + fn supports_add_column_if_not_exists(&self) -> bool { + true + } } diff --git a/src/dialect/snowflake.rs b/src/dialect/snowflake.rs index 3ebc168645..78bc834680 100644 --- a/src/dialect/snowflake.rs +++ b/src/dialect/snowflake.rs @@ -721,6 +721,11 @@ impl Dialect for SnowflakeDialect { fn supports_comma_separated_trim(&self) -> bool { true } + + /// See + fn supports_add_column_if_not_exists(&self) -> bool { + true + } } // Peeks ahead to identify tokens that are expected after diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 6daad0f6e5..65c78d9b24 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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 { diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index ea542b5020..2e3922ef15 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -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, .. } => {