Skip to content

Commit 4b10271

Browse files
committed
Change get_rule_expr so that when the input is a List, it displays the
list elements comma-separated instead of barfing. This allows elimination of half a dozen redundant copies of that behavior, and also makes the world safe again for pg_get_expr() applied to pg_index.indexprs, per gripe from Alexander Zhiltsov.
1 parent 05c561e commit 4b10271

File tree

1 file changed

+30
-70
lines changed

1 file changed

+30
-70
lines changed

src/backend/utils/adt/ruleutils.c

Lines changed: 30 additions & 70 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.181 2004/09/13 20:07:13 tgl Exp $
6+
* $PostgreSQL: pgsql/src/backend/utils/adt/ruleutils.c,v 1.182 2004/10/07 20:36:52 tgl Exp $
77
*
88
* This software is copyrighted by Jan Wieck - Hamburg.
99
*
@@ -2351,15 +2351,7 @@ get_insert_query_def(Query *query, deparse_context *context)
23512351
{
23522352
appendContextKeyword(context, "VALUES (",
23532353
-PRETTYINDENT_STD, PRETTYINDENT_STD, 2);
2354-
sep = "";
2355-
foreach(l, strippedexprs)
2356-
{
2357-
Node *expr = lfirst(l);
2358-
2359-
appendStringInfo(buf, sep);
2360-
sep = ", ";
2361-
get_rule_expr(expr, context, false);
2362-
}
2354+
get_rule_expr((Node *) strippedexprs, context, false);
23632355
appendStringInfoChar(buf, ')');
23642356
}
23652357
else
@@ -2938,7 +2930,9 @@ get_rule_expr(Node *node, deparse_context *context,
29382930
/*
29392931
* Each level of get_rule_expr must emit an indivisible term
29402932
* (parenthesized if necessary) to ensure result is reparsed into the
2941-
* same expression tree.
2933+
* same expression tree. The only exception is that when the input
2934+
* is a List, we emit the component items comma-separated with no
2935+
* surrounding decoration; this is convenient for most callers.
29422936
*
29432937
* There might be some work left here to support additional node types.
29442938
*/
@@ -3269,20 +3263,10 @@ get_rule_expr(Node *node, deparse_context *context,
32693263
case T_ArrayExpr:
32703264
{
32713265
ArrayExpr *arrayexpr = (ArrayExpr *) node;
3272-
ListCell *element;
3273-
char *sep;
32743266

32753267
appendStringInfo(buf, "ARRAY[");
3276-
sep = "";
3277-
foreach(element, arrayexpr->elements)
3278-
{
3279-
Node *e = (Node *) lfirst(element);
3280-
3281-
appendStringInfo(buf, sep);
3282-
get_rule_expr(e, context, true);
3283-
sep = ", ";
3284-
}
3285-
appendStringInfo(buf, "]");
3268+
get_rule_expr((Node *) arrayexpr->elements, context, true);
3269+
appendStringInfoChar(buf, ']');
32863270
}
32873271
break;
32883272

@@ -3348,40 +3332,20 @@ get_rule_expr(Node *node, deparse_context *context,
33483332
case T_CoalesceExpr:
33493333
{
33503334
CoalesceExpr *coalesceexpr = (CoalesceExpr *) node;
3351-
ListCell *arg;
3352-
char *sep;
33533335

33543336
appendStringInfo(buf, "COALESCE(");
3355-
sep = "";
3356-
foreach(arg, coalesceexpr->args)
3357-
{
3358-
Node *e = (Node *) lfirst(arg);
3359-
3360-
appendStringInfo(buf, sep);
3361-
get_rule_expr(e, context, true);
3362-
sep = ", ";
3363-
}
3364-
appendStringInfo(buf, ")");
3337+
get_rule_expr((Node *) coalesceexpr->args, context, true);
3338+
appendStringInfoChar(buf, ')');
33653339
}
33663340
break;
33673341

33683342
case T_NullIfExpr:
33693343
{
33703344
NullIfExpr *nullifexpr = (NullIfExpr *) node;
3371-
ListCell *arg;
3372-
char *sep;
33733345

33743346
appendStringInfo(buf, "NULLIF(");
3375-
sep = "";
3376-
foreach(arg, nullifexpr->args)
3377-
{
3378-
Node *e = (Node *) lfirst(arg);
3379-
3380-
appendStringInfo(buf, sep);
3381-
get_rule_expr(e, context, true);
3382-
sep = ", ";
3383-
}
3384-
appendStringInfo(buf, ")");
3347+
get_rule_expr((Node *) nullifexpr->args, context, true);
3348+
appendStringInfoChar(buf, ')');
33853349
}
33863350
break;
33873351

@@ -3478,6 +3442,21 @@ get_rule_expr(Node *node, deparse_context *context,
34783442
appendStringInfo(buf, "DEFAULT");
34793443
break;
34803444

3445+
case T_List:
3446+
{
3447+
char *sep;
3448+
ListCell *l;
3449+
3450+
sep = "";
3451+
foreach(l, (List *) node)
3452+
{
3453+
appendStringInfo(buf, sep);
3454+
get_rule_expr((Node *) lfirst(l), context, showimplicit);
3455+
sep = ", ";
3456+
}
3457+
}
3458+
break;
3459+
34813460
default:
34823461
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
34833462
break;
@@ -3560,7 +3539,6 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
35603539
Oid argtypes[FUNC_MAX_ARGS];
35613540
int nargs;
35623541
ListCell *l;
3563-
char *sep;
35643542

35653543
/*
35663544
* If the function call came from an implicit coercion, then just show
@@ -3613,14 +3591,7 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
36133591

36143592
appendStringInfo(buf, "%s(",
36153593
generate_function_name(funcoid, nargs, argtypes));
3616-
3617-
sep = "";
3618-
foreach(l, expr->args)
3619-
{
3620-
appendStringInfo(buf, sep);
3621-
sep = ", ";
3622-
get_rule_expr((Node *) lfirst(l), context, true);
3623-
}
3594+
get_rule_expr((Node *) expr->args, context, true);
36243595
appendStringInfoChar(buf, ')');
36253596
}
36263597

@@ -3787,8 +3758,6 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
37873758
{
37883759
StringInfo buf = context->buf;
37893760
Query *query = (Query *) (sublink->subselect);
3790-
ListCell *l;
3791-
char *sep;
37923761
bool need_paren;
37933762

37943763
if (sublink->subLinkType == ARRAY_SUBLINK)
@@ -3801,19 +3770,10 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
38013770
need_paren = (list_length(sublink->lefthand) > 1);
38023771
if (need_paren)
38033772
appendStringInfoChar(buf, '(');
3804-
3805-
sep = "";
3806-
foreach(l, sublink->lefthand)
3807-
{
3808-
appendStringInfo(buf, sep);
3809-
sep = ", ";
3810-
get_rule_expr((Node *) lfirst(l), context, true);
3811-
}
3812-
3773+
get_rule_expr((Node *) sublink->lefthand, context, true);
38133774
if (need_paren)
3814-
appendStringInfo(buf, ") ");
3815-
else
3816-
appendStringInfoChar(buf, ' ');
3775+
appendStringInfoChar(buf, ')');
3776+
appendStringInfoChar(buf, ' ');
38173777
}
38183778

38193779
need_paren = true;

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