65 List *groupClauses,
List *groupClauseCommonVars,
67 bool have_non_var_grouping,
68 List **func_grouped_rels);
72 List *groupClauses,
bool hasJoinRTEs,
73 bool have_non_var_grouping);
120 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
130 Assert(numDirectArgs >= 0);
141 forboth(lc, aargs, lc2, aggorder)
152 torder, tlist, sortby);
205 foreach(lc, tdistinct)
214 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
215 errmsg(
"could not identify an ordering operator for type %s",
217 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
252 agg->aggargtypes = argtypes;
274 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
275 errmsg(
"GROUPING must have fewer than 32 arguments"),
280 Node *current_result;
286 result_list =
lappend(result_list, current_result);
289 result->args = result_list;
294 return (
Node *) result;
324 p_levelsup = &agg->agglevelsup;
344 *p_levelsup = min_varlevel;
347 while (min_varlevel-- > 0)
378 err =
_(
"aggregate functions are not allowed in JOIN conditions");
380 err =
_(
"grouping operations are not allowed in JOIN conditions");
390 err =
_(
"aggregate functions are not allowed in FROM clause of their own query level");
392 err =
_(
"grouping operations are not allowed in FROM clause of their own query level");
397 err =
_(
"aggregate functions are not allowed in functions in FROM");
399 err =
_(
"grouping operations are not allowed in functions in FROM");
407 err =
_(
"aggregate functions are not allowed in policy expressions");
409 err =
_(
"grouping operations are not allowed in policy expressions");
426 err =
_(
"aggregate functions are not allowed in window RANGE");
428 err =
_(
"grouping operations are not allowed in window RANGE");
433 err =
_(
"aggregate functions are not allowed in window ROWS");
435 err =
_(
"grouping operations are not allowed in window ROWS");
440 err =
_(
"aggregate functions are not allowed in window GROUPS");
442 err =
_(
"grouping operations are not allowed in window GROUPS");
455 err =
_(
"aggregate functions are not allowed in MERGE WHEN conditions");
457 err =
_(
"grouping operations are not allowed in MERGE WHEN conditions");
484 err =
_(
"aggregate functions are not allowed in check constraints");
486 err =
_(
"grouping operations are not allowed in check constraints");
493 err =
_(
"aggregate functions are not allowed in DEFAULT expressions");
495 err =
_(
"grouping operations are not allowed in DEFAULT expressions");
500 err =
_(
"aggregate functions are not allowed in index expressions");
502 err =
_(
"grouping operations are not allowed in index expressions");
507 err =
_(
"aggregate functions are not allowed in index predicates");
509 err =
_(
"grouping operations are not allowed in index predicates");
514 err =
_(
"aggregate functions are not allowed in statistics expressions");
516 err =
_(
"grouping operations are not allowed in statistics expressions");
521 err =
_(
"aggregate functions are not allowed in transform expressions");
523 err =
_(
"grouping operations are not allowed in transform expressions");
528 err =
_(
"aggregate functions are not allowed in EXECUTE parameters");
530 err =
_(
"grouping operations are not allowed in EXECUTE parameters");
535 err =
_(
"aggregate functions are not allowed in trigger WHEN conditions");
537 err =
_(
"grouping operations are not allowed in trigger WHEN conditions");
542 err =
_(
"aggregate functions are not allowed in partition bound");
544 err =
_(
"grouping operations are not allowed in partition bound");
549 err =
_(
"aggregate functions are not allowed in partition key expressions");
551 err =
_(
"grouping operations are not allowed in partition key expressions");
557 err =
_(
"aggregate functions are not allowed in column generation expressions");
559 err =
_(
"grouping operations are not allowed in column generation expressions");
565 err =
_(
"aggregate functions are not allowed in CALL arguments");
567 err =
_(
"grouping operations are not allowed in CALL arguments");
573 err =
_(
"aggregate functions are not allowed in COPY FROM WHERE conditions");
575 err =
_(
"grouping operations are not allowed in COPY FROM WHERE conditions");
594 (
errcode(ERRCODE_GROUPING_ERROR),
602 err =
_(
"aggregate functions are not allowed in %s");
605 err =
_(
"grouping operations are not allowed in %s");
608 (
errcode(ERRCODE_GROUPING_ERROR),
666 agglevel =
context.min_agglevel;
668 else if (
context.min_agglevel < 0)
669 agglevel =
context.min_varlevel;
676 if (agglevel ==
context.min_agglevel)
684 (
errcode(ERRCODE_GROUPING_ERROR),
685 errmsg(
"aggregate function calls cannot be nested"),
705 (
errcode(ERRCODE_GROUPING_ERROR),
706 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
710 if (
context.min_agglevel >= 0 &&
context.min_agglevel <= agglevel)
712 (
errcode(ERRCODE_GROUPING_ERROR),
713 errmsg(
"aggregate function calls cannot be nested"),
729 int varlevelsup = ((
Var *) node)->varlevelsup;
732 varlevelsup -=
context->sublevels_up;
734 if (varlevelsup >= 0)
736 if (
context->min_varlevel < 0 ||
737 context->min_varlevel > varlevelsup)
738 context->min_varlevel = varlevelsup;
744 int agglevelsup = ((
Aggref *) node)->agglevelsup;
747 agglevelsup -=
context->sublevels_up;
749 if (agglevelsup >= 0)
751 if (
context->min_agglevel < 0 ||
752 context->min_agglevel > agglevelsup)
753 context->min_agglevel = agglevelsup;
762 agglevelsup -=
context->sublevels_up;
764 if (agglevelsup >= 0)
766 if (
context->min_agglevel < 0 ||
767 context->min_agglevel > agglevelsup)
768 context->min_agglevel = agglevelsup;
778 if (
context->sublevels_up == 0)
783 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
784 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
785 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
789 (
errcode(ERRCODE_GROUPING_ERROR),
790 errmsg(
"aggregate function calls cannot contain window function calls"),
842 (
errcode(ERRCODE_WINDOWING_ERROR),
843 errmsg(
"window function calls cannot be nested"),
869 err =
_(
"window functions are not allowed in JOIN conditions");
876 err =
_(
"window functions are not allowed in functions in FROM");
882 err =
_(
"window functions are not allowed in policy expressions");
895 err =
_(
"window functions are not allowed in window definitions");
906 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
931 err =
_(
"window functions are not allowed in check constraints");
935 err =
_(
"window functions are not allowed in DEFAULT expressions");
938 err =
_(
"window functions are not allowed in index expressions");
941 err =
_(
"window functions are not allowed in statistics expressions");
944 err =
_(
"window functions are not allowed in index predicates");
947 err =
_(
"window functions are not allowed in transform expressions");
950 err =
_(
"window functions are not allowed in EXECUTE parameters");
953 err =
_(
"window functions are not allowed in trigger WHEN conditions");
956 err =
_(
"window functions are not allowed in partition bound");
959 err =
_(
"window functions are not allowed in partition key expressions");
962 err =
_(
"window functions are not allowed in CALL arguments");
965 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
968 err =
_(
"window functions are not allowed in column generation expressions");
984 (
errcode(ERRCODE_WINDOWING_ERROR),
989 (
errcode(ERRCODE_WINDOWING_ERROR),
991 errmsg(
"window functions are not allowed in %s",
1016 if (refwin->
name && strcmp(refwin->
name, windef->
name) == 0)
1024 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1025 errmsg(
"window \"%s\" does not exist", windef->
name),
1089 List *groupClauseCommonVars =
NIL;
1090 bool have_non_var_grouping;
1091 List *func_grouped_rels =
NIL;
1094 bool hasSelfRefRTEs;
1114 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
1115 errmsg(
"too many grouping sets present (maximum 4096)"),
1151 hasJoinRTEs = hasSelfRefRTEs =
false;
1159 hasSelfRefRTEs =
true;
1178 groupClauses =
lappend(groupClauses, expr);
1189 (
Node *) groupClauses);
1200 have_non_var_grouping =
false;
1201 foreach(l, groupClauses)
1207 have_non_var_grouping =
true;
1212 groupClauseCommonVars =
lappend(groupClauseCommonVars, tle->
expr);
1228 qry->hasGroupRTE =
true;
1246 groupClauses, hasJoinRTEs,
1247 have_non_var_grouping);
1252 groupClauses, groupClauseCommonVars,
1254 have_non_var_grouping,
1255 &func_grouped_rels);
1259 groupClauses, hasJoinRTEs,
1260 have_non_var_grouping);
1265 groupClauses, groupClauseCommonVars,
1267 have_non_var_grouping,
1268 &func_grouped_rels);
1273 if (pstate->
p_hasAggs && hasSelfRefRTEs)
1275 (
errcode(ERRCODE_INVALID_RECURSION),
1276 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1306 List *groupClauses,
List *groupClauseCommonVars,
1308 bool have_non_var_grouping,
1309 List **func_grouped_rels)
1316 context.groupClauses = groupClauses;
1317 context.groupClauseCommonVars = groupClauseCommonVars;
1318 context.gset_common = gset_common;
1319 context.have_non_var_grouping = have_non_var_grouping;
1320 context.func_grouped_rels = func_grouped_rels;
1322 context.in_agg_direct_args =
false;
1339 if ((
int) agg->agglevelsup ==
context->sublevels_up)
1353 context->in_agg_direct_args =
true;
1357 context->in_agg_direct_args =
false;
1358 return (
Node *) agg;
1367 if ((
int) agg->agglevelsup >
context->sublevels_up)
1388 if (
context->have_non_var_grouping &&
context->sublevels_up == 0)
1392 foreach(gl,
context->groupClauses)
1435 if (!
context->have_non_var_grouping ||
context->sublevels_up != 0)
1439 foreach(gl,
context->groupClauses)
1485 context->groupClauseCommonVars,
1486 &
context->qry->constraintDeps))
1496 if (
context->sublevels_up == 0)
1498 (
errcode(ERRCODE_GROUPING_ERROR),
1499 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1500 rte->eref->aliasname,
attname),
1502 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1506 (
errcode(ERRCODE_GROUPING_ERROR),
1507 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1508 rte->eref->aliasname,
attname),
1523 return (
Node *) newnode;
1542 List *groupClauses,
bool hasJoinRTEs,
1543 bool have_non_var_grouping)
1549 context.hasJoinRTEs = hasJoinRTEs;
1550 context.groupClauses = groupClauses;
1553 context.have_non_var_grouping = have_non_var_grouping;
1554 context.func_grouped_rels = NULL;
1556 context.in_agg_direct_args =
false;
1576 if ((
int) agg->agglevelsup ==
context->sublevels_up)
1587 context->in_agg_direct_args =
true;
1590 context->in_agg_direct_args =
false;
1600 if ((
int) agg->agglevelsup >
context->sublevels_up)
1618 foreach(lc, grp->args)
1638 foreach(gl,
context->groupClauses)
1654 else if (
context->have_non_var_grouping &&
1657 foreach(gl,
context->groupClauses)
1671 (
errcode(ERRCODE_GROUPING_ERROR),
1672 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1679 grp->refs = ref_list;
1727 if (
context->qry->groupingSets &&
1729 var->varnullingrels =
1768 while (curgroup_size > 0)
1771 int i = curgroup_size;
1773 foreach(lc, rollup_val)
1787 result =
lappend(result, current_result);
1803 Assert(number_bits < 31);
1805 num_sets = (1U << number_bits);
1807 for (
i = 0;
i < num_sets;
i++)
1813 foreach(lc, cube_list)
1826 result =
lappend(result, current_result);
1901 if (groupingSets ==
NIL)
1904 foreach(lc, groupingSets)
1915 if (limit >= 0 && numsets > limit)
1918 expanded_groups =
lappend(expanded_groups, current_result);
1938 foreach(lc2, result)
1945 new_result =
lappend(new_result,
1949 result = new_result;
1961 foreach(cell, result)
1999 int numArguments = 0;
2004 foreach(lc, aggref->aggargtypes)
2009 return numArguments;
2029 if (IsPolymorphicType(aggtranstype))
2032 Oid *declaredArgTypes;
2041 Assert(agg_nargs <= numArguments);
2048 pfree(declaredArgTypes);
2050 return aggtranstype;
2063 foreach(lc, aggref->
args)
2076 if (!pt->typbyval &&
2119 int agg_num_direct_inputs,
2122 Oid agg_input_collation,
2126 Expr **invtransfnexpr)
2137 for (
i = agg_num_direct_inputs;
i < agg_num_inputs;
i++)
2147 agg_input_collation,
2149 fexpr->funcvariadic = agg_variadic;
2150 *transfnexpr = (
Expr *) fexpr;
2155 if (invtransfnexpr != NULL)
2163 agg_input_collation,
2165 fexpr->funcvariadic = agg_variadic;
2166 *invtransfnexpr = (
Expr *) fexpr;
2169 *invtransfnexpr = NULL;
2179 Expr **serialfnexpr)
2193 *serialfnexpr = (
Expr *) fexpr;
2202 Expr **deserialfnexpr)
2217 *deserialfnexpr = (
Expr *) fexpr;
2226 int num_finalfn_inputs,
2228 Oid agg_result_type,
2229 Oid agg_input_collation,
2242 for (
i = 0;
i < num_finalfn_inputs - 1;
i++)
2252 agg_input_collation,
2272 argp->paramtypmod = -1;
2273 argp->paramcollid = argcollation;
2275 return (
Node *) argp;
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define Assert(condition)
#define OidIsValid(objectId)
static void PGresult * res
int errmsg_internal(const char *fmt,...)
int errdetail(const char *fmt,...)
int errhint(const char *fmt,...)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
void err(int eval, const char *fmt,...)
#define HeapTupleIsValid(tuple)
static int pg_cmp_s32(int32 a, int32 b)
if(TABLE==NULL||TABLE_index==NULL)
void list_sort(List *list, list_sort_comparator cmp)
List * list_truncate(List *list, int new_size)
List * lappend(List *list, void *datum)
List * list_intersection_int(const List *list1, const List *list2)
List * lappend_int(List *list, int datum)
List * lappend_oid(List *list, Oid datum)
bool list_member_int(const List *list, int datum)
List * list_copy_tail(const List *oldlist, int nskip)
int list_int_cmp(const ListCell *p1, const ListCell *p2)
List * list_concat(List *list1, const List *list2)
List * list_union_int(const List *list1, const List *list2)
Oid get_func_signature(Oid funcid, Oid **argtypes, int *nargs)
Datum lca(PG_FUNCTION_ARGS)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
void pfree(void *pointer)
Oid exprType(const Node *expr)
int exprLocation(const Node *expr)
#define expression_tree_mutator(n, m, c)
#define query_tree_walker(q, w, c, f)
#define expression_tree_walker(n, w, c)
#define query_tree_mutator(q, m, c, f)
#define IsA(nodeptr, _type_)
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
static int check_agg_arguments(ParseState *pstate, List *directargs, List *args, Expr *filter)
static void check_agglevels_and_constraints(ParseState *pstate, Node *expr)
static Var * buildGroupedVar(int attnum, Index ressortgroupref, substitute_grouped_columns_context *context)
void build_aggregate_finalfn_expr(Oid *agg_input_types, int num_finalfn_inputs, Oid agg_state_type, Oid agg_result_type, Oid agg_input_collation, Oid finalfn_oid, Expr **finalfnexpr)
static Node * make_agg_arg(Oid argtype, Oid argcollation)
static void finalize_grouping_exprs(Node *node, ParseState *pstate, Query *qry, List *groupClauses, bool hasJoinRTEs, bool have_non_var_grouping)
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
Oid resolve_aggregate_transtype(Oid aggfuncid, Oid aggtranstype, Oid *inputTypes, int numArguments)
void build_aggregate_deserialfn_expr(Oid deserialfn_oid, Expr **deserialfnexpr)
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
static bool check_agg_arguments_walker(Node *node, check_agg_arguments_context *context)
static Node * substitute_grouped_columns_mutator(Node *node, substitute_grouped_columns_context *context)
void parseCheckAggregates(ParseState *pstate, Query *qry)
static int cmp_list_len_asc(const ListCell *a, const ListCell *b)
void build_aggregate_transfn_expr(Oid *agg_input_types, int agg_num_inputs, int agg_num_direct_inputs, bool agg_variadic, Oid agg_state_type, Oid agg_input_collation, Oid transfn_oid, Oid invtransfn_oid, Expr **transfnexpr, Expr **invtransfnexpr)
static bool finalize_grouping_exprs_walker(Node *node, substitute_grouped_columns_context *context)
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
static int cmp_list_len_contents_asc(const ListCell *a, const ListCell *b)
static List * expand_groupingset_node(GroupingSet *gs)
static Node * substitute_grouped_columns(Node *node, ParseState *pstate, Query *qry, List *groupClauses, List *groupClauseCommonVars, List *gset_common, bool have_non_var_grouping, List **func_grouped_rels)
bool agg_args_support_sendreceive(Aggref *aggref)
int get_aggregate_argtypes(Aggref *aggref, Oid *inputTypes)
void build_aggregate_serialfn_expr(Oid serialfn_oid, Expr **serialfnexpr)
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
List * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
List * transformDistinctClause(ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
Oid enforce_generic_type_consistency(const Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
Node * transformExpr(ParseState *pstate, Node *expr, ParseExprKind exprKind)
const char * ParseExprKindName(ParseExprKind exprKind)
int parser_errposition(ParseState *pstate, int location)
@ EXPR_KIND_EXECUTE_PARAMETER
@ EXPR_KIND_COLUMN_DEFAULT
@ EXPR_KIND_STATS_EXPRESSION
@ EXPR_KIND_INDEX_EXPRESSION
@ EXPR_KIND_MERGE_RETURNING
@ EXPR_KIND_PARTITION_BOUND
@ EXPR_KIND_FUNCTION_DEFAULT
@ EXPR_KIND_WINDOW_FRAME_RANGE
@ EXPR_KIND_FROM_SUBSELECT
@ EXPR_KIND_WINDOW_FRAME_GROUPS
@ EXPR_KIND_PARTITION_EXPRESSION
@ EXPR_KIND_INDEX_PREDICATE
@ EXPR_KIND_INSERT_TARGET
@ EXPR_KIND_ALTER_COL_TRANSFORM
@ EXPR_KIND_UPDATE_TARGET
@ EXPR_KIND_SELECT_TARGET
@ EXPR_KIND_GENERATED_COLUMN
@ EXPR_KIND_CALL_ARGUMENT
@ EXPR_KIND_FROM_FUNCTION
@ EXPR_KIND_UPDATE_SOURCE
@ EXPR_KIND_CHECK_CONSTRAINT
@ EXPR_KIND_WINDOW_PARTITION
@ EXPR_KIND_WINDOW_FRAME_ROWS
@ EXPR_KIND_VALUES_SINGLE
ParseNamespaceItem * addRangeTableEntryForGroup(ParseState *pstate, List *groupClauses)
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
#define FRAMEOPTION_DEFAULTS
#define rt_fetch(rangetable_index, rangetable)
bool check_functional_grouping(Oid relid, Index varno, Index varlevelsup, List *grouping_columns, List **constraintDeps)
static int list_length(const List *l)
#define forboth(cell1, list1, cell2, list2)
#define foreach_delete_current(lst, var_or_cell)
#define for_each_from(cell, lst, N)
#define list_make2(x1, x2)
FormData_pg_type * Form_pg_type
static Datum ObjectIdGetDatum(Oid X)
bool contain_windowfuncs(Node *node)
int locate_agg_of_level(Node *node, int levelsup)
int locate_windowfunc(Node *node)
ParseNamespaceColumn * p_nscolumns
ParseState * parentParseState
ParseNamespaceItem * p_grouping_nsitem
ParseExprKind p_expr_kind
List ** func_grouped_rels
List * groupClauseCommonVars
bool have_non_var_grouping
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Node * flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
int locate_var_of_level(Node *node, int levelsup)