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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix up handling of simple-form CASE with constant test expressio
Date: 2010-12-19 20:32:59
Message-ID: E1PUPwB-0003HB-EO@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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
------
master

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

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

Browse pgsql-committers by date

  From Date Subject
Next Message Magnus Hagander 2010-12-19 20:33:39 pgsql: Remove thread dumping constant that requires newer Platform SDK
Previous Message Tom Lane 2010-12-19 17:49:17 pgsql: Fix erroneous parsing of tsquery input "... & !(subexpression) |