62 List *groupClauses,
List *groupClauseCommonVars,
63 bool have_non_var_grouping,
64 List **func_grouped_rels);
68 List *groupClauses,
bool hasJoinRTEs,
69 bool have_non_var_grouping);
124 agg->aggargtypes = argtypes;
126 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
136 Assert(numDirectArgs >= 0);
147 forboth(lc, aargs, lc2, aggorder)
158 torder, tlist, sortby);
211 foreach(lc, tdistinct)
220 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
221 errmsg(
"could not identify an ordering operator for type %s",
223 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
257 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
258 errmsg(
"GROUPING must have fewer than 32 arguments"),
263 Node *current_result;
269 result_list =
lappend(result_list, current_result);
272 result->args = result_list;
277 return (
Node *) result;
307 p_levelsup = &agg->agglevelsup;
327 *p_levelsup = min_varlevel;
330 while (min_varlevel-- > 0)
361 err =
_(
"aggregate functions are not allowed in JOIN conditions");
363 err =
_(
"grouping operations are not allowed in JOIN conditions");
375 err =
_(
"aggregate functions are not allowed in FROM clause of their own query level");
377 err =
_(
"grouping operations are not allowed in FROM clause of their own query level");
382 err =
_(
"aggregate functions are not allowed in functions in FROM");
384 err =
_(
"grouping operations are not allowed in functions in FROM");
392 err =
_(
"aggregate functions are not allowed in policy expressions");
394 err =
_(
"grouping operations are not allowed in policy expressions");
411 err =
_(
"aggregate functions are not allowed in window RANGE");
413 err =
_(
"grouping operations are not allowed in window RANGE");
418 err =
_(
"aggregate functions are not allowed in window ROWS");
420 err =
_(
"grouping operations are not allowed in window ROWS");
425 err =
_(
"aggregate functions are not allowed in window GROUPS");
427 err =
_(
"grouping operations are not allowed in window GROUPS");
440 err =
_(
"aggregate functions are not allowed in MERGE WHEN conditions");
442 err =
_(
"grouping operations are not allowed in MERGE WHEN conditions");
468 err =
_(
"aggregate functions are not allowed in check constraints");
470 err =
_(
"grouping operations are not allowed in check constraints");
477 err =
_(
"aggregate functions are not allowed in DEFAULT expressions");
479 err =
_(
"grouping operations are not allowed in DEFAULT expressions");
484 err =
_(
"aggregate functions are not allowed in index expressions");
486 err =
_(
"grouping operations are not allowed in index expressions");
491 err =
_(
"aggregate functions are not allowed in index predicates");
493 err =
_(
"grouping operations are not allowed in index predicates");
498 err =
_(
"aggregate functions are not allowed in statistics expressions");
500 err =
_(
"grouping operations are not allowed in statistics expressions");
505 err =
_(
"aggregate functions are not allowed in transform expressions");
507 err =
_(
"grouping operations are not allowed in transform expressions");
512 err =
_(
"aggregate functions are not allowed in EXECUTE parameters");
514 err =
_(
"grouping operations are not allowed in EXECUTE parameters");
519 err =
_(
"aggregate functions are not allowed in trigger WHEN conditions");
521 err =
_(
"grouping operations are not allowed in trigger WHEN conditions");
526 err =
_(
"aggregate functions are not allowed in partition bound");
528 err =
_(
"grouping operations are not allowed in partition bound");
533 err =
_(
"aggregate functions are not allowed in partition key expressions");
535 err =
_(
"grouping operations are not allowed in partition key expressions");
541 err =
_(
"aggregate functions are not allowed in column generation expressions");
543 err =
_(
"grouping operations are not allowed in column generation expressions");
549 err =
_(
"aggregate functions are not allowed in CALL arguments");
551 err =
_(
"grouping operations are not allowed in CALL arguments");
557 err =
_(
"aggregate functions are not allowed in COPY FROM WHERE conditions");
559 err =
_(
"grouping operations are not allowed in COPY FROM WHERE conditions");
578 (
errcode(ERRCODE_GROUPING_ERROR),
586 err =
_(
"aggregate functions are not allowed in %s");
589 err =
_(
"grouping operations are not allowed in %s");
592 (
errcode(ERRCODE_GROUPING_ERROR),
668 (
errcode(ERRCODE_GROUPING_ERROR),
669 errmsg(
"aggregate function calls cannot be nested"),
689 (
errcode(ERRCODE_GROUPING_ERROR),
690 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
696 (
errcode(ERRCODE_GROUPING_ERROR),
697 errmsg(
"aggregate function calls cannot be nested"),
713 int varlevelsup = ((
Var *) node)->varlevelsup;
718 if (varlevelsup >= 0)
728 int agglevelsup = ((
Aggref *) node)->agglevelsup;
733 if (agglevelsup >= 0)
748 if (agglevelsup >= 0)
767 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
768 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
769 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
773 (
errcode(ERRCODE_GROUPING_ERROR),
774 errmsg(
"aggregate function calls cannot contain window function calls"),
788 context->sublevels_up--;
826 (
errcode(ERRCODE_WINDOWING_ERROR),
827 errmsg(
"window function calls cannot be nested"),
853 err =
_(
"window functions are not allowed in JOIN conditions");
860 err =
_(
"window functions are not allowed in functions in FROM");
866 err =
_(
"window functions are not allowed in policy expressions");
879 err =
_(
"window functions are not allowed in window definitions");
890 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
914 err =
_(
"window functions are not allowed in check constraints");
918 err =
_(
"window functions are not allowed in DEFAULT expressions");
921 err =
_(
"window functions are not allowed in index expressions");
924 err =
_(
"window functions are not allowed in statistics expressions");
927 err =
_(
"window functions are not allowed in index predicates");
930 err =
_(
"window functions are not allowed in transform expressions");
933 err =
_(
"window functions are not allowed in EXECUTE parameters");
936 err =
_(
"window functions are not allowed in trigger WHEN conditions");
939 err =
_(
"window functions are not allowed in partition bound");
942 err =
_(
"window functions are not allowed in partition key expressions");
945 err =
_(
"window functions are not allowed in CALL arguments");
948 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
951 err =
_(
"window functions are not allowed in column generation expressions");
967 (
errcode(ERRCODE_WINDOWING_ERROR),
972 (
errcode(ERRCODE_WINDOWING_ERROR),
974 errmsg(
"window functions are not allowed in %s",
999 if (refwin->
name && strcmp(refwin->
name, windef->
name) == 0)
1007 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1008 errmsg(
"window \"%s\" does not exist", windef->
name),
1070 List *groupClauseCommonVars =
NIL;
1071 bool have_non_var_grouping;
1072 List *func_grouped_rels =
NIL;
1075 bool hasSelfRefRTEs;
1095 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
1096 errmsg(
"too many grouping sets present (maximum 4096)"),
1132 hasJoinRTEs = hasSelfRefRTEs =
false;
1140 hasSelfRefRTEs =
true;
1159 groupClauses =
lappend(groupClauses, expr);
1170 (
Node *) groupClauses);
1181 have_non_var_grouping =
false;
1182 foreach(l, groupClauses)
1188 have_non_var_grouping =
true;
1193 groupClauseCommonVars =
lappend(groupClauseCommonVars, tle->
expr);
1210 groupClauses, hasJoinRTEs,
1211 have_non_var_grouping);
1215 groupClauses, groupClauseCommonVars,
1216 have_non_var_grouping,
1217 &func_grouped_rels);
1221 groupClauses, hasJoinRTEs,
1222 have_non_var_grouping);
1226 groupClauses, groupClauseCommonVars,
1227 have_non_var_grouping,
1228 &func_grouped_rels);
1233 if (pstate->
p_hasAggs && hasSelfRefRTEs)
1235 (
errcode(ERRCODE_INVALID_RECURSION),
1236 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1264 List *groupClauses,
List *groupClauseCommonVars,
1265 bool have_non_var_grouping,
1266 List **func_grouped_rels)
1416 &context->
qry->constraintDeps))
1428 (
errcode(ERRCODE_GROUPING_ERROR),
1429 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1432 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1436 (
errcode(ERRCODE_GROUPING_ERROR),
1437 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1452 context->sublevels_up--;
1472 List *groupClauses,
bool hasJoinRTEs,
1473 bool have_non_var_grouping)
1547 foreach(lc, grp->args)
1600 (
errcode(ERRCODE_GROUPING_ERROR),
1601 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1608 grp->refs = ref_list;
1625 context->sublevels_up--;
1665 while (curgroup_size > 0)
1668 int i = curgroup_size;
1670 foreach(lc, rollup_val)
1684 result =
lappend(result, current_result);
1700 Assert(number_bits < 31);
1702 num_sets = (1U << number_bits);
1704 for (
i = 0;
i < num_sets;
i++)
1710 foreach(lc, cube_list)
1723 result =
lappend(result, current_result);
1752 return (la > lb) ? 1 : (la == lb) ? 0 : -1;
1798 if (groupingSets ==
NIL)
1801 foreach(lc, groupingSets)
1812 if (limit >= 0 && numsets > limit)
1815 expanded_groups =
lappend(expanded_groups, current_result);
1835 foreach(lc2, result)
1842 new_result =
lappend(new_result,
1846 result = new_result;
1858 foreach(cell, result)
1896 int numArguments = 0;
1901 foreach(lc, aggref->aggargtypes)
1906 return numArguments;
1926 if (IsPolymorphicType(aggtranstype))
1929 Oid *declaredArgTypes;
1938 Assert(agg_nargs <= numArguments);
1945 pfree(declaredArgTypes);
1947 return aggtranstype;
1960 foreach(lc, aggref->
args)
1973 if (!pt->typbyval &&
2016 int agg_num_direct_inputs,
2019 Oid agg_input_collation,
2023 Expr **invtransfnexpr)
2034 for (
i = agg_num_direct_inputs;
i < agg_num_inputs;
i++)
2044 agg_input_collation,
2046 fexpr->funcvariadic = agg_variadic;
2047 *transfnexpr = (
Expr *) fexpr;
2052 if (invtransfnexpr != NULL)
2060 agg_input_collation,
2062 fexpr->funcvariadic = agg_variadic;
2063 *invtransfnexpr = (
Expr *) fexpr;
2066 *invtransfnexpr = NULL;
2076 Expr **serialfnexpr)
2090 *serialfnexpr = (
Expr *) fexpr;
2099 Expr **deserialfnexpr)
2114 *deserialfnexpr = (
Expr *) fexpr;
2123 int num_finalfn_inputs,
2125 Oid agg_result_type,
2126 Oid agg_input_collation,
2139 for (
i = 0;
i < num_finalfn_inputs - 1;
i++)
2149 agg_input_collation,
2169 argp->paramtypmod = -1;
2170 argp->paramcollid = argcollation;
2172 return (
Node *) argp;
#define OidIsValid(objectId)
static void PGresult * res
elog(ERROR, "%s: %s", p2, msg)
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)
if(TABLE==NULL||TABLE_index==NULL)
Assert(fmt[strlen(fmt) - 1] !='\n')
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)
void pfree(void *pointer)
Oid exprType(const Node *expr)
int exprLocation(const Node *expr)
#define query_tree_walker(q, w, c, f)
#define expression_tree_walker(n, w, c)
#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)
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)
static void check_ungrouped_columns(Node *node, ParseState *pstate, Query *qry, List *groupClauses, List *groupClauseCommonVars, bool have_non_var_grouping, List **func_grouped_rels)
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 bool check_ungrouped_columns_walker(Node *node, check_ungrouped_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)
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
static bool finalize_grouping_exprs_walker(Node *node, check_ungrouped_columns_context *context)
static int cmp_list_len_contents_asc(const ListCell *a, const ListCell *b)
static List * expand_groupingset_node(GroupingSet *gs)
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_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)
#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 for_each_from(cell, lst, N)
#define list_make2(x1, x2)
#define foreach_delete_current(lst, cell)
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)
ParseState * parentParseState
ParseExprKind p_expr_kind
List * groupClauseCommonVars
bool have_non_var_grouping
List ** func_grouped_rels
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)