Fix MERGE's test for unreachable WHEN clauses.

The former code would only detect an unreachable WHEN clause if it had
an AND condition. Fix, so that unreachable unconditional WHEN clauses
are also detected.

Back-patch to v15, where MERGE was added.

Discussion: https://postgr.es/m/CAEZATCVQ=7E2z4cSBB49jjeGGsB6WeoYQY32NDeSvcHiLUZ=ow@mail.gmail.com
This commit is contained in:
Dean Rasheed 2023-01-10 14:17:47 +00:00
parent d952373a98
commit f026c16a2c
3 changed files with 5 additions and 5 deletions

View File

@ -155,12 +155,12 @@ transformMergeStmt(ParseState *pstate, MergeStmt *stmt)
/*
* Check for unreachable WHEN clauses
*/
if (mergeWhenClause->condition == NULL)
is_terminal[when_type] = true;
else if (is_terminal[when_type])
if (is_terminal[when_type])
ereport(ERROR,
(errcode(ERRCODE_SYNTAX_ERROR),
errmsg("unreachable WHEN clause specified after unconditional WHEN clause")));
if (mergeWhenClause->condition == NULL)
is_terminal[when_type] = true;
}
/*

View File

@ -659,7 +659,7 @@ USING source AS s
ON t.tid = s.sid
WHEN MATCHED THEN /* Terminal WHEN clause for MATCHED */
DELETE
WHEN MATCHED AND s.delta > 0 THEN
WHEN MATCHED THEN
UPDATE SET balance = t.balance - s.delta;
ERROR: unreachable WHEN clause specified after unconditional WHEN clause
ROLLBACK;

View File

@ -438,7 +438,7 @@ USING source AS s
ON t.tid = s.sid
WHEN MATCHED THEN /* Terminal WHEN clause for MATCHED */
DELETE
WHEN MATCHED AND s.delta > 0 THEN
WHEN MATCHED THEN
UPDATE SET balance = t.balance - s.delta;
ROLLBACK;