Re: Table name scope (was Re: Outer joins aren't working with views)

From: Thomas Lockhart <lockhart(at)alumni(dot)caltech(dot)edu>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org, pgsql-hackers(at)postgresql(dot)org
Subject: Re: Table name scope (was Re: Outer joins aren't working with views)
Date: 2000-12-16 07:38:20
Message-ID: 3A3B1BEC.FBC968B4@alumni.caltech.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

> The first and second items here seem to be perfectly clear that the
> names t1 and t2 have scope across the whole SELECT statement and are not
> hidden within the <joined table> formed by the OUTER JOIN clause.

You are right. If there is a "correlation name", then those underlying
table names become invisible, but that was not in the example here.
Rereading my Date and Darwen clarified this for me. However, there are
*some* columns for which this explicit table qualification is not
allowed, including in the example of NATURAL JOIN.

Date and Darwen, 4th ed, pp 142-144 discuss various aspects of join
scope and behavior. For NATURAL JOIN, the columns with common names
forming the join columns *lose* their underlying table name, since they
can't be traced back to a column from a specific table (the table of
origin is ambiguous). And for a NATURAL JOIN, it is impossible to get
back two columns with the same name, since those columns were unified by
the join process.

The process is required to join on the columns with names in common, and
to swallow one of each pair in the result. How should you refer to the
column that remains?

create table t1 (id int, id2 int);
create table t2 (id int, name text);
select * from t1 natural left outer join t2;

must return something from the set of columns (id, id2, name), and two
columns of name "id" will not be visible. Also, column "id" cannot be
qualified with a table name. So

select t1.id from t1 natural join t2;

is not legal (though perhaps could be justified as an extension). The
columns *not* involved in the join operation, id2 and name, *can* be
qualified by the underlying table name, but the only way to get the same
for "id" after the natural join is to use a correlation name. e.g.

select tx.id from (t1 natural join t2) as tx;
select t1.id2 from t1 natural join t2;

are both legal.

> It'd be useful to check the above example against Oracle and other
> implementations, but the parts of the spec that I can follow seem
> to say that we've got the right behavior now.

Oracle does not support SQL9x join syntax, so we can't ask it for an
example. Not sure about the others.

Comments?

- Thomas

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Nabil Sayegh 2000-12-16 11:45:03 Re: JDBC Driver Authentication Bug
Previous Message Tom Lane 2000-12-16 06:38:33 Table name scope (was Re: Outer joins aren't working with views)

Browse pgsql-hackers by date

  From Date Subject
Next Message Michael Meskes 2000-12-16 16:27:18 Re: Great Bridge PostgreSQL products and services
Previous Message Tom Lane 2000-12-16 06:38:33 Table name scope (was Re: Outer joins aren't working with views)