pg_get_viewdef 7.4 et al - Mailing list pgsql-hackers

From Andreas Pflug
Subject pg_get_viewdef 7.4 et al
Date
Msg-id 3E92CC0E.9020200@web.de
Whole thread Raw
Responses Re: pg_get_viewdef 7.4 et al  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
OK,
here's my proposal for a more economical use of parentheses.

- implicit casted variables are not put in parentheses    
(myVar)::integer  -> myVar::integer, no chance to misinterpret this
- explicit CASTs are represented as CAST(... AS ...) for better 
distinction from implicit ones
- a join's ON expression will not have parentheses automatically  JOIN 
xxx ON (a.oid=b.relid) -> JOIN xx ON a.oid=b.relid
- JOIN clauses are only in parens if the right expression of a JOIN is a 
JOIN itself. For the left fork, the usual left-to-right order is 
evaluated by default, so no parentheses are needed.
- Operators will have their operands plain if they are simple. If 
T_OpExpr or T_BoolExpr operands are complex themselves, the operands are 
put in parens(a+b) -> a+b,  a+b*c -> a+(b*c)

This strategy greatly improves readability.

Regards,
Andreas


===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/backend/utils/adt/ruleutils.c,v
retrieving revision 1.137
diff -r1.137 ruleutils.c
2126a2127,2147> /*>  * get_rule_expr_paren>  * checks if the expression is of same kind as <type>>  * if so,
parenthesesare needed.>  * currently only used for T_OpExpr and T_BoolExpr>  */> static void get_rule_expr_paren(Node
*node,deparse_context *context, 
 
bool showimplicit, NodeTag type)> {>     StringInfo    buf = context->buf;>>     bool need_paren = (nodeTag(node) ==
type);>    if (need_paren)>         appendStringInfoChar(buf, '(');>>     get_rule_expr(node, context, showimplicit);>>
   if (need_paren)>         appendStringInfoChar(buf, ')');> }>>
 
2293,2294c2314,2315
<                         appendStringInfoChar(buf, '(');
<                         get_rule_expr((Node *) lfirst(args), context, 
false);
--->                         get_rule_expr_paren((Node *) lfirst(args), 
context,>                                     false, T_BoolExpr);
2297,2299c2318,2320
<                             appendStringInfo(buf, " AND ");
<                             get_rule_expr((Node *) lfirst(args), context,
<                                           false);
--->                                 appendStringInfo(buf, " AND ");>
get_rule_expr_paren((Node*) 
 
lfirst(args), context,>                                         false, T_BoolExpr);
2301d2321
<                         appendStringInfoChar(buf, ')');
2305,2306c2325,2326
<                         appendStringInfoChar(buf, '(');
<                         get_rule_expr((Node *) lfirst(args), context, 
false);
--->                         get_rule_expr_paren((Node *) lfirst(args), 
context,>                                     false, T_BoolExpr);
2310,2311c2330,2331
<                             get_rule_expr((Node *) lfirst(args), context,
<                                           false);
--->                             get_rule_expr_paren((Node *) 
lfirst(args), context,>                                         false, T_BoolExpr);
2313d2332
<                         appendStringInfoChar(buf, ')');
2317,2319c2336,2338
<                         appendStringInfo(buf, "(NOT ");
<                         get_rule_expr((Node *) lfirst(args), context, 
false);
<                         appendStringInfoChar(buf, ')');
--->                         appendStringInfo(buf, "NOT ");>                         get_rule_expr_paren((Node *)
lfirst(args),
 
context,>                                   false, T_BoolExpr);
2394d2412
<                     appendStringInfoChar(buf, '(');
2396c2414
<                     appendStringInfo(buf, ")::%s",
--->                     appendStringInfo(buf, "::%s",
2536d2553
<                     appendStringInfoChar(buf, '(');
2538c2555
<                     appendStringInfo(buf, ")::%s",
--->                     appendStringInfo(buf, "::%s",
2566d2582
<     appendStringInfoChar(buf, '(');
2573c2589
<         get_rule_expr(arg1, context, true);
--->         get_rule_expr_paren(arg1, context, true, T_OpExpr);
2578c2594
<         get_rule_expr(arg2, context, true);
--->         get_rule_expr_paren(arg2, context, true, T_OpExpr);
2585a2602,2604>         bool need_paren = (nodeTag(arg) == T_OpExpr);>         if (need_paren)>
appendStringInfoChar(buf,'(');
 
2597,2599c2616,2618
<                                  generate_operator_name(opno,
<                                                         InvalidOid,
<                                                         exprType(arg)));
--->                          generate_operator_name(opno,>                                     InvalidOid,>
                        exprType(arg)));
 
2605,2607c2624,2626
<                                  generate_operator_name(opno,
<                                                         exprType(arg),
<                                                         InvalidOid));
--->                          generate_operator_name(opno,>                                     exprType(arg),>
                           InvalidOid));
 
2612a2632,2633>         if (need_paren)>             appendStringInfoChar(buf, ')');
2614d2634
<     appendStringInfoChar(buf, ')');
2663c2683
<         appendStringInfoChar(buf, '(');
--->         appendStringInfo(buf, "CAST(");
2665c2685
<         appendStringInfo(buf, ")::%s",
--->         appendStringInfo(buf, " AS %s)",
3112a3133>         bool need_paren = !IsA(j->rarg, RangeTblRef);
3114d3134
<         appendStringInfoChar(buf, '(');
3141a3162,3165>>         if (need_paren)>             appendStringInfoChar(buf, '(');>
3142a3167,3170>>         if (need_paren)>             appendStringInfoChar(buf, ')');>
3161c3189
<                 appendStringInfo(buf, " ON (");
--->                 appendStringInfo(buf, " ON ");
3163d3190
<                 appendStringInfoChar(buf, ')');
3166,3167c3193
<         appendStringInfoChar(buf, ')');
<         /* Yes, it's correct to put alias after the right paren ... */
--->



pgsql-hackers by date:

Previous
From: "Ron Peacetree"
Date:
Subject: Re: Anyone working on better transaction locking?
Next
From: "Ron Peacetree"
Date:
Subject: Re: No merge sort?