4.1.6. Operator Precedence

4.1.6. Operator Precedence
4.1.6. 操作符优先级
Table 4.2 shows the precedence and associativity of the operators in PostgreSQL. Most operators  have the same precedence and are left-associative. The precedence and associativity of the operators  is hard-wired into the parser.
表4.2展示了PostgreSQL中运算符的优先级和关联性。 大多数运算符具有相同的优先级,并且是左关联的。 运算符的优先级和关联性硬连接到解析器中。
 
You will sometimes need to add parentheses when using combinations of binary and unary operators.
当使用二进制和一元运算符的组合时,有时需要添加括号以明确优先级。
 
For instance:
例如:
 
SELECT 5 ! - 6;
 
will be parsed as:
会被解析为:
 
SELECT 5 ! (- 6);
 
because the parser has no idea — until it is too late — that ! is defined as a postfix operator, not an  infix one. To get the desired behavior in this case, you must write:
因为解析器并不知道!被定义为了后缀运算符(阶乘)。此例中如果想得到想要的结果,需要这样写:
 
SELECT (5 !) - 6;  --5的阶乘减去6
 
This is the price one pays for extensibility.
这是人们为可扩展性而付出的代价。
Note that the operator precedence rules also apply to user-defined operators that have the same names  as the built-in operators mentioned above. For example, if you define a “+” operator for some custom  data type it will have the same precedence as the built-in “+” operator, no matter what yours does.
请注意,运算符优先级规则也适用于与上述内置运算符同名的用户自定义的运算符。 例如,如果为某些自定义数据类型定义“ +”运算符,则无论您做什么,它的优先级都与内置“ +”运算符相同。
 
When a schema-qualified operator name is used in the OPERATOR syntax, as for example in:
在OPERATOR语法中使用模式限定的运算符名称时,例如:
 
SELECT 3 OPERATOR(pg_catalog.+) 4;
 
the OPERATOR construct is taken to have the default precedence shown in Table 4.2 for “any other  operator”. This is true no matter which specific operator appears inside OPERATOR() .
对于“任何其他运算符”,OPERATOR构造具有表4.2中所示的默认优先级。 无论哪个特定运算符出现在OPERATOR()中都是如此。
 
Note
注:
PostgreSQL versions before 9.5 used slightly different operator precedence rules. In  particular, <= >= and <> used to be treated as generic operators; IS tests used to have  higher priority; and NOT BETWEEN and related constructs acted inconsistently, being  taken in some cases as having the precedence of NOT rather than BETWEEN . These  rules were changed for better compliance with the SQL standard and to reduce confusion
from inconsistent treatment of logically equivalent constructs. In most cases,  these changes will result in no behavioral change, or perhaps in “no such operator”  failures which can be resolved by adding parentheses. However there are corner cases  in which a query might change behavior without any parsing error being reported. If  you are concerned about whether these changes have silently broken something, you  can test your application with the configuration parameter operator_precedence_warning  turned on to see if any warnings are logged.
PostgreSQL9.5之前的版本使用的运算符优先级略有不同。 特别是,<=> =和<>被视为通用运算符; IS测试具有更高的优先级; 和NOT BETWEEN及其相关构造的行为不一致,在某些情况下被认为具有NOT而不是BETWEEN的优先权。 更改这些规则是为了更好地符合SQL标准,并减少 来自逻辑等效构造的不一致处理而产生的混乱。 在大多数情况下,这些更改不会导致任何行为更改,或者虽然可能导致“没有此类操作符”而失败,但是可以通过添加括号来解决。 但是,在某些特殊情况下,查询可能会更改行为而不会报告任何解析错误。 如果您担心这些更改是否悄悄地破坏了某些内容,则可以启用配置参数operator_precedence_warning以测试您的应用程序,以查看是否有记录任何警告。
发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104233482
今日推荐