Skip to content

Commit

Permalink
Fix: interval precedence parsing bug (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
georgesittas authored Jun 27, 2023
1 parent 32a86aa commit 4de255c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2863,7 +2863,11 @@ def _parse_interval(self) -> t.Optional[exp.Interval]:
if not self._match(TokenType.INTERVAL):
return None

this = self._parse_primary() or self._parse_term()
if self._match(TokenType.STRING, advance=False):
this = self._parse_primary()
else:
this = self._parse_term()

unit = self._parse_function() or self._parse_var()

# Most dialects support, e.g., the form INTERVAL '5' day, thus we try to parse
Expand Down
6 changes: 6 additions & 0 deletions tests/dialects/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,12 @@ def test_mysql(self):
self.validate_all("CAST(x AS SIGNED INTEGER)", write={"mysql": "CAST(x AS SIGNED)"})
self.validate_all("CAST(x AS UNSIGNED)", write={"mysql": "CAST(x AS UNSIGNED)"})
self.validate_all("CAST(x AS UNSIGNED INTEGER)", write={"mysql": "CAST(x AS UNSIGNED)"})
self.validate_all(
"SELECT DATE_ADD('2023-06-23 12:00:00', INTERVAL 2 * 2 MONTH) FROM foo",
write={
"mysql": "SELECT DATE_ADD('2023-06-23 12:00:00', INTERVAL (2 * 2) MONTH) FROM foo",
},
)
self.validate_all(
"SELECT * FROM t LOCK IN SHARE MODE", write={"mysql": "SELECT * FROM t FOR SHARE"}
)
Expand Down

0 comments on commit 4de255c

Please sign in to comment.