• Tom Lane's avatar
    Fix up handling of simple-form CASE with constant test expression. · 1b19e2c0
    Tom Lane authored
    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
    514ce7a3 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.
    1b19e2c0
clauses.c 130 KB