Skip to content

Commit 0bd7305

Browse files
committed
Make decompilation of optimized CASE constructs more robust.
We had some hacks in ruleutils.c to cope with various odd transformations that the optimizer could do on a CASE foo WHEN "CaseTestExpr = RHS" clause. However, the fundamental impossibility of covering all cases was exposed by Heikki, who pointed out that the "=" operator could get replaced by an inlined SQL function, which could contain nearly anything at all. So give up on the hacks and just print the expression as-is if we fail to recognize it as "CaseTestExpr = RHS". (We must cover that case so that decompiled rules print correctly; but we are not under any obligation to make EXPLAIN output be 100% valid SQL in all cases, and already could not do so in some other cases.) This approach requires that we have some printable representation of the CaseTestExpr node type; I used "CASE_TEST_EXPR". Back-patch to all supported branches, since the problem case fails in all.
1 parent c2a366d commit 0bd7305

File tree

1 file changed

+33
-34
lines changed

1 file changed

+33
-34
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4893,50 +4893,36 @@ get_rule_expr(Node *node, deparse_context *context,
48934893
CaseWhen *when = (CaseWhen *) lfirst(temp);
48944894
Node *w = (Node *) when->expr;
48954895

4896-
if (!PRETTY_INDENT(context))
4897-
appendStringInfoChar(buf, ' ');
4898-
appendContextKeyword(context, "WHEN ",
4899-
0, 0, 0);
49004896
if (caseexpr->arg)
49014897
{
49024898
/*
4903-
* The parser should have produced WHEN clauses of the
4904-
* form "CaseTestExpr = RHS"; we want to show just the
4905-
* RHS. If the user wrote something silly like "CASE
4906-
* boolexpr WHEN TRUE THEN ...", then the optimizer's
4907-
* simplify_boolean_equality() may have reduced this
4908-
* to just "CaseTestExpr" or "NOT CaseTestExpr", for
4909-
* which we have to show "TRUE" or "FALSE". We have
4910-
* also to consider the possibility that an implicit
4911-
* coercion was inserted between the CaseTestExpr and
4912-
* the operator.
4899+
* The parser should have produced WHEN clauses of
4900+
* the form "CaseTestExpr = RHS", possibly with an
4901+
* implicit coercion inserted above the CaseTestExpr.
4902+
* For accurate decompilation of rules it's essential
4903+
* that we show just the RHS. However in an
4904+
* expression that's been through the optimizer, the
4905+
* WHEN clause could be almost anything (since the
4906+
* equality operator could have been expanded into an
4907+
* inline function). If we don't recognize the form
4908+
* of the WHEN clause, just punt and display it as-is.
49134909
*/
49144910
if (IsA(w, OpExpr))
49154911
{
49164912
List *args = ((OpExpr *) w)->args;
4917-
Node *rhs;
49184913

4919-
Assert(list_length(args) == 2);
4920-
Assert(IsA(strip_implicit_coercions(linitial(args)),
4921-
CaseTestExpr));
4922-
rhs = (Node *) lsecond(args);
4923-
get_rule_expr(rhs, context, false);
4914+
if (list_length(args) == 2 &&
4915+
IsA(strip_implicit_coercions(linitial(args)),
4916+
CaseTestExpr))
4917+
w = (Node *) lsecond(args);
49244918
}
4925-
else if (IsA(strip_implicit_coercions(w),
4926-
CaseTestExpr))
4927-
appendStringInfo(buf, "TRUE");
4928-
else if (not_clause(w))
4929-
{
4930-
Assert(IsA(strip_implicit_coercions((Node *) get_notclausearg((Expr *) w)),
4931-
CaseTestExpr));
4932-
appendStringInfo(buf, "FALSE");
4933-
}
4934-
else
4935-
elog(ERROR, "unexpected CASE WHEN clause: %d",
4936-
(int) nodeTag(w));
49374919
}
4938-
else
4939-
get_rule_expr(w, context, false);
4920+
4921+
if (!PRETTY_INDENT(context))
4922+
appendStringInfoChar(buf, ' ');
4923+
appendContextKeyword(context, "WHEN ",
4924+
0, 0, 0);
4925+
get_rule_expr(w, context, false);
49404926
appendStringInfo(buf, " THEN ");
49414927
get_rule_expr((Node *) when->result, context, true);
49424928
}
@@ -4952,6 +4938,19 @@ get_rule_expr(Node *node, deparse_context *context,
49524938
}
49534939
break;
49544940

4941+
case T_CaseTestExpr:
4942+
{
4943+
/*
4944+
* Normally we should never get here, since for expressions
4945+
* that can contain this node type we attempt to avoid
4946+
* recursing to it. But in an optimized expression we might
4947+
* be unable to avoid that (see comments for CaseExpr). If we
4948+
* do see one, print it as CASE_TEST_EXPR.
4949+
*/
4950+
appendStringInfo(buf, "CASE_TEST_EXPR");
4951+
}
4952+
break;
4953+
49554954
case T_ArrayExpr:
49564955
{
49574956
ArrayExpr *arrayexpr = (ArrayExpr *) node;

0 commit comments

Comments
 (0)