Skip to content

Commit a9d0f63

Browse files
authored
Feat(DuckDB): Transpile unix_millis to epoch_ms (#6224)
1 parent 78d7733 commit a9d0f63

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

sqlglot/dialects/duckdb.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,7 @@ class Generator(generator.Generator):
805805
exp.cast(e.this, exp.DataType.Type.TIMESTAMP),
806806
),
807807
exp.UnixMicros: lambda self, e: self.func("EPOCH_US", _implicit_datetime_cast(e.this)),
808+
exp.UnixMillis: lambda self, e: self.func("EPOCH_MS", _implicit_datetime_cast(e.this)),
808809
exp.UnixToStr: lambda self, e: self.func(
809810
"STRFTIME", self.func("TO_TIMESTAMP", e.this), self.format_time(e)
810811
),

tests/dialects/test_bigquery.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2540,6 +2540,22 @@ def test_unix_micros(self):
25402540
},
25412541
)
25422542

2543+
def test_unix_millis(self):
2544+
self.validate_all(
2545+
"SELECT UNIX_MILLIS('2008-12-25 15:30:00+00')",
2546+
write={
2547+
"bigquery": "SELECT UNIX_MILLIS('2008-12-25 15:30:00+00')",
2548+
"duckdb": "SELECT EPOCH_MS(CAST('2008-12-25 15:30:00+00' AS TIMESTAMPTZ))",
2549+
},
2550+
)
2551+
self.validate_all(
2552+
"SELECT UNIX_MILLIS(TIMESTAMP '2008-12-25 15:30:00+00')",
2553+
write={
2554+
"bigquery": "SELECT UNIX_MILLIS(CAST('2008-12-25 15:30:00+00' AS TIMESTAMP))",
2555+
"duckdb": "SELECT EPOCH_MS(CAST('2008-12-25 15:30:00+00' AS TIMESTAMPTZ))",
2556+
},
2557+
)
2558+
25432559
def test_regexp_extract(self):
25442560
self.validate_identity("REGEXP_EXTRACT(x, '(?<)')")
25452561
self.validate_identity("REGEXP_EXTRACT(`foo`, 'bar: (.+?)', 1, 1)")

0 commit comments

Comments
 (0)