69 List *groupClauses,
List *groupClauseCommonVars,
71 bool have_non_var_grouping,
72 List **func_grouped_rels);
76 List *groupClauses,
bool hasJoinRTEs,
77 bool have_non_var_grouping);
114 List *args,
List *aggorder,
bool agg_distinct)
124 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
134 Assert(numDirectArgs >= 0);
145 forboth(lc, aargs, lc2, aggorder)
156 torder, tlist, sortby);
209 foreach(lc, tdistinct)
218 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
219 errmsg(
"could not identify an ordering operator for type %s",
221 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
256 agg->aggargtypes = argtypes;
272 List *args = p->args;
278 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
279 errmsg(
"GROUPING must have fewer than 32 arguments"),
284 Node *current_result;
290 result_list =
lappend(result_list, current_result);
293 result->args = result_list;
328 p_levelsup = &agg->agglevelsup;
349 *p_levelsup = min_varlevel;
352 while (min_varlevel-- > 0)
383 err =
_(
"aggregate functions are not allowed in JOIN conditions");
385 err =
_(
"grouping operations are not allowed in JOIN conditions");
395 err =
_(
"aggregate functions are not allowed in FROM clause of their own query level");
397 err =
_(
"grouping operations are not allowed in FROM clause of their own query level");
402 err =
_(
"aggregate functions are not allowed in functions in FROM");
404 err =
_(
"grouping operations are not allowed in functions in FROM");
412 err =
_(
"aggregate functions are not allowed in policy expressions");
414 err =
_(
"grouping operations are not allowed in policy expressions");
431 err =
_(
"aggregate functions are not allowed in window RANGE");
433 err =
_(
"grouping operations are not allowed in window RANGE");
438 err =
_(
"aggregate functions are not allowed in window ROWS");
440 err =
_(
"grouping operations are not allowed in window ROWS");
445 err =
_(
"aggregate functions are not allowed in window GROUPS");
447 err =
_(
"grouping operations are not allowed in window GROUPS");
460 err =
_(
"aggregate functions are not allowed in MERGE WHEN conditions");
462 err =
_(
"grouping operations are not allowed in MERGE WHEN conditions");
489 err =
_(
"aggregate functions are not allowed in check constraints");
491 err =
_(
"grouping operations are not allowed in check constraints");
498 err =
_(
"aggregate functions are not allowed in DEFAULT expressions");
500 err =
_(
"grouping operations are not allowed in DEFAULT expressions");
505 err =
_(
"aggregate functions are not allowed in index expressions");
507 err =
_(
"grouping operations are not allowed in index expressions");
512 err =
_(
"aggregate functions are not allowed in index predicates");
514 err =
_(
"grouping operations are not allowed in index predicates");
519 err =
_(
"aggregate functions are not allowed in statistics expressions");
521 err =
_(
"grouping operations are not allowed in statistics expressions");
526 err =
_(
"aggregate functions are not allowed in transform expressions");
528 err =
_(
"grouping operations are not allowed in transform expressions");
533 err =
_(
"aggregate functions are not allowed in EXECUTE parameters");
535 err =
_(
"grouping operations are not allowed in EXECUTE parameters");
540 err =
_(
"aggregate functions are not allowed in trigger WHEN conditions");
542 err =
_(
"grouping operations are not allowed in trigger WHEN conditions");
547 err =
_(
"aggregate functions are not allowed in partition bound");
549 err =
_(
"grouping operations are not allowed in partition bound");
554 err =
_(
"aggregate functions are not allowed in partition key expressions");
556 err =
_(
"grouping operations are not allowed in partition key expressions");
562 err =
_(
"aggregate functions are not allowed in column generation expressions");
564 err =
_(
"grouping operations are not allowed in column generation expressions");
570 err =
_(
"aggregate functions are not allowed in CALL arguments");
572 err =
_(
"grouping operations are not allowed in CALL arguments");
578 err =
_(
"aggregate functions are not allowed in COPY FROM WHERE conditions");
580 err =
_(
"grouping operations are not allowed in COPY FROM WHERE conditions");
589 err =
_(
"aggregate functions are not allowed in FOR PORTION OF expressions");
591 err =
_(
"grouping operations are not allowed in FOR PORTION OF expressions");
597 err =
_(
"aggregate functions are not allowed in property definition expressions");
599 err =
_(
"grouping operations are not allowed in property definition expressions");
614 (
errcode(ERRCODE_GROUPING_ERROR),
622 err =
_(
"aggregate functions are not allowed in %s");
625 err =
_(
"grouping operations are not allowed in %s");
628 (
errcode(ERRCODE_GROUPING_ERROR),
707 (
errcode(ERRCODE_GROUPING_ERROR),
708 errmsg(
"aggregate function calls cannot be nested"),
720 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
721 errmsg(
"outer-level aggregate cannot use a nested CTE"),
722 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
723 context.
min_cte->eref->aliasname),
743 (
errcode(ERRCODE_GROUPING_ERROR),
744 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
750 (
errcode(ERRCODE_GROUPING_ERROR),
751 errmsg(
"aggregate function calls cannot be nested"),
757 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
758 errmsg(
"outer-level aggregate cannot use a nested CTE"),
759 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
760 context.
min_cte->eref->aliasname),
774 int varlevelsup = ((
Var *) node)->varlevelsup;
779 if (varlevelsup >= 0)
789 int agglevelsup = ((
Aggref *) node)->agglevelsup;
794 if (agglevelsup >= 0)
809 if (agglevelsup >= 0)
828 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
829 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
830 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
834 (
errcode(ERRCODE_GROUPING_ERROR),
835 errmsg(
"aggregate function calls cannot contain window function calls"),
851 if (ctelevelsup >= 0)
911 (
errcode(ERRCODE_WINDOWING_ERROR),
912 errmsg(
"window function calls cannot be nested"),
938 err =
_(
"window functions are not allowed in JOIN conditions");
945 err =
_(
"window functions are not allowed in functions in FROM");
951 err =
_(
"window functions are not allowed in policy expressions");
964 err =
_(
"window functions are not allowed in window definitions");
975 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
1000 err =
_(
"window functions are not allowed in check constraints");
1004 err =
_(
"window functions are not allowed in DEFAULT expressions");
1007 err =
_(
"window functions are not allowed in index expressions");
1010 err =
_(
"window functions are not allowed in statistics expressions");
1013 err =
_(
"window functions are not allowed in index predicates");
1016 err =
_(
"window functions are not allowed in transform expressions");
1019 err =
_(
"window functions are not allowed in EXECUTE parameters");
1022 err =
_(
"window functions are not allowed in trigger WHEN conditions");
1025 err =
_(
"window functions are not allowed in partition bound");
1028 err =
_(
"window functions are not allowed in partition key expressions");
1031 err =
_(
"window functions are not allowed in CALL arguments");
1034 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
1037 err =
_(
"window functions are not allowed in column generation expressions");
1043 err =
_(
"window functions are not allowed in property definition expressions");
1046 err =
_(
"window functions are not allowed in FOR PORTION OF expressions");
1059 (
errcode(ERRCODE_WINDOWING_ERROR),
1064 (
errcode(ERRCODE_WINDOWING_ERROR),
1066 errmsg(
"window functions are not allowed in %s",
1091 if (refwin->
name && strcmp(refwin->
name, windef->
name) == 0)
1099 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1100 errmsg(
"window \"%s\" does not exist", windef->
name),
1164 List *groupClauseCommonVars =
NIL;
1165 bool have_non_var_grouping;
1166 List *func_grouped_rels =
NIL;
1169 bool hasSelfRefRTEs;
1189 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
1190 errmsg(
"too many grouping sets present (maximum 4096)"),
1226 hasJoinRTEs = hasSelfRefRTEs =
false;
1234 hasSelfRefRTEs =
true;
1253 groupClauses =
lappend(groupClauses, expr);
1270 qry->hasGroupRTE =
true;
1280 groupClauses = (
List *)
1292 have_non_var_grouping =
false;
1293 foreach(l, groupClauses)
1299 have_non_var_grouping =
true;
1304 groupClauseCommonVars =
lappend(groupClauseCommonVars, tle->
expr);
1323 groupClauses, hasJoinRTEs,
1324 have_non_var_grouping);
1329 groupClauses, groupClauseCommonVars,
1331 have_non_var_grouping,
1332 &func_grouped_rels);
1336 groupClauses, hasJoinRTEs,
1337 have_non_var_grouping);
1342 groupClauses, groupClauseCommonVars,
1344 have_non_var_grouping,
1345 &func_grouped_rels);
1350 if (pstate->
p_hasAggs && hasSelfRefRTEs)
1352 (
errcode(ERRCODE_INVALID_RECURSION),
1353 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1372 List *groupClauses,
List *groupClauseCommonVars,
1374 bool have_non_var_grouping,
1375 List **func_grouped_rels)
1425 return (
Node *) agg;
1467 foreach(gl, groupClauses)
1561 &context->
qry->constraintDeps))
1573 (
errcode(ERRCODE_GROUPING_ERROR),
1574 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1575 rte->eref->aliasname,
attname),
1577 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1581 (
errcode(ERRCODE_GROUPING_ERROR),
1582 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1583 rte->eref->aliasname,
attname),
1616 return (
Node *) newnode;
1635 List *groupClauses,
bool hasJoinRTEs,
1636 bool have_non_var_grouping)
1712 foreach(lc, grp->args)
1764 foreach(gl, groupClauses)
1778 (
errcode(ERRCODE_GROUPING_ERROR),
1779 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1786 grp->refs = ref_list;
1854 var->varnullingrels =
1893 while (curgroup_size > 0)
1896 int i = curgroup_size;
1898 foreach(lc, rollup_val)
1928 Assert(number_bits < 31);
1930 num_sets = (1U << number_bits);
1932 for (
i = 0;
i < num_sets;
i++)
1938 foreach(lc, cube_list)
2026 if (groupingSets ==
NIL)
2029 foreach(lc, groupingSets)
2040 if (limit >= 0 && numsets > limit)
2043 expanded_groups =
lappend(expanded_groups, current_result);
2070 new_result =
lappend(new_result,
2124 int numArguments = 0;
2129 foreach(lc, aggref->aggargtypes)
2134 return numArguments;
2154 if (IsPolymorphicType(aggtranstype))
2157 Oid *declaredArgTypes;
2166 Assert(agg_nargs <= numArguments);
2173 pfree(declaredArgTypes);
2175 return aggtranstype;
2188 foreach(lc, aggref->
args)
2201 if (
type == RECORDOID)
2210 if (!pt->typbyval &&
2253 int agg_num_direct_inputs,
2256 Oid agg_input_collation,
2260 Expr **invtransfnexpr)
2271 for (
i = agg_num_direct_inputs;
i < agg_num_inputs;
i++)
2281 agg_input_collation,
2283 fexpr->funcvariadic = agg_variadic;
2284 *transfnexpr = (
Expr *) fexpr;
2289 if (invtransfnexpr != NULL)
2297 agg_input_collation,
2299 fexpr->funcvariadic = agg_variadic;
2300 *invtransfnexpr = (
Expr *) fexpr;
2303 *invtransfnexpr = NULL;
2313 Expr **serialfnexpr)
2327 *serialfnexpr = (
Expr *) fexpr;
2336 Expr **deserialfnexpr)
2351 *deserialfnexpr = (
Expr *) fexpr;
2360 int num_finalfn_inputs,
2362 Oid agg_result_type,
2363 Oid agg_input_collation,
2376 for (
i = 0;
i < num_finalfn_inputs - 1;
i++)
2386 agg_input_collation,
2409 return (
Node *) argp;
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define Assert(condition)
#define OidIsValid(objectId)
int errcode(int sqlerrcode)
int errhint(const char *fmt,...) pg_attribute_printf(1
int errdetail(const char *fmt,...) pg_attribute_printf(1
int int errmsg_internal(const char *fmt,...) pg_attribute_printf(1
#define ereport(elevel,...)
bool equal(const void *a, const void *b)
void err(int eval, const char *fmt,...)
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
static int pg_cmp_s32(int32 a, int32 b)
List * lappend(List *list, void *datum)
void list_sort(List *list, list_sort_comparator cmp)
List * list_copy_tail(const List *oldlist, int nskip)
List * list_concat(List *list1, const List *list2)
List * lappend_int(List *list, int datum)
List * list_intersection_int(const List *list1, const List *list2)
List * lappend_oid(List *list, Oid datum)
bool list_member_int(const List *list, int datum)
int list_int_cmp(const ListCell *p1, const ListCell *p2)
List * list_truncate(List *list, int new_size)
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)
Var * makeVar(int varno, AttrNumber varattno, Oid vartype, int32 vartypmod, Oid varcollid, Index varlevelsup)
TargetEntry * makeTargetEntry(Expr *expr, AttrNumber resno, char *resname, bool resjunk)
FuncExpr * makeFuncExpr(Oid funcid, Oid rettype, List *args, Oid funccollid, Oid inputcollid, CoercionForm fformat)
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 QTW_EXAMINE_RTES_BEFORE
#define IsA(nodeptr, _type_)
static void check_agglevels_and_constraints(ParseState *pstate, Node *expr)
static Var * buildGroupedVar(int attnum, Index ressortgroupref, substitute_grouped_columns_context *context)
Node * transformGroupingFunc(ParseState *pstate, GroupingFunc *p)
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)
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 int check_agg_arguments(ParseState *pstate, List *directargs, List *args, Expr *filter, int agglocation)
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)
List * expand_grouping_sets(List *groupingSets, bool groupDistinct, int limit)
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 * transformSortClause(ParseState *pstate, List *orderlist, List **targetlist, ParseExprKind exprKind, bool useSQL99)
List * transformDistinctClause(ParseState *pstate, List **targetlist, List *sortClause, bool is_agg)
List * addTargetToSortList(ParseState *pstate, TargetEntry *tle, List *sortlist, List *targetlist, SortBy *sortby)
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_PROPGRAPH_PROPERTY
@ 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
char * get_rte_attribute_name(RangeTblEntry *rte, AttrNumber attnum)
ParseNamespaceItem * addRangeTableEntryForGroup(ParseState *pstate, List *groupClauses)
#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)
static void * list_nth(const List *list, int n)
#define list_make2(x1, x2)
END_CATALOG_STRUCT typedef 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)
void IncrementVarSublevelsUp(Node *node, int delta_sublevels_up, int min_sublevels_up)
ParseNamespaceColumn * p_nscolumns
ParseState * parentParseState
ParseNamespaceItem * p_grouping_nsitem
ParseExprKind p_expr_kind
List ** func_grouped_rels
List * groupClauseCommonVars
bool have_non_var_grouping
List * groupClauseSubLevels
void ReleaseSysCache(HeapTuple tuple)
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Node * flatten_join_alias_for_parser(Query *query, Node *node, int sublevels_up)
int locate_var_of_level(Node *node, int levelsup)