Repository: tobymao/sqlglot

Issue:
double `INTERVAL` in `date_add` when performing a roundtrip with `dialect="duckdb"`
I think the example is self explanatory:

```
In [1]: import sqlglot
   ...: import duckdb

In [2]: sql = "select date_add(cast('2020-01-01' as date), interval 1 day)"

In [3]: ast = sqlglot.parse_one(sql, dialect="duckdb")

In [4]: ast.sql(dialect="duckdb")
Out[4]: "SELECT CAST('2020-01-01' AS DATE) + INTERVAL (INTERVAL '1' DAY) DAY"

In [5]: duckdb.execute(sql).fetchall()
Out[5]: [(datetime.datetime(2020, 1, 2, 0, 0),)]

In [6]: duckdb.execute(ast.sql(dialect="duckdb")).fetchall()
---------------------------------------------------------------------------
ConversionException                       Traceback (most recent call last)
Cell In[6], line 1
----> 1 duckdb.execute(ast.sql(dialect="duckdb")).fetchall()

ConversionException: Conversion Error: Unimplemented type for cast (INTERVAL -> DOUBLE)

```

Discussion:

Solution patch:
diff --git a/sqlglot/dialects/duckdb.py b/sqlglot/dialects/duckdb.py
index 76fafcbbcf..7d7a671014 100644
--- a/sqlglot/dialects/duckdb.py
+++ b/sqlglot/dialects/duckdb.py
@@ -76,7 +76,10 @@ def _date_delta_sql(self: DuckDB.Generator, expression: DATETIME_DELTA) -> str:
 
     this = exp.cast(this, to_type) if to_type else this
 
-    return f"{self.sql(this)} {op} {self.sql(exp.Interval(this=expression.expression, unit=unit))}"
+    expr = expression.expression
+    interval = expr if isinstance(expr, exp.Interval) else exp.Interval(this=expr, unit=unit)
+
+    return f"{self.sql(this)} {op} {self.sql(interval)}"
 
 
 # BigQuery -> DuckDB conversion for the DATE function


Test patch:
diff --git a/tests/dialects/test_duckdb.py b/tests/dialects/test_duckdb.py
index 6cc42f30e0..a9b32004a2 100644
--- a/tests/dialects/test_duckdb.py
+++ b/tests/dialects/test_duckdb.py
@@ -910,6 +910,10 @@ def test_duckdb(self):
                 "postgres": "SELECT 'ThOmAs' ~* 'thomas'",
             },
         )
+        self.validate_identity(
+            "SELECT DATE_ADD(CAST('2020-01-01' AS DATE), INTERVAL 1 DAY)",
+            "SELECT CAST('2020-01-01' AS DATE) + INTERVAL '1' DAY",
+        )
 
     def test_array_index(self):
         with self.assertLogs(helper_logger) as cm: