pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Invalid SQL not rejected?
4+ messages / 4 participants
[nested] [flat]

* Invalid SQL not rejected?
@ 2013-04-12 00:17  Craig James <cjames@emolecules.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Craig James @ 2013-04-12 00:17 UTC (permalink / raw)
  To: pgsql-admin

Hmmm.... the subselect is invalid, but not rejected.  The outer select
returns every row in the customer_order_matches table.  This seems pretty
wrong. This is PG 9.2.1 running on Ubuntu.


db=> select count(1) from customer_order_matches where
customer_order_item_id in (select customer_order_item_id from
customer_order where customer_order_id = 105862140);
 count
-------
 36180
(1 row)

db=> select customer_order_item_id from customer_order where
customer_order_id = 105862140;
ERROR:  column "customer_order_item_id" does not exist
LINE 1: select customer_order_item_id from customer_order where cust...
               ^

Craig

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Invalid SQL not rejected?
@ 2013-04-12 00:44  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Craig James <cjames@emolecules.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2013-04-12 00:44 UTC (permalink / raw)
  To: Craig James <cjames@emolecules.com>; +Cc: pgsql-admin

Craig James <cjames@emolecules.com> writes:
> Hmmm.... the subselect is invalid, but not rejected.  The outer select
> returns every row in the customer_order_matches table.  This seems pretty
> wrong. This is PG 9.2.1 running on Ubuntu.

> db=> select count(1) from customer_order_matches where
> customer_order_item_id in (select customer_order_item_id from
> customer_order where customer_order_id = 105862140);
>  count
> -------
>  36180
> (1 row)

Unfortunately, if customer_order_item_id doesn't exist in
customer_order, that's still a perfectly valid outer reference
to customer_order_matches.customer_order_item_id.  People get
confused by that kind of thing regularly, but it's required by
spec to work like this.

			regards, tom lane


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Invalid SQL not rejected?
@ 2013-04-12 01:28  Julian Glass <tempura@internode.on.net>
  parent: Craig James <cjames@emolecules.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Julian Glass @ 2013-04-12 01:28 UTC (permalink / raw)
  To: pgsql-admin

On 12/04/13 10:17, Craig James wrote:
> Hmmm.... the subselect is invalid, but not rejected.  The outer select
> returns every row in the customer_order_matches table.  This seems
> pretty wrong. This is PG 9.2.1 running on Ubuntu.
>
>
> db=> select count(1) from customer_order_matches where
> customer_order_item_id in (select customer_order_item_id from
> customer_order where customer_order_id = 105862140);
>  count
> -------
>  36180
> (1 row)
>
> db=> select customer_order_item_id from customer_order where
> customer_order_id = 105862140;
> ERROR:  column "customer_order_item_id" does not exist
> LINE 1: select customer_order_item_id from customer_order where cust...
>                ^
>
> Craig
The "customer_order_item_id" in the subselect is referencing the
column and therefore the value of the outer query "customer_order_item_id"
in the query result.
i.e:
SELECT generate_series FROM generate_series(1,100) WHERE generate_series
IN (SELECT generate_series);
Which is always going to be true. (x == x).

You might want to explicitly reference tables (and alias). Such as:

SELECT count(*) FROM customer_order_matches com
    WHERE com.customer_order_item_id IN
        (SELECT co.customer_order_item_id
            FROM customer_order co
            WHERE co.customer_order_id = 105862140);

Although I'm making asumptions with the "customer_order_id".
Jules.


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Invalid SQL not rejected?
@ 2013-04-12 01:52  Kevin Grittner <kgrittn@ymail.com>
  parent: Julian Glass <tempura@internode.on.net>
  0 siblings, 0 replies; 4+ messages in thread

From: Kevin Grittner @ 2013-04-12 01:52 UTC (permalink / raw)
  To: Julian Glass <tempura@internode.on.net>; pgsql-admin

Julian Glass <tempura@internode.on.net> wrote:

> You might want to explicitly reference tables (and alias).

I agree.  I find that the best defense against several classes of
silent misbehavior is to alias all table references and qualify all
column references with the alias -- or use a tool which does this
for you.

--
Kevin Grittner
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


-- 
Sent via pgsql-admin mailing list (pgsql-admin@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-admin



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2013-04-12 01:52 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-04-12 00:17 Invalid SQL not rejected? Craig James <cjames@emolecules.com>
2013-04-12 00:44 ` Tom Lane <tgl@sss.pgh.pa.us>
2013-04-12 01:28 ` Julian Glass <tempura@internode.on.net>
2013-04-12 01:52   ` Kevin Grittner <kgrittn@ymail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox