From a16e0e6236209fd904e729db177f361110b5e118 Mon Sep 17 00:00:00 2001 From: George Sittas Date: Fri, 13 Dec 2024 16:59:38 +0200 Subject: [PATCH] Fix: another interval parsing edge case --- sqlglot/parser.py | 10 +++++----- tests/dialects/test_postgres.py | 3 +++ 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/sqlglot/parser.py b/sqlglot/parser.py index fbbc8f14d4..48b59ecfaa 100644 --- a/sqlglot/parser.py +++ b/sqlglot/parser.py @@ -4620,14 +4620,14 @@ def _parse_interval(self, match_interval: bool = True) -> t.Optional[exp.Add | e this = exp.Literal.string(this.to_py()) elif this and this.is_string: parts = exp.INTERVAL_STRING_RE.findall(this.name) - if len(parts) == 1: - if unit: - # Unconsume the eagerly-parsed unit, since the real unit was part of the string - self._retreat(self._index - 1) + if parts and unit: + # Unconsume the eagerly-parsed unit, since the real unit was part of the string + unit = None + self._retreat(self._index - 1) + if len(parts) == 1: this = exp.Literal.string(parts[0][0]) unit = self.expression(exp.Var, this=parts[0][1].upper()) - if self.INTERVAL_SPANS and self._match_text_seq("TO"): unit = self.expression( exp.IntervalSpan, this=unit, expression=self._parse_var(any_token=True, upper=True) diff --git a/tests/dialects/test_postgres.py b/tests/dialects/test_postgres.py index 204151ed38..acdb2d4a83 100644 --- a/tests/dialects/test_postgres.py +++ b/tests/dialects/test_postgres.py @@ -71,6 +71,9 @@ def test_postgres(self): self.validate_identity("EXEC AS myfunc @id = 123", check_command_warning=True) self.validate_identity("SELECT CURRENT_USER") self.validate_identity("SELECT * FROM ONLY t1") + self.validate_identity( + "SELECT * FROM t WHERE some_column >= CURRENT_DATE + INTERVAL '1 day 1 hour' AND some_another_column IS TRUE" + ) self.validate_identity( """UPDATE "x" SET "y" = CAST('0 days 60.000000 seconds' AS INTERVAL) WHERE "x"."id" IN (2, 3)""" )