Skip to content

Commit

Permalink
Fix(parser): solve interval parsing bug (#2157)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Sep 5, 2023
1 parent dcacef1 commit db1303c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 4 additions & 4 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3154,12 +3154,12 @@ def _parse_interval(self) -> t.Optional[exp.Interval]:

if len(parts) == 2:
if unit:
# this is not actually a unit, it's something else
# This is not actually a unit, it's something else (e.g. a "window side")
unit = None
self._retreat(self._index - 1)
else:
this = exp.Literal.string(parts[0])
unit = self.expression(exp.Var, this=parts[1])

this = exp.Literal.string(parts[0])
unit = self.expression(exp.Var, this=parts[1])

return self.expression(exp.Interval, this=this, unit=unit)

Expand Down
8 changes: 8 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,11 @@ def test_parse_terse_coalesce(self):
parse_one("SELECT a, b ?? c ?? 'No Data' FROM z").sql(),
"SELECT a, COALESCE(COALESCE(b, c), 'No Data') FROM z",
)

def test_parse_intervals(self):
ast = parse_one(
"SELECT a FROM tbl WHERE a <= DATE '1998-12-01' - INTERVAL '71 days' GROUP BY b"
)

self.assertEqual(ast.find(exp.Interval).this.sql(), "'71'")
self.assertEqual(ast.find(exp.Interval).unit.assert_is(exp.Var).sql(), "days")

0 comments on commit db1303c

Please sign in to comment.