Skip to content

Commit 9de426b

Browse files
author
Nikita Glukhov
committed
Add function formats
1 parent 13fe2c1 commit 9de426b

File tree

8 files changed

+76
-6
lines changed

8 files changed

+76
-6
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

+6
Original file line numberDiff line numberDiff line change
@@ -2481,11 +2481,13 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
24812481
Aggref *expr = (Aggref *) node;
24822482

24832483
APP_JUMB(expr->aggfnoid);
2484+
APP_JUMB(expr->aggformat);
24842485
JumbleExpr(jstate, (Node *) expr->aggdirectargs);
24852486
JumbleExpr(jstate, (Node *) expr->args);
24862487
JumbleExpr(jstate, (Node *) expr->aggorder);
24872488
JumbleExpr(jstate, (Node *) expr->aggdistinct);
24882489
JumbleExpr(jstate, (Node *) expr->aggfilter);
2490+
JumbleExpr(jstate, (Node *) expr->aggformatopts);
24892491
}
24902492
break;
24912493
case T_GroupingFunc:
@@ -2501,8 +2503,10 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
25012503

25022504
APP_JUMB(expr->winfnoid);
25032505
APP_JUMB(expr->winref);
2506+
APP_JUMB(expr->winformat);
25042507
JumbleExpr(jstate, (Node *) expr->args);
25052508
JumbleExpr(jstate, (Node *) expr->aggfilter);
2509+
JumbleExpr(jstate, (Node *) expr->winformatopts);
25062510
}
25072511
break;
25082512
case T_ArrayRef:
@@ -2520,7 +2524,9 @@ JumbleExpr(pgssJumbleState *jstate, Node *node)
25202524
FuncExpr *expr = (FuncExpr *) node;
25212525

25222526
APP_JUMB(expr->funcid);
2527+
APP_JUMB(expr->funcformat2);
25232528
JumbleExpr(jstate, (Node *) expr->args);
2529+
JumbleExpr(jstate, (Node *) expr->funcformatopts);
25242530
}
25252531
break;
25262532
case T_NamedArgExpr:

src/backend/nodes/copyfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,8 @@ _copyAggref(const Aggref *from)
14411441
COPY_SCALAR_FIELD(aggkind);
14421442
COPY_SCALAR_FIELD(agglevelsup);
14431443
COPY_SCALAR_FIELD(aggsplit);
1444+
COPY_SCALAR_FIELD(aggformat);
1445+
COPY_NODE_FIELD(aggformatopts);
14441446
COPY_LOCATION_FIELD(location);
14451447

14461448
return newnode;
@@ -1480,6 +1482,8 @@ _copyWindowFunc(const WindowFunc *from)
14801482
COPY_SCALAR_FIELD(winref);
14811483
COPY_SCALAR_FIELD(winstar);
14821484
COPY_SCALAR_FIELD(winagg);
1485+
COPY_SCALAR_FIELD(winformat);
1486+
COPY_NODE_FIELD(winformatopts);
14831487
COPY_LOCATION_FIELD(location);
14841488

14851489
return newnode;
@@ -1521,6 +1525,8 @@ _copyFuncExpr(const FuncExpr *from)
15211525
COPY_SCALAR_FIELD(funccollid);
15221526
COPY_SCALAR_FIELD(inputcollid);
15231527
COPY_NODE_FIELD(args);
1528+
COPY_SCALAR_FIELD(funcformat2);
1529+
COPY_NODE_FIELD(funcformatopts);
15241530
COPY_LOCATION_FIELD(location);
15251531

15261532
return newnode;

src/backend/nodes/equalfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ _equalAggref(const Aggref *a, const Aggref *b)
226226
COMPARE_SCALAR_FIELD(aggkind);
227227
COMPARE_SCALAR_FIELD(agglevelsup);
228228
COMPARE_SCALAR_FIELD(aggsplit);
229+
COMPARE_SCALAR_FIELD(aggformat);
230+
COMPARE_NODE_FIELD(aggformatopts);
229231
COMPARE_LOCATION_FIELD(location);
230232

231233
return true;
@@ -258,6 +260,8 @@ _equalWindowFunc(const WindowFunc *a, const WindowFunc *b)
258260
COMPARE_SCALAR_FIELD(winref);
259261
COMPARE_SCALAR_FIELD(winstar);
260262
COMPARE_SCALAR_FIELD(winagg);
263+
COMPARE_SCALAR_FIELD(winformat);
264+
COMPARE_NODE_FIELD(winformatopts);
261265
COMPARE_LOCATION_FIELD(location);
262266

