Skip to content

Commit 2617551

Browse files
committed
Teach deparsing of CASE expressions to cope with the simplified forms
that simplify_boolean_equality() may leave behind. This is only relevant if the user writes something a bit silly, like CASE x=y WHEN TRUE THEN. Per example from Michael Fuhr; may or may not explain bug #2106.
1 parent 69525fc commit 2617551

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* back to source text
44
*
55
* IDENTIFICATION
6-
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.209 2005/11/22 18:17:23 momjian Exp $
6+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.210 2005/12/10 19:21:03 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -3389,22 +3389,47 @@ get_rule_expr(Node *node, deparse_context *context,
33893389
foreach(temp, caseexpr->args)
33903390
{
33913391
CaseWhen *when = (CaseWhen *) lfirst(temp);
3392+
Node *w = (Node *) when->expr;
33923393

33933394
if (!PRETTY_INDENT(context))
33943395
appendStringInfoChar(buf, ' ');
33953396
appendContextKeyword(context, "WHEN ",
33963397
0, 0, 0);
33973398
if (caseexpr->arg)
33983399
{
3399-
/* Show only the RHS of "CaseTestExpr = RHS" */
3400-
Node *rhs;
3400+
/*
3401+
* The parser should have produced WHEN clauses of
3402+
* the form "CaseTestExpr = RHS"; we want to show
3403+
* just the RHS. If the user wrote something silly
3404+
* like "CASE boolexpr WHEN TRUE THEN ...", then
3405+
* the optimizer's simplify_boolean_equality() may
3406+
* have reduced this to just "CaseTestExpr" or
3407+
* "NOT CaseTestExpr", for which we have to show
3408+
* "TRUE" or "FALSE".
3409+
*/
3410+
if (IsA(w, OpExpr))
3411+
{
3412+
Node *rhs;
34013413

3402-
Assert(IsA(when->expr, OpExpr));
3403-
rhs = (Node *) lsecond(((OpExpr *) when->expr)->args);
3404-
get_rule_expr(rhs, context, false);
3414+
Assert(IsA(linitial(((OpExpr *) w)->args),
3415+
CaseTestExpr));
3416+
rhs = (Node *) lsecond(((OpExpr *) w)->args);
3417+
get_rule_expr(rhs, context, false);
3418+
}
3419+
else if (IsA(w, CaseTestExpr))
3420+
appendStringInfo(buf, "TRUE");
3421+
else if (not_clause(w))
3422+
{
3423+
Assert(IsA(get_notclausearg((Expr *) w),
3424+
CaseTestExpr));
3425+
appendStringInfo(buf, "FALSE");
3426+
}
3427+
else
3428+
elog(ERROR, "unexpected CASE WHEN clause: %d",
3429+
(int) nodeTag(w));
34053430
}
34063431
else
3407-
get_rule_expr((Node *) when->expr, context, false);
3432+
get_rule_expr(w, context, false);
34083433
appendStringInfo(buf, " THEN ");
34093434
get_rule_expr((Node *) when->result, context, true);
34103435
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy