Repository: tobymao/sqlglot

Issue:
[BUG] Conversion from SQL Server's DATEADD(quarter...) to PostgreSQL not working properly
## Type of issue: Bug Report
Conversion from SQL Server's DATEADD(quarter...) to PostgreSQL not working properly

## Description
When trying to convert SQL on read dialect 'tsql' for SQL Server's DATEADD(quarter...) the conversion is not correct and does not fail

## Steps to reproduce
```python
import sqlglot

example_query = "SELECT category, SUM(revenue) as total_revenue FROM events WHERE event_date >= DATEADD(quarter, -1, GETDATE()) GROUP BY category;"
parsed_postgres = sqlglot.transpile(example_query , read="tsql", write="postgres")
print(parsed_postgres)
```

## Expected behaviour
This conversion should show something like:
```sql
SELECT category, SUM(revenue) AS total_revenue FROM events WHERE event_date >= CURRENT_TIMESTAMP + INTERVAL '-3 MONTHS' GROUP BY category
```

## Actual behavior
Right now it will show:
```sql
SELECT category, SUM(revenue) AS total_revenue FROM events WHERE event_date >= CURRENT_TIMESTAMP + INTERVAL '-1 QUARTER' GROUP BY category
```

## Notes
1. The current behavior will fail because, as shown in [postgres docs](https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-INTERVAL-INPUT), QUARTER is not supported. However, the structure of the query is correct. Changing this "N QUARTER" to "3*N months" should work.
2. The number before "QUARTER" does not matter, that's why it should always be 3 times the number of quarter months.

Thanks for this amazing library I just discovered it and it's coming handy! 💯

Discussion:

Solution patch:
diff --git a/sqlglot/dialects/postgres.py b/sqlglot/dialects/postgres.py
index 97589d5fbd..61a6dba438 100644
--- a/sqlglot/dialects/postgres.py
+++ b/sqlglot/dialects/postgres.py
@@ -742,3 +742,12 @@ def isascii_sql(self, expression: exp.IsAscii) -> str:
         @unsupported_args("this")
         def currentschema_sql(self, expression: exp.CurrentSchema) -> str:
             return "CURRENT_SCHEMA"
+
+        def interval_sql(self, expression: exp.Interval) -> str:
+            unit = expression.text("unit").lower()
+
+            if unit.startswith("quarter") and isinstance(expression.this, exp.Literal):
+                expression.this.replace(exp.Literal.number(int(expression.this.to_py()) * 3))
+                expression.args["unit"].replace(exp.var("MONTH"))
+
+            return super().interval_sql(expression)


Test patch:
diff --git a/tests/dialects/test_postgres.py b/tests/dialects/test_postgres.py
index 957eb9d5b9..c732688e55 100644
--- a/tests/dialects/test_postgres.py
+++ b/tests/dialects/test_postgres.py
@@ -372,6 +372,14 @@ def test_postgres(self):
             pretty=True,
         )
 
+        self.validate_all(
+            "SELECT CURRENT_TIMESTAMP + INTERVAL '-3 MONTH'",
+            read={
+                "mysql": "SELECT DATE_ADD(CURRENT_TIMESTAMP, INTERVAL -1 QUARTER)",
+                "postgres": "SELECT CURRENT_TIMESTAMP + INTERVAL '-3 MONTH'",
+                "tsql": "SELECT DATEADD(QUARTER, -1, GETDATE())",
+            },
+        )
         self.validate_all(
             "SELECT ARRAY[]::INT[] AS foo",
             write={