Bug or Feature? Subquery issue.

Started by Josh Berkusover 22 years ago4 messagesbugs
Jump to latest
#1Josh Berkus
josh@agliodbs.com

Folks,

Came across this counter-intuitive behavior on IRC today:

test1=> create table vhost(idvhost serial primary key, foo integer);
NOTICE: CREATE TABLE will create implicit sequence "vhost_idvhost_seq" for
"serial" column "vhost.idvhost"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "vhost_pkey"
for table "vhost"
CREATE TABLE
test1=> create table domain(iddomain serial primary key, bar integer);
NOTICE: CREATE TABLE will create implicit sequence "domain_iddomain_seq" for
"serial" column "domain.iddomain"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "domain_pkey"
for table "domain"
CREATE TABLE
test1=> create table forwarding(idforwarding serial primary key, iddomain
integer references domain, baz integer);
NOTICE: CREATE TABLE will create implicit sequence
"forwarding_idforwarding_seq" for "serial" column "forwarding.idforwarding"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"forwarding_pkey" for table "forwarding"
ERROR: relation "forwarding_idforwarding_seq" already exists
test1=> insert into domain
test1-> values (100, 5);
INSERT 147824 1
test1=> insert into forwarding
test1-> values (1, 100, 15);
INSERT 147825 1
test1=> insert into vhost values (100, 15);
INSERT 147826 1
test1=> --this generates an error
test1=> select iddomain from vhost where IDvhost = 100;
ERROR: column "iddomain" does not exist
test1=> -- This should generate an error, because IDdomain isn't a column of
vhost
test1=> --instead it deletes a row.
test1=> delete from forwarding where iddomain in (select iddomain from vhost
where idvhost = 100);
DELETE 1
test1=>

According to Neil, what's happening is that "select iddomain" in the subquery
is grabbing the iddomain column from the forwarding table in the outer query.
This is not intutive, for certain; however, what I don't know is if it's SQL
Spec.

So, my question: does the SQL spec allow for citing the outer query in the
SELECT target list of a subquery?

If yes, this is a feature, if no, a bug.

--
-Josh Berkus
Aglio Database Solutions
San Francisco

#2Stephan Szabo
sszabo@megazone23.bigpanda.com
In reply to: Josh Berkus (#1)
Re: Bug or Feature? Subquery issue.

On Tue, 21 Oct 2003, Josh Berkus wrote:

Folks,

Came across this counter-intuitive behavior on IRC today:

test1=> --this generates an error
test1=> select iddomain from vhost where IDvhost = 100;
ERROR: column "iddomain" does not exist
test1=> -- This should generate an error, because IDdomain isn't a column of
vhost
test1=> --instead it deletes a row.
test1=> delete from forwarding where iddomain in (select iddomain from vhost
where idvhost = 100);
DELETE 1
test1=>

According to Neil, what's happening is that "select iddomain" in the subquery
is grabbing the iddomain column from the forwarding table in the outer query.
This is not intutive, for certain; however, what I don't know is if it's SQL
Spec.

So, my question: does the SQL spec allow for citing the outer query in the
SELECT target list of a subquery?

AFAICT yes. I don't see anything that would limit a column reference that
was an outer reference from being in the target list in general (there are
specific limitations for some subcases) at least in sql92.

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Josh Berkus (#1)
Re: Bug or Feature? Subquery issue.

Josh Berkus <josh@agliodbs.com> writes:

Came across this counter-intuitive behavior on IRC today:

Given that this step in your example failed:

test1=> create table forwarding(idforwarding serial primary key, iddomain
integer references domain, baz integer);
NOTICE: CREATE TABLE will create implicit sequence
"forwarding_idforwarding_seq" for "serial" column "forwarding.idforwarding"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index
"forwarding_pkey" for table "forwarding"
ERROR: relation "forwarding_idforwarding_seq" already exists

it's impossible to be certain what situation you are really
testing... but assuming that that isn't affecting the results,

test1=> select iddomain from vhost where IDvhost = 100;
ERROR: column "iddomain" does not exist
test1=> -- This should generate an error, because IDdomain isn't a column of
vhost
test1=> --instead it deletes a row.
test1=> delete from forwarding where iddomain in (select iddomain from vhost
where idvhost = 100);

This is absolutely NOT an error. iddomain in the subquery is a
legitimate outer reference, if it's not otherwise known in the subquery.
There is no clause in the SQL spec that says that outer references are
invisible in any context ... even if it means you just deleted your
whole table, which is what I think will happen here...

regards, tom lane

#4Josh Berkus
josh@agliodbs.com
In reply to: Tom Lane (#3)
Re: Bug or Feature? Subquery issue.

Tom,

This is absolutely NOT an error. iddomain in the subquery is a
legitimate outer reference, if it's not otherwise known in the subquery.
There is no clause in the SQL spec that says that outer references are
invisible in any context ... even if it means you just deleted your
whole table, which is what I think will happen here...

Yup, that's what happened.

Wasn't sure. We're OK then.

--
Josh Berkus
Aglio Database Solutions
San Francisco