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)
219 errmsg(
"could not identify an ordering operator for type %s",
221 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
242 foreach(
lc,
agg->aggdirectargs)
256 agg->aggargtypes = argtypes;
272 List *args = p->args;
279 errmsg(
"GROUPING must have fewer than 32 arguments"),
298 return (
Node *) result;
326 filter =
agg->aggfilter;
327 location =
agg->location;
335 location =
grp->location;
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");
590 err =
_(
"aggregate functions are not allowed in property definition expressions");
592 err =
_(
"grouping operations are not allowed in property definition expressions");
615 err =
_(
"aggregate functions are not allowed in %s");
618 err =
_(
"grouping operations are not allowed in %s");
701 errmsg(
"aggregate function calls cannot be nested"),
714 errmsg(
"outer-level aggregate cannot use a nested CTE"),
715 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
716 context.
min_cte->eref->aliasname),
737 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
744 errmsg(
"aggregate function calls cannot be nested"),
751 errmsg(
"outer-level aggregate cannot use a nested CTE"),
752 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
753 context.
min_cte->eref->aliasname),
767 int varlevelsup = ((
Var *) node)->varlevelsup;
772 if (varlevelsup >= 0)
782 int agglevelsup = ((
Aggref *) node)->agglevelsup;
787 if (agglevelsup >= 0)
802 if (agglevelsup >= 0)
822 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
823 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
828 errmsg(
"aggregate function calls cannot contain window function calls"),
839 int ctelevelsup =
rte->ctelevelsup;
844 if (ctelevelsup >= 0)
905 errmsg(
"window function calls cannot be nested"),
931 err =
_(
"window functions are not allowed in JOIN conditions");
938 err =
_(
"window functions are not allowed in functions in FROM");
944 err =
_(
"window functions are not allowed in policy expressions");
957 err =
_(
"window functions are not allowed in window definitions");
968 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
993 err =
_(
"window functions are not allowed in check constraints");
997 err =
_(
"window functions are not allowed in DEFAULT expressions");
1000 err =
_(
"window functions are not allowed in index expressions");
1003 err =
_(
"window functions are not allowed in statistics expressions");
1006 err =
_(
"window functions are not allowed in index predicates");
1009 err =
_(
"window functions are not allowed in transform expressions");
1012 err =
_(
"window functions are not allowed in EXECUTE parameters");
1015 err =
_(
"window functions are not allowed in trigger WHEN conditions");
1018 err =
_(
"window functions are not allowed in partition bound");
1021 err =
_(
"window functions are not allowed in partition key expressions");
1024 err =
_(
"window functions are not allowed in CALL arguments");
1027 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
1030 err =
_(
"window functions are not allowed in column generation expressions");
1036 err =
_(
"window functions are not allowed in property definition expressions");
1056 errmsg(
"window functions are not allowed in %s",
1154 List *groupClauseCommonVars =
NIL;
1155 bool have_non_var_grouping;
1156 List *func_grouped_rels =
NIL;
1180 errmsg(
"too many grouping sets present (maximum 4096)"),
1243 groupClauses =
lappend(groupClauses, expr);
1260 qry->hasGroupRTE =
true;
1270 groupClauses = (
List *)
1282 have_non_var_grouping =
false;
1283 foreach(l, groupClauses)
1289 have_non_var_grouping =
true;
1294 groupClauseCommonVars =
lappend(groupClauseCommonVars,
tle->expr);
1313 groupClauses, hasJoinRTEs,
1314 have_non_var_grouping);
1319 groupClauses, groupClauseCommonVars,
1321 have_non_var_grouping,
1322 &func_grouped_rels);
1326 groupClauses, hasJoinRTEs,
1327 have_non_var_grouping);
1332 groupClauses, groupClauseCommonVars,
1334 have_non_var_grouping,
1335 &func_grouped_rels);
1343 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1362 List *groupClauses,
List *groupClauseCommonVars,
1364 bool have_non_var_grouping,
1365 List **func_grouped_rels)
1457 foreach(
gl, groupClauses)
1466 tle->ressortgroupref,
1513 gvar->varlevelsup == 0)
1517 tle->ressortgroupref,
1551 &context->
qry->constraintDeps))
1564 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1567 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1572 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1625 List *groupClauses,
bool hasJoinRTEs,
1626 bool have_non_var_grouping)
1702 foreach(
lc,
grp->args)
1732 gvar->varlevelsup == 0)
1734 ref =
tle->ressortgroupref;
1754 foreach(
gl, groupClauses)
1760 ref =
tle->ressortgroupref;
1769 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1839 var->varnosyn =
nscol->p_varnosyn;
1840 var->varattnosyn =
nscol->p_varattnosyn;
1844 var->varnullingrels =
1950 foreach(
lc,
gs->content)
2016 if (groupingSets ==
NIL)
2019 foreach(
lc, groupingSets)
2030 if (limit >= 0 && numsets > limit)
2053 foreach(
lc2, result)
2076 foreach(cell, result)
2114 int numArguments = 0;
2119 foreach(
lc, aggref->aggargtypes)
2124 return numArguments;
2165 return aggtranstype;
2178 foreach(
lc, aggref->
args)
2200 if (!
pt->typbyval &&
2395 argp->paramtype = argtype;
2396 argp->paramtypmod = -1;
2398 argp->location = -1;
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)
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)