Skip to content

Commit 238f705

Browse files
authored
Feat(DuckDB): Transpile unix_seconds to epoch (#6225)
* Feat(DuckDB): Transpile unix_seconds to epoch * PR feedback
1 parent 2781f41 commit 238f705

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,11 @@ class Generator(generator.Generator):
806806
),
807807
exp.UnixMicros: lambda self, e: self.func("EPOCH_US", _implicit_datetime_cast(e.this)),
808808
exp.UnixMillis: lambda self, e: self.func("EPOCH_MS", _implicit_datetime_cast(e.this)),
809+
exp.UnixSeconds: lambda self, e: self.sql(
810+
exp.cast(
811+
self.func("EPOCH", _implicit_datetime_cast(e.this)), exp.DataType.Type.BIGINT
812+
)
813+
),
809814
exp.UnixToStr: lambda self, e: self.func(
810815
"STRFTIME", self.func("TO_TIMESTAMP", e.this), self.format_time(e)
811816
),

tests/dialects/test_bigquery.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2516,7 +2516,7 @@ def test_unix_seconds(self):
25162516
write={
25172517
"spark": "SELECT UNIX_SECONDS('2008-12-25 15:30:00+00')",
25182518
"databricks": "SELECT UNIX_SECONDS('2008-12-25 15:30:00+00')",
2519-
"duckdb": "SELECT DATE_DIFF('SECONDS', CAST('1970-01-01 00:00:00+00' AS TIMESTAMPTZ), '2008-12-25 15:30:00+00')",
2519+
"duckdb": "SELECT CAST(EPOCH(CAST('2008-12-25 15:30:00+00' AS TIMESTAMPTZ)) AS BIGINT)",
25202520
"snowflake": "SELECT TIMESTAMPDIFF(SECONDS, CAST('1970-01-01 00:00:00+00' AS TIMESTAMPTZ), '2008-12-25 15:30:00+00')",
25212521
},
25222522
)

0 commit comments

Comments
 (0)