63 List *groupClauses,
List *groupClauseCommonVars,
64 bool have_non_var_grouping,
65 List **func_grouped_rels);
69 List *groupClauses,
bool hasJoinRTEs,
70 bool have_non_var_grouping);
115 if (AGGKIND_IS_ORDERED_SET(agg->aggkind))
125 Assert(numDirectArgs >= 0);
136 forboth(lc, aargs, lc2, aggorder)
147 torder, tlist, sortby);
200 foreach(lc, tdistinct)
209 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
210 errmsg(
"could not identify an ordering operator for type %s",
212 errdetail(
"Aggregates with DISTINCT must be able to sort their inputs."),
247 agg->aggargtypes = argtypes;
269 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
270 errmsg(
"GROUPING must have fewer than 32 arguments"),
275 Node *current_result;
281 result_list =
lappend(result_list, current_result);
284 result->args = result_list;
289 return (
Node *) result;
319 p_levelsup = &agg->agglevelsup;
339 *p_levelsup = min_varlevel;
342 while (min_varlevel-- > 0)
373 err =
_(
"aggregate functions are not allowed in JOIN conditions");
375 err =
_(
"grouping operations are not allowed in JOIN conditions");
387 err =
_(
"aggregate functions are not allowed in FROM clause of their own query level");
389 err =
_(
"grouping operations are not allowed in FROM clause of their own query level");
394 err =
_(
"aggregate functions are not allowed in functions in FROM");
396 err =
_(
"grouping operations are not allowed in functions in FROM");
404 err =
_(
"aggregate functions are not allowed in policy expressions");
406 err =
_(
"grouping operations are not allowed in policy expressions");
423 err =
_(
"aggregate functions are not allowed in window RANGE");
425 err =
_(
"grouping operations are not allowed in window RANGE");
430 err =
_(
"aggregate functions are not allowed in window ROWS");
432 err =
_(
"grouping operations are not allowed in window ROWS");
437 err =
_(
"aggregate functions are not allowed in window GROUPS");
439 err =
_(
"grouping operations are not allowed in window GROUPS");
452 err =
_(
"aggregate functions are not allowed in MERGE WHEN conditions");
454 err =
_(
"grouping operations are not allowed in MERGE WHEN conditions");
481 err =
_(
"aggregate functions are not allowed in check constraints");
483 err =
_(
"grouping operations are not allowed in check constraints");
490 err =
_(
"aggregate functions are not allowed in DEFAULT expressions");
492 err =
_(
"grouping operations are not allowed in DEFAULT expressions");
497 err =
_(
"aggregate functions are not allowed in index expressions");
499 err =
_(
"grouping operations are not allowed in index expressions");
504 err =
_(
"aggregate functions are not allowed in index predicates");
506 err =
_(
"grouping operations are not allowed in index predicates");
511 err =
_(
"aggregate functions are not allowed in statistics expressions");
513 err =
_(
"grouping operations are not allowed in statistics expressions");
518 err =
_(
"aggregate functions are not allowed in transform expressions");
520 err =
_(
"grouping operations are not allowed in transform expressions");
525 err =
_(
"aggregate functions are not allowed in EXECUTE parameters");
527 err =
_(
"grouping operations are not allowed in EXECUTE parameters");
532 err =
_(
"aggregate functions are not allowed in trigger WHEN conditions");
534 err =
_(
"grouping operations are not allowed in trigger WHEN conditions");
539 err =
_(
"aggregate functions are not allowed in partition bound");
541 err =
_(
"grouping operations are not allowed in partition bound");
546 err =
_(
"aggregate functions are not allowed in partition key expressions");
548 err =
_(
"grouping operations are not allowed in partition key expressions");
554 err =
_(
"aggregate functions are not allowed in column generation expressions");
556 err =
_(
"grouping operations are not allowed in column generation expressions");
562 err =
_(
"aggregate functions are not allowed in CALL arguments");
564 err =
_(
"grouping operations are not allowed in CALL arguments");
570 err =
_(
"aggregate functions are not allowed in COPY FROM WHERE conditions");
572 err =
_(
"grouping operations are not allowed in COPY FROM WHERE conditions");
591 (
errcode(ERRCODE_GROUPING_ERROR),
599 err =
_(
"aggregate functions are not allowed in %s");
602 err =
_(
"grouping operations are not allowed in %s");
605 (
errcode(ERRCODE_GROUPING_ERROR),
663 agglevel =
context.min_agglevel;
665 else if (
context.min_agglevel < 0)
666 agglevel =
context.min_varlevel;
673 if (agglevel ==
context.min_agglevel)
681 (
errcode(ERRCODE_GROUPING_ERROR),
682 errmsg(
"aggregate function calls cannot be nested"),
702 (
errcode(ERRCODE_GROUPING_ERROR),
703 errmsg(
"outer-level aggregate cannot contain a lower-level variable in its direct arguments"),
707 if (
context.min_agglevel >= 0 &&
context.min_agglevel <= agglevel)
709 (
errcode(ERRCODE_GROUPING_ERROR),
710 errmsg(
"aggregate function calls cannot be nested"),
726 int varlevelsup = ((
Var *) node)->varlevelsup;
729 varlevelsup -=
context->sublevels_up;
731 if (varlevelsup >= 0)
733 if (
context->min_varlevel < 0 ||
734 context->min_varlevel > varlevelsup)
735 context->min_varlevel = varlevelsup;
741 int agglevelsup = ((
Aggref *) node)->agglevelsup;
744 agglevelsup -=
context->sublevels_up;
746 if (agglevelsup >= 0)
748 if (
context->min_agglevel < 0 ||
749 context->min_agglevel > agglevelsup)
750 context->min_agglevel = agglevelsup;
759 agglevelsup -=
context->sublevels_up;
761 if (agglevelsup >= 0)
763 if (
context->min_agglevel < 0 ||
764 context->min_agglevel > agglevelsup)
765 context->min_agglevel = agglevelsup;
775 if (
context->sublevels_up == 0)
780 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
781 errmsg(
"aggregate function calls cannot contain set-returning function calls"),
782 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
786 (
errcode(ERRCODE_GROUPING_ERROR),
787 errmsg(
"aggregate function calls cannot contain window function calls"),
839 (
errcode(ERRCODE_WINDOWING_ERROR),
840 errmsg(
"window function calls cannot be nested"),
866 err =
_(
"window functions are not allowed in JOIN conditions");
873 err =
_(
"window functions are not allowed in functions in FROM");
879 err =
_(
"window functions are not allowed in policy expressions");
892 err =
_(
"window functions are not allowed in window definitions");
903 err =
_(
"window functions are not allowed in MERGE WHEN conditions");
928 err =
_(
"window functions are not allowed in check constraints");
932 err =
_(
"window functions are not allowed in DEFAULT expressions");
935 err =
_(
"window functions are not allowed in index expressions");
938 err =
_(
"window functions are not allowed in statistics expressions");
941 err =
_(
"window functions are not allowed in index predicates");
944 err =
_(
"window functions are not allowed in transform expressions");
947 err =
_(
"window functions are not allowed in EXECUTE parameters");
950 err =
_(
"window functions are not allowed in trigger WHEN conditions");
953 err =
_(
"window functions are not allowed in partition bound");
956 err =
_(
"window functions are not allowed in partition key expressions");
959 err =
_(
"window functions are not allowed in CALL arguments");
962 err =
_(
"window functions are not allowed in COPY FROM WHERE conditions");
965 err =
_(
"window functions are not allowed in column generation expressions");
981 (
errcode(ERRCODE_WINDOWING_ERROR),
986 (
errcode(ERRCODE_WINDOWING_ERROR),
988 errmsg(
"window functions are not allowed in %s",
1013 if (refwin->
name && strcmp(refwin->
name, windef->
name) == 0)
1021 (
errcode(ERRCODE_UNDEFINED_OBJECT),
1022 errmsg(
"window \"%s\" does not exist", windef->
name),
1084 List *groupClauseCommonVars =
NIL;
1085 bool have_non_var_grouping;
1086 List *func_grouped_rels =
NIL;
1089 bool hasSelfRefRTEs;
1109 (
errcode(ERRCODE_STATEMENT_TOO_COMPLEX),
1110 errmsg(
"too many grouping sets present (maximum 4096)"),
1146 hasJoinRTEs = hasSelfRefRTEs =
false;
1154 hasSelfRefRTEs =
true;
1173 groupClauses =
lappend(groupClauses, expr);
1184 (
Node *) groupClauses);
1195 have_non_var_grouping =
false;
1196 foreach(l, groupClauses)
1202 have_non_var_grouping =
true;
1207 groupClauseCommonVars =
lappend(groupClauseCommonVars, tle->
expr);
1224 groupClauses, hasJoinRTEs,
1225 have_non_var_grouping);
1229 groupClauses, groupClauseCommonVars,
1230 have_non_var_grouping,
1231 &func_grouped_rels);
1235 groupClauses, hasJoinRTEs,
1236 have_non_var_grouping);
1240 groupClauses, groupClauseCommonVars,
1241 have_non_var_grouping,
1242 &func_grouped_rels);
1247 if (pstate->
p_hasAggs && hasSelfRefRTEs)
1249 (
errcode(ERRCODE_INVALID_RECURSION),
1250 errmsg(
"aggregate functions are not allowed in a recursive query's recursive term"),
1278 List *groupClauses,
List *groupClauseCommonVars,
1279 bool have_non_var_grouping,
1280 List **func_grouped_rels)
1287 context.groupClauses = groupClauses;
1288 context.groupClauseCommonVars = groupClauseCommonVars;
1289 context.have_non_var_grouping = have_non_var_grouping;
1290 context.func_grouped_rels = func_grouped_rels;
1292 context.in_agg_direct_args =
false;
1312 if ((
int) agg->agglevelsup ==
context->sublevels_up)
1325 context->in_agg_direct_args =
true;
1328 context->in_agg_direct_args =
false;
1338 if ((
int) agg->agglevelsup >
context->sublevels_up)
1359 if (
context->have_non_var_grouping &&
context->sublevels_up == 0)
1361 foreach(gl,
context->groupClauses)
1388 if (!
context->have_non_var_grouping ||
context->sublevels_up != 0)
1390 foreach(gl,
context->groupClauses)
1429 context->groupClauseCommonVars,
1430 &
context->qry->constraintDeps))
1440 if (
context->sublevels_up == 0)
1442 (
errcode(ERRCODE_GROUPING_ERROR),
1443 errmsg(
"column \"%s.%s\" must appear in the GROUP BY clause or be used in an aggregate function",
1444 rte->eref->aliasname,
attname),
1446 errdetail(
"Direct arguments of an ordered-set aggregate must use only grouped columns.") : 0,
1450 (
errcode(ERRCODE_GROUPING_ERROR),
1451 errmsg(
"subquery uses ungrouped column \"%s.%s\" from outer query",
1452 rte->eref->aliasname,
attname),
1486 List *groupClauses,
bool hasJoinRTEs,
1487 bool have_non_var_grouping)
1493 context.hasJoinRTEs = hasJoinRTEs;
1494 context.groupClauses = groupClauses;
1496 context.have_non_var_grouping = have_non_var_grouping;
1497 context.func_grouped_rels = NULL;
1499 context.in_agg_direct_args =
false;
1519 if ((
int) agg->agglevelsup ==
context->sublevels_up)
1530 context->in_agg_direct_args =
true;
1533 context->in_agg_direct_args =
false;
1543 if ((
int) agg->agglevelsup >
context->sublevels_up)
1561 foreach(lc, grp->args)
1581 foreach(gl,
context->groupClauses)
1597 else if (
context->have_non_var_grouping &&
1600 foreach(gl,
context->groupClauses)
1614 (
errcode(ERRCODE_GROUPING_ERROR),
1615 errmsg(
"arguments to GROUPING must be grouping expressions of the associated query level"),
1622 grp->refs = ref_list;
1679 while (curgroup_size > 0)
1682 int i = curgroup_size;
1684 foreach(lc, rollup_val)
1698 result =
lappend(result, current_result);
1714 Assert(number_bits < 31);
1716 num_sets = (1U << number_bits);
1718 for (
i = 0;
i < num_sets;
i++)
1724 foreach(lc, cube_list)
1737 result =
lappend(result, current_result);
1812 if (groupingSets ==
NIL)
1815 foreach(lc, groupingSets)
1826 if (limit >= 0 && numsets > limit)
1829 expanded_groups =
lappend(expanded_groups, current_result);
1849 foreach(lc2, result)
1856 new_result =
lappend(new_result,
1860 result = new_result;
1872 foreach(cell, result)
1910 int numArguments = 0;
1915 foreach(lc, aggref->aggargtypes)
1920 return numArguments;
1940 if (IsPolymorphicType(aggtranstype))
1943 Oid *declaredArgTypes;
1952 Assert(agg_nargs <= numArguments);
1959 pfree(declaredArgTypes);
1961 return aggtranstype;
1974 foreach(lc, aggref->
args)
1987 if (!pt->typbyval &&
2030 int agg_num_direct_inputs,
2033 Oid agg_input_collation,
2037 Expr **invtransfnexpr)
2048 for (
i = agg_num_direct_inputs;
i < agg_num_inputs;
i++)
2058 agg_input_collation,
2060 fexpr->funcvariadic = agg_variadic;
2061 *transfnexpr = (
Expr *) fexpr;
2066 if (invtransfnexpr != NULL)
2074 agg_input_collation,
2076 fexpr->funcvariadic = agg_variadic;
2077 *invtransfnexpr = (
Expr *) fexpr;
2080 *invtransfnexpr = NULL;
2090 Expr **serialfnexpr)
2104 *serialfnexpr = (
Expr *) fexpr;
2113 Expr **deserialfnexpr)
2128 *deserialfnexpr = (
Expr *) fexpr;
2137 int num_finalfn_inputs,
2139 Oid agg_result_type,
2140 Oid agg_input_collation,
2153 for (
i = 0;
i < num_finalfn_inputs - 1;
i++)
2163 agg_input_collation,
2183 argp->paramtypmod = -1;
2184 argp->paramcollid = argcollation;
2186 return (
Node *) argp;
#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)
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_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)
#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)
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)