简体   繁体   中英

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:

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 . This seems completely strange since based on language, logic, and sql conventions, usually you have (eg IN ):

variable FUNCTION(set)

instead of.

variable = FUNCTION(set) , unless ofcourse operator is a summation/count operation returning one result :)

It would make more senseto have variable ANY(Set/Array) instead of variable=ANY(Set/Array) . Similar example is the IN function.

Can anyone explain what is going on here?

IN (...) is basically equivalent to = ANY (ARRAY[...])

Crucially, ANY is not a function . 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.

The reason that = is required before ANY is that ANY can apply to other operators too. 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."

You can use > ANY (ARRAY[...]) or whatever. It's a general purpose operator that isn't restricted to = . Notably useful for LIKE ANY (albeit with somewhat bad performance).

There is ALL too, which does much the same thing but returns true only if all results are true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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