Skip to content

Commit

Permalink
Feat(redshift): improve transpilation of ADD_MONTHS function (#1945)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jul 21, 2023
1 parent 46b5dfa commit b82573b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlglot/dialects/redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Redshift(Postgres):
class Parser(Postgres.Parser):
FUNCTIONS = {
**Postgres.Parser.FUNCTIONS,
"ADD_MONTHS": lambda args: exp.DateAdd(
this=exp.TsOrDsToDate(this=seq_get(args, 0)),
expression=seq_get(args, 1),
unit=exp.var("month"),
),
"DATEADD": lambda args: exp.DateAdd(
this=exp.TsOrDsToDate(this=seq_get(args, 2)),
expression=seq_get(args, 1),
Expand All @@ -37,7 +42,6 @@ class Parser(Postgres.Parser):
expression=exp.TsOrDsToDate(this=seq_get(args, 1)),
unit=seq_get(args, 0),
),
"NVL": exp.Coalesce.from_arg_list,
"STRTOL": exp.FromBase.from_arg_list,
}

Expand Down
7 changes: 7 additions & 0 deletions tests/dialects/test_redshift.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ def test_redshift(self):
self.validate_identity("foo$")
self.validate_identity("$foo")

self.validate_all(
"SELECT ADD_MONTHS('2008-03-31', 1)",
write={
"redshift": "SELECT DATEADD(month, 1, '2008-03-31')",
"trino": "SELECT DATE_ADD('month', 1, CAST(CAST('2008-03-31' AS TIMESTAMP) AS DATE))",
},
)
self.validate_all(
"SELECT STRTOL('abc', 16)",
read={
Expand Down

0 comments on commit b82573b

Please sign in to comment.