Skip to content

Commit d3472c6

Browse files
authored
Fix: Add postgres transpilation for TIME_TO_UNIX (#3305)
1 parent 3b606a5 commit d3472c6

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

sqlglot/dialects/postgres.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ class Generator(generator.Generator):
510510
exp.TsOrDsAdd: _date_add_sql("+"),
511511
exp.TsOrDsDiff: _date_diff_sql,
512512
exp.UnixToTime: lambda self, e: self.func("TO_TIMESTAMP", e.this),
513+
exp.TimeToUnix: lambda self, e: self.func(
514+
"DATE_PART", exp.Literal.string("epoch"), e.this
515+
),
513516
exp.VariancePop: rename_func("VAR_POP"),
514517
exp.Variance: rename_func("VAR_SAMP"),
515518
exp.Xor: bool_xor_sql,

tests/dialects/test_postgres.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,12 @@ def test_postgres(self):
467467
"tsql": "SELECT (CAST('2016-01-10' AS DATE), CAST('2016-02-01' AS DATE)) OVERLAPS (CAST('2016-01-20' AS DATE), CAST('2016-02-10' AS DATE))",
468468
},
469469
)
470+
self.validate_all(
471+
"SELECT DATE_PART('epoch', '2023-01-04 04:05:06.789')",
472+
read={
473+
"": "SELECT TIME_TO_UNIX('2023-01-04 04:05:06.789')",
474+
},
475+
)
470476
self.validate_all(
471477
"x ^ y",
472478
write={

0 commit comments

Comments
 (0)