68 List *groupClauses,
List *groupClauseCommonVars,
70 bool have_non_var_grouping,
71 List **func_grouped_rels);
75 List *groupClauses,
bool hasJoinRTEs,
76 bool have_non_var_grouping);
123 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
133 Assert(numDirectArgs >= 0);
144 forboth(lc, aargs, lc2, aggorder)
155 torder, tlist, sortby);
208 foreach(lc, tdistinct)
217 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
218 errmsg(
"could not identify an ordering operator for type %s",
220 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
255 agg->aggargtypes = argtypes;
277 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
278 errmsg(
"GROUPING must have fewer than 32 arguments"),
283 Node *current_result;
289 result_list =
lappend(result_list, current_result);
292 result->args = result_list;
297 return (
Node *) result;
327 p_levelsup = &agg->agglevelsup;
348 *p_levelsup = min_varlevel;
351 while (min_varlevel-- > 0)
382 err =
_(
"aggregate functions are not allowed in JOIN conditions");
384 err =
_(
"grouping operations are not allowed in JOIN conditions");
394 err =
_(
"aggregate functions are not allowed in FROM clause of their own query level");
396 err =
_(
"grouping operations are not allowed in FROM clause of their own query level");
401 err =
_(
"aggregate functions are not allowed in functions in FROM");
403 err =
_(
"grouping operations are not allowed in functions in FROM");
411 err =
_(
"aggregate functions are not allowed in policy expressions");
413 err =
_(
"grouping operations are not allowed in policy expressions");
430 err =
_(
"aggregate functions are not allowed in window RANGE");
432 err =
_(
"grouping operations are not allowed in window RANGE");
437 err =
_(
"aggregate functions are not allowed in window ROWS");
439 err =
_(
"grouping operations are not allowed in window ROWS");
444 err =
_(
"aggregate functions are not allowed in window GROUPS");
446 err =
_(
"grouping operations are not allowed in window GROUPS");
459 err =
_(
"aggregate functions are not allowed in MERGE WHEN conditions");
461 err =
_(
"grouping operations are not allowed in MERGE WHEN conditions");
488 err =
_(
"aggregate functions are not allowed in check constraints");
490 err =
_(
"grouping operations are not allowed in check constraints");
497 err =
_(
"aggregate functions are not allowed in DEFAULT expressions");
499 err =
_(
"grouping operations are not allowed in DEFAULT expressions");
504 err =
_(
"aggregate functions are not allowed in index expressions");
506 err =
_(
"grouping operations are not allowed in index expressions");
511 err =
_(
"aggregate functions are not allowed in index predicates");
513 err =
_(
"grouping operations are not allowed in index predicates");
518 err =
_(
"aggregate functions are not allowed in statistics expressions");
520 err =
_(
"grouping operations are not allowed in statistics expressions");
525 err =
_(
"aggregate functions are not allowed in transform expressions");
527 err =
_(
"grouping operations are not allowed in transform expressions");
532 err =
_(
"aggregate functions are not allowed in EXECUTE parameters");
534 err =
_(
"grouping operations are not allowed in EXECUTE parameters");
539 err =
_(
"aggregate functions are not allowed in trigger WHEN conditions");
541 err =
_(
"grouping operations are not allowed in trigger WHEN conditions");
546 err =
_(
"aggregate functions are not allowed in partition bound");
548 err =
_(
"grouping operations are not allowed in partition bound");
553 err =
_(
"aggregate functions are not allowed in partition key expressions");
555 err =
_(
"grouping operations are not allowed in partition key expressions");
561 err =
_(
"aggregate functions are not allowed in column generation expressions");
563 err =
_(
"grouping operations are not allowed in column generation expressions");
569 err =
_(
"aggregate functions are not allowed in CALL arguments");
571 err =
_(
"grouping operations are not allowed in CALL arguments");
577 err =
_(
"aggregate functions are not allowed in COPY FROM WHERE conditions");
579 err =
_(
"grouping operations are not allowed in COPY FROM WHERE conditions");
598 (
errcode(ERRCODE_GROUPING_ERROR),
606 err =
_(
"aggregate functions are not allowed in %s");
609 err =
_(
"grouping operations are not allowed in %s");
612 (
errcode(ERRCODE_GROUPING_ERROR),
691 (
errcode(ERRCODE_GROUPING_ERROR),
692 errmsg(
"aggregate function calls cannot be nested"),
704 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
705 errmsg(
"outer-level aggregate cannot use a nested CTE"),
706 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
707 context.
min_cte->eref->aliasname),
727 (
errcode(ERRCODE_GROUPING_ERROR),
728 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
734 (
errcode(ERRCODE_GROUPING_ERROR),
735 errmsg(
"aggregate function calls cannot be nested"),
741 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
742 errmsg(
"outer-level aggregate cannot use a nested CTE"),
743 errdetail(
"CTE \"%s\" is below the aggregate's semantic level.",
744 context.
min_cte->eref->aliasname),
758 int varlevelsup = ((
Var *) node)->varlevelsup;
763 if (varlevelsup >= 0)
773 int agglevelsup = ((
Aggref *) node)->agglevelsup;
778 if (agglevelsup >= 0)
793 if (agglevelsup >= 0)
812 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
813 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
814 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
818 (
errcode(ERRCODE_GROUPING_ERROR),
819 errmsg(
"aggregate function calls cannot contain window function calls"),
835 if (ctelevelsup >= 0)
895 (
errcode(ERRCODE_WINDOWING_ERROR),
896 errmsg(
"window function calls cannot be nested"),
922 err =
_(
"window functions are not allowed in JOIN conditions");
929 err =
_(
"window functions are not allowed in functions in FROM");
935 err =
_(
"window functions are not allowed in policy expressions");
948 err =
_(
"window functions are not allowed in window definitions");
959 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
984 err =
_(
"window functions are not allowed in check constraints");
988 err =
_(
"window functions are not allowed in DEFAULT expressions");
991 err =
_(
"window functions are not allowed in index expressions");
994 err =
_(
"window functions are not allowed in statistics expressions");
997 err =
_(
"window functions are not allowed in index predicates");
1000 err =
_(
"window functions are not allowed in transform expressions");
1003 err =
_(
"window functions are not allowed in EXECUTE parameters");
1006 err =
_(
"window functions are not allowed in trigger WHEN conditions");
1009 err =
_(
"window functions are not allowed in partition bound");
1012 err =
_(
"window functions are not allowed in partition key expressions");
1015 err =
_(
"window functions are not allowed in CALL arguments");
1018 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
1021 err =
_(
"window functions are not allowed in column generation expressions");
1037 (
errcode(ERRCODE_WINDOWING_ERROR),
1042 (
errcode(ERRCODE_WINDOWING_ERROR),
1044 errmsg(
"window functions are not allowed in %s",
1069 if (refwin->
name && strcmp(refwin->
name, windef->
name) == 0)
1077 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1078 errmsg(
"window \"%s\" does not exist", windef->
name),
1142 List *groupClauseCommonVars =
NIL;
1143 bool have_non_var_grouping;
1144 List *func_grouped_rels =
NIL;
1147 bool hasSelfRefRTEs;
1167 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
1168 errmsg(
"too many grouping sets present (maximum 4096)"),
1204 hasJoinRTEs = hasSelfRefRTEs =
false;
1212 hasSelfRefRTEs =
true;
1231 groupClauses =
lappend(groupClauses, expr);
1242 (
Node *) groupClauses);
1253 have_non_var_grouping =
false;
1254 foreach(l, groupClauses)
1260 have_non_var_grouping =
true;
1265 groupClauseCommonVars =
lappend(groupClauseCommonVars, tle->
expr);
1281 qry->hasGroupRTE =
true;
1299 groupClauses, hasJoinRTEs,
1300 have_non_var_grouping);
1305 groupClauses, groupClauseCommonVars,
1307 have_non_var_grouping,
1308 &func_grouped_rels);
1312 groupClauses, hasJoinRTEs,
1313 have_non_var_grouping);
1318 groupClauses, groupClauseCommonVars,
1320 have_non_var_grouping,
1321 &func_grouped_rels);
1326 if (pstate->
p_hasAggs && hasSelfRefRTEs)
1328 (
errcode(ERRCODE_INVALID_RECURSION),
1329 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1359 List *groupClauses,
List *groupClauseCommonVars,
1361 bool have_non_var_grouping,
1362 List **func_grouped_rels)
1411 return (
Node *) agg;
1539 &context->
qry->constraintDeps))
1551 (
errcode(ERRCODE_GROUPING_ERROR),
1552 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1553 rte->eref->aliasname,
attname),
1555 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1559 (
errcode(ERRCODE_GROUPING_ERROR),
1560 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1561 rte->eref->aliasname,
attname),
1576 return (
Node *) newnode;
1595 List *groupClauses,
bool hasJoinRTEs,
1596 bool have_non_var_grouping)
1671 foreach(lc, grp->args)
1724 (
errcode(ERRCODE_GROUPING_ERROR),
1725 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1732 grp->refs = ref_list;
1782 var->varnullingrels =
1821 while (curgroup_size > 0)
1824 int i = curgroup_size;
1826 foreach(lc, rollup_val)
1840 result =
lappend(result, current_result);
1856 Assert(number_bits < 31);
1858 num_sets = (1U << number_bits);
1860 for (
i = 0;
i < num_sets;
i++)
1866 foreach(lc, cube_list)
1879 result =
lappend(result, current_result);
1954 if (groupingSets ==
NIL)
1957 foreach(lc, groupingSets)
1968 if (limit >= 0 && numsets > limit)
1971 expanded_groups =
lappend(expanded_groups, current_result);
1991 foreach(lc2, result)
1998 new_result =
lappend(new_result,
2002 result = new_result;
2014 foreach(cell, result)
2052 int numArguments = 0;
2057 foreach(lc, aggref->aggargtypes)
2062 return numArguments;
2082 if (IsPolymorphicType(aggtranstype))
2085 Oid *declaredArgTypes;
2094 Assert(agg_nargs <= numArguments);
2101 pfree(declaredArgTypes);
2103 return aggtranstype;
2116 foreach(lc, aggref->
args)
2129 if (
type == RECORDOID)
2138 if (!pt->typbyval &&
2181 int agg_num_direct_inputs,
2184 Oid agg_input_collation,
2188 Expr **invtransfnexpr)
2199 for (
i = agg_num_direct_inputs;
i < agg_num_inputs;
i++)
2209 agg_input_collation,
2211 fexpr->funcvariadic = agg_variadic;
2212 *transfnexpr = (
Expr *) fexpr;
2217 if (invtransfnexpr != NULL)
2225 agg_input_collation,
2227 fexpr->funcvariadic = agg_variadic;
2228 *invtransfnexpr = (
Expr *) fexpr;
2231 *invtransfnexpr = NULL;
2241 Expr **serialfnexpr)
2255 *serialfnexpr = (
Expr *) fexpr;
2264 Expr **deserialfnexpr)
2279 *deserialfnexpr = (
Expr *) fexpr;
2288 int num_finalfn_inputs,
2290 Oid agg_result_type,
2291 Oid agg_input_collation,
2304 for (
i = 0;
i < num_finalfn_inputs - 1;
i++)
2314 agg_input_collation,
2337 return (
Node *) argp;
Bitmapset * bms_add_member(Bitmapset *a, int x)
#define OidIsValid(objectId)
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,...)
Assert(PointerIsAligned(start, uint64))
#define HeapTupleIsValid(tuple)
static void * GETSTRUCT(const HeapTupleData *tuple)
static int pg_cmp_s32(int32 a, int32 b)
if(TABLE==NULL||TABLE_index==NULL)
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_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)
#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)
TargetEntry * get_sortgroupclause_tle(SortGroupClause *sgClause, List *targetList)
Node * get_sortgroupclause_expr(SortGroupClause *sgClause, List *targetList)
Node * flatten_join_alias_vars(PlannerInfo *root, Query *query, Node *node)
int locate_var_of_level(Node *node, int levelsup)