Skip to content

Commit 8e65a53

Browse files
committed
Allow IF NOT EXISTS after table name for Snowflake
Although not documented under https://docs.snowflake.com/en/sql-reference/sql/create-table, it is valid to put the `IF NOT EXIST` _after_ the table name when creating a table for Snowflake. This adds support to check for `IF NOT EXIST` again after parsing the table name if it wasn't found before the name.
1 parent 9fc9009 commit 8e65a53

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/dialect/snowflake.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,11 @@ pub fn parse_create_table(
401401
let if_not_exists = parser.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
402402
let table_name = parser.parse_object_name(false)?;
403403

404+
// Snowflake allows `IF NOT EXIST` both before and after the table name so if we haven't
405+
// already seen that before the table name, try to parse it again.
406+
let if_not_exists =
407+
if_not_exists || parser.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
408+
404409
let mut builder = CreateTableBuilder::new(table_name)
405410
.or_replace(or_replace)
406411
.if_not_exists(if_not_exists)

tests/sqlparser_snowflake.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,18 @@ fn test_snowflake_create_table_if_not_exists() {
446446
}
447447
_ => unreachable!(),
448448
}
449+
450+
for sql in [
451+
r#"CREATE TABLE IF NOT EXISTS "A"."B"."C" (v VARIANT)"#,
452+
r#"CREATE TABLE "A"."B"."C" IF NOT EXISTS (v VARIANT)"#,
453+
r#"CREATE TRANSIENT TABLE IF NOT EXISTS "A"."B"."C" (v VARIANT)"#,
454+
r#"CREATE TRANSIENT TABLE "A"."B"."C" IF NOT EXISTS (v VARIANT)"#,
455+
] {
456+
// We don't use `verified_stmt` here because the parser won't produce a 1:1 result of the
457+
// query. We will always put the `IF NOT EXIST` before the column name when displaying the
458+
// query.
459+
assert!(snowflake().parse_sql_statements(sql).is_ok());
460+
}
449461
}
450462

451463
#[test]

0 commit comments

Comments
 (0)