Note on scalar subquery syntax - Mailing list pgsql-general

From Kevin Murphy
Subject Note on scalar subquery syntax
Date
Msg-id 42F0C94A.2030607@genome.chop.edu
Whole thread Raw
Responses Re: Note on scalar subquery syntax  (Peter Fein <pfein@pobox.com>)
Re: Note on scalar subquery syntax  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
I thought this might be helpful in the future to other duffers such as
myself.

The following is my big contribution to the documentation of the use of
scalar subqueries ;-):

You have to wrap a scalar subquery in its own parentheses even where you
might think it to be unnecessary, such as when the subquery is the sole
argument to a function.

As an example, I wrote a function to explode, or unpack, the elements of
an array onto separate rows (its return type is SETOF whatever), but it
took me a while to figure out how to use it effectively in queries.

You have to use it like this:

RIGHT--> select * from array_explode((select array_col from table1 where
col2 = 'something'));

Note the "extra" set of parentheses.  These are crucial: the syntax is
invalid without these, as in:

WRONG--> select * from array_explode(select array_col from table1 where
col2 = 'something');

And no, as mentioned in many archived list messages, you can NOT do the
following, which is what a lot of people (including me) seem to try first:

WRONG--> select array_explode(array_col) from table1 where col2 =
'something');

(The previous command results in the error message: "set-valued function
called in context that cannot accept a set").

-Kevin Murphy


pgsql-general by date:

Previous
From: Richard Huxton
Date:
Subject: Re: Hello
Next
From: Jaime Casanova
Date:
Subject: Re: Hello