pgsql: Fix up handling of simple-form CASE with constant test expressio - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Fix up handling of simple-form CASE with constant test expressio
Date
Msg-id E1PUPwB-0003HE-Fk@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Fix up handling of simple-form CASE with constant test expression.

eval_const_expressions() can replace CaseTestExprs with constants when
the surrounding CASE's test expression is a constant.  This confuses
ruleutils.c's heuristic for deparsing simple-form CASEs, leading to
Assert failures or "unexpected CASE WHEN clause" errors.  I had put in
a hack solution for that years ago (see commit
514ce7a331c5bea8e55b106d624e55732a002295 of 2006-10-01), but bug #5794
from Peter Speck shows that that solution failed to cover all cases.

Fortunately, there's a much better way, which came to me upon reflecting
that Peter's "CASE TRUE WHEN" seemed pretty redundant: we can "simplify"
the simple-form CASE to the general form of CASE, by simply omitting the
constant test expression from the rebuilt CASE construct.  This is
intuitively valid because there is no need for the executor to evaluate
the test expression at runtime; it will never be referenced, because any
CaseTestExprs that would have referenced it are now replaced by constants.
This won't save a whole lot of cycles, since evaluating a Const is pretty
cheap, but a cycle saved is a cycle earned.  In any case it beats kluging
ruleutils.c still further.  So this patch improves const-simplification
and reverts the previous change in ruleutils.c.

Back-patch to all supported branches.  The bug exists in 8.1 too, but it's
out of warranty.

Branch
------
REL8_3_STABLE

Details
-------
http://git.postgresql.org/gitweb?p=postgresql.git;a=commitdiff;h=a6a77ae407be6decaf4d630b546bc66a6737216c

Modified Files
--------------
src/backend/optimizer/util/clauses.c |   16 +++++++++++++++-
src/backend/utils/adt/ruleutils.c    |   16 ++++++----------
2 files changed, 21 insertions(+), 11 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Fix up handling of simple-form CASE with constant test expressio
Next
From: Tom Lane
Date:
Subject: pgsql: Fix up handling of simple-form CASE with constant test expressio