263267
return true;
@@ -289,6 +293,8 @@ _equalFuncExpr(const FuncExpr *a, const FuncExpr *b)
289293
COMPARE_SCALAR_FIELD(funccollid);
290294
COMPARE_SCALAR_FIELD(inputcollid);
291295
COMPARE_NODE_FIELD(args);
296+
COMPARE_SCALAR_FIELD(funcformat2);
297+
COMPARE_NODE_FIELD(funcformatopts);
292298
COMPARE_LOCATION_FIELD(location);
293299

294300
return true;

src/backend/nodes/outfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,8 @@ _outAggref(StringInfo str, const Aggref *node)
12191219
WRITE_CHAR_FIELD(aggkind);
12201220
WRITE_UINT_FIELD(agglevelsup);
12211221
WRITE_ENUM_FIELD(aggsplit, AggSplit);
1222+
WRITE_ENUM_FIELD(aggformat, FuncFormat);
1223+
WRITE_NODE_FIELD(aggformatopts);
12221224
WRITE_LOCATION_FIELD(location);
12231225
}
12241226

@@ -1248,6 +1250,8 @@ _outWindowFunc(StringInfo str, const WindowFunc *node)
12481250
WRITE_UINT_FIELD(winref);
12491251
WRITE_BOOL_FIELD(winstar);
12501252
WRITE_BOOL_FIELD(winagg);
1253+
WRITE_ENUM_FIELD(winformat, FuncFormat);
1254+
WRITE_NODE_FIELD(winformatopts);
12511255
WRITE_LOCATION_FIELD(location);
12521256
}
12531257

@@ -1279,6 +1283,8 @@ _outFuncExpr(StringInfo str, const FuncExpr *node)
12791283
WRITE_OID_FIELD(funccollid);
12801284
WRITE_OID_FIELD(inputcollid);
12811285
WRITE_NODE_FIELD(args);
1286+
WRITE_ENUM_FIELD(funcformat2, FuncFormat);
1287+
WRITE_NODE_FIELD(funcformatopts);
12821288
WRITE_LOCATION_FIELD(location);
12831289
}
12841290

src/backend/nodes/readfuncs.c

+6
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,8 @@ _readAggref(void)
611611
READ_CHAR_FIELD(aggkind);
612612
READ_UINT_FIELD(agglevelsup);
613613
READ_ENUM_FIELD(aggsplit, AggSplit);
614+
READ_ENUM_FIELD(aggformat, FuncFormat);
615+
READ_NODE_FIELD(aggformatopts);
614616
READ_LOCATION_FIELD(location);
615617

616618
READ_DONE();
@@ -650,6 +652,8 @@ _readWindowFunc(void)
650652
READ_UINT_FIELD(winref);
651653
READ_BOOL_FIELD(winstar);
652654
READ_BOOL_FIELD(winagg);
655+
READ_ENUM_FIELD(winformat, FuncFormat);
656+
READ_NODE_FIELD(winformatopts);
653657
READ_LOCATION_FIELD(location);
654658

655659
READ_DONE();
@@ -691,6 +695,8 @@ _readFuncExpr(void)
691695
READ_OID_FIELD(funccollid);
692696
READ_OID_FIELD(inputcollid);
693697
READ_NODE_FIELD(args);
698+
READ_ENUM_FIELD(funcformat2, FuncFormat);
699+
READ_NODE_FIELD(funcformatopts);
694700
READ_LOCATION_FIELD(location);
695701

696702
READ_DONE();

src/backend/optimizer/util/clauses.c

+4
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,8 @@ eval_const_expressions_mutator(Node *node,
26922692
newexpr->winref = expr->winref;
26932693
newexpr->winstar = expr->winstar;
26942694
newexpr->winagg = expr->winagg;
2695+
newexpr->winformat = expr->winformat;
2696+
newexpr->winformatopts = copyObject(expr->winformatopts);
26952697
newexpr->location = expr->location;
26962698

26972699
return (Node *) newexpr;
@@ -2738,6 +2740,8 @@ eval_const_expressions_mutator(Node *node,
27382740
newexpr->funccollid = expr->funccollid;
27392741
newexpr->inputcollid = expr->inputcollid;
27402742
newexpr->args = args;
2743+
newexpr->funcformat2 = expr->funcformat2;
2744+
newexpr->funcformatopts = copyObject(expr->funcformatopts);
27412745
newexpr->location = expr->location;
27422746
return (Node *) newexpr;
27432747
}

src/backend/utils/adt/ruleutils.c

+31-6
Original file line numberDiff line numberDiff line change
@@ -9045,6 +9045,16 @@ get_oper_expr(OpExpr *expr, deparse_context *context)
90459045
appendStringInfoChar(buf, ')');
90469046
}
90479047

