IN with arrays

Поиск
Список
Период
Сортировка
Искать

IN with arrays

От:
Peter Eisentraut <peter_e@gmx.net>
Дата:
I'm wondering why a IN b isn't equivalent to a = ANY b for arrays, as it 
is for subqueries.

That is, why can't you write

SELECT 1 IN ( ARRAY[1, 2, 3] );

when you can write

SELECT 1 = ANY ( ARRAY[1, 2, 3] );

?

I'm guessing that there is a semantic inconsistency between these 
expressions, as the first one considers what is in parentheses as a 
list, the second one as a single expression.  That would be very bad.

-- 
Peter Eisentraut
http://developer.postgresql.org/~petere/

Re: IN with arrays

От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Peter Eisentraut  writes:
> That is, why can't you write
> SELECT 1 IN ( ARRAY[1, 2, 3] );
> when you can write
> SELECT 1 = ANY ( ARRAY[1, 2, 3] );
> ?

The two syntaxes are in fact *not* equivalent according to SQL92.
= ANY derives from
         ::=                
         ::=  | 
         ::= ALL
         ::= SOME | ANY

(notice the RHS *must* be a 
) whereas IN comes from ::= [ NOT ] IN ::=
| ::= { }... The form "expr = ANY (non-query-expr)" is therefore a spec extension, which we are free to define as we wish, and we defined it to be a scalar-vs-array-elements comparison. But I don't see any way that we can interpret "expr IN (other-expr)" as anything except a variant spelling for a simple equality test. regards, tom lane
FAQ