Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C6C7CB5D8B2 for ; Sun, 22 May 2011 00:24:04 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 45597-01 for ; Sun, 22 May 2011 03:23:58 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [173.230.139.50]) by mail.postgresql.org (Postfix) with ESMTP id 08761B5D801 for ; Sun, 22 May 2011 00:23:57 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by www.postnewspapers.com.au (Postfix) with ESMTP id 80F182D; Sat, 21 May 2011 23:23:56 -0400 (EDT) X-Virus-Scanned: amavisd-new at postnewspapers.com.au.com Received: from www.postnewspapers.com.au ([127.0.0.1]) by localhost (www.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id q1aR8Kd83ku9; Sat, 21 May 2011 23:23:55 -0400 (EDT) Received: from mail.postnewspapers.com.au (ppp171-128.static.internode.on.net [150.101.171.128]) by www.postnewspapers.com.au (Postfix) with ESMTP id 7829227; Sat, 21 May 2011 23:23:55 -0400 (EDT) Received: from localhost (localhost [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 37756AE52A; Sun, 22 May 2011 11:23:54 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (mail.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id SdWU9mF1r5mB; Sun, 22 May 2011 11:23:49 +0800 (WST) Received: from [10.1.1.5] (124-169-254-58.dyn.iinet.net.au [124.169.254.58]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTPSA id 5E3AAAE4CA; Sun, 22 May 2011 11:23:48 +0800 (WST) Message-ID: <4DD881C0.9020401@postnewspapers.com.au> Date: Sun, 22 May 2011 11:23:44 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110428 Fedora/3.1.10-1.fc14 Thunderbird/3.1.10 MIME-Version: 1.0 To: Karl Koster CC: pgsql-sql@postgresql.org Subject: Re: unnest in SELECT References: <4DD6BD57.1080109@kdresources.com> In-Reply-To: <4DD6BD57.1080109@kdresources.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/125 X-Sequence-Number: 35409 On 05/21/2011 03:13 AM, Karl Koster wrote: > I have a couple of questions regarding unnest. > > 1) If I issue a select statement "select unnest(vector1) as v from > some_table", I cannot seem to use the column alias v in a WHERE or > HAVING clause. I can use it in an ORDER BY or GROUP by clause. Is this > the way it is supposed to work? Yes, and it's what the SQL standard requires. Otherwise, how would this query work? SELECT a/b FROM sometable WHERE b <> 0; ? The SELECT list has to be processed only once the database has already decided which rows it applies to and how. Use unnest in a FROM clause, eg: SELECT v1.* FROM unnest(vector) ... This may require a join and/or subquery to obtain 'vector'. > 2) If I issue a select statement "select unnest(vector1) as v1, > unnest(vector2) as v2 from some_table" and vector1 has a length of 3 and > vector2 has a length of 4, the result set will have 12 rows with the > data of vector1 repeating 4 times and vector2 repeating 3 times. > Shouldn't the content of the shorter array(s) simply be return null in > it's respective column and the result set be the size of the longest array? unnest is a set-returning function, and it doesn't really make that much sense to have them in the SELECT list anyway. Few databases support it, and PostgreSQL's behavior is a historical quirk that I think most people here hope will go quietly away at some point. Use unnest in a FROM clause. -- Craig Ringer