38 List *fargs,
int numAggregatedArgs,
39 Oid *actual_arg_types,
Oid *declared_arg_types);
42 Node *first_arg,
int location);
71 bool is_column = (fn ==
NULL);
75 bool agg_star = (fn ? fn->
agg_star :
false);
87 Oid *declared_arg_types;
114 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
115 errmsg_plural(
"cannot pass more than %d argument to a function",
116 "cannot pass more than %d arguments to a function",
141 !is_column && !agg_within_group)
147 actual_arg_types[nargs++] = argtype;
169 foreach(lc, argnames)
171 if (strcmp(na->
name, (
char *)
lfirst(lc)) == 0)
173 (
errcode(ERRCODE_SYNTAX_ERROR),
174 errmsg(
"argument name \"%s\" used more than once",
184 (
errcode(ERRCODE_SYNTAX_ERROR),
185 errmsg(
"positional argument cannot follow named argument"),
202 if (nargs == 1 && agg_order ==
NIL && agg_filter ==
NULL && !agg_star &&
203 !agg_distinct && over ==
NULL && !func_variadic && argnames ==
NIL &&
206 Oid argtype = actual_arg_types[0];
244 !func_variadic,
true,
245 &funcid, &rettype, &retset,
247 &declared_arg_types, &argdefaults);
258 actual_arg_types[0], rettype, -1,
269 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
270 errmsg(
"%s(*) specified, but %s is not an aggregate function",
276 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
277 errmsg(
"DISTINCT specified, but %s is not an aggregate function",
280 if (agg_within_group)
282 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
283 errmsg(
"WITHIN GROUP specified, but %s is not an aggregate function",
286 if (agg_order !=
NIL)
288 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
289 errmsg(
"ORDER BY specified, but %s is not an aggregate function",
294 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
295 errmsg(
"FILTER specified, but %s is not an aggregate function",
300 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
301 errmsg(
"OVER specified, but %s is not a window function nor an aggregate function",
316 elog(
ERROR,
"cache lookup failed for aggregate %u", funcid);
318 aggkind = classForm->aggkind;
319 catDirectArgs = classForm->aggnumdirectargs;
325 int numAggregatedArgs;
328 if (!agg_within_group)
330 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
331 errmsg(
"WITHIN GROUP is required for ordered-set aggregate %s",
336 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
337 errmsg(
"OVER is not supported for ordered-set aggregate %s",
355 numDirectArgs = nargs - numAggregatedArgs;
356 Assert(numDirectArgs >= 0);
361 if (numDirectArgs != catDirectArgs)
363 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
364 errmsg(
"function %s does not exist",
368 errhint(
"There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.",
370 catDirectArgs, numDirectArgs),
386 pronargs -= nvargs - 1;
387 if (catDirectArgs < pronargs)
390 if (numDirectArgs != catDirectArgs)
392 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
393 errmsg(
"function %s does not exist",
397 errhint(
"There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.",
399 catDirectArgs, numDirectArgs),
415 if (nvargs != 2 * numAggregatedArgs)
417 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
418 errmsg(
"function %s does not exist",
422 errhint(
"To use the hypothetical-set aggregate %s, the number of hypothetical direct arguments (here %d) must match the number of ordering columns (here %d).",
424 nvargs - numAggregatedArgs, numAggregatedArgs),
429 if (nvargs <= numAggregatedArgs)
431 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
432 errmsg(
"function %s does not exist",
436 errhint(
"There is an ordered-set aggregate %s, but it requires at least %d direct arguments.",
447 actual_arg_types, declared_arg_types);
452 if (agg_within_group)
454 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
455 errmsg(
"%s is not an ordered-set aggregate, so it cannot have WITHIN GROUP",
467 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
468 errmsg(
"window function %s requires an OVER clause",
472 if (agg_within_group)
474 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
475 errmsg(
"window function %s cannot have WITHIN GROUP",
495 (
errcode(ERRCODE_AMBIGUOUS_FUNCTION),
496 errmsg(
"function %s is not unique",
499 errhint(
"Could not choose a best candidate function. "
500 "You might need to add explicit type casts."),
502 else if (
list_length(agg_order) > 1 && !agg_within_group)
506 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
507 errmsg(
"function %s does not exist",
510 errhint(
"No aggregate function matches the given name and argument types. "
511 "Perhaps you misplaced ORDER BY; ORDER BY must appear "
512 "after all regular arguments of the aggregate."),
517 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
518 errmsg(
"function %s does not exist",
521 errhint(
"No function matches the given name and argument types. "
522 "You might need to add explicit type casts."),
533 nargsplusdefs = nargs;
534 foreach(l, argdefaults)
541 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
542 errmsg_plural(
"cannot pass more than %d argument to a function",
543 "cannot pass more than %d arguments to a function",
548 actual_arg_types[nargsplusdefs++] =
exprType(expr);
573 func_variadic =
false;
580 if (nvargs > 0 && vatype !=
ANYOID)
583 int non_var_args = nargs - nvargs;
586 Assert(non_var_args >= 0);
596 (
errcode(ERRCODE_UNDEFINED_OBJECT),
597 errmsg(
"could not find array type for data type %s",
609 func_variadic =
true;
616 if (nargs > 0 && vatype ==
ANYOID && func_variadic)
618 Oid va_arr_typid = actual_arg_types[nargs - 1];
622 (
errcode(ERRCODE_DATATYPE_MISMATCH),
623 errmsg(
"VARIADIC argument must be an array"),
633 funcexpr->
funcid = funcid;
639 funcexpr->
args = fargs;
642 retval = (
Node *) funcexpr;
666 if (fargs ==
NIL && !agg_star && !agg_within_group)
668 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
669 errmsg(
"%s(*) must be used to call a parameterless aggregate function",
675 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
676 errmsg(
"aggregates cannot return sets"),
690 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
691 errmsg(
"aggregates cannot use named arguments"),
697 retval = (
Node *) aggref;
705 Assert(!agg_within_group);
722 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
723 errmsg(
"DISTINCT is not implemented for window functions"),
730 if (wfunc->
winagg && fargs ==
NIL && !agg_star)
732 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
733 errmsg(
"%s(*) must be used to call a parameterless aggregate function",
740 if (agg_order !=
NIL)
742 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
743 errmsg(
"aggregate ORDER BY is not implemented for window functions"),
749 if (!wfunc->
winagg && agg_filter)
751 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
752 errmsg(
"FILTER is not implemented for non-aggregate window functions"),
757 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
758 errmsg(
"window functions cannot return sets"),
764 retval = (
Node *) wfunc;
796 for (current_candidate = raw_candidates;
797 current_candidate !=
NULL;
798 current_candidate = next_candidate)
800 next_candidate = current_candidate->
next;
804 current_candidate->
next = *candidates;
805 *candidates = current_candidate;
877 Oid *current_typeids;
887 bool current_is_preferred;
889 bool resolved_unknowns;
894 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
895 errmsg_plural(
"cannot pass more than %d argument to a function",
896 "cannot pass more than %d arguments to a function",
914 for (i = 0; i < nargs; i++)
932 last_candidate =
NULL;
933 for (current_candidate = candidates;
934 current_candidate !=
NULL;
935 current_candidate = current_candidate->
next)
937 current_typeids = current_candidate->
args;
939 for (i = 0; i < nargs; i++)
942 current_typeids[i] == input_base_typeids[i])
947 if ((nmatch > nbestMatch) || (last_candidate ==
NULL))
950 candidates = current_candidate;
951 last_candidate = current_candidate;
955 else if (nmatch == nbestMatch)
957 last_candidate->
next = current_candidate;
958 last_candidate = current_candidate;
967 if (ncandidates == 1)
977 for (i = 0; i < nargs; i++)
981 last_candidate =
NULL;
982 for (current_candidate = candidates;
983 current_candidate !=
NULL;
984 current_candidate = current_candidate->
next)
986 current_typeids = current_candidate->
args;
988 for (i = 0; i < nargs; i++)
992 if (current_typeids[i] == input_base_typeids[i] ||
998 if ((nmatch > nbestMatch) || (last_candidate ==
NULL))
1000 nbestMatch = nmatch;
1001 candidates = current_candidate;
1002 last_candidate = current_candidate;
1005 else if (nmatch == nbestMatch)
1007 last_candidate->
next = current_candidate;
1008 last_candidate = current_candidate;
1016 if (ncandidates == 1)
1047 resolved_unknowns =
false;
1048 for (i = 0; i < nargs; i++)
1054 resolved_unknowns =
true;
1056 slot_has_preferred_type[
i] =
false;
1057 have_conflict =
false;
1058 for (current_candidate = candidates;
1059 current_candidate !=
NULL;
1060 current_candidate = current_candidate->
next)
1062 current_typeids = current_candidate->
args;
1063 current_type = current_typeids[
i];
1066 ¤t_is_preferred);
1070 slot_category[
i] = current_category;
1071 slot_has_preferred_type[
i] = current_is_preferred;
1073 else if (current_category == slot_category[i])
1076 slot_has_preferred_type[
i] |= current_is_preferred;
1084 slot_category[
i] = current_category;
1085 slot_has_preferred_type[
i] = current_is_preferred;
1092 have_conflict =
true;
1099 resolved_unknowns =
false;
1104 if (resolved_unknowns)
1108 first_candidate = candidates;
1109 last_candidate =
NULL;
1110 for (current_candidate = candidates;
1111 current_candidate !=
NULL;
1112 current_candidate = current_candidate->
next)
1116 current_typeids = current_candidate->
args;
1117 for (i = 0; i < nargs; i++)
1121 current_type = current_typeids[
i];
1124 ¤t_is_preferred);
1125 if (current_category != slot_category[i])
1130 if (slot_has_preferred_type[i] && !current_is_preferred)
1139 last_candidate = current_candidate;
1146 last_candidate->
next = current_candidate->
next;
1148 first_candidate = current_candidate->
next;
1155 candidates = first_candidate;
1160 if (ncandidates == 1)
1174 if (nunknowns < nargs)
1178 for (i = 0; i < nargs; i++)
1183 known_type = input_base_typeids[
i];
1184 else if (known_type != input_base_typeids[i])
1195 for (i = 0; i < nargs; i++)
1196 input_base_typeids[i] = known_type;
1198 last_candidate =
NULL;
1199 for (current_candidate = candidates;
1200 current_candidate !=
NULL;
1201 current_candidate = current_candidate->
next)
1203 current_typeids = current_candidate->
args;
1207 if (++ncandidates > 1)
1209 last_candidate = current_candidate;
1212 if (ncandidates == 1)
1216 return last_candidate;
1262 bool expand_variadic,
1263 bool expand_defaults,
1284 *true_typeids =
NULL;
1290 expand_variadic, expand_defaults,
1297 for (best_candidate = raw_candidates;
1298 best_candidate !=
NULL;
1299 best_candidate = best_candidate->
next)
1301 if (memcmp(argtypes, best_candidate->
args, nargs *
sizeof(
Oid)) == 0)
1305 if (best_candidate ==
NULL)
1346 if (nargs == 1 && fargs !=
NIL && fargnames ==
NIL)
1352 Oid sourceType = argtypes[0];
1392 *rettype = targetType;
1396 *true_typeids = argtypes;
1405 if (raw_candidates !=
NULL)
1413 ¤t_candidates);
1416 if (ncandidates == 1)
1417 best_candidate = current_candidates;
1422 else if (ncandidates > 1)
1426 current_candidates);
1432 if (!best_candidate)
1457 if (fargnames !=
NIL && !expand_variadic && nargs > 0 &&
1458 best_candidate->
argnumbers[nargs - 1] != nargs - 1)
1461 *funcid = best_candidate->
oid;
1462 *nvargs = best_candidate->
nvargs;
1463 *true_typeids = best_candidate->
args;
1488 elog(
ERROR,
"cache lookup failed for function %u",
1489 best_candidate->
oid);
1491 *rettype = pform->prorettype;
1492 *retset = pform->proretset;
1493 *vatype = pform->provariadic;
1495 if (argdefaults && best_candidate->
ndargs > 0)
1497 Datum proargdefaults;
1503 if (best_candidate->
ndargs > pform->pronargdefaults)
1504 elog(
ERROR,
"not enough default arguments");
1532 defargnumbers =
NULL;
1534 for (i = 0; i < best_candidate->
ndargs; i++)
1538 i = pform->pronargs - pform->pronargdefaults;
1539 foreach(lc, defaults)
1547 *argdefaults = newdefaults;
1558 while (ndelete-- > 0)
1560 *argdefaults = defaults;
1563 if (pform->proisagg)
1565 else if (pform->proiswindow)
1594 int numAggregatedArgs,
1595 Oid *actual_arg_types,
1596 Oid *declared_arg_types)
1600 numNonHypotheticalArgs;
1604 numDirectArgs =
list_length(fargs) - numAggregatedArgs;
1605 numNonHypotheticalArgs = numDirectArgs - numAggregatedArgs;
1607 if (numNonHypotheticalArgs < 0)
1608 elog(
ERROR,
"incorrect number of arguments to hypothetical-set aggregate");
1618 for (i = numNonHypotheticalArgs; i < numDirectArgs; i++)
1620 int aargpos = numDirectArgs + (i - numNonHypotheticalArgs);
1624 if (declared_arg_types[i] != declared_arg_types[aargpos])
1625 elog(
ERROR,
"hypothetical-set aggregate has inconsistent declared argument types");
1628 if (declared_arg_types[i] !=
ANYOID)
1647 actual_arg_types[i],
1652 actual_arg_types[
i] = commontype;
1655 actual_arg_types[aargpos],
1660 actual_arg_types[aargpos] = commontype;
1688 Oid *actual_arg_types,
1689 Oid *declared_arg_types)
1694 foreach(current_fargs, fargs)
1697 if (actual_arg_types[i] != declared_arg_types[i])
1711 actual_arg_types[i],
1712 declared_arg_types[i], -1,
1722 actual_arg_types[i],
1723 declared_arg_types[i], -1,
1727 lfirst(current_fargs) = node;
1784 if (
IsA(first_arg,
Var) &&
1790 ((
Var *) first_arg)->varno,
1791 ((
Var *) first_arg)->varlevelsup);
1803 if (
IsA(first_arg,
Var) &&
1810 for (i = 0; i < tupdesc->
natts; i++)
1814 if (strcmp(funcname,
NameStr(att->attname)) == 0 &&
1820 fselect->
arg = (
Expr *) first_arg;
1826 return (
Node *) fselect;
1847 List *argnames,
const Oid *argtypes)
1861 for (i = 0; i < nargs; i++)
1865 if (i >= numposargs)
1884 List *argnames,
const Oid *argtypes)
1887 nargs, argnames, argtypes);
1913 if (memcmp(argtypes, clist->
args, nargs *
sizeof(
Oid)) == 0)
1915 clist = clist->
next;
1920 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
1921 errmsg(
"function %s does not exist",
1944 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1945 errmsg_plural(
"functions cannot have more than %d argument",
1946 "functions cannot have more than %d arguments",
1951 for (i = 0; i < argcount; i++)
1956 args_item =
lnext(args_item);
1984 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
1985 errmsg_plural(
"functions cannot have more than %d argument",
1986 "functions cannot have more than %d arguments",
1991 foreach(lc, argtypes)
2007 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
2008 errmsg(
"aggregate %s(*) does not exist",
2012 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
2013 errmsg(
"aggregate %s does not exist",
2021 elog(
ERROR,
"cache lookup failed for function %u", oid);
2024 if (!pform->proisagg)
2031 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
2032 errmsg(
"function %s is not an aggregate",
#define list_make2(x1, x2)
Node * scanRTEForColumn(ParseState *pstate, RangeTblEntry *rte, char *colname, int location, int fuzzy_rte_penalty, FuzzyAttrMatchState *fuzzystate)
Type LookupTypeName(ParseState *pstate, const TypeName *typeName, int32 *typmod_p, bool missing_ok)
static void unify_hypothetical_args(ParseState *pstate, List *fargs, int numAggregatedArgs, Oid *actual_arg_types, Oid *declared_arg_types)
void * stringToNode(char *str)
#define IsA(nodeptr, _type_)
int errhint(const char *fmt,...)
int exprLocation(const Node *expr)
Oid LookupAggNameTypeNames(List *aggname, List *argtypes, bool noError)
int func_match_argtypes(int nargs, Oid *input_typeids, FuncCandidateList raw_candidates, FuncCandidateList *candidates)
int errmsg_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
static Oid FuncNameAsType(List *funcname)
Oid get_array_type(Oid typid)
Form_pg_attribute * attrs
List * list_truncate(List *list, int new_size)
Node * coerce_type(ParseState *pstate, Node *node, Oid inputTypeId, Oid targetTypeId, int32 targetTypeMod, CoercionContext ccontext, CoercionForm cformat, int location)
int errcode(int sqlerrcode)
Node * ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, FuncCall *fn, int location)
FormData_pg_type * Form_pg_type
List * list_delete_ptr(List *list, void *datum)
void make_fn_arguments(ParseState *pstate, List *fargs, Oid *actual_arg_types, Oid *declared_arg_types)
List * list_copy_tail(const List *oldlist, int nskip)
TypeFuncClass get_expr_result_type(Node *expr, Oid *resultTypeId, TupleDesc *resultTupleDesc)
#define OidIsValid(objectId)
bool IsPreferredType(TYPCATEGORY category, Oid type)
#define SearchSysCache1(cacheId, key1)
#define AGGKIND_IS_ORDERED_SET(kind)
Oid args[FLEXIBLE_ARRAY_MEMBER]
void pfree(void *pointer)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
void appendStringInfo(StringInfo str, const char *fmt,...)
#define ObjectIdGetDatum(X)
#define TYPCATEGORY_INVALID
FuncCandidateList FuncnameGetCandidates(List *names, int nargs, List *argnames, bool expand_variadic, bool expand_defaults, bool missing_ok)
Oid enforce_generic_type_consistency(Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
void appendStringInfoString(StringInfo str, const char *s)
#define Anum_pg_proc_proargdefaults
static ListCell * list_head(const List *l)
const char * funcname_signature_string(const char *funcname, int nargs, List *argnames, const Oid *argtypes)
FormData_pg_attribute * Form_pg_attribute
Oid LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError)
TupleDesc expandRecordVariable(ParseState *pstate, Var *var, int levelsup)
struct _FuncCandidateList * next
#define ereport(elevel, rest)
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
const char * func_signature_string(List *funcname, int nargs, List *argnames, const Oid *argtypes)
List * lappend(List *list, void *datum)
void appendStringInfoChar(StringInfo str, char ch)
void initStringInfo(StringInfo str)
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
char * NameListToString(List *names)
#define TextDatumGetCString(d)
void ReleaseSysCache(HeapTuple tuple)
Datum SysCacheGetAttr(int cacheId, HeapTuple tup, AttrNumber attributeNumber, bool *isNull)
RangeTblEntry * GetRTEByRangeTablePosn(ParseState *pstate, int varno, int sublevels_up)
FormData_pg_proc * Form_pg_proc
TYPCATEGORY TypeCategory(Oid type)
static void * fn(void *arg)
#define TYPCATEGORY_STRING
void bms_free(Bitmapset *a)
#define HeapTupleIsValid(tuple)
#define Assert(condition)
FormData_pg_aggregate * Form_pg_aggregate
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
#define ISCOMPLEX(typeid)
Oid exprType(const Node *expr)
static int list_length(const List *l)
int parser_errposition(ParseState *pstate, int location)
Bitmapset * bms_add_member(Bitmapset *a, int x)
FuncDetailCode func_get_detail(List *funcname, List *fargs, List *fargnames, int nargs, Oid *argtypes, bool expand_variadic, bool expand_defaults, Oid *funcid, Oid *rettype, bool *retset, int *nvargs, Oid *vatype, Oid **true_typeids, List **argdefaults)
#define InvalidAttrNumber
Oid LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
Oid get_base_element_type(Oid typid)
bool can_coerce_type(int nargs, Oid *input_typeids, Oid *target_typeids, CoercionContext ccontext)
FuncCandidateList func_select_candidate(int nargs, Oid *input_typeids, FuncCandidateList candidates)
int errmsg(const char *fmt,...)
CoercionPathType find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, CoercionContext ccontext, Oid *funcid)
Oid typeTypeRelid(Type typ)
TypeName * makeTypeNameFromNameList(List *names)
Oid select_common_type(ParseState *pstate, List *exprs, const char *context, Node **which_expr)
#define AGGKIND_HYPOTHETICAL
Oid getBaseType(Oid typid)
static Node * ParseComplexProjection(ParseState *pstate, char *funcname, Node *first_arg, int location)
void get_type_category_preferred(Oid typid, char *typcategory, bool *typispreferred)
bool bms_is_member(int x, const Bitmapset *a)
List * list_delete_first(List *list)
Oid LookupTypeNameOid(ParseState *pstate, const TypeName *typeName, bool missing_ok)
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)