00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "postgres.h"
00016
00017 #include "catalog/pg_constraint.h"
00018 #include "nodes/makefuncs.h"
00019 #include "nodes/nodeFuncs.h"
00020 #include "optimizer/tlist.h"
00021 #include "parser/parse_agg.h"
00022 #include "parser/parse_clause.h"
00023 #include "parser/parse_expr.h"
00024 #include "parser/parsetree.h"
00025 #include "rewrite/rewriteManip.h"
00026 #include "utils/builtins.h"
00027
00028
00029 typedef struct
00030 {
00031 ParseState *pstate;
00032 int min_varlevel;
00033 int min_agglevel;
00034 int sublevels_up;
00035 } check_agg_arguments_context;
00036
00037 typedef struct
00038 {
00039 ParseState *pstate;
00040 Query *qry;
00041 List *groupClauses;
00042 bool have_non_var_grouping;
00043 List **func_grouped_rels;
00044 int sublevels_up;
00045 } check_ungrouped_columns_context;
00046
00047 static int check_agg_arguments(ParseState *pstate, List *args);
00048 static bool check_agg_arguments_walker(Node *node,
00049 check_agg_arguments_context *context);
00050 static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
00051 List *groupClauses, bool have_non_var_grouping,
00052 List **func_grouped_rels);
00053 static bool check_ungrouped_columns_walker(Node *node,
00054 check_ungrouped_columns_context *context);
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 void
00077 transformAggregateCall(ParseState *pstate, Aggref *agg,
00078 List *args, List *aggorder, bool agg_distinct)
00079 {
00080 List *tlist;
00081 List *torder;
00082 List *tdistinct = NIL;
00083 AttrNumber attno;
00084 int save_next_resno;
00085 int min_varlevel;
00086 ListCell *lc;
00087 const char *err;
00088 bool errkind;
00089
00090
00091
00092
00093
00094 tlist = NIL;
00095 attno = 1;
00096 foreach(lc, args)
00097 {
00098 Expr *arg = (Expr *) lfirst(lc);
00099 TargetEntry *tle = makeTargetEntry(arg, attno++, NULL, false);
00100
00101 tlist = lappend(tlist, tle);
00102 }
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 save_next_resno = pstate->p_next_resno;
00114 pstate->p_next_resno = attno;
00115
00116 torder = transformSortClause(pstate,
00117 aggorder,
00118 &tlist,
00119 EXPR_KIND_ORDER_BY,
00120 true ,
00121 true );
00122
00123
00124
00125
00126 if (agg_distinct)
00127 {
00128 tdistinct = transformDistinctClause(pstate, &tlist, torder, true);
00129
00130
00131
00132
00133
00134 foreach(lc, tdistinct)
00135 {
00136 SortGroupClause *sortcl = (SortGroupClause *) lfirst(lc);
00137
00138 if (!OidIsValid(sortcl->sortop))
00139 {
00140 Node *expr = get_sortgroupclause_expr(sortcl, tlist);
00141
00142 ereport(ERROR,
00143 (errcode(ERRCODE_UNDEFINED_FUNCTION),
00144 errmsg("could not identify an ordering operator for type %s",
00145 format_type_be(exprType(expr))),
00146 errdetail("Aggregates with DISTINCT must be able to sort their inputs."),
00147 parser_errposition(pstate, exprLocation(expr))));
00148 }
00149 }
00150 }
00151
00152
00153 agg->args = tlist;
00154 agg->aggorder = torder;
00155 agg->aggdistinct = tdistinct;
00156
00157 pstate->p_next_resno = save_next_resno;
00158
00159
00160
00161
00162
00163 min_varlevel = check_agg_arguments(pstate, agg->args);
00164 agg->agglevelsup = min_varlevel;
00165
00166
00167 while (min_varlevel-- > 0)
00168 pstate = pstate->parentParseState;
00169 pstate->p_hasAggs = true;
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181 err = NULL;
00182 errkind = false;
00183 switch (pstate->p_expr_kind)
00184 {
00185 case EXPR_KIND_NONE:
00186 Assert(false);
00187 break;
00188 case EXPR_KIND_OTHER:
00189
00190 break;
00191 case EXPR_KIND_JOIN_ON:
00192 case EXPR_KIND_JOIN_USING:
00193 err = _("aggregate functions are not allowed in JOIN conditions");
00194 break;
00195 case EXPR_KIND_FROM_SUBSELECT:
00196
00197 Assert(pstate->p_lateral_active);
00198
00199 err = _("aggregate functions are not allowed in FROM clause of their own query level");
00200 break;
00201 case EXPR_KIND_FROM_FUNCTION:
00202 err = _("aggregate functions are not allowed in functions in FROM");
00203 break;
00204 case EXPR_KIND_WHERE:
00205 errkind = true;
00206 break;
00207 case EXPR_KIND_HAVING:
00208
00209 break;
00210 case EXPR_KIND_WINDOW_PARTITION:
00211
00212 break;
00213 case EXPR_KIND_WINDOW_ORDER:
00214
00215 break;
00216 case EXPR_KIND_WINDOW_FRAME_RANGE:
00217 err = _("aggregate functions are not allowed in window RANGE");
00218 break;
00219 case EXPR_KIND_WINDOW_FRAME_ROWS:
00220 err = _("aggregate functions are not allowed in window ROWS");
00221 break;
00222 case EXPR_KIND_SELECT_TARGET:
00223
00224 break;
00225 case EXPR_KIND_INSERT_TARGET:
00226 case EXPR_KIND_UPDATE_SOURCE:
00227 case EXPR_KIND_UPDATE_TARGET:
00228 errkind = true;
00229 break;
00230 case EXPR_KIND_GROUP_BY:
00231 errkind = true;
00232 break;
00233 case EXPR_KIND_ORDER_BY:
00234
00235 break;
00236 case EXPR_KIND_DISTINCT_ON:
00237
00238 break;
00239 case EXPR_KIND_LIMIT:
00240 case EXPR_KIND_OFFSET:
00241 errkind = true;
00242 break;
00243 case EXPR_KIND_RETURNING:
00244 errkind = true;
00245 break;
00246 case EXPR_KIND_VALUES:
00247 errkind = true;
00248 break;
00249 case EXPR_KIND_CHECK_CONSTRAINT:
00250 case EXPR_KIND_DOMAIN_CHECK:
00251 err = _("aggregate functions are not allowed in check constraints");
00252 break;
00253 case EXPR_KIND_COLUMN_DEFAULT:
00254 case EXPR_KIND_FUNCTION_DEFAULT:
00255 err = _("aggregate functions are not allowed in DEFAULT expressions");
00256 break;
00257 case EXPR_KIND_INDEX_EXPRESSION:
00258 err = _("aggregate functions are not allowed in index expressions");
00259 break;
00260 case EXPR_KIND_INDEX_PREDICATE:
00261 err = _("aggregate functions are not allowed in index predicates");
00262 break;
00263 case EXPR_KIND_ALTER_COL_TRANSFORM:
00264 err = _("aggregate functions are not allowed in transform expressions");
00265 break;
00266 case EXPR_KIND_EXECUTE_PARAMETER:
00267 err = _("aggregate functions are not allowed in EXECUTE parameters");
00268 break;
00269 case EXPR_KIND_TRIGGER_WHEN:
00270 err = _("aggregate functions are not allowed in trigger WHEN conditions");
00271 break;
00272
00273
00274
00275
00276
00277
00278
00279
00280 }
00281 if (err)
00282 ereport(ERROR,
00283 (errcode(ERRCODE_GROUPING_ERROR),
00284 errmsg_internal("%s", err),
00285 parser_errposition(pstate, agg->location)));
00286 if (errkind)
00287 ereport(ERROR,
00288 (errcode(ERRCODE_GROUPING_ERROR),
00289
00290 errmsg("aggregate functions are not allowed in %s",
00291 ParseExprKindName(pstate->p_expr_kind)),
00292 parser_errposition(pstate, agg->location)));
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311 static int
00312 check_agg_arguments(ParseState *pstate, List *args)
00313 {
00314 int agglevel;
00315 check_agg_arguments_context context;
00316
00317 context.pstate = pstate;
00318 context.min_varlevel = -1;
00319 context.min_agglevel = -1;
00320 context.sublevels_up = 0;
00321
00322 (void) expression_tree_walker((Node *) args,
00323 check_agg_arguments_walker,
00324 (void *) &context);
00325
00326
00327
00328
00329
00330 if (context.min_varlevel < 0)
00331 {
00332 if (context.min_agglevel < 0)
00333 return 0;
00334 agglevel = context.min_agglevel;
00335 }
00336 else if (context.min_agglevel < 0)
00337 agglevel = context.min_varlevel;
00338 else
00339 agglevel = Min(context.min_varlevel, context.min_agglevel);
00340
00341
00342
00343
00344 if (agglevel == context.min_agglevel)
00345 ereport(ERROR,
00346 (errcode(ERRCODE_GROUPING_ERROR),
00347 errmsg("aggregate function calls cannot be nested"),
00348 parser_errposition(pstate,
00349 locate_agg_of_level((Node *) args,
00350 agglevel))));
00351
00352 return agglevel;
00353 }
00354
00355 static bool
00356 check_agg_arguments_walker(Node *node,
00357 check_agg_arguments_context *context)
00358 {
00359 if (node == NULL)
00360 return false;
00361 if (IsA(node, Var))
00362 {
00363 int varlevelsup = ((Var *) node)->varlevelsup;
00364
00365
00366 varlevelsup -= context->sublevels_up;
00367
00368 if (varlevelsup >= 0)
00369 {
00370 if (context->min_varlevel < 0 ||
00371 context->min_varlevel > varlevelsup)
00372 context->min_varlevel = varlevelsup;
00373 }
00374 return false;
00375 }
00376 if (IsA(node, Aggref))
00377 {
00378 int agglevelsup = ((Aggref *) node)->agglevelsup;
00379
00380
00381 agglevelsup -= context->sublevels_up;
00382
00383 if (agglevelsup >= 0)
00384 {
00385 if (context->min_agglevel < 0 ||
00386 context->min_agglevel > agglevelsup)
00387 context->min_agglevel = agglevelsup;
00388 }
00389
00390 return false;
00391 }
00392
00393 if (IsA(node, WindowFunc))
00394 ereport(ERROR,
00395 (errcode(ERRCODE_GROUPING_ERROR),
00396 errmsg("aggregate function calls cannot contain window function calls"),
00397 parser_errposition(context->pstate,
00398 ((WindowFunc *) node)->location)));
00399 if (IsA(node, Query))
00400 {
00401
00402 bool result;
00403
00404 context->sublevels_up++;
00405 result = query_tree_walker((Query *) node,
00406 check_agg_arguments_walker,
00407 (void *) context,
00408 0);
00409 context->sublevels_up--;
00410 return result;
00411 }
00412 return expression_tree_walker(node,
00413 check_agg_arguments_walker,
00414 (void *) context);
00415 }
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428 void
00429 transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc,
00430 WindowDef *windef)
00431 {
00432 const char *err;
00433 bool errkind;
00434
00435
00436
00437
00438
00439 if (pstate->p_hasWindowFuncs &&
00440 contain_windowfuncs((Node *) wfunc->args))
00441 ereport(ERROR,
00442 (errcode(ERRCODE_WINDOWING_ERROR),
00443 errmsg("window function calls cannot be nested"),
00444 parser_errposition(pstate,
00445 locate_windowfunc((Node *) wfunc->args))));
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457 err = NULL;
00458 errkind = false;
00459 switch (pstate->p_expr_kind)
00460 {
00461 case EXPR_KIND_NONE:
00462 Assert(false);
00463 break;
00464 case EXPR_KIND_OTHER:
00465
00466 break;
00467 case EXPR_KIND_JOIN_ON:
00468 case EXPR_KIND_JOIN_USING:
00469 err = _("window functions are not allowed in JOIN conditions");
00470 break;
00471 case EXPR_KIND_FROM_SUBSELECT:
00472
00473 errkind = true;
00474 break;
00475 case EXPR_KIND_FROM_FUNCTION:
00476 err = _("window functions are not allowed in functions in FROM");
00477 break;
00478 case EXPR_KIND_WHERE:
00479 errkind = true;
00480 break;
00481 case EXPR_KIND_HAVING:
00482 errkind = true;
00483 break;
00484 case EXPR_KIND_WINDOW_PARTITION:
00485 case EXPR_KIND_WINDOW_ORDER:
00486 case EXPR_KIND_WINDOW_FRAME_RANGE:
00487 case EXPR_KIND_WINDOW_FRAME_ROWS:
00488 err = _("window functions are not allowed in window definitions");
00489 break;
00490 case EXPR_KIND_SELECT_TARGET:
00491
00492 break;
00493 case EXPR_KIND_INSERT_TARGET:
00494 case EXPR_KIND_UPDATE_SOURCE:
00495 case EXPR_KIND_UPDATE_TARGET:
00496 errkind = true;
00497 break;
00498 case EXPR_KIND_GROUP_BY:
00499 errkind = true;
00500 break;
00501 case EXPR_KIND_ORDER_BY:
00502
00503 break;
00504 case EXPR_KIND_DISTINCT_ON:
00505
00506 break;
00507 case EXPR_KIND_LIMIT:
00508 case EXPR_KIND_OFFSET:
00509 errkind = true;
00510 break;
00511 case EXPR_KIND_RETURNING:
00512 errkind = true;
00513 break;
00514 case EXPR_KIND_VALUES:
00515 errkind = true;
00516 break;
00517 case EXPR_KIND_CHECK_CONSTRAINT:
00518 case EXPR_KIND_DOMAIN_CHECK:
00519 err = _("window functions are not allowed in check constraints");
00520 break;
00521 case EXPR_KIND_COLUMN_DEFAULT:
00522 case EXPR_KIND_FUNCTION_DEFAULT:
00523 err = _("window functions are not allowed in DEFAULT expressions");
00524 break;
00525 case EXPR_KIND_INDEX_EXPRESSION:
00526 err = _("window functions are not allowed in index expressions");
00527 break;
00528 case EXPR_KIND_INDEX_PREDICATE:
00529 err = _("window functions are not allowed in index predicates");
00530 break;
00531 case EXPR_KIND_ALTER_COL_TRANSFORM:
00532 err = _("window functions are not allowed in transform expressions");
00533 break;
00534 case EXPR_KIND_EXECUTE_PARAMETER:
00535 err = _("window functions are not allowed in EXECUTE parameters");
00536 break;
00537 case EXPR_KIND_TRIGGER_WHEN:
00538 err = _("window functions are not allowed in trigger WHEN conditions");
00539 break;
00540
00541
00542
00543
00544
00545
00546
00547
00548 }
00549 if (err)
00550 ereport(ERROR,
00551 (errcode(ERRCODE_WINDOWING_ERROR),
00552 errmsg_internal("%s", err),
00553 parser_errposition(pstate, wfunc->location)));
00554 if (errkind)
00555 ereport(ERROR,
00556 (errcode(ERRCODE_WINDOWING_ERROR),
00557
00558 errmsg("window functions are not allowed in %s",
00559 ParseExprKindName(pstate->p_expr_kind)),
00560 parser_errposition(pstate, wfunc->location)));
00561
00562
00563
00564
00565
00566
00567
00568 if (windef->name)
00569 {
00570 Index winref = 0;
00571 ListCell *lc;
00572
00573 Assert(windef->refname == NULL &&
00574 windef->partitionClause == NIL &&
00575 windef->orderClause == NIL &&
00576 windef->frameOptions == FRAMEOPTION_DEFAULTS);
00577
00578 foreach(lc, pstate->p_windowdefs)
00579 {
00580 WindowDef *refwin = (WindowDef *) lfirst(lc);
00581
00582 winref++;
00583 if (refwin->name && strcmp(refwin->name, windef->name) == 0)
00584 {
00585 wfunc->winref = winref;
00586 break;
00587 }
00588 }
00589 if (lc == NULL)
00590 ereport(ERROR,
00591 (errcode(ERRCODE_UNDEFINED_OBJECT),
00592 errmsg("window \"%s\" does not exist", windef->name),
00593 parser_errposition(pstate, windef->location)));
00594 }
00595 else
00596 {
00597 Index winref = 0;
00598 ListCell *lc;
00599
00600 foreach(lc, pstate->p_windowdefs)
00601 {
00602 WindowDef *refwin = (WindowDef *) lfirst(lc);
00603
00604 winref++;
00605 if (refwin->refname && windef->refname &&
00606 strcmp(refwin->refname, windef->refname) == 0)
00607 ;
00608 else if (!refwin->refname && !windef->refname)
00609 ;
00610 else
00611 continue;
00612 if (equal(refwin->partitionClause, windef->partitionClause) &&
00613 equal(refwin->orderClause, windef->orderClause) &&
00614 refwin->frameOptions == windef->frameOptions &&
00615 equal(refwin->startOffset, windef->startOffset) &&
00616 equal(refwin->endOffset, windef->endOffset))
00617 {
00618
00619 wfunc->winref = winref;
00620 break;
00621 }
00622 }
00623 if (lc == NULL)
00624 {
00625 pstate->p_windowdefs = lappend(pstate->p_windowdefs, windef);
00626 wfunc->winref = list_length(pstate->p_windowdefs);
00627 }
00628 }
00629
00630 pstate->p_hasWindowFuncs = true;
00631 }
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645 void
00646 parseCheckAggregates(ParseState *pstate, Query *qry)
00647 {
00648 List *groupClauses = NIL;
00649 bool have_non_var_grouping;
00650 List *func_grouped_rels = NIL;
00651 ListCell *l;
00652 bool hasJoinRTEs;
00653 bool hasSelfRefRTEs;
00654 PlannerInfo *root;
00655 Node *clause;
00656
00657
00658 Assert(pstate->p_hasAggs || qry->groupClause || qry->havingQual);
00659
00660
00661
00662
00663
00664 hasJoinRTEs = hasSelfRefRTEs = false;
00665 foreach(l, pstate->p_rtable)
00666 {
00667 RangeTblEntry *rte = (RangeTblEntry *) lfirst(l);
00668
00669 if (rte->rtekind == RTE_JOIN)
00670 hasJoinRTEs = true;
00671 else if (rte->rtekind == RTE_CTE && rte->self_reference)
00672 hasSelfRefRTEs = true;
00673 }
00674
00675
00676
00677
00678
00679 foreach(l, qry->groupClause)
00680 {
00681 SortGroupClause *grpcl = (SortGroupClause *) lfirst(l);
00682 Node *expr;
00683
00684 expr = get_sortgroupclause_expr(grpcl, qry->targetList);
00685 if (expr == NULL)
00686 continue;
00687 groupClauses = lcons(expr, groupClauses);
00688 }
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698 if (hasJoinRTEs)
00699 {
00700 root = makeNode(PlannerInfo);
00701 root->parse = qry;
00702 root->planner_cxt = CurrentMemoryContext;
00703 root->hasJoinRTEs = true;
00704
00705 groupClauses = (List *) flatten_join_alias_vars(root,
00706 (Node *) groupClauses);
00707 }
00708 else
00709 root = NULL;
00710
00711
00712
00713
00714
00715
00716 have_non_var_grouping = false;
00717 foreach(l, groupClauses)
00718 {
00719 if (!IsA((Node *) lfirst(l), Var))
00720 {
00721 have_non_var_grouping = true;
00722 break;
00723 }
00724 }
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734 clause = (Node *) qry->targetList;
00735 if (hasJoinRTEs)
00736 clause = flatten_join_alias_vars(root, clause);
00737 check_ungrouped_columns(clause, pstate, qry,
00738 groupClauses, have_non_var_grouping,
00739 &func_grouped_rels);
00740
00741 clause = (Node *) qry->havingQual;
00742 if (hasJoinRTEs)
00743 clause = flatten_join_alias_vars(root, clause);
00744 check_ungrouped_columns(clause, pstate, qry,
00745 groupClauses, have_non_var_grouping,
00746 &func_grouped_rels);
00747
00748
00749
00750
00751 if (pstate->p_hasAggs && hasSelfRefRTEs)
00752 ereport(ERROR,
00753 (errcode(ERRCODE_INVALID_RECURSION),
00754 errmsg("aggregate functions are not allowed in a recursive query's recursive term"),
00755 parser_errposition(pstate,
00756 locate_agg_of_level((Node *) qry, 0))));
00757 }
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780 static void
00781 check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry,
00782 List *groupClauses, bool have_non_var_grouping,
00783 List **func_grouped_rels)
00784 {
00785 check_ungrouped_columns_context context;
00786
00787 context.pstate = pstate;
00788 context.qry = qry;
00789 context.groupClauses = groupClauses;
00790 context.have_non_var_grouping = have_non_var_grouping;
00791 context.func_grouped_rels = func_grouped_rels;
00792 context.sublevels_up = 0;
00793 check_ungrouped_columns_walker(node, &context);
00794 }
00795
00796 static bool
00797 check_ungrouped_columns_walker(Node *node,
00798 check_ungrouped_columns_context *context)
00799 {
00800 ListCell *gl;
00801
00802 if (node == NULL)
00803 return false;
00804 if (IsA(node, Const) ||
00805 IsA(node, Param))
00806 return false;
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 if (IsA(node, Aggref) &&
00817 (int) ((Aggref *) node)->agglevelsup >= context->sublevels_up)
00818 return false;
00819
00820
00821
00822
00823
00824
00825
00826
00827 if (context->have_non_var_grouping && context->sublevels_up == 0)
00828 {
00829 foreach(gl, context->groupClauses)
00830 {
00831 if (equal(node, lfirst(gl)))
00832 return false;
00833 }
00834 }
00835
00836
00837
00838
00839
00840
00841
00842 if (IsA(node, Var))
00843 {
00844 Var *var = (Var *) node;
00845 RangeTblEntry *rte;
00846 char *attname;
00847
00848 if (var->varlevelsup != context->sublevels_up)
00849 return false;
00850
00851
00852
00853
00854 if (!context->have_non_var_grouping || context->sublevels_up != 0)
00855 {
00856 foreach(gl, context->groupClauses)
00857 {
00858 Var *gvar = (Var *) lfirst(gl);
00859
00860 if (IsA(gvar, Var) &&
00861 gvar->varno == var->varno &&
00862 gvar->varattno == var->varattno &&
00863 gvar->varlevelsup == 0)
00864 return false;
00865 }
00866 }
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884 if (list_member_int(*context->func_grouped_rels, var->varno))
00885 return false;
00886
00887 Assert(var->varno > 0 &&
00888 (int) var->varno <= list_length(context->pstate->p_rtable));
00889 rte = rt_fetch(var->varno, context->pstate->p_rtable);
00890 if (rte->rtekind == RTE_RELATION)
00891 {
00892 if (check_functional_grouping(rte->relid,
00893 var->varno,
00894 0,
00895 context->groupClauses,
00896 &context->qry->constraintDeps))
00897 {
00898 *context->func_grouped_rels =
00899 lappend_int(*context->func_grouped_rels, var->varno);
00900 return false;
00901 }
00902 }
00903
00904
00905 attname = get_rte_attribute_name(rte, var->varattno);
00906 if (context->sublevels_up == 0)
00907 ereport(ERROR,
00908 (errcode(ERRCODE_GROUPING_ERROR),
00909 errmsg("column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
00910 rte->eref->aliasname, attname),
00911 parser_errposition(context->pstate, var->location)));
00912 else
00913 ereport(ERROR,
00914 (errcode(ERRCODE_GROUPING_ERROR),
00915 errmsg("subquery uses ungrouped column \"%s.%s\" from outer query",
00916 rte->eref->aliasname, attname),
00917 parser_errposition(context->pstate, var->location)));
00918 }
00919
00920 if (IsA(node, Query))
00921 {
00922
00923 bool result;
00924
00925 context->sublevels_up++;
00926 result = query_tree_walker((Query *) node,
00927 check_ungrouped_columns_walker,
00928 (void *) context,
00929 0);
00930 context->sublevels_up--;
00931 return result;
00932 }
00933 return expression_tree_walker(node, check_ungrouped_columns_walker,
00934 (void *) context);
00935 }
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956 void
00957 build_aggregate_fnexprs(Oid *agg_input_types,
00958 int agg_num_inputs,
00959 Oid agg_state_type,
00960 Oid agg_result_type,
00961 Oid agg_input_collation,
00962 Oid transfn_oid,
00963 Oid finalfn_oid,
00964 Expr **transfnexpr,
00965 Expr **finalfnexpr)
00966 {
00967 Param *argp;
00968 List *args;
00969 int i;
00970
00971
00972
00973
00974
00975
00976
00977 argp = makeNode(Param);
00978 argp->paramkind = PARAM_EXEC;
00979 argp->paramid = -1;
00980 argp->paramtype = agg_state_type;
00981 argp->paramtypmod = -1;
00982 argp->paramcollid = agg_input_collation;
00983 argp->location = -1;
00984
00985 args = list_make1(argp);
00986
00987 for (i = 0; i < agg_num_inputs; i++)
00988 {
00989 argp = makeNode(Param);
00990 argp->paramkind = PARAM_EXEC;
00991 argp->paramid = -1;
00992 argp->paramtype = agg_input_types[i];
00993 argp->paramtypmod = -1;
00994 argp->paramcollid = agg_input_collation;
00995 argp->location = -1;
00996 args = lappend(args, argp);
00997 }
00998
00999 *transfnexpr = (Expr *) makeFuncExpr(transfn_oid,
01000 agg_state_type,
01001 args,
01002 InvalidOid,
01003 agg_input_collation,
01004 COERCE_EXPLICIT_CALL);
01005
01006
01007 if (!OidIsValid(finalfn_oid))
01008 {
01009 *finalfnexpr = NULL;
01010 return;
01011 }
01012
01013
01014
01015
01016 argp = makeNode(Param);
01017 argp->paramkind = PARAM_EXEC;
01018 argp->paramid = -1;
01019 argp->paramtype = agg_state_type;
01020 argp->paramtypmod = -1;
01021 argp->paramcollid = agg_input_collation;
01022 argp->location = -1;
01023 args = list_make1(argp);
01024
01025 *finalfnexpr = (Expr *) makeFuncExpr(finalfn_oid,
01026 agg_result_type,
01027 args,
01028 InvalidOid,
01029 agg_input_collation,
01030 COERCE_EXPLICIT_CALL);
01031 }