Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id D82D413380FA for ; Tue, 22 Feb 2011 17:50:30 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 30501-01-3 for ; Tue, 22 Feb 2011 21:50:24 +0000 (UTC) X-Greylist: delayed 00:06:36.933354 by SQLgrey-1.7.6 Received: from alexander.swishmail.com (alexander.swishmail.com [208.72.56.31]) by mail.postgresql.org (Postfix) with ESMTP id 93D261337D61 for ; Tue, 22 Feb 2011 17:35:25 -0400 (AST) Received: (qmail 94080 invoked by uid 89); 22 Feb 2011 21:28:45 -0000 Received: from unknown (HELO ?127.0.0.1?) (eric.schwarzenbach@wrycan.com@209.6.93.90) by alexander.swishmail.com with ESMTPA; 22 Feb 2011 21:28:45 -0000 Message-ID: <4D642A87.5090405@blackbrook.org> Date: Tue, 22 Feb 2011 16:28:39 -0500 From: Eric Schwarzenbach User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.5.0 MIME-Version: 1.0 To: Scott Dunbar CC: pgsql-bugs@postgresql.org Subject: Re: BUG #5898: Nested "in" clauses hide bad column names References: <201102221726.p1MHQPDh069580@wwwmaster.postgresql.org> <3314.1298396703@sss.pgh.pa.us> <4D63FB6B.3060804@xigole.com> In-Reply-To: <4D63FB6B.3060804@xigole.com> Content-Type: multipart/alternative; boundary="------------090109060700080401030109" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.899 tagged_above=-5 required=5 tests=BAYES_00=-1.9, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001 X-Spam-Level: X-Archive-Number: 201102/201 X-Sequence-Number: 29473 This is a multi-part message in MIME format. --------------090109060700080401030109 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Using table aliases prevents such problems by disambiguating the column names. For example, if you had table aliases in just one part of this query as in the below, you would have gotten an error instead of deleting all those rows: select respondent_id from respondent where respondent_id in (select cs.respondent_id from chat_session cs where cs.project_id in (select project_id from project where company_id = 4)); Cheers, Eric On 2/22/2011 1:07 PM, Scott Dunbar wrote: > Yes, you're correct. I guess this makes sense but it does seem > strange that I can enter garbage in a query but it still runs. And in > my case the output from this (the entire table) was then used in a > delete statement that toasted the entire table. Allowing bogus SQL > just seems "wrong" but I do understand what's going on. > > Thanks for your help. > > > On 02/22/2011 10:45 AM, Tom Lane wrote: >> "Scott Dunbar" writes: >>> I have a nested in clause like: >>> select respondent_id from respondent where respondent_id in (select >>> respondent_id from chat_session where project_id in (select project_id from >>> project where company_id = 4)); >>> However, in this example, there is no column named respondent_id in the >>> chat_session table. >> Probably there is one in respondent, though? This behavior is not a bug >> --- what you have there is an outer reference, and it is working exactly >> as specified by the SQL standard. Sub-selects would be a whole lot less >> useful if they couldn't refer to variables of the outer query. >> >> regards, tom lane > > > -- > Scott Dunbar > Xigole Systems, Inc. > Enterprise software consulting, development, and hosting > 303·667·6343 --------------090109060700080401030109 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit Using table aliases prevents such problems by disambiguating the column names. For example, if you had table aliases in just one part of this query as in the below, you would have gotten an error instead of deleting all those rows:

select respondent_id from respondent where respondent_id in (select
cs.respondent_id from chat_session cs where cs.project_id in (select project_id from
project where company_id = 4));


Cheers,

Eric


On 2/22/2011 1:07 PM, Scott Dunbar wrote:
Yes, you're correct.  I guess this makes sense but it does seem strange that I can enter garbage in a query but it still runs.  And in my case the output from this (the entire table) was then used in a delete statement that toasted the entire table.  Allowing bogus SQL just seems "wrong" but I do understand what's going on.

Thanks for your help.


On 02/22/2011 10:45 AM, Tom Lane wrote:
"Scott Dunbar" <scott@xigole.com> writes:
I have a nested in clause like:
select respondent_id from respondent where respondent_id in (select
respondent_id from chat_session where project_id in (select project_id from
project where company_id = 4));
However, in this example, there is no column named respondent_id in the
chat_session table.
Probably there is one in respondent, though?  This behavior is not a bug
--- what you have there is an outer reference, and it is working exactly
as specified by the SQL standard.  Sub-selects would be a whole lot less
useful if they couldn't refer to variables of the outer query.

			regards, tom lane


--
Scott Dunbar
Xigole Systems, Inc.
Enterprise software consulting, development, and hosting
303·667·6343

--------------090109060700080401030109--