Skip to content

Commit

Permalink
Fix: interval preceding closes #1715
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jun 2, 2023
1 parent 17dc0e1 commit 92dbace
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
23 changes: 14 additions & 9 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ class Parser(metaclass=_Parser):

WINDOW_ALIAS_TOKENS = ID_VAR_TOKENS - {TokenType.ROWS}
WINDOW_BEFORE_PAREN_TOKENS = {TokenType.OVER}
WINDOW_SIDES = {"FOLLOWING", "PRECEDING"}

ADD_CONSTRAINT_TOKENS = {TokenType.CONSTRAINT, TokenType.PRIMARY_KEY, TokenType.FOREIGN_KEY}

Expand Down Expand Up @@ -2804,15 +2805,19 @@ def _parse_interval(self) -> t.Optional[exp.Expression]:

# Most dialects support, e.g., the form INTERVAL '5' day, thus we try to parse
# each INTERVAL expression into this canonical form so it's easy to transpile
if this and isinstance(this, exp.Literal):
if this.is_number:
this = exp.Literal.string(this.name)

# Try to not clutter Snowflake's multi-part intervals like INTERVAL '1 day, 1 year'
if this and this.is_number:
this = exp.Literal.string(this.name)
elif this and this.is_string:
parts = this.name.split()
if not unit and len(parts) <= 2:
this = exp.Literal.string(seq_get(parts, 0))
unit = self.expression(exp.Var, this=seq_get(parts, 1))

if len(parts) == 2:
if unit:
# this is not actually a unit, it's something else
unit = None
self._retreat(self._index - 1)
else:
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 Expand Up @@ -3975,7 +3980,7 @@ def _parse_window_spec(self) -> t.Dict[str, t.Optional[str | exp.Expression]]:
or (self._match_text_seq("CURRENT", "ROW") and "CURRENT ROW")
or self._parse_bitwise()
),
"side": self._match_texts(("PRECEDING", "FOLLOWING")) and self._prev.text,
"side": self._match_texts(self.WINDOW_SIDES) and self._prev.text,
}

def _parse_alias(
Expand Down
3 changes: 3 additions & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ def test_ddl(self):
)

def test_postgres(self):
self.validate_identity(
"""LAST_VALUE("col1") OVER (ORDER BY "col2" RANGE BETWEEN INTERVAL '1 day' PRECEDING AND '1 month' FOLLOWING)"""
)
self.validate_identity("SELECT ARRAY[1, 2, 3] @> ARRAY[1, 2]")
self.validate_identity("SELECT ARRAY[1, 2, 3] <@ ARRAY[1, 2]")
self.validate_identity("SELECT ARRAY[1, 2, 3] && ARRAY[1, 2]")
Expand Down

0 comments on commit 92dbace

Please sign in to comment.