From e39c5a53e0f448d2d80fdbf3357b36eefe83306c Mon Sep 17 00:00:00 2001 From: Erin Drummond Date: Tue, 17 Jun 2025 00:30:28 +0000 Subject: [PATCH] Fix(duckdb): Generate correct DETACH syntax if IF EXISTS is set --- sqlglot/dialects/duckdb.py | 9 +++++++++ tests/dialects/test_duckdb.py | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py index 149bed6fb9..1383e6516c 100644 --- a/sqlglot/dialects/duckdb.py +++ b/sqlglot/dialects/duckdb.py @@ -1164,3 +1164,12 @@ def numbertostr_sql(self, expression: exp.NumberToStr) -> str: def autoincrementcolumnconstraint_sql(self, _) -> str: self.unsupported("The AUTOINCREMENT column constraint is not supported by DuckDB") return "" + + def detach_sql(self, expression: exp.Detach) -> str: + this = self.sql(expression, "this") + # the DATABASE keyword is required if IF EXISTS is set + # without it, DuckDB throws an error: Parser Error: syntax error at or near "exists" (Line Number: 1) + # ref: https://duckdb.org/docs/stable/sql/statements/attach.html#detach-syntax + exists_sql = " DATABASE IF EXISTS" if expression.args.get("exists") else "" + + return f"DETACH{exists_sql} {this}" diff --git a/tests/dialects/test_duckdb.py b/tests/dialects/test_duckdb.py index 1176e6371b..69fadce5f4 100644 --- a/tests/dialects/test_duckdb.py +++ b/tests/dialects/test_duckdb.py @@ -1494,7 +1494,11 @@ def test_attach_detach(self): # DETACH self.validate_identity("DETACH new_database") - self.validate_identity("DETACH IF EXISTS file") + + # when 'if exists' is set, the syntax is DETACH DATABASE, not DETACH + # ref: https://duckdb.org/docs/stable/sql/statements/attach.html#detach-syntax + self.validate_identity("DETACH IF EXISTS file", "DETACH DATABASE IF EXISTS file") + self.validate_identity("DETACH DATABASE IF EXISTS file", "DETACH DATABASE IF EXISTS file") self.validate_identity("DETACH DATABASE db", "DETACH db")