Skip to content

Commit

Permalink
Fix(duckdb): ensure 'day' will be generated for exp.DateDiff by defau…
Browse files Browse the repository at this point in the history
…lt (#1944)
  • Loading branch information
georgesittas authored Jul 21, 2023
1 parent d145410 commit 46b5dfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sqlglot/dialects/duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Generator(generator.Generator):
exp.DateFromParts: rename_func("MAKE_DATE"),
exp.DateSub: _date_delta_sql,
exp.DateDiff: lambda self, e: self.func(
"DATE_DIFF", f"'{e.args.get('unit', 'day')}'", e.expression, e.this
"DATE_DIFF", f"'{e.args.get('unit') or 'day'}'", e.expression, e.this
),
exp.DateStrToDate: datestrtodate_sql,
exp.DateToDi: lambda self, e: f"CAST(STRFTIME({self.sql(e, 'this')}, {DuckDB.DATEINT_FORMAT}) AS INT)",
Expand Down
9 changes: 9 additions & 0 deletions tests/dialects/test_duckdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ def test_duckdb(self):
self.validate_all("0x1010", write={"": "0 AS x1010"})
self.validate_all("x ~ y", write={"duckdb": "REGEXP_MATCHES(x, y)"})
self.validate_all("SELECT * FROM 'x.y'", write={"duckdb": 'SELECT * FROM "x.y"'})
self.validate_all(
"DATE_DIFF('day', CAST(b AS DATE), CAST(a AS DATE))",
read={
"duckdb": "DATE_DIFF('day', CAST(b AS DATE), CAST(a AS DATE))",
"hive": "DATEDIFF(a, b)",
"spark": "DATEDIFF(a, b)",
"spark2": "DATEDIFF(a, b)",
},
)
self.validate_all(
"XOR(a, b)",
read={
Expand Down

0 comments on commit 46b5dfa

Please sign in to comment.