Skip to content

Commit 315ef82

Browse files
author
Nikita Glukhov
committed
Add function formats
1 parent f9767d4 commit 315ef82

File tree

8 files changed

+76
-6
lines changed

8 files changed

+76
-6
lines changed

contrib/pg_stat_statements/pg_stat_statements.c

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,6 +1432,8 @@ _copyAggref(const Aggref *from)
14321432
COPY_SCALAR_FIELD(aggkind);
14331433
COPY_SCALAR_FIELD(agglevelsup);
14341434
COPY_SCALAR_FIELD(aggsplit);
1435+
COPY_SCALAR_FIELD(aggformat);
1436+
COPY_NODE_FIELD(aggformatopts);
14351437
COPY_LOCATION_FIELD(location);
14361438

14371439
return newnode;
@@ -1471,6 +1473,8 @@ _copyWindowFunc(const WindowFunc *from)
14711473
COPY_SCALAR_FIELD(winref);
14721474
COPY_SCALAR_FIELD(winstar);
14731475
COPY_SCALAR_FIELD(winagg);
1476+
COPY_SCALAR_FIELD(winformat);
1477+
COPY_NODE_FIELD(winformatopts);
14741478
COPY_LOCATION_FIELD(location);
14751479

14761480
return newnode;
@@ -1512,6 +1516,8 @@ _copyFuncExpr(const FuncExpr *from)
15121516
COPY_SCALAR_FIELD(funccollid);
15131517
COPY_SCALAR_FIELD(inputcollid);
15141518
COPY_NODE_FIELD(args);
1519+
COPY_SCALAR_FIELD(funcformat2);
1520+
COPY_NODE_FIELD(funcformatopts);
15151521
COPY_LOCATION_FIELD(location);
15161522

15171523
return newnode;

src/backend/nodes/equalfuncs.c

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,8 @@ _outAggref(StringInfo str, const Aggref *node)
12071207
WRITE_CHAR_FIELD(aggkind);
12081208
WRITE_UINT_FIELD(agglevelsup);
12091209
WRITE_ENUM_FIELD(aggsplit, AggSplit);
1210+
WRITE_ENUM_FIELD(aggformat, FuncFormat);
1211+
WRITE_NODE_FIELD(aggformatopts);
12101212
WRITE_LOCATION_FIELD(location);
12111213
}
12121214

@@ -1236,6 +1238,8 @@ _outWindowFunc(StringInfo str, const WindowFunc *node)
12361238
WRITE_UINT_FIELD(winref);
12371239
WRITE_BOOL_FIELD(winstar);
12381240
WRITE_BOOL_FIELD(winagg);
1241+
WRITE_ENUM_FIELD(winformat, FuncFormat);
1242+
WRITE_NODE_FIELD(winformatopts);
12391243
WRITE_LOCATION_FIELD(location);
12401244
}
12411245

@@ -1267,6 +1271,8 @@ _outFuncExpr(StringInfo str, const FuncExpr *node)
12671271
WRITE_OID_FIELD(funccollid);
12681272
WRITE_OID_FIELD(inputcollid);
12691273
WRITE_NODE_FIELD(args);
1274+
WRITE_ENUM_FIELD(funcformat2, FuncFormat);
1275+
WRITE_NODE_FIELD(funcformatopts);
12701276
WRITE_LOCATION_FIELD(location);
12711277
}
12721278

src/backend/nodes/readfuncs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ _readAggref(void)
600600
READ_CHAR_FIELD(aggkind);
601601
READ_UINT_FIELD(agglevelsup);
602602
READ_ENUM_FIELD(aggsplit, AggSplit);
603+
READ_ENUM_FIELD(aggformat, FuncFormat);
604+
READ_NODE_FIELD(aggformatopts);
603605
READ_LOCATION_FIELD(location);
604606

605607
READ_DONE();
@@ -639,6 +641,8 @@ _readWindowFunc(void)
639641
READ_UINT_FIELD(winref);
640642
READ_BOOL_FIELD(winstar);
641643
READ_BOOL_FIELD(winagg);
644+
READ_ENUM_FIELD(winformat, FuncFormat);
645+
READ_NODE_FIELD(winformatopts);
642646
READ_LOCATION_FIELD(location);
643647

644648
READ_DONE();
@@ -680,6 +684,8 @@ _readFuncExpr(void)
680684
READ_OID_FIELD(funccollid);
681685
READ_OID_FIELD(inputcollid);
682686
READ_NODE_FIELD(args);
687+
READ_ENUM_FIELD(funcformat2, FuncFormat);
688+
READ_NODE_FIELD(funcformatopts);
683689
READ_LOCATION_FIELD(location);
684690

685691
READ_DONE();

