94{
95 bool is_column = (
fn == NULL);
97 Expr *agg_filter = NULL;
99 bool agg_within_group = (
fn ?
fn->agg_within_group :
false);
100 bool agg_star = (
fn ?
fn->agg_star :
false);
101 bool agg_distinct = (
fn ?
fn->agg_distinct :
false);
102 bool func_variadic = (
fn ?
fn->func_variadic :
false);
105 bool could_be_projection;
109 Node *first_arg = NULL;
110 int nargs;
111 int nargsplusdefs;
113 Oid *declared_arg_types;
117 bool retset;
118 int nvargs;
121 int fgc_flags;
122 char aggkind = 0;
124
125 /*
126 * If there's an aggregate filter, transform it using transformWhereClause
127 */
128 if (
fn &&
fn->agg_filter != NULL)
131 "FILTER");
132
133 /*
134 * Most of the rest of the parser just assumes that functions do not have
135 * more than FUNC_MAX_ARGS parameters. We have to test here to protect
136 * against array overruns, etc. Of course, this may not be a function,
137 * but the test doesn't hurt.
138 */
141 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
142 errmsg_plural(
"cannot pass more than %d argument to a function",
143 "cannot pass more than %d arguments to a function",
147
148 /*
149 * Extract arg type info in preparation for function lookup.
150 *
151 * If any arguments are Param markers of type VOID, we discard them from
152 * the parameter list. This is a hack to allow the JDBC driver to not have
153 * to distinguish "input" and "output" parameter symbols while parsing
154 * function-call constructs. Don't do this if dealing with column syntax,
155 * nor if we had WITHIN GROUP (because in that case it's critical to keep
156 * the argument count unchanged).
157 */
158 nargs = 0;
159 foreach(l, fargs)
160 {
163
165 !is_column && !agg_within_group)
166 {
168 continue;
169 }
170
171 actual_arg_types[nargs++] = argtype;
172 }
173
174 /*
175 * Check for named arguments; if there are any, build a list of names.
176 *
177 * We allow mixed notation (some named and some not), but only with all
178 * the named parameters after all the unnamed ones. So the name list
179 * corresponds to the last N actual parameters and we don't need any extra
180 * bookkeeping to match things up.
181 */
183 foreach(l, fargs)
184 {
186
188 {
191
192 /* Reject duplicate arg names */
193 foreach(lc, argnames)
194 {
195 if (strcmp(na->name, (
char *)
lfirst(lc)) == 0)
197 (
errcode(ERRCODE_SYNTAX_ERROR),
198 errmsg(
"argument name \"%s\" used more than once",
199 na->name),
201 }
202 argnames =
lappend(argnames, na->name);
203 }
204 else
205 {
208 (
errcode(ERRCODE_SYNTAX_ERROR),
209 errmsg(
"positional argument cannot follow named argument"),
211 }
212 }
213
214 if (fargs)
215 {
217 Assert(first_arg != NULL);
218 }
219
220 /*
221 * Decide whether it's legitimate to consider the construct to be a column
222 * projection. For that, there has to be a single argument of complex
223 * type, the function name must not be qualified, and there cannot be any
224 * syntactic decoration that'd require it to be a function (such as
225 * aggregate or variadic decoration, or named arguments).
226 */
227 could_be_projection = (nargs == 1 && !proc_call &&
228 agg_order ==
NIL && agg_filter == NULL &&
229 !agg_star && !agg_distinct && over == NULL &&
230 !func_variadic && argnames ==
NIL &&
232 (actual_arg_types[0] == RECORDOID ||
234
235 /*
236 * If it's column syntax, check for column projection case first.
237 */
238 if (could_be_projection && is_column)
239 {
242 first_arg,
243 location);
244 if (retval)
245 return retval;
246
247 /*
248 * If ParseComplexProjection doesn't recognize it as a projection,
249 * just press on.
250 */
251 }
252
253 /*
254 * func_get_detail looks up the function in the catalogs, does
255 * disambiguation for polymorphic functions, handles inheritance, and
256 * returns the funcid and type and set or singleton status of the
257 * function's return value. It also returns the true argument types to
258 * the function.
259 *
260 * Note: for a named-notation or variadic function call, the reported
261 * "true" types aren't really what is in pg_proc: the types are reordered
262 * to match the given argument order of named arguments, and a variadic
263 * argument is replaced by a suitable number of copies of its element
264 * type. We'll fix up the variadic case below. We may also have to deal
265 * with default arguments.
266 */
267
269
271 actual_arg_types,
272 !func_variadic, true, proc_call,
273 &fgc_flags,
274 &funcid, &rettype, &retset,
275 &nvargs, &vatype,
276 &declared_arg_types, &argdefaults);
277
279
280 /*
281 * Check for various wrong-kind-of-routine cases.
282 */
283
284 /* If this is a CALL, reject things that aren't procedures */
285 if (proc_call &&
291 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
292 errmsg(
"%s is not a procedure",
294 argnames,
295 actual_arg_types)),
296 errhint(
"To call a function, use SELECT."),
298 /* Conversely, if not a CALL, reject procedures */
301 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
302 errmsg(
"%s is a procedure",
304 argnames,
305 actual_arg_types)),
306 errhint(
"To call a procedure, use CALL."),
308
312 {
313 /*
314 * In these cases, complain if there was anything indicating it must
315 * be an aggregate or window function.
316 */
317 if (agg_star)
319 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
320 errmsg(
"%s(*) specified, but %s is not an aggregate function",
324 if (agg_distinct)
326 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
327 errmsg(
"DISTINCT specified, but %s is not an aggregate function",
330 if (agg_within_group)
332 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
333 errmsg(
"WITHIN GROUP specified, but %s is not an aggregate function",
336 if (agg_order !=
NIL)
338 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
339 errmsg(
"ORDER BY specified, but %s is not an aggregate function",
342 if (agg_filter)
344 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
345 errmsg(
"FILTER specified, but %s is not an aggregate function",
348 if (over)
350 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
351 errmsg(
"OVER specified, but %s is not a window function nor an aggregate function",
354 }
355
356 /*
357 * So far so good, so do some fdresult-type-specific processing.
358 */
360 {
361 /* Nothing special to do for these cases. */
362 }
364 {
365 /*
366 * It's an aggregate; fetch needed info from the pg_aggregate entry.
367 */
370 int catDirectArgs;
371
374 elog(
ERROR,
"cache lookup failed for aggregate %u", funcid);
376 aggkind = classForm->aggkind;
377 catDirectArgs = classForm->aggnumdirectargs;
379
380 /* Now check various disallowed cases. */
381 if (AGGKIND_IS_ORDERED_SET(aggkind))
382 {
383 int numAggregatedArgs;
384 int numDirectArgs;
385
386 if (!agg_within_group)
388 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
389 errmsg(
"WITHIN GROUP is required for ordered-set aggregate %s",
392 if (over)
394 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
395 errmsg(
"OVER is not supported for ordered-set aggregate %s",
398 /* gram.y rejects DISTINCT + WITHIN GROUP */
400 /* gram.y rejects VARIADIC + WITHIN GROUP */
402
403 /*
404 * Since func_get_detail was working with an undifferentiated list
405 * of arguments, it might have selected an aggregate that doesn't
406 * really match because it requires a different division of direct
407 * and aggregated arguments. Check that the number of direct
408 * arguments is actually OK; if not, throw an "undefined function"
409 * error, similarly to the case where a misplaced ORDER BY is used
410 * in a regular aggregate call.
411 */
413 numDirectArgs = nargs - numAggregatedArgs;
414 Assert(numDirectArgs >= 0);
415
417 {
418 /* Test is simple if aggregate isn't variadic */
419 if (numDirectArgs != catDirectArgs)
421 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
422 errmsg(
"function %s does not exist",
424 argnames,
425 actual_arg_types)),
426 errhint_plural(
"There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.",
427 "There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.",
428 catDirectArgs,
430 catDirectArgs, numDirectArgs),
432 }
433 else
434 {
435 /*
436 * If it's variadic, we have two cases depending on whether
437 * the agg was "... ORDER BY VARIADIC" or "..., VARIADIC ORDER
438 * BY VARIADIC". It's the latter if catDirectArgs equals
439 * pronargs; to save a catalog lookup, we reverse-engineer
440 * pronargs from the info we got from func_get_detail.
441 */
443
445 if (nvargs > 1)
448 {
449 /* VARIADIC isn't part of direct args, so still easy */
450 if (numDirectArgs != catDirectArgs)
452 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
453 errmsg(
"function %s does not exist",
455 argnames,
456 actual_arg_types)),
457 errhint_plural(
"There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.",
458 "There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.",
459 catDirectArgs,
461 catDirectArgs, numDirectArgs),
463 }
464 else
465 {
466 /*
467 * Both direct and aggregated args were declared variadic.
468 * For a standard ordered-set aggregate, it's okay as long
469 * as there aren't too few direct args. For a
470 * hypothetical-set aggregate, we assume that the
471 * hypothetical arguments are those that matched the
472 * variadic parameter; there must be just as many of them
473 * as there are aggregated arguments.
474 */
475 if (aggkind == AGGKIND_HYPOTHETICAL)
476 {
477 if (nvargs != 2 * numAggregatedArgs)
479 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
480 errmsg(
"function %s does not exist",
482 argnames,
483 actual_arg_types)),
484 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).",
486 nvargs - numAggregatedArgs, numAggregatedArgs),
488 }
489 else
490 {
491 if (nvargs <= numAggregatedArgs)
493 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
494 errmsg(
"function %s does not exist",
496 argnames,
497 actual_arg_types)),
498 errhint_plural(
"There is an ordered-set aggregate %s, but it requires at least %d direct argument.",
499 "There is an ordered-set aggregate %s, but it requires at least %d direct arguments.",
500 catDirectArgs,
502 catDirectArgs),
504 }
505 }
506 }
507
508 /* Check type matching of hypothetical arguments */
509 if (aggkind == AGGKIND_HYPOTHETICAL)
511 actual_arg_types, declared_arg_types);
512 }
513 else
514 {
515 /* Normal aggregate, so it can't have WITHIN GROUP */
516 if (agg_within_group)
518 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
519 errmsg(
"%s is not an ordered-set aggregate, so it cannot have WITHIN GROUP",
522
523 /* It also can't treat nulls as a window function */
526 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
527 errmsg(
"aggregate functions do not accept RESPECT/IGNORE NULLS"),
529 }
530 }
532 {
533 /*
534 * True window functions must be called with a window definition.
535 */
536 if (!over)
538 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
539 errmsg(
"window function %s requires an OVER clause",
542 /* And, per spec, WITHIN GROUP isn't allowed */
543 if (agg_within_group)
545 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
546 errmsg(
"window function %s cannot have WITHIN GROUP",
549 }
551 {
552 /*
553 * We interpreted it as a type coercion. coerce_type can handle these
554 * cases, so why duplicate code...
555 */
557 actual_arg_types[0], rettype, -1,
559 }
561 {
562 /*
563 * We found multiple possible functional matches. If we are dealing
564 * with attribute notation, return failure, letting the caller report
565 * "no such column" (we already determined there wasn't one). If
566 * dealing with function notation, report "ambiguous function",
567 * regardless of whether there's also a column by this name.
568 */
569 if (is_column)
570 return NULL;
571
572 if (proc_call)
574 (
errcode(ERRCODE_AMBIGUOUS_FUNCTION),
575 errmsg(
"procedure %s is not unique",
577 actual_arg_types)),
578 errdetail(
"Could not choose a best candidate procedure."),
579 errhint(
"You might need to add explicit type casts."),
581 else
583 (
errcode(ERRCODE_AMBIGUOUS_FUNCTION),
584 errmsg(
"function %s is not unique",
586 actual_arg_types)),
587 errdetail(
"Could not choose a best candidate function."),
588 errhint(
"You might need to add explicit type casts."),
590 }
591 else
592 {
593 /*
594 * Not found as a function. If we are dealing with attribute
595 * notation, return failure, letting the caller report "no such
596 * column" (we already determined there wasn't one).
597 */
598 if (is_column)
599 return NULL;
600
601 /*
602 * Check for column projection interpretation, since we didn't before.
603 */
604 if (could_be_projection)
605 {
608 first_arg,
609 location);
610 if (retval)
611 return retval;
612 }
613
614 /*
615 * No function, and no column either. Since we're dealing with
616 * function notation, report "function/procedure does not exist".
617 * Depending on what was returned in fgc_flags, we can add some color
618 * to that with detail or hint messages.
619 */
620 if (
list_length(agg_order) > 1 && !agg_within_group)
621 {
622 /* It's agg(x, ORDER BY y,z) ... perhaps misplaced ORDER BY */
624 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
625 errmsg(
"function %s does not exist",
627 actual_arg_types)),
628 errdetail(
"No aggregate function matches the given name and argument types."),
629 errhint(
"Perhaps you misplaced ORDER BY; ORDER BY must appear "
630 "after all regular arguments of the aggregate."),
632 }
633 else if (proc_call)
635 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
636 errmsg(
"procedure %s does not exist",
638 actual_arg_types)),
640 proc_call),
642 else
644 (
errcode(ERRCODE_UNDEFINED_FUNCTION),
645 errmsg(
"function %s does not exist",
647 actual_arg_types)),
649 proc_call),
651 }
652
653 /*
654 * If there are default arguments, we have to include their types in
655 * actual_arg_types for the purpose of checking generic type consistency.
656 * However, we do NOT put them into the generated parse node, because
657 * their actual values might change before the query gets run. The
658 * planner has to insert the up-to-date values at plan time.
659 */
660 nargsplusdefs = nargs;
661 foreach(l, argdefaults)
662 {
664
665 /* probably shouldn't happen ... */
668 (
errcode(ERRCODE_TOO_MANY_ARGUMENTS),
669 errmsg_plural(
"cannot pass more than %d argument to a function",
670 "cannot pass more than %d arguments to a function",
674
675 actual_arg_types[nargsplusdefs++] =
exprType(expr);
676 }
677
678 /*
679 * enforce consistency with polymorphic argument and return types,
680 * possibly adjusting return type or declared_arg_types (which will be
681 * used as the cast destination by make_fn_arguments)
682 */
684 declared_arg_types,
685 nargsplusdefs,
686 rettype,
687 false);
688
689 /* perform the necessary typecasting of arguments */
691
692 /*
693 * If the function isn't actually variadic, forget any VARIADIC decoration
694 * on the call. (Perhaps we should throw an error instead, but
695 * historically we've allowed people to write that.)
696 */
698 {
700 func_variadic = false;
701 }
702
703 /*
704 * If it's a variadic function call, transform the last nvargs arguments
705 * into an array --- unless it's an "any" variadic.
706 */
707 if (nvargs > 0 && vatype != ANYOID)
708 {
710 int non_var_args = nargs - nvargs;
712
713 Assert(non_var_args >= 0);
716
717 newa->elements = vargs;
718 /* assume all the variadic arguments were coerced to the same type */
723 (
errcode(ERRCODE_UNDEFINED_OBJECT),
724 errmsg(
"could not find array type for data type %s",
727 /* array_collid will be set by parse_collate.c */
728 newa->multidims = false;
730
732
733 /* We could not have had VARIADIC marking before ... */
735 /* ... but now, it's a VARIADIC call */
736 func_variadic = true;
737 }
738
739 /*
740 * If an "any" variadic is called with explicit VARIADIC marking, insist
741 * that the variadic parameter be of some array type.
742 */
743 if (nargs > 0 && vatype == ANYOID && func_variadic)
744 {
745 Oid va_arr_typid = actual_arg_types[nargs - 1];
746
749 (
errcode(ERRCODE_DATATYPE_MISMATCH),
750 errmsg(
"VARIADIC argument must be an array"),
753 }
754
755 /* if it returns a set, check that's OK */
756 if (retset)
758
759 /* build the appropriate output structure */
761 {
763
764 funcexpr->
funcid = funcid;
765 funcexpr->funcresulttype = rettype;
766 funcexpr->funcretset = retset;
767 funcexpr->funcvariadic = func_variadic;
768 funcexpr->funcformat = funcformat;
769 /* funccollid and inputcollid will be set by parse_collate.c */
770 funcexpr->
args = fargs;
772
773 retval = (
Node *) funcexpr;
774 }
776 {
777 /* aggregate function */
779
781 aggref->aggtype = rettype;
782 /* aggcollid and inputcollid will be set by parse_collate.c */
783 aggref->aggtranstype =
InvalidOid;
/* will be set by planner */
784 /* aggargtypes will be set by transformAggregateCall */
785 /* aggdirectargs and args will be set by transformAggregateCall */
786 /* aggorder and aggdistinct will be set by transformAggregateCall */
788 aggref->aggstar = agg_star;
789 aggref->aggvariadic = func_variadic;
790 aggref->aggkind = aggkind;
791 aggref->aggpresorted = false;
792 /* agglevelsup will be set by transformAggregateCall */
794 aggref->aggno = -1; /* planner will set aggno and aggtransno */
795 aggref->aggtransno = -1;
797
798 /*
799 * Reject attempt to call a parameterless aggregate without (*)
800 * syntax. This is mere pedantry but some folks insisted ...
801 */
802 if (fargs ==
NIL && !agg_star && !agg_within_group)
804 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
805 errmsg(
"%s(*) must be used to call a parameterless aggregate function",
808
809 if (retset)
811 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
812 errmsg(
"aggregates cannot return sets"),
814
815 /*
816 * We might want to support named arguments later, but disallow it for
817 * now. We'd need to figure out the parsed representation (should the
818 * NamedArgExprs go above or below the TargetEntry nodes?) and then
819 * teach the planner to reorder the list properly. Or maybe we could
820 * make transformAggregateCall do that? However, if you'd also like
821 * to allow default arguments for aggregates, we'd need to do it in
822 * planning to avoid semantic problems.
823 */
826 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
827 errmsg(
"aggregates cannot use named arguments"),
829
830 /* parse_agg.c does additional aggregate-specific processing */
832
833 retval = (
Node *) aggref;
834 }
835 else
836 {
837 /* window function */
839
840 Assert(over);
/* lack of this was checked above */
841 Assert(!agg_within_group);
/* also checked above */
842
844 wfunc->wintype = rettype;
845 /* wincollid and inputcollid will be set by parse_collate.c */
847 /* winref will be set by transformWindowFuncCall */
848 wfunc->winstar = agg_star;
852 wfunc->runCondition =
NIL;
854
855 /*
856 * agg_star is allowed for aggregate functions but distinct isn't
857 */
858 if (agg_distinct)
860 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
861 errmsg(
"DISTINCT is not implemented for window functions"),
863
864 /*
865 * Reject attempt to call a parameterless aggregate without (*)
866 * syntax. This is mere pedantry but some folks insisted ...
867 */
868 if (wfunc->winagg && fargs ==
NIL && !agg_star)
870 (
errcode(ERRCODE_WRONG_OBJECT_TYPE),
871 errmsg(
"%s(*) must be used to call a parameterless aggregate function",
874
875 /*
876 * ordered aggs not allowed in windows yet
877 */
878 if (agg_order !=
NIL)
880 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
881 errmsg(
"aggregate ORDER BY is not implemented for window functions"),
883
884 /*
885 * FILTER is not yet supported with true window functions
886 */
887 if (!wfunc->winagg && agg_filter)
889 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
890 errmsg(
"FILTER is not implemented for non-aggregate window functions"),
892
893 /*
894 * Window functions can't either take or return sets
895 */
898 (
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
899 errmsg(
"window function calls cannot contain set-returning function calls"),
900 errhint(
"You might be able to move the set-returning function into a LATERAL FROM item."),
903
904 if (retset)
906 (
errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
907 errmsg(
"window functions cannot return sets"),
909
910 /* parse_agg.c does additional window-func-specific processing */
912
913 retval = (
Node *) wfunc;
914 }
915
916 /* if it returns a set, remember it for error checks at higher levels */
917 if (retset)
919
920 return retval;
921}
int errdetail(const char *fmt,...)
int errhint_plural(const char *fmt_singular, const char *fmt_plural, unsigned long n,...)
List * list_copy_tail(const List *oldlist, int nskip)
List * list_truncate(List *list, int new_size)
Oid get_base_element_type(Oid typid)
Oid get_array_type(Oid typid)
Oid exprType(const Node *expr)
void transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, WindowDef *windef)
void transformAggregateCall(ParseState *pstate, Aggref *agg, List *args, List *aggorder, bool agg_distinct)
Node * transformWhereClause(ParseState *pstate, Node *clause, ParseExprKind exprKind, const char *constructName)
Oid enforce_generic_type_consistency(const Oid *actual_arg_types, Oid *declared_arg_types, int nargs, Oid rettype, bool allow_poly)
FuncDetailCode func_get_detail(List *funcname, List *fargs, List *fargnames, int nargs, Oid *argtypes, bool expand_variadic, bool expand_defaults, bool include_out_arguments, int *fgc_flags, Oid *funcid, Oid *rettype, bool *retset, int *nvargs, Oid *vatype, Oid **true_typeids, List **argdefaults)
static Node * ParseComplexProjection(ParseState *pstate, const char *funcname, Node *first_arg, int location)
void make_fn_arguments(ParseState *pstate, List *fargs, Oid *actual_arg_types, Oid *declared_arg_types)
static void unify_hypothetical_args(ParseState *pstate, List *fargs, int numAggregatedArgs, Oid *actual_arg_types, Oid *declared_arg_types)
void check_srf_call_placement(ParseState *pstate, Node *last_srf, int location)
static int func_lookup_failure_details(int fgc_flags, List *argnames, bool proc_call)
void cancel_parser_errposition_callback(ParseCallbackState *pcbstate)
void setup_parser_errposition_callback(ParseCallbackState *pcbstate, ParseState *pstate, int location)
FormData_pg_aggregate * Form_pg_aggregate
#define foreach_delete_current(lst, var_or_cell)
static void * fn(void *arg)