Allow an alias for the target table in UPDATE/DELETE

From: Atsushi Ogawa <atsushi(dot)ogawa(at)gmail(dot)com>
To: pgsql-patches(at)postgresql(dot)org
Subject: Allow an alias for the target table in UPDATE/DELETE
Date: 2005-12-01 14:23:11
Message-ID: 613787150512010623l16c528f3x@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-patches

I made a patch to allow an alias for target table in UPDATE/DELETE
This is a TODO item.
> o Allow an alias to be provided for the target table in UPDATE/DELETE
> This is not SQL-spec but many DBMSs allow it.
Example:
UPDATE accounts AS a SET a.abalance = a.abalance + 10 WHERE a.aid = 1;

I think that there are two viewpoints of this patch:
(1)I moved SET to reserved words to avoid shift/reduce conflicts.
It is because the parser confused by whether SET is a keyword or
an alias in SQL 'UPDATE tbl SET ...'.

(2)About processing when column identifier of SET clause is specified
like 'AAA.BBB'. 'AAA' is a composite column now. When an alias for
target table is supported, 'AAA' is a composite column or a table.
How do we distinguish these?

The syntax rule of the composite type is as follows:
-----------------------------------------------------------------------
SELECT item.name FROM on_hand WHERE item.price > 9.99;

This will not work since the name item is taken to be a table name,
not a field name, per SQL syntax rules. You must write it like this:

SELECT (item).name FROM on_hand WHERE (item).price > 9.99;
-----------------------------------------------------------------------
but...
-----------------------------------------------------------------------
We can update an individual subfield of a composite column:

UPDATE mytab SET complex_col.r = (complex_col).r + 1 WHERE ...;

Notice here that we don't need to (and indeed cannot) put parentheses
around the column name appearing just after SET, but we do need
parentheses when referencing the same column in the expression to
the right of the equal sign.
-----------------------------------------------------------------------

The syntax rule is different in SET clause and other clauses.
Incompatibility is caused if SET clause is changed to the same
rule as other clauses to allow an alias for target table.

To keep compatibility, I implemented the following rules:
Eat up a first element of column identifier when the first element
is a name or an alias for target table. And, make the second
element a column name.

Example:
UPDATE tbl AS t SET t.col = 1;
=> 't' is eaten up, and 'col' becomes a column name.

--- Atsushi Ogawa

Attachment Content-Type Size
allow_alias_update.patch application/octet-stream 8.8 KB

Responses

Browse pgsql-patches by date

  From Date Subject
Next Message Tom Lane 2005-12-01 15:07:31 Re: [PATCHES] A couple of proposed pgbench changes
Previous Message Alvaro Herrera 2005-12-01 13:36:29 Re: aclchk.c refactor