src/backend/optimizer/util/clauses.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2653,6 +2653,8 @@ eval_const_expressions_mutator(Node *node,
26532653
newexpr->winref = expr->winref;
26542654
newexpr->winstar = expr->winstar;
26552655
newexpr->winagg = expr->winagg;
2656+
newexpr->winformat = expr->winformat;
2657+
newexpr->winformatopts = copyObject(expr->winformatopts);
26562658
newexpr->location = expr->location;
26572659

26582660
return (Node *) newexpr;
@@ -2699,6 +2701,8 @@ eval_const_expressions_mutator(Node *node,
26992701
newexpr->funccollid = expr->funccollid;
27002702
newexpr->inputcollid = expr->inputcollid;
27012703
newexpr->args = args;
2704+
newexpr->funcformat2 = expr->funcformat2;
2705+
newexpr->funcformatopts = copyObject(expr->funcformatopts);
27022706
newexpr->location = expr->location;
27032707
return (Node *) newexpr;
27042708
}

src/backend/utils/adt/ruleutils.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8974,6 +8974,16 @@ get_oper_expr(OpExpr *expr, deparse_context *context)
89748974
appendStringInfoChar(buf, ')');
89758975
}
89768976

8977+
static void
8978+
get_func_opts(FuncFormat aggformat, Node *aggformatopts, deparse_context *context)
8979+
{
8980+
switch (aggformat)
8981+
{
8982+
default:
8983+
break;
8984+
}
8985+
}
8986+
89778987
/*
89788988
* get_func_expr - Parse back a FuncExpr node
89798989
*/
@@ -8988,6 +8998,7 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
89888998
List *argnames;
89898999
bool use_variadic;
89909000
ListCell *l;
9001+
const char *funcname;
89919002

89929003
/*
89939004
* If the function call came from an implicit coercion, then just show the
@@ -9042,12 +9053,19 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
90429053
nargs++;
90439054
}
90449055

9045-
appendStringInfo(buf, "%s(",
9046-
generate_function_name(funcoid, nargs,
9047-
argnames, argtypes,
9048-
expr->funcvariadic,
9049-
&use_variadic,
9050-
context->special_exprkind));
9056+
switch (expr->funcformat2)
9057+
{
9058+
default:
9059+
funcname = generate_function_name(funcoid, nargs,
9060+
argnames, argtypes,
9061+
expr->funcvariadic,
9062+
&use_variadic,
9063+
context->special_exprkind);
9064+
break;
9065+
}
9066+
9067+
appendStringInfo(buf, "%s(", funcname);
9068+
90519069
nargs = 0;
90529070
foreach(l, expr->args)
90539071
{
@@ -9057,6 +9075,9 @@ get_func_expr(FuncExpr *expr, deparse_context *context,
90579075
appendStringInfoString(buf, "VARIADIC ");
90589076
get_rule_expr((Node *) lfirst(l), context, true);
90599077
}
9078+
9079+
get_func_opts(expr->funcformat2, expr->funcformatopts, context);
9080+
90609081
appendStringInfoChar(buf, ')');
90619082
}
90629083

@@ -9155,6 +9176,8 @@ get_agg_expr(Aggref *aggref, deparse_context *context,
91559176
}
91569177
}
91579178

9179+
get_func_opts(aggref->aggformat, aggref->aggformatopts, context);
9180+
91589181
if (aggref->aggfilter != NULL)
91599182
{
91609183
appendStringInfoString(buf, ") FILTER (WHERE ");
@@ -9221,6 +9244,8 @@ get_windowfunc_expr(WindowFunc *wfunc, deparse_context *context)
92219244
else
92229245
get_rule_expr((Node *) wfunc->args, context, true);
92239246

9247+
get_func_opts(wfunc->winformat, wfunc->winformatopts, context);
9248+
92249249
if (wfunc->aggfilter != NULL)
92259250
{
92269251
appendStringInfoString(buf, ") FILTER (WHERE ");

src/include/nodes/primnodes.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,11 @@ typedef struct Param
249249
int location; /* token location, or -1 if unknown */
250250
} Param;
251251

252+
typedef enum FuncFormat
253+
{
254+
FUNCFMT_REGULAR = 0,
255+
} FuncFormat;
256+
252257
/*
253258
* Aggref
254259
*
@@ -308,6 +313,8 @@ typedef struct Aggref
308313
char aggkind; /* aggregate kind (see pg_aggregate.h) */
309314
Index agglevelsup; /* > 0 if agg belongs to outer query */
310315
AggSplit aggsplit; /* expected agg-splitting mode of parent Agg */
316+
FuncFormat aggformat; /* how to display this aggregate */
317+
Node *aggformatopts; /* display options, if any */
311318
int location; /* token location, or -1 if unknown */
312319
} Aggref;
313320

@@ -361,6 +368,8 @@ typedef struct WindowFunc
361368
Index winref; /* index of associated WindowClause */
362369
bool winstar; /* true if argument list was really '*' */
363370
bool winagg; /* is function a simple aggregate? */
371+
FuncFormat winformat; /* how to display this window function */
372+
Node *winformatopts; /* display options, if any */
364373
int location; /* token location, or -1 if unknown */
365374
} WindowFunc;
366375

@@ -456,6 +465,8 @@ typedef struct FuncExpr
456465
Oid funccollid; /* OID of collation of result */
457466
Oid inputcollid; /* OID of collation that function should use */
458467
List *args; /* arguments to the function */
468+
FuncFormat funcformat2; /* how to display this function call */
469+
Node *funcformatopts; /* display options, if any */
459470
int location; /* token location, or -1 if unknown */
460471
} FuncExpr;
461472

0 commit comments

Comments
 (0)