Skip to content

Commit

Permalink
Fix: alias snowflake timediff/timestampdiff to datediff closes #1851
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 29, 2023
1 parent 28e1024 commit 5dabb96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 7 additions & 3 deletions sqlglot/dialects/snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ def _parse_object_construct(args: t.List) -> t.Union[exp.StarMap, exp.Struct]:
)


def _parse_datediff(args: t.List) -> exp.DateDiff:
return exp.DateDiff(this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0))


def _unix_to_time_sql(self: generator.Generator, expression: exp.UnixToTime) -> str:
scale = expression.args.get("scale")
timestamp = self.sql(expression, "this")
Expand Down Expand Up @@ -214,15 +218,15 @@ class Parser(parser.Parser):
"DATEADD": lambda args: exp.DateAdd(
this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0)
),
"DATEDIFF": lambda args: exp.DateDiff(
this=seq_get(args, 2), expression=seq_get(args, 1), unit=seq_get(args, 0)
),
"DATEDIFF": _parse_datediff,
"DIV0": _div0_to_if,
"IFF": exp.If.from_arg_list,
"NULLIFZERO": _nullifzero_to_if,
"OBJECT_CONSTRUCT": _parse_object_construct,
"RLIKE": exp.RegexpLike.from_arg_list,
"SQUARE": lambda args: exp.Pow(this=seq_get(args, 0), expression=exp.Literal.number(2)),
"TIMEDIFF": _parse_datediff,
"TIMESTAMPDIFF": _parse_datediff,
"TO_ARRAY": exp.Array.from_arg_list,
"TO_VARCHAR": exp.ToChar.from_arg_list,
"TO_TIMESTAMP": _snowflake_to_timestamp,
Expand Down
8 changes: 8 additions & 0 deletions tests/dialects/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,14 @@ def test_timestamps(self):
},
)
self.validate_identity("DATEDIFF(DAY, 5, CAST('2008-12-25' AS DATE))")
self.validate_identity(
"TIMEDIFF(DAY, 5, CAST('2008-12-25' AS DATE))",
"DATEDIFF(DAY, 5, CAST('2008-12-25' AS DATE))",
)
self.validate_identity(
"TIMESTAMPDIFF(DAY, 5, CAST('2008-12-25' AS DATE))",
"DATEDIFF(DAY, 5, CAST('2008-12-25' AS DATE))",
)

def test_semi_structured_types(self):
self.validate_identity("SELECT CAST(a AS VARIANT)")
Expand Down

0 comments on commit 5dabb96

Please sign in to comment.