The parentheses rules of PostgreSQL, is there a summarized guide?

偶尔善良 提交于 2019-12-01 06:30:50
Peter Krauss

"Is there a summarized guide?", well... The answer is no, so: hands-on! This answer is a Wiki, let's write.

Summarized guide

Let,

  • F() a an usual function. (ex. ROUND)
  • L() a function-like operator (ex. ANY)
  • f a operator-like function (ex. current_date)
  • Op an operator
  • Op1, Op2 are distinct operators
  • A, B, C values or expressions
  • S a expression-list, as "(A,B,C)"

The rules, using these elements, are in the form

  • rule: notes.

"pure" mathematical expressions

When Op, Op1, Op2 are mathematical operators (ex. +, -. *), and F() is a mathematical function (ex. ROUND()).

Rules for scalar expressions and "pure array expressions":

  • A Op B = (A Op B): the parentheses is optional.
  • A Op1 B Op2 C: need to check precedence.
  • (A Op1 B) Op2 C: enforce "first (A Op1 B)".
  • A Op1 (B Op2 C): enforce "first (B Op2 C)".
  • F(A) = (F(A)) = F((A)) = (F((A))): the parentheses are optional.
  • S = (S): the external parentheses are optional.
  • f=(f): the parentheses are optional.

Expressions with function-like operators

Rules for operators as ALL, ANY, ROW, SOME, etc.

  • L(A) = L((A)): the parentheses is optional in the argument.
  • (L(A)): SYNTAX ERROR.

...More rules? Please help editing here.

ANY is a function-like construct. Like (almost) any other function in Postgres it requires parentheses around its parameters. Makes the syntax consistent and helps the parser avoid ambiguities.

You can think of ANY() like a shorthand for unnest() condensed to a single expression.

One might argue an additional set of parentheses around the set-variant of ANY. But that would be ambiguous, since a list of values in parentheses is interpreted as a single ROW type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!