Re: Why does this array query fail? - Mailing list pgsql-general

From Ken Tanzer
Subject Re: Why does this array query fail?
Date
Msg-id CAD3a31UNyYS4Oq0=uhrzKOQ8rsswara4zndP+HA9p3N+53_wgg@mail.gmail.com
Whole thread Raw
In response to Re: Why does this array query fail?  (Ken Tanzer <ken.tanzer@gmail.com>)
Responses Re: Why does this array query fail?  (David Johnston <polobo@yahoo.com>)
List pgsql-general
Well I partially take back my last question.  In the error message, I missed the non-array / array part of "integer = text[]"

But I'm still confused.  My subselect returns an array.  If I cast it to a text array, ANY is happy.  But if I don't do so, what exactly does Postgres think my subquery has yielded?  And the error message still doesn't seem to make sense...



On Mon, Sep 16, 2013 at 6:59 PM, Ken Tanzer <ken.tanzer@gmail.com> wrote:
OK I tried that and see it works with the cast.  But now I'm confused about both what exactly is failing without the cast, and about the resulting error message.

Is the query failing because PG doesn't understand the subquery is yielding an array?  Seems unlikely.  But if the problem is a type mismatch between 'test' (on the left) and my subquery, I'd expect the same error message as if I try to compare an int to a text array:

SELECT 'found' WHERE 1 = ANY( (SELECT ARRAY['test','pass','fail']));
ERROR:  operator does not exist: integer = text[]
LINE 1: SELECT 'found' WHERE 1 = ANY( (SELECT ARRAY['test','pass','f...
                               ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

Instead of the error message I actually got:

ERROR:  array value must start with "{" or dimension information

Thanks.

Ken





On Mon, Sep 16, 2013 at 6:42 PM, bricklen <bricklen@gmail.com> wrote:

On Mon, Sep 16, 2013 at 6:36 PM, Ken Tanzer <ken.tanzer@gmail.com> wrote:
Thanks for the explanation.  I think I at least understand what it's doing now.  I'm either surprised or confused though, as I was under the impression that you could substitute a subquery for a value pretty much anywhere, but I guess that's not the case?

Cheers,
Ken


Your subquery can also be explicitly casted to make it work. Note the "::TEXT[]"

SELECT 'found' WHERE 'test' = ANY( (SELECT ARRAY['test','pass','fail'])::TEXT[] );



--
AGENCY Software  
A data system that puts you in control
100% Free Software

learn more about AGENCY or
follow the discussion.



--
AGENCY Software  
A data system that puts you in control
100% Free Software
(253) 245-3801

learn more about AGENCY or
follow the discussion.

pgsql-general by date:

Previous
From: Ken Tanzer
Date:
Subject: Re: Why does this array query fail?
Next
From: David Johnston
Date:
Subject: Re: Why does this array query fail?