[DEPARSE] An alias named 'set' on an UPDATE, DELETE or MERGE target does not read back
Hi hackers,
When the target of an UPDATE, DELETE or MERGE in a rule or in a
BEGIN ATOMIC function body has the alias "set", ruleutils.c prints
the alias bare, and the text no longer parses. The two attached
patches add tests and fix it.
The problem
-----------
The example below shows the output before and after the patch
(only the body of the rule is shown):
create table t (a int, b int);
create rule r as on insert to t
do also update t "set" set b = 1 where "set".a = 2;
select pg_get_ruledef(oid) from pg_rewrite where rulename = 'r';
before: ON INSERT TO public.t DO UPDATE t set SET b = 1
WHERE (set.a = 2);
ERROR: syntax error at or near "b"
after: ON INSERT TO public.t DO UPDATE t AS set SET b = 1
WHERE (set.a = 2);
DELETE FROM t "set" and MERGE INTO t "set" are printed the same
way and fail at "set". pg_dump prints rules and function bodies
with the same code, so a dump of such a database does not restore.
The target of these three statements is parsed by
relation_expr_opt_alias. On seeing "set" after the relation name,
the parser has to decide with one token of lookahead whether it is
an alias or the SET keyword of UPDATE, and gram.y resolves that in
favor of the keyword (see the comment above
relation_expr_opt_alias). DELETE and MERGE use the same
production, so they are caught too, although no SET clause follows
there. As SET is an unreserved keyword, quote_identifier() leaves
"set" bare.
The fix
-------
0002 gives get_rte_alias() a dml_target argument and prints AS
before an alias named "set" on the target of UPDATE, DELETE and
MERGE. INSERT already prints AS always. An alias named "set" in
FROM is not ambiguous, so it is printed as before.
Quoting the alias only at that position, as in
UPDATE t "set" SET ..., would work as well. Which of the two
would people prefer?
Tests
-----
0001 adds the tests with the output before the fix, and 0002
updates it, so that the diff shows what changes. The rules and
the function are printed, dropped and created again from the
printed text.
statement where "set" is object
--------- ---------------------- -------------------------
UPDATE target alias rule
DELETE target alias rule
MERGE target alias BEGIN ATOMIC function
SELECT FROM alias (unchanged) view
Back branches
-------------
I think this should be back-patched to 15. I applied both
patches to REL_15_STABLE through REL_19_STABLE.
git am applies them cleanly on each branch, and make check passes,
also with the test patch alone.
This came out of an AI-assisted audit of ruleutils.c. I
will post the other problems it found as separate,
independent reports as I work through them.
Best regards,
Henson
Attachments:
t254176_1v1-0001-Add-tests-for-a-relation-alias-named-SET-in-DML-t.patchapplication/octet-stream; name=v1-0001-Add-tests-for-a-relation-alias-named-SET-in-DML-t.patchDownload+150-1
v1-0002-Write-AS-before-an-alias-named-SET-of-a-DML-targe.patchapplication/octet-stream; name=v1-0002-Write-AS-before-an-alias-named-SET-of-a-DML-targe.patchDownload+51-32
Henson Choi <assam258@gmail.com> writes:
When the target of an UPDATE, DELETE or MERGE in a rule or in a
BEGIN ATOMIC function body has the alias "set", ruleutils.c prints
the alias bare, and the text no longer parses. The two attached
patches add tests and fix it.
Another good catch, but here again I find your solution
overcomplicated and perhaps not very future-proof. Let's just
emit AS all the time in these statements.
regards, tom lane
Hi Tom,
Another good catch, but here again I find your solution
overcomplicated and perhaps not very future-proof. Let's just
emit AS all the time in these statements.
Agreed. The attached v2 writes AS before the alias of an UPDATE,
DELETE or MERGE target every time, as INSERT already does.
The tests are trimmed to UPDATE and DELETE in rules and MERGE in
a BEGIN ATOMIC function body, each with a target aliased "set"
and recreated from its deparsed text. Existing output in rules and
returning changes too, as in UPDATE rules_log AS trgt SET ... . The
full regression suite passes on master.
This changes the deparsed text of every aliased target. AS has been
accepted there in all supported branches, and the code change applies
cleanly to REL_15 through REL_19. I built each branch and ran the
regression tests.
Best regards,
Henson
Attachments:
t254176_2v2-0001-Add-tests-for-a-relation-alias-named-SET-in-DML-t.patchapplication/octet-stream; name=v2-0001-Add-tests-for-a-relation-alias-named-SET-in-DML-t.patchDownload+81-1
v2-0002-Always-write-AS-before-the-alias-of-a-DML-target.patchapplication/octet-stream; name=v2-0002-Always-write-AS-before-the-alias-of-a-DML-target.patchDownload+47-30
Import Notes
Reply to msg id not found: 249856.1792338548@sss.pgh.pa.us