Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres' inline power operator has incorrect operator precedence #1555

Closed
vegarsti opened this issue May 5, 2023 · 4 comments
Closed

Postgres' inline power operator has incorrect operator precedence #1555

vegarsti opened this issue May 5, 2023 · 4 comments
Assignees

Comments

@vegarsti
Copy link
Contributor

vegarsti commented May 5, 2023

In Postgres, x / y ^ z is parsed as (x / y) ^ z when it should be x / (y ^ z):

>>> import sqlglot
>>> import sqlglot
>>> sqlglot.__version__
'12.1.0'
>>> sqlglot.parse_one("select x / y ^ z", read="postgres")
(SELECT expressions:
  (POW this:
    (DIV this:
      (COLUMN this:
        (IDENTIFIER this: x, quoted: False)), expression:
      (COLUMN this:
        (IDENTIFIER this: y, quoted: False))), expression:
    (COLUMN this:
      (IDENTIFIER this: z, quoted: False))))

Docs link: https://www.postgresql.org/docs/15/functions-math.html

Test case in 4eb737d:

self.validate_all(
"x / y ^ z",
write={
"": "x / POWER(y, z)",
"postgres": "x / y ^ z",
},
)

@vegarsti
Copy link
Contributor Author

vegarsti commented May 5, 2023

I think this is the culprit:

FACTOR = {
**parser.Parser.FACTOR,
TokenType.CARET: exp.Pow,
}

It should have higher precedence than factors

@vegarsti
Copy link
Contributor Author

vegarsti commented May 5, 2023

Spent some time trying various things with the parser, but not getting anywhere yet.

@georgesittas georgesittas self-assigned this May 5, 2023
@tobymao tobymao assigned tobymao and unassigned georgesittas May 5, 2023
@tobymao
Copy link
Owner

tobymao commented May 5, 2023

i'm looking at this

@tobymao tobymao closed this as completed in 54a7637 May 5, 2023
@vegarsti
Copy link
Contributor Author

vegarsti commented May 5, 2023

Thanks!

adrianisk pushed a commit to adrianisk/sqlglot that referenced this issue Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants