简体   繁体   English

在postgres过程中,为什么在使用数组作为参数的任何函数之前需要“=”?

[英]Why required “=” before ANY function with array as param, in postgres procedure?

I was answering a postgres question yesterday, and also came across a postgres thread ( here ) where they describe the following error: 我昨天回答了一个postgres问题,并且还遇到了一个postgres线程( 这里 ),他们描述了以下错误:

ERROR:  operator does not exist: text = text[]
HINT:  No operator matches the given name and argument type(s). You
might need to add explicit type casts.

The error seems to appear whenever an ARRAY string type is fed to ANY without using = ANY . 只要在不使用= ANY情况下将ARRAY字符串类型提供给ANY ,似乎就会出现错误。 This seems completely strange since based on language, logic, and sql conventions, usually you have (eg IN ): 这看起来很奇怪,因为基于语言,逻辑和SQL约定,通常你有(例如IN ):

variable FUNCTION(set)

instead of. 代替。

variable = FUNCTION(set) , unless ofcourse operator is a summation/count operation returning one result :) variable = FUNCTION(set)除非ofcourse运算符是一个summation / count操作返回一个结果:)

It would make more senseto have variable ANY(Set/Array) instead of variable=ANY(Set/Array) . 更有意义的是有variable ANY(Set/Array)而不是variable=ANY(Set/Array) Similar example is the IN function. 类似的例子是IN功能。

Can anyone explain what is going on here? 谁能解释一下这里发生了什么?

IN (...) is basically equivalent to = ANY (ARRAY[...]) IN (...)基本等同于= ANY (ARRAY[...])

Crucially, ANY is not a function . 至关重要的是, ANY不是一个功能 It's syntax defined by the SQL standard, and is no more a function than GROUP BY or the OVER clause in a window function. 它是由SQL标准定义的语法,不再是窗口函数中的GROUP BYOVER子句的函数。

The reason that = is required before ANY is that ANY can apply to other operators too. ANY之前需要=的原因是ANY也可以适用于其他运营商。 What it means is "Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element." 这意味着“在左边对着数组中的每个元素测试操作符,如果对于至少一个元素测试为真,则返回true。”

You can use > ANY (ARRAY[...]) or whatever. 您可以使用> ANY (ARRAY[...])或其他任何东西。 It's a general purpose operator that isn't restricted to = . 它是一个通用运算符,不限于= Notably useful for LIKE ANY (albeit with somewhat bad performance). 特别适用于LIKE ANY (尽管表现有些不好)。

There is ALL too, which does much the same thing but returns true only if all results are true. 还有ALL ,它做了很多相同的事情,但只有在所有结果都为真时才返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM