Skip to content

Commit 6010302

Browse files
authored
Feat(postgres): transpile QUARTER interval unit (#5015)
1 parent 06db235 commit 6010302

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

sqlglot/dialects/postgres.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,12 @@ def isascii_sql(self, expression: exp.IsAscii) -> str:
742742
@unsupported_args("this")
743743
def currentschema_sql(self, expression: exp.CurrentSchema) -> str:
744744
return "CURRENT_SCHEMA"
745+
746+
def interval_sql(self, expression: exp.Interval) -> str:
747+
unit = expression.text("unit").lower()
748+
749+
if unit.startswith("quarter") and isinstance(expression.this, exp.Literal):
750+
expression.this.replace(exp.Literal.number(int(expression.this.to_py()) * 3))
751+
expression.args["unit"].replace(exp.var("MONTH"))
752+
753+
return super().interval_sql(expression)

tests/dialects/test_postgres.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ def test_postgres(self):
372372
pretty=True,
373373
)
374374

375+
self.validate_all(
376+
"SELECT CURRENT_TIMESTAMP + INTERVAL '-3 MONTH'",
377+
read={
378+
"mysql": "SELECT DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 QUARTER)",
379+
"postgres": "SELECT CURRENT_TIMESTAMP + INTERVAL '-3 MONTH'",
380+
"tsql": "SELECT DATEADD(QUARTER, -1, GETDATE())",
381+
},
382+
)
375383
self.validate_all(
376384
"SELECT ARRAY[]::INT[] AS foo",
377385
write={

0 commit comments

Comments
 (0)