9048+
static void
9049+
get_func_opts(FuncFormat aggformat, Node *aggformatopts, deparse_context *context)
9050+
{
9051+
switch (aggformat)
9052+
{
9053+
default:
9054+
break;
9055+
}
9056+
}
9057+
90489058
/*
90499059
* get_func_expr - Parse back a FuncExpr node
90509060
*/
@@ -9059,6 +9069,7 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
90599069
List *argnames;
90609070
bool use_variadic;
90619071
ListCell *l;
9072+
const char *funcname;
90629073

90639074
/*
90649075
* If the function call came from an implicit coercion, then just show the
@@ -9113,12 +9124,19 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
91139124
nargs++;
91149125
}
91159126

9116-
appendStringInfo(buf, "%s(",
9117-
generate_function_name(funcoid, nargs,
9118-
argnames, argtypes,
9119-
expr->funcvariadic,
9120-
&use_variadic,
9121-
context->special_exprkind));
9127+
switch (expr->funcformat2)
9128+
{
9129+
default:
9130+
funcname = generate_function_name(funcoid, nargs,
9131+
argnames, argtypes,
9132+
expr->funcvariadic,
9133+
&use_variadic,
9134+
context->special_exprkind);
9135+
break;
9136+
}
9137+
9138+
appendStringInfo(buf, "%s(", funcname);
9139+
91229140
nargs = 0;
91239141
foreach(l, expr->args)
91249142
{
@@ -9128,6 +9146,9 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
91289146
appendStringInfoString(buf, "VARIADIC ");
91299147
get_rule_expr((Node *) lfirst(l), context, true);
91309148
}
9149+
9150+
get_func_opts(expr->funcformat2, expr->funcformatopts, context);
9151+
91319152
appendStringInfoChar(buf, ')');
91329153
}
91339154

@@ -9226,6 +9247,8 @@ get_agg_expr(Aggref *aggref, deparse_context *context,
92269247
}
92279248
}
92289249

9250+
get_func_opts(aggref->aggformat, aggref->aggformatopts, context);
9251+
92299252
if (aggref->aggfilter != NULL)
92309253
{
92319254
appendStringInfoString(buf, ") FILTER (WHERE ");
@@ -9292,6 +9315,8 @@ get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
92929315
else
92939316
get_rule_expr((Node *) wfunc->args, context, true);
92949317

9318+
get_func_opts(wfunc->winformat, wfunc->winformatopts, context);
9319+
92959320
if (wfunc->aggfilter != NULL)
92969321
{
92979322
appendStringInfoString(buf, ") FILTER (WHERE ");

src/include/nodes/primnodes.h

+11
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ typedef struct Param
252252
int location; /* token location, or -1 if unknown */
253253
} Param;
254254

255+
typedef enum FuncFormat
256+
{
257+
FUNCFMT_REGULAR = 0,
258+
} FuncFormat;
259+
255260
/*
256261
* Aggref
257262
*
@@ -311,6 +316,8 @@ typedef struct Aggref
311316
char aggkind; /* aggregate kind (see pg_aggregate.h) */
312317
Index agglevelsup; /* > 0 if agg belongs to outer query */
313318
AggSplit aggsplit; /* expected agg-splitting mode of parent Agg */
319+
FuncFormat aggformat; /* how to display this aggregate */
320+
Node *aggformatopts; /* display options, if any */
314321
int location; /* token location, or -1 if unknown */
315322
} Aggref;
316323

@@ -364,6 +371,8 @@ typedef struct WindowFunc
364371
Index winref; /* index of associated WindowClause */
365372
bool winstar; /* true if argument list was really '*' */
366373
bool winagg; /* is function a simple aggregate? */
374+
FuncFormat winformat; /* how to display this window function */
375+
Node *winformatopts; /* display options, if any */
367376
int location; /* token location, or -1 if unknown */
368377
} WindowFunc;
369378

@@ -459,6 +468,8 @@ typedef struct FuncExpr
459468
Oid funccollid; /* OID of collation of result */
460469
Oid inputcollid; /* OID of collation that function should use */
461470
List *args; /* arguments to the function */
471+
FuncFormat funcformat2; /* how to display this function call */
472+
Node *funcformatopts; /* display options, if any */
462473
int location; /* token location, or -1 if unknown */
463474
} FuncExpr;
464475

0 commit comments

Comments
 (0)