Skip to content

Commit

Permalink
fix: set div type on multiplication closes #2878
Browse files Browse the repository at this point in the history
  • Loading branch information
tobymao committed Jan 23, 2024
1 parent a642758 commit 0648453
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
23 changes: 15 additions & 8 deletions sqlglot/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3567,14 +3567,21 @@ def _parse_term(self) -> t.Optional[exp.Expression]:
return self._parse_tokens(self._parse_factor, self.TERM)

def _parse_factor(self) -> t.Optional[exp.Expression]:
if self.EXPONENT:
factor = self._parse_tokens(self._parse_exponent, self.FACTOR)
else:
factor = self._parse_tokens(self._parse_unary, self.FACTOR)
if isinstance(factor, exp.Div):
factor.args["typed"] = self.dialect.TYPED_DIVISION
factor.args["safe"] = self.dialect.SAFE_DIVISION
return factor
parse_method = self._parse_exponent if self.EXPONENT else self._parse_unary
this = parse_method()

while self._match_set(self.FACTOR):
this = self.expression(
self.FACTOR[self._prev.token_type],
this=this,
comments=self._prev_comments,
expression=parse_method(),
)
if isinstance(this, exp.Div):
this.args["typed"] = self.dialect.TYPED_DIVISION
this.args["safe"] = self.dialect.SAFE_DIVISION

return this

def _parse_exponent(self) -> t.Optional[exp.Expression]:
return self._parse_tokens(self._parse_unary, self.EXPONENT)
Expand Down
1 change: 1 addition & 0 deletions tests/dialects/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class TestPostgres(Validator):
dialect = "postgres"

def test_postgres(self):
self.validate_identity("CAST(1 AS DECIMAL) / CAST(2 AS DECIMAL) * -100")
self.validate_identity("EXEC AS myfunc @id = 123", check_command_warning=True)

expr = parse_one(
Expand Down

0 comments on commit 0648453

Please sign in to comment.