| 1 |
/*------------------------------------------------------------------------- |
| 2 |
* |
| 3 |
* parse_func.c |
| 4 |
* handle function calls in parser |
| 5 |
* |
| 6 |
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 7 |
* Portions Copyright (c) 1994, Regents of the University of California |
| 8 |
* |
| 9 |
* |
| 10 |
* IDENTIFICATION |
| 11 |
* src/backend/parser/parse_func.c |
| 12 |
* |
| 13 |
*------------------------------------------------------------------------- |
| 14 |
*/ |
| 15 |
#include "postgres.h" |
| 16 |
|
| 17 |
#include "access/htup_details.h" |
| 18 |
#include "catalog/pg_aggregate.h" |
| 19 |
#include "catalog/pg_proc.h" |
| 20 |
#include "catalog/pg_type.h" |
| 21 |
#include "funcapi.h" |
| 22 |
#include "lib/stringinfo.h" |
| 23 |
#include "nodes/makefuncs.h" |
| 24 |
#include "nodes/nodeFuncs.h" |
| 25 |
#include "parser/parse_agg.h" |
| 26 |
#include "parser/parse_clause.h" |
| 27 |
#include "parser/parse_coerce.h" |
| 28 |
#include "parser/parse_expr.h" |
| 29 |
#include "parser/parse_func.h" |
| 30 |
#include "parser/parse_relation.h" |
| 31 |
#include "parser/parse_target.h" |
| 32 |
#include "parser/parse_type.h" |
| 33 |
#include "utils/builtins.h" |
| 34 |
#include "utils/lsyscache.h" |
| 35 |
#include "utils/syscache.h" |
| 36 |
|
| 37 |
|
| 38 |
/* Possible error codes from LookupFuncNameInternal */ |
| 39 |
typedef enum |
| 40 |
{ |
| 41 |
FUNCLOOKUP_NOSUCHFUNC, |
| 42 |
FUNCLOOKUP_AMBIGUOUS, |
| 43 |
} FuncLookupError; |
| 44 |
|
| 45 |
static void unify_hypothetical_args(ParseState *pstate, |
| 46 |
List *fargs, int numAggregatedArgs, |
| 47 |
Oid *actual_arg_types, Oid *declared_arg_types); |
| 48 |
static Oid FuncNameAsType(List *funcname); |
| 49 |
static Node *ParseComplexProjection(ParseState *pstate, const char *funcname, |
| 50 |
Node *first_arg, int location); |
| 51 |
static Oid LookupFuncNameInternal(ObjectType objtype, List *funcname, |
| 52 |
int nargs, const Oid *argtypes, |
| 53 |
bool include_out_arguments, bool missing_ok, |
| 54 |
FuncLookupError *lookupError); |
| 55 |
|
| 56 |
|
| 57 |
/* |
| 58 |
* Parse a function call |
| 59 |
* |
| 60 |
* For historical reasons, Postgres tries to treat the notations tab.col |
| 61 |
* and col(tab) as equivalent: if a single-argument function call has an |
| 62 |
* argument of complex type and the (unqualified) function name matches |
| 63 |
* any attribute of the type, we can interpret it as a column projection. |
| 64 |
* Conversely a function of a single complex-type argument can be written |
| 65 |
* like a column reference, allowing functions to act like computed columns. |
| 66 |
* |
| 67 |
* If both interpretations are possible, we prefer the one matching the |
| 68 |
* syntactic form, but otherwise the form does not matter. |
| 69 |
* |
| 70 |
* Hence, both cases come through here. If fn is null, we're dealing with |
| 71 |
* column syntax not function syntax. In the function-syntax case, |
| 72 |
* the FuncCall struct is needed to carry various decoration that applies |
| 73 |
* to aggregate and window functions. |
| 74 |
* |
| 75 |
* Also, when fn is null, we return NULL on failure rather than |
| 76 |
* reporting a no-such-function error. |
| 77 |
* |
| 78 |
* The argument expressions (in fargs) must have been transformed |
| 79 |
* already. However, nothing in *fn has been transformed. |
| 80 |
* |
| 81 |
* last_srf should be a copy of pstate->p_last_srf from just before we |
| 82 |
* started transforming fargs. If the caller knows that fargs couldn't |
| 83 |
* contain any SRF calls, last_srf can just be pstate->p_last_srf. |
| 84 |
* |
| 85 |
* proc_call is true if we are considering a CALL statement, so that the |
| 86 |
* name must resolve to a procedure name, not anything else. This flag |
| 87 |
* also specifies that the argument list includes any OUT-mode arguments. |
| 88 |
*/ |
| 89 |
Node * |
| 90 |
ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, |
| 91 |
Node *last_srf, FuncCall *fn, bool proc_call, int location) |
| 92 |
{ |
| 93 |
bool is_column = (fn == NULL); |
| 94 |
List *agg_order = (fn ? fn->agg_order : NIL); |
| 95 |
Expr *agg_filter = NULL; |
| 96 |
WindowDef *over = (fn ? fn->over : NULL); |
| 97 |
bool agg_within_group = (fn ? fn->agg_within_group : false); |
| 98 |
bool agg_star = (fn ? fn->agg_star : false); |
| 99 |
bool agg_distinct = (fn ? fn->agg_distinct : false); |
| 100 |
bool func_variadic = (fn ? fn->func_variadic : false); |
| 101 |
CoercionForm funcformat = (fn ? fn->funcformat : COERCE_EXPLICIT_CALL); |
| 102 |
bool could_be_projection; |
| 103 |
Oid rettype; |
| 104 |
Oid funcid; |
| 105 |
ListCell *l; |
| 106 |
Node *first_arg = NULL; |
| 107 |
int nargs; |
| 108 |
int nargsplusdefs; |
| 109 |
Oid actual_arg_types[FUNC_MAX_ARGS]; |
| 110 |
Oid *declared_arg_types; |
| 111 |
List *argnames; |
| 112 |
List *argdefaults; |
| 113 |
Node *retval; |
| 114 |
bool retset; |
| 115 |
int nvargs; |
| 116 |
Oid vatype; |
| 117 |
FuncDetailCode fdresult; |
| 118 |
char aggkind = 0; |
| 119 |
ParseCallbackState pcbstate; |
| 120 |
|
| 121 |
/* |
| 122 |
* If there's an aggregate filter, transform it using transformWhereClause |
| 123 |
*/ |
| 124 |
if (fn && fn->agg_filter != NULL) |
| 125 |
agg_filter = (Expr *) transformWhereClause(pstate, fn->agg_filter, |
| 126 |
EXPR_KIND_FILTER, |
| 127 |
"FILTER"); |
| 128 |
|
| 129 |
/* |
| 130 |
* Most of the rest of the parser just assumes that functions do not have |
| 131 |
* more than FUNC_MAX_ARGS parameters. We have to test here to protect |
| 132 |
* against array overruns, etc. Of course, this may not be a function, |
| 133 |
* but the test doesn't hurt. |
| 134 |
*/ |
| 135 |
if (list_length(fargs) > FUNC_MAX_ARGS) |
| 136 |
ereport(ERROR, |
| 137 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 138 |
errmsg_plural("cannot pass more than %d argument to a function", |
| 139 |
"cannot pass more than %d arguments to a function", |
| 140 |
FUNC_MAX_ARGS, |
| 141 |
FUNC_MAX_ARGS), |
| 142 |
parser_errposition(pstate, location))); |
| 143 |
|
| 144 |
/* |
| 145 |
* Extract arg type info in preparation for function lookup. |
| 146 |
* |
| 147 |
* If any arguments are Param markers of type VOID, we discard them from |
| 148 |
* the parameter list. This is a hack to allow the JDBC driver to not have |
| 149 |
* to distinguish "input" and "output" parameter symbols while parsing |
| 150 |
* function-call constructs. Don't do this if dealing with column syntax, |
| 151 |
* nor if we had WITHIN GROUP (because in that case it's critical to keep |
| 152 |
* the argument count unchanged). |
| 153 |
*/ |
| 154 |
nargs = 0; |
| 155 |
foreach(l, fargs) |
| 156 |
{ |
| 157 |
Node *arg = lfirst(l); |
| 158 |
Oid argtype = exprType(arg); |
| 159 |
|
| 160 |
if (argtype == VOIDOID && IsA(arg, Param) && |
| 161 |
!is_column && !agg_within_group) |
| 162 |
{ |
| 163 |
fargs = foreach_delete_current(fargs, l); |
| 164 |
continue; |
| 165 |
} |
| 166 |
|
| 167 |
actual_arg_types[nargs++] = argtype; |
| 168 |
} |
| 169 |
|
| 170 |
/* |
| 171 |
* Check for named arguments; if there are any, build a list of names. |
| 172 |
* |
| 173 |
* We allow mixed notation (some named and some not), but only with all |
| 174 |
* the named parameters after all the unnamed ones. So the name list |
| 175 |
* corresponds to the last N actual parameters and we don't need any extra |
| 176 |
* bookkeeping to match things up. |
| 177 |
*/ |
| 178 |
argnames = NIL; |
| 179 |
foreach(l, fargs) |
| 180 |
{ |
| 181 |
Node *arg = lfirst(l); |
| 182 |
|
| 183 |
if (IsA(arg, NamedArgExpr)) |
| 184 |
{ |
| 185 |
NamedArgExpr *na = (NamedArgExpr *) arg; |
| 186 |
ListCell *lc; |
| 187 |
|
| 188 |
/* Reject duplicate arg names */ |
| 189 |
foreach(lc, argnames) |
| 190 |
{ |
| 191 |
if (strcmp(na->name, (char *) lfirst(lc)) == 0) |
| 192 |
ereport(ERROR, |
| 193 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 194 |
errmsg("argument name \"%s\" used more than once", |
| 195 |
na->name), |
| 196 |
parser_errposition(pstate, na->location))); |
| 197 |
} |
| 198 |
argnames = lappend(argnames, na->name); |
| 199 |
} |
| 200 |
else |
| 201 |
{ |
| 202 |
if (argnames != NIL) |
| 203 |
ereport(ERROR, |
| 204 |
(errcode(ERRCODE_SYNTAX_ERROR), |
| 205 |
errmsg("positional argument cannot follow named argument"), |
| 206 |
parser_errposition(pstate, exprLocation(arg)))); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (fargs) |
| 211 |
{ |
| 212 |
first_arg = linitial(fargs); |
| 213 |
Assert(first_arg != NULL); |
| 214 |
} |
| 215 |
|
| 216 |
/* |
| 217 |
* Decide whether it's legitimate to consider the construct to be a column |
| 218 |
* projection. For that, there has to be a single argument of complex |
| 219 |
* type, the function name must not be qualified, and there cannot be any |
| 220 |
* syntactic decoration that'd require it to be a function (such as |
| 221 |
* aggregate or variadic decoration, or named arguments). |
| 222 |
*/ |
| 223 |
could_be_projection = (nargs == 1 && !proc_call && |
| 224 |
agg_order == NIL && agg_filter == NULL && |
| 225 |
!agg_star && !agg_distinct && over == NULL && |
| 226 |
!func_variadic && argnames == NIL && |
| 227 |
list_length(funcname) == 1 && |
| 228 |
(actual_arg_types[0] == RECORDOID || |
| 229 |
ISCOMPLEX(actual_arg_types[0]))); |
| 230 |
|
| 231 |
/* |
| 232 |
* If it's column syntax, check for column projection case first. |
| 233 |
*/ |
| 234 |
if (could_be_projection && is_column) |
| 235 |
{ |
| 236 |
retval = ParseComplexProjection(pstate, |
| 237 |
strVal(linitial(funcname)), |
| 238 |
first_arg, |
| 239 |
location); |
| 240 |
if (retval) |
| 241 |
return retval; |
| 242 |
|
| 243 |
/* |
| 244 |
* If ParseComplexProjection doesn't recognize it as a projection, |
| 245 |
* just press on. |
| 246 |
*/ |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* func_get_detail looks up the function in the catalogs, does |
| 251 |
* disambiguation for polymorphic functions, handles inheritance, and |
| 252 |
* returns the funcid and type and set or singleton status of the |
| 253 |
* function's return value. It also returns the true argument types to |
| 254 |
* the function. |
| 255 |
* |
| 256 |
* Note: for a named-notation or variadic function call, the reported |
| 257 |
* "true" types aren't really what is in pg_proc: the types are reordered |
| 258 |
* to match the given argument order of named arguments, and a variadic |
| 259 |
* argument is replaced by a suitable number of copies of its element |
| 260 |
* type. We'll fix up the variadic case below. We may also have to deal |
| 261 |
* with default arguments. |
| 262 |
*/ |
| 263 |
|
| 264 |
setup_parser_errposition_callback(&pcbstate, pstate, location); |
| 265 |
|
| 266 |
fdresult = func_get_detail(funcname, fargs, argnames, nargs, |
| 267 |
actual_arg_types, |
| 268 |
!func_variadic, true, proc_call, |
| 269 |
&funcid, &rettype, &retset, |
| 270 |
&nvargs, &vatype, |
| 271 |
&declared_arg_types, &argdefaults); |
| 272 |
|
| 273 |
cancel_parser_errposition_callback(&pcbstate); |
| 274 |
|
| 275 |
/* |
| 276 |
* Check for various wrong-kind-of-routine cases. |
| 277 |
*/ |
| 278 |
|
| 279 |
/* If this is a CALL, reject things that aren't procedures */ |
| 280 |
if (proc_call && |
| 281 |
(fdresult == FUNCDETAIL_NORMAL || |
| 282 |
fdresult == FUNCDETAIL_AGGREGATE || |
| 283 |
fdresult == FUNCDETAIL_WINDOWFUNC || |
| 284 |
fdresult == FUNCDETAIL_COERCION)) |
| 285 |
ereport(ERROR, |
| 286 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 287 |
errmsg("%s is not a procedure", |
| 288 |
func_signature_string(funcname, nargs, |
| 289 |
argnames, |
| 290 |
actual_arg_types)), |
| 291 |
errhint("To call a function, use SELECT."), |
| 292 |
parser_errposition(pstate, location))); |
| 293 |
/* Conversely, if not a CALL, reject procedures */ |
| 294 |
if (fdresult == FUNCDETAIL_PROCEDURE && !proc_call) |
| 295 |
ereport(ERROR, |
| 296 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 297 |
errmsg("%s is a procedure", |
| 298 |
func_signature_string(funcname, nargs, |
| 299 |
argnames, |
| 300 |
actual_arg_types)), |
| 301 |
errhint("To call a procedure, use CALL."), |
| 302 |
parser_errposition(pstate, location))); |
| 303 |
|
| 304 |
if (fdresult == FUNCDETAIL_NORMAL || |
| 305 |
fdresult == FUNCDETAIL_PROCEDURE || |
| 306 |
fdresult == FUNCDETAIL_COERCION) |
| 307 |
{ |
| 308 |
/* |
| 309 |
* In these cases, complain if there was anything indicating it must |
| 310 |
* be an aggregate or window function. |
| 311 |
*/ |
| 312 |
if (agg_star) |
| 313 |
ereport(ERROR, |
| 314 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 315 |
errmsg("%s(*) specified, but %s is not an aggregate function", |
| 316 |
NameListToString(funcname), |
| 317 |
NameListToString(funcname)), |
| 318 |
parser_errposition(pstate, location))); |
| 319 |
if (agg_distinct) |
| 320 |
ereport(ERROR, |
| 321 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 322 |
errmsg("DISTINCT specified, but %s is not an aggregate function", |
| 323 |
NameListToString(funcname)), |
| 324 |
parser_errposition(pstate, location))); |
| 325 |
if (agg_within_group) |
| 326 |
ereport(ERROR, |
| 327 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 328 |
errmsg("WITHIN GROUP specified, but %s is not an aggregate function", |
| 329 |
NameListToString(funcname)), |
| 330 |
parser_errposition(pstate, location))); |
| 331 |
if (agg_order != NIL) |
| 332 |
ereport(ERROR, |
| 333 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 334 |
errmsg("ORDER BY specified, but %s is not an aggregate function", |
| 335 |
NameListToString(funcname)), |
| 336 |
parser_errposition(pstate, location))); |
| 337 |
if (agg_filter) |
| 338 |
ereport(ERROR, |
| 339 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 340 |
errmsg("FILTER specified, but %s is not an aggregate function", |
| 341 |
NameListToString(funcname)), |
| 342 |
parser_errposition(pstate, location))); |
| 343 |
if (over) |
| 344 |
ereport(ERROR, |
| 345 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 346 |
errmsg("OVER specified, but %s is not a window function nor an aggregate function", |
| 347 |
NameListToString(funcname)), |
| 348 |
parser_errposition(pstate, location))); |
| 349 |
} |
| 350 |
|
| 351 |
/* |
| 352 |
* So far so good, so do some fdresult-type-specific processing. |
| 353 |
*/ |
| 354 |
if (fdresult == FUNCDETAIL_NORMAL || fdresult == FUNCDETAIL_PROCEDURE) |
| 355 |
{ |
| 356 |
/* Nothing special to do for these cases. */ |
| 357 |
} |
| 358 |
else if (fdresult == FUNCDETAIL_AGGREGATE) |
| 359 |
{ |
| 360 |
/* |
| 361 |
* It's an aggregate; fetch needed info from the pg_aggregate entry. |
| 362 |
*/ |
| 363 |
HeapTuple tup; |
| 364 |
Form_pg_aggregate classForm; |
| 365 |
int catDirectArgs; |
| 366 |
|
| 367 |
tup = SearchSysCache1(AGGFNOID, ObjectIdGetDatum(funcid)); |
| 368 |
if (!HeapTupleIsValid(tup)) /* should not happen */ |
| 369 |
elog(ERROR, "cache lookup failed for aggregate %u", funcid); |
| 370 |
classForm = (Form_pg_aggregate) GETSTRUCT(tup); |
| 371 |
aggkind = classForm->aggkind; |
| 372 |
catDirectArgs = classForm->aggnumdirectargs; |
| 373 |
ReleaseSysCache(tup); |
| 374 |
|
| 375 |
/* Now check various disallowed cases. */ |
| 376 |
if (AGGKIND_IS_ORDERED_SET(aggkind)) |
| 377 |
{ |
| 378 |
int numAggregatedArgs; |
| 379 |
int numDirectArgs; |
| 380 |
|
| 381 |
if (!agg_within_group) |
| 382 |
ereport(ERROR, |
| 383 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 384 |
errmsg("WITHIN GROUP is required for ordered-set aggregate %s", |
| 385 |
NameListToString(funcname)), |
| 386 |
parser_errposition(pstate, location))); |
| 387 |
if (over) |
| 388 |
ereport(ERROR, |
| 389 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 390 |
errmsg("OVER is not supported for ordered-set aggregate %s", |
| 391 |
NameListToString(funcname)), |
| 392 |
parser_errposition(pstate, location))); |
| 393 |
/* gram.y rejects DISTINCT + WITHIN GROUP */ |
| 394 |
Assert(!agg_distinct); |
| 395 |
/* gram.y rejects VARIADIC + WITHIN GROUP */ |
| 396 |
Assert(!func_variadic); |
| 397 |
|
| 398 |
/* |
| 399 |
* Since func_get_detail was working with an undifferentiated list |
| 400 |
* of arguments, it might have selected an aggregate that doesn't |
| 401 |
* really match because it requires a different division of direct |
| 402 |
* and aggregated arguments. Check that the number of direct |
| 403 |
* arguments is actually OK; if not, throw an "undefined function" |
| 404 |
* error, similarly to the case where a misplaced ORDER BY is used |
| 405 |
* in a regular aggregate call. |
| 406 |
*/ |
| 407 |
numAggregatedArgs = list_length(agg_order); |
| 408 |
numDirectArgs = nargs - numAggregatedArgs; |
| 409 |
Assert(numDirectArgs >= 0); |
| 410 |
|
| 411 |
if (!OidIsValid(vatype)) |
| 412 |
{ |
| 413 |
/* Test is simple if aggregate isn't variadic */ |
| 414 |
if (numDirectArgs != catDirectArgs) |
| 415 |
ereport(ERROR, |
| 416 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 417 |
errmsg("function %s does not exist", |
| 418 |
func_signature_string(funcname, nargs, |
| 419 |
argnames, |
| 420 |
actual_arg_types)), |
| 421 |
errhint_plural("There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.", |
| 422 |
"There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", |
| 423 |
catDirectArgs, |
| 424 |
NameListToString(funcname), |
| 425 |
catDirectArgs, numDirectArgs), |
| 426 |
parser_errposition(pstate, location))); |
| 427 |
} |
| 428 |
else |
| 429 |
{ |
| 430 |
/* |
| 431 |
* If it's variadic, we have two cases depending on whether |
| 432 |
* the agg was "... ORDER BY VARIADIC" or "..., VARIADIC ORDER |
| 433 |
* BY VARIADIC". It's the latter if catDirectArgs equals |
| 434 |
* pronargs; to save a catalog lookup, we reverse-engineer |
| 435 |
* pronargs from the info we got from func_get_detail. |
| 436 |
*/ |
| 437 |
int pronargs; |
| 438 |
|
| 439 |
pronargs = nargs; |
| 440 |
if (nvargs > 1) |
| 441 |
pronargs -= nvargs - 1; |
| 442 |
if (catDirectArgs < pronargs) |
| 443 |
{ |
| 444 |
/* VARIADIC isn't part of direct args, so still easy */ |
| 445 |
if (numDirectArgs != catDirectArgs) |
| 446 |
ereport(ERROR, |
| 447 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 448 |
errmsg("function %s does not exist", |
| 449 |
func_signature_string(funcname, nargs, |
| 450 |
argnames, |
| 451 |
actual_arg_types)), |
| 452 |
errhint_plural("There is an ordered-set aggregate %s, but it requires %d direct argument, not %d.", |
| 453 |
"There is an ordered-set aggregate %s, but it requires %d direct arguments, not %d.", |
| 454 |
catDirectArgs, |
| 455 |
NameListToString(funcname), |
| 456 |
catDirectArgs, numDirectArgs), |
| 457 |
parser_errposition(pstate, location))); |
| 458 |
} |
| 459 |
else |
| 460 |
{ |
| 461 |
/* |
| 462 |
* Both direct and aggregated args were declared variadic. |
| 463 |
* For a standard ordered-set aggregate, it's okay as long |
| 464 |
* as there aren't too few direct args. For a |
| 465 |
* hypothetical-set aggregate, we assume that the |
| 466 |
* hypothetical arguments are those that matched the |
| 467 |
* variadic parameter; there must be just as many of them |
| 468 |
* as there are aggregated arguments. |
| 469 |
*/ |
| 470 |
if (aggkind == AGGKIND_HYPOTHETICAL) |
| 471 |
{ |
| 472 |
if (nvargs != 2 * numAggregatedArgs) |
| 473 |
ereport(ERROR, |
| 474 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 475 |
errmsg("function %s does not exist", |
| 476 |
func_signature_string(funcname, nargs, |
| 477 |
argnames, |
| 478 |
actual_arg_types)), |
| 479 |
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).", |
| 480 |
NameListToString(funcname), |
| 481 |
nvargs - numAggregatedArgs, numAggregatedArgs), |
| 482 |
parser_errposition(pstate, location))); |
| 483 |
} |
| 484 |
else |
| 485 |
{ |
| 486 |
if (nvargs <= numAggregatedArgs) |
| 487 |
ereport(ERROR, |
| 488 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 489 |
errmsg("function %s does not exist", |
| 490 |
func_signature_string(funcname, nargs, |
| 491 |
argnames, |
| 492 |
actual_arg_types)), |
| 493 |
errhint_plural("There is an ordered-set aggregate %s, but it requires at least %d direct argument.", |
| 494 |
"There is an ordered-set aggregate %s, but it requires at least %d direct arguments.", |
| 495 |
catDirectArgs, |
| 496 |
NameListToString(funcname), |
| 497 |
catDirectArgs), |
| 498 |
parser_errposition(pstate, location))); |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/* Check type matching of hypothetical arguments */ |
| 504 |
if (aggkind == AGGKIND_HYPOTHETICAL) |
| 505 |
unify_hypothetical_args(pstate, fargs, numAggregatedArgs, |
| 506 |
actual_arg_types, declared_arg_types); |
| 507 |
} |
| 508 |
else |
| 509 |
{ |
| 510 |
/* Normal aggregate, so it can't have WITHIN GROUP */ |
| 511 |
if (agg_within_group) |
| 512 |
ereport(ERROR, |
| 513 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 514 |
errmsg("%s is not an ordered-set aggregate, so it cannot have WITHIN GROUP", |
| 515 |
NameListToString(funcname)), |
| 516 |
parser_errposition(pstate, location))); |
| 517 |
} |
| 518 |
} |
| 519 |
else if (fdresult == FUNCDETAIL_WINDOWFUNC) |
| 520 |
{ |
| 521 |
/* |
| 522 |
* True window functions must be called with a window definition. |
| 523 |
*/ |
| 524 |
if (!over) |
| 525 |
ereport(ERROR, |
| 526 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 527 |
errmsg("window function %s requires an OVER clause", |
| 528 |
NameListToString(funcname)), |
| 529 |
parser_errposition(pstate, location))); |
| 530 |
/* And, per spec, WITHIN GROUP isn't allowed */ |
| 531 |
if (agg_within_group) |
| 532 |
ereport(ERROR, |
| 533 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 534 |
errmsg("window function %s cannot have WITHIN GROUP", |
| 535 |
NameListToString(funcname)), |
| 536 |
parser_errposition(pstate, location))); |
| 537 |
} |
| 538 |
else if (fdresult == FUNCDETAIL_COERCION) |
| 539 |
{ |
| 540 |
/* |
| 541 |
* We interpreted it as a type coercion. coerce_type can handle these |
| 542 |
* cases, so why duplicate code... |
| 543 |
*/ |
| 544 |
return coerce_type(pstate, linitial(fargs), |
| 545 |
actual_arg_types[0], rettype, -1, |
| 546 |
COERCION_EXPLICIT, COERCE_EXPLICIT_CALL, location); |
| 547 |
} |
| 548 |
else if (fdresult == FUNCDETAIL_MULTIPLE) |
| 549 |
{ |
| 550 |
/* |
| 551 |
* We found multiple possible functional matches. If we are dealing |
| 552 |
* with attribute notation, return failure, letting the caller report |
| 553 |
* "no such column" (we already determined there wasn't one). If |
| 554 |
* dealing with function notation, report "ambiguous function", |
| 555 |
* regardless of whether there's also a column by this name. |
| 556 |
*/ |
| 557 |
if (is_column) |
| 558 |
return NULL; |
| 559 |
|
| 560 |
if (proc_call) |
| 561 |
ereport(ERROR, |
| 562 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 563 |
errmsg("procedure %s is not unique", |
| 564 |
func_signature_string(funcname, nargs, argnames, |
| 565 |
actual_arg_types)), |
| 566 |
errhint("Could not choose a best candidate procedure. " |
| 567 |
"You might need to add explicit type casts."), |
| 568 |
parser_errposition(pstate, location))); |
| 569 |
else |
| 570 |
ereport(ERROR, |
| 571 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 572 |
errmsg("function %s is not unique", |
| 573 |
func_signature_string(funcname, nargs, argnames, |
| 574 |
actual_arg_types)), |
| 575 |
errhint("Could not choose a best candidate function. " |
| 576 |
"You might need to add explicit type casts."), |
| 577 |
parser_errposition(pstate, location))); |
| 578 |
} |
| 579 |
else |
| 580 |
{ |
| 581 |
/* |
| 582 |
* Not found as a function. If we are dealing with attribute |
| 583 |
* notation, return failure, letting the caller report "no such |
| 584 |
* column" (we already determined there wasn't one). |
| 585 |
*/ |
| 586 |
if (is_column) |
| 587 |
return NULL; |
| 588 |
|
| 589 |
/* |
| 590 |
* Check for column projection interpretation, since we didn't before. |
| 591 |
*/ |
| 592 |
if (could_be_projection) |
| 593 |
{ |
| 594 |
retval = ParseComplexProjection(pstate, |
| 595 |
strVal(linitial(funcname)), |
| 596 |
first_arg, |
| 597 |
location); |
| 598 |
if (retval) |
| 599 |
return retval; |
| 600 |
} |
| 601 |
|
| 602 |
/* |
| 603 |
* No function, and no column either. Since we're dealing with |
| 604 |
* function notation, report "function does not exist". |
| 605 |
*/ |
| 606 |
if (list_length(agg_order) > 1 && !agg_within_group) |
| 607 |
{ |
| 608 |
/* It's agg(x, ORDER BY y,z) ... perhaps misplaced ORDER BY */ |
| 609 |
ereport(ERROR, |
| 610 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 611 |
errmsg("function %s does not exist", |
| 612 |
func_signature_string(funcname, nargs, argnames, |
| 613 |
actual_arg_types)), |
| 614 |
errhint("No aggregate function matches the given name and argument types. " |
| 615 |
"Perhaps you misplaced ORDER BY; ORDER BY must appear " |
| 616 |
"after all regular arguments of the aggregate."), |
| 617 |
parser_errposition(pstate, location))); |
| 618 |
} |
| 619 |
else if (proc_call) |
| 620 |
ereport(ERROR, |
| 621 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 622 |
errmsg("procedure %s does not exist", |
| 623 |
func_signature_string(funcname, nargs, argnames, |
| 624 |
actual_arg_types)), |
| 625 |
errhint("No procedure matches the given name and argument types. " |
| 626 |
"You might need to add explicit type casts."), |
| 627 |
parser_errposition(pstate, location))); |
| 628 |
else |
| 629 |
ereport(ERROR, |
| 630 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 631 |
errmsg("function %s does not exist", |
| 632 |
func_signature_string(funcname, nargs, argnames, |
| 633 |
actual_arg_types)), |
| 634 |
errhint("No function matches the given name and argument types. " |
| 635 |
"You might need to add explicit type casts."), |
| 636 |
parser_errposition(pstate, location))); |
| 637 |
} |
| 638 |
|
| 639 |
/* |
| 640 |
* If there are default arguments, we have to include their types in |
| 641 |
* actual_arg_types for the purpose of checking generic type consistency. |
| 642 |
* However, we do NOT put them into the generated parse node, because |
| 643 |
* their actual values might change before the query gets run. The |
| 644 |
* planner has to insert the up-to-date values at plan time. |
| 645 |
*/ |
| 646 |
nargsplusdefs = nargs; |
| 647 |
foreach(l, argdefaults) |
| 648 |
{ |
| 649 |
Node *expr = (Node *) lfirst(l); |
| 650 |
|
| 651 |
/* probably shouldn't happen ... */ |
| 652 |
if (nargsplusdefs >= FUNC_MAX_ARGS) |
| 653 |
ereport(ERROR, |
| 654 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 655 |
errmsg_plural("cannot pass more than %d argument to a function", |
| 656 |
"cannot pass more than %d arguments to a function", |
| 657 |
FUNC_MAX_ARGS, |
| 658 |
FUNC_MAX_ARGS), |
| 659 |
parser_errposition(pstate, location))); |
| 660 |
|
| 661 |
actual_arg_types[nargsplusdefs++] = exprType(expr); |
| 662 |
} |
| 663 |
|
| 664 |
/* |
| 665 |
* enforce consistency with polymorphic argument and return types, |
| 666 |
* possibly adjusting return type or declared_arg_types (which will be |
| 667 |
* used as the cast destination by make_fn_arguments) |
| 668 |
*/ |
| 669 |
rettype = enforce_generic_type_consistency(actual_arg_types, |
| 670 |
declared_arg_types, |
| 671 |
nargsplusdefs, |
| 672 |
rettype, |
| 673 |
false); |
| 674 |
|
| 675 |
/* |
| 676 |
* Reject any attempt to call a function that takes or returns type |
| 677 |
* internal from SQL. (The FUNCDETAIL_COERCION case does not reach this |
| 678 |
* check because of the early return above, but that's okay because we |
| 679 |
* disallow coercions to or from type internal.) Note that we are |
| 680 |
* checking the resolved argument and result types, so this will reject |
| 681 |
* calls to polymorphic functions that pass internal-type arguments. The |
| 682 |
* casting rules should prevent that anyway, since we won't cast internal |
| 683 |
* to any polymorphic type, but no harm in being doubly sure. |
| 684 |
*/ |
| 685 |
for (int i = 0; i < nargsplusdefs; i++) |
| 686 |
{ |
| 687 |
if (declared_arg_types[i] == INTERNALOID) |
| 688 |
ereport(ERROR, |
| 689 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 690 |
errmsg("functions accepting type \"%s\" cannot be called explicitly", |
| 691 |
"internal"), |
| 692 |
parser_errposition(pstate, location))); |
| 693 |
} |
| 694 |
if (rettype == INTERNALOID) |
| 695 |
ereport(ERROR, |
| 696 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 697 |
errmsg("functions returning type \"%s\" cannot be called explicitly", |
| 698 |
"internal"), |
| 699 |
parser_errposition(pstate, location))); |
| 700 |
|
| 701 |
/* perform the necessary typecasting of arguments */ |
| 702 |
make_fn_arguments(pstate, fargs, actual_arg_types, declared_arg_types); |
| 703 |
|
| 704 |
/* |
| 705 |
* If the function isn't actually variadic, forget any VARIADIC decoration |
| 706 |
* on the call. (Perhaps we should throw an error instead, but |
| 707 |
* historically we've allowed people to write that.) |
| 708 |
*/ |
| 709 |
if (!OidIsValid(vatype)) |
| 710 |
{ |
| 711 |
Assert(nvargs == 0); |
| 712 |
func_variadic = false; |
| 713 |
} |
| 714 |
|
| 715 |
/* |
| 716 |
* If it's a variadic function call, transform the last nvargs arguments |
| 717 |
* into an array --- unless it's an "any" variadic. |
| 718 |
*/ |
| 719 |
if (nvargs > 0 && vatype != ANYOID) |
| 720 |
{ |
| 721 |
ArrayExpr *newa = makeNode(ArrayExpr); |
| 722 |
int non_var_args = nargs - nvargs; |
| 723 |
List *vargs; |
| 724 |
|
| 725 |
Assert(non_var_args >= 0); |
| 726 |
vargs = list_copy_tail(fargs, non_var_args); |
| 727 |
fargs = list_truncate(fargs, non_var_args); |
| 728 |
|
| 729 |
newa->elements = vargs; |
| 730 |
/* assume all the variadic arguments were coerced to the same type */ |
| 731 |
newa->element_typeid = exprType((Node *) linitial(vargs)); |
| 732 |
newa->array_typeid = get_array_type(newa->element_typeid); |
| 733 |
if (!OidIsValid(newa->array_typeid)) |
| 734 |
ereport(ERROR, |
| 735 |
(errcode(ERRCODE_UNDEFINED_OBJECT), |
| 736 |
errmsg("could not find array type for data type %s", |
| 737 |
format_type_be(newa->element_typeid)), |
| 738 |
parser_errposition(pstate, exprLocation((Node *) vargs)))); |
| 739 |
/* array_collid will be set by parse_collate.c */ |
| 740 |
newa->multidims = false; |
| 741 |
newa->location = exprLocation((Node *) vargs); |
| 742 |
|
| 743 |
fargs = lappend(fargs, newa); |
| 744 |
|
| 745 |
/* We could not have had VARIADIC marking before ... */ |
| 746 |
Assert(!func_variadic); |
| 747 |
/* ... but now, it's a VARIADIC call */ |
| 748 |
func_variadic = true; |
| 749 |
} |
| 750 |
|
| 751 |
/* |
| 752 |
* If an "any" variadic is called with explicit VARIADIC marking, insist |
| 753 |
* that the variadic parameter be of some array type. |
| 754 |
*/ |
| 755 |
if (nargs > 0 && vatype == ANYOID && func_variadic) |
| 756 |
{ |
| 757 |
Oid va_arr_typid = actual_arg_types[nargs - 1]; |
| 758 |
|
| 759 |
if (!OidIsValid(get_base_element_type(va_arr_typid))) |
| 760 |
ereport(ERROR, |
| 761 |
(errcode(ERRCODE_DATATYPE_MISMATCH), |
| 762 |
errmsg("VARIADIC argument must be an array"), |
| 763 |
parser_errposition(pstate, |
| 764 |
exprLocation((Node *) llast(fargs))))); |
| 765 |
} |
| 766 |
|
| 767 |
/* if it returns a set, check that's OK */ |
| 768 |
if (retset) |
| 769 |
check_srf_call_placement(pstate, last_srf, location); |
| 770 |
|
| 771 |
/* build the appropriate output structure */ |
| 772 |
if (fdresult == FUNCDETAIL_NORMAL || fdresult == FUNCDETAIL_PROCEDURE) |
| 773 |
{ |
| 774 |
FuncExpr *funcexpr = makeNode(FuncExpr); |
| 775 |
|
| 776 |
funcexpr->funcid = funcid; |
| 777 |
funcexpr->funcresulttype = rettype; |
| 778 |
funcexpr->funcretset = retset; |
| 779 |
funcexpr->funcvariadic = func_variadic; |
| 780 |
funcexpr->funcformat = funcformat; |
| 781 |
/* funccollid and inputcollid will be set by parse_collate.c */ |
| 782 |
funcexpr->args = fargs; |
| 783 |
funcexpr->location = location; |
| 784 |
|
| 785 |
retval = (Node *) funcexpr; |
| 786 |
} |
| 787 |
else if (fdresult == FUNCDETAIL_AGGREGATE && !over) |
| 788 |
{ |
| 789 |
/* aggregate function */ |
| 790 |
Aggref *aggref = makeNode(Aggref); |
| 791 |
|
| 792 |
aggref->aggfnoid = funcid; |
| 793 |
aggref->aggtype = rettype; |
| 794 |
/* aggcollid and inputcollid will be set by parse_collate.c */ |
| 795 |
aggref->aggtranstype = InvalidOid; /* will be set by planner */ |
| 796 |
/* aggargtypes will be set by transformAggregateCall */ |
| 797 |
/* aggdirectargs and args will be set by transformAggregateCall */ |
| 798 |
/* aggorder and aggdistinct will be set by transformAggregateCall */ |
| 799 |
aggref->aggfilter = agg_filter; |
| 800 |
aggref->aggstar = agg_star; |
| 801 |
aggref->aggvariadic = func_variadic; |
| 802 |
aggref->aggkind = aggkind; |
| 803 |
aggref->aggpresorted = false; |
| 804 |
/* agglevelsup will be set by transformAggregateCall */ |
| 805 |
aggref->aggsplit = AGGSPLIT_SIMPLE; /* planner might change this */ |
| 806 |
aggref->aggno = -1; /* planner will set aggno and aggtransno */ |
| 807 |
aggref->aggtransno = -1; |
| 808 |
aggref->location = location; |
| 809 |
|
| 810 |
/* |
| 811 |
* The argument-count limit for aggregates is one less than for other |
| 812 |
* kinds of functions (cf. AggregateCreate). Now that we know it's an |
| 813 |
* aggregate, apply the stricter limit. We need an explicit check |
| 814 |
* because hypothetical-set aggregates don't have a fixed number of |
| 815 |
* arguments, so having matched the pg_proc entry proves nothing. |
| 816 |
*/ |
| 817 |
if (list_length(fargs) > FUNC_MAX_ARGS - 1) |
| 818 |
ereport(ERROR, |
| 819 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 820 |
errmsg_plural("aggregates cannot have more than %d argument", |
| 821 |
"aggregates cannot have more than %d arguments", |
| 822 |
FUNC_MAX_ARGS - 1, |
| 823 |
FUNC_MAX_ARGS - 1), |
| 824 |
parser_errposition(pstate, location))); |
| 825 |
|
| 826 |
/* |
| 827 |
* Reject attempt to call a parameterless aggregate without (*) |
| 828 |
* syntax. This is mere pedantry but some folks insisted ... |
| 829 |
*/ |
| 830 |
if (fargs == NIL && !agg_star && !agg_within_group) |
| 831 |
ereport(ERROR, |
| 832 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 833 |
errmsg("%s(*) must be used to call a parameterless aggregate function", |
| 834 |
NameListToString(funcname)), |
| 835 |
parser_errposition(pstate, location))); |
| 836 |
|
| 837 |
if (retset) |
| 838 |
ereport(ERROR, |
| 839 |
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION), |
| 840 |
errmsg("aggregates cannot return sets"), |
| 841 |
parser_errposition(pstate, location))); |
| 842 |
|
| 843 |
/* |
| 844 |
* We might want to support named arguments later, but disallow it for |
| 845 |
* now. We'd need to figure out the parsed representation (should the |
| 846 |
* NamedArgExprs go above or below the TargetEntry nodes?) and then |
| 847 |
* teach the planner to reorder the list properly. Or maybe we could |
| 848 |
* make transformAggregateCall do that? However, if you'd also like |
| 849 |
* to allow default arguments for aggregates, we'd need to do it in |
| 850 |
* planning to avoid semantic problems. |
| 851 |
*/ |
| 852 |
if (argnames != NIL) |
| 853 |
ereport(ERROR, |
| 854 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 855 |
errmsg("aggregates cannot use named arguments"), |
| 856 |
parser_errposition(pstate, location))); |
| 857 |
|
| 858 |
/* parse_agg.c does additional aggregate-specific processing */ |
| 859 |
transformAggregateCall(pstate, aggref, fargs, agg_order, agg_distinct); |
| 860 |
|
| 861 |
retval = (Node *) aggref; |
| 862 |
} |
| 863 |
else |
| 864 |
{ |
| 865 |
/* window function */ |
| 866 |
WindowFunc *wfunc = makeNode(WindowFunc); |
| 867 |
|
| 868 |
Assert(over); /* lack of this was checked above */ |
| 869 |
Assert(!agg_within_group); /* also checked above */ |
| 870 |
|
| 871 |
wfunc->winfnoid = funcid; |
| 872 |
wfunc->wintype = rettype; |
| 873 |
/* wincollid and inputcollid will be set by parse_collate.c */ |
| 874 |
wfunc->args = fargs; |
| 875 |
/* winref will be set by transformWindowFuncCall */ |
| 876 |
wfunc->winstar = agg_star; |
| 877 |
wfunc->winagg = (fdresult == FUNCDETAIL_AGGREGATE); |
| 878 |
wfunc->aggfilter = agg_filter; |
| 879 |
wfunc->runCondition = NIL; |
| 880 |
wfunc->location = location; |
| 881 |
|
| 882 |
/* |
| 883 |
* agg_star is allowed for aggregate functions but distinct isn't |
| 884 |
*/ |
| 885 |
if (agg_distinct) |
| 886 |
ereport(ERROR, |
| 887 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 888 |
errmsg("DISTINCT is not implemented for window functions"), |
| 889 |
parser_errposition(pstate, location))); |
| 890 |
|
| 891 |
/* |
| 892 |
* As above, enforce the correct argument-count limit if it's really |
| 893 |
* an aggregate. |
| 894 |
*/ |
| 895 |
if (wfunc->winagg && list_length(fargs) > FUNC_MAX_ARGS - 1) |
| 896 |
ereport(ERROR, |
| 897 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 898 |
errmsg_plural("aggregates cannot have more than %d argument", |
| 899 |
"aggregates cannot have more than %d arguments", |
| 900 |
FUNC_MAX_ARGS - 1, |
| 901 |
FUNC_MAX_ARGS - 1), |
| 902 |
parser_errposition(pstate, location))); |
| 903 |
|
| 904 |
/* |
| 905 |
* Reject attempt to call a parameterless aggregate without (*) |
| 906 |
* syntax. This is mere pedantry but some folks insisted ... |
| 907 |
*/ |
| 908 |
if (wfunc->winagg && fargs == NIL && !agg_star) |
| 909 |
ereport(ERROR, |
| 910 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 911 |
errmsg("%s(*) must be used to call a parameterless aggregate function", |
| 912 |
NameListToString(funcname)), |
| 913 |
parser_errposition(pstate, location))); |
| 914 |
|
| 915 |
/* |
| 916 |
* ordered aggs not allowed in windows yet |
| 917 |
*/ |
| 918 |
if (agg_order != NIL) |
| 919 |
ereport(ERROR, |
| 920 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 921 |
errmsg("aggregate ORDER BY is not implemented for window functions"), |
| 922 |
parser_errposition(pstate, location))); |
| 923 |
|
| 924 |
/* |
| 925 |
* FILTER is not yet supported with true window functions |
| 926 |
*/ |
| 927 |
if (!wfunc->winagg && agg_filter) |
| 928 |
ereport(ERROR, |
| 929 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 930 |
errmsg("FILTER is not implemented for non-aggregate window functions"), |
| 931 |
parser_errposition(pstate, location))); |
| 932 |
|
| 933 |
/* |
| 934 |
* Window functions can't either take or return sets |
| 935 |
*/ |
| 936 |
if (pstate->p_last_srf != last_srf) |
| 937 |
ereport(ERROR, |
| 938 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 939 |
errmsg("window function calls cannot contain set-returning function calls"), |
| 940 |
errhint("You might be able to move the set-returning function into a LATERAL FROM item."), |
| 941 |
parser_errposition(pstate, |
| 942 |
exprLocation(pstate->p_last_srf)))); |
| 943 |
|
| 944 |
if (retset) |
| 945 |
ereport(ERROR, |
| 946 |
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION), |
| 947 |
errmsg("window functions cannot return sets"), |
| 948 |
parser_errposition(pstate, location))); |
| 949 |
|
| 950 |
/* parse_agg.c does additional window-func-specific processing */ |
| 951 |
transformWindowFuncCall(pstate, wfunc, over); |
| 952 |
|
| 953 |
retval = (Node *) wfunc; |
| 954 |
} |
| 955 |
|
| 956 |
/* if it returns a set, remember it for error checks at higher levels */ |
| 957 |
if (retset) |
| 958 |
pstate->p_last_srf = retval; |
| 959 |
|
| 960 |
return retval; |
| 961 |
} |
| 962 |
|
| 963 |
|
| 964 |
/* func_match_argtypes() |
| 965 |
* |
| 966 |
* Given a list of candidate functions (having the right name and number |
| 967 |
* of arguments) and an array of input datatype OIDs, produce a shortlist of |
| 968 |
* those candidates that actually accept the input datatypes (either exactly |
| 969 |
* or by coercion), and return the number of such candidates. |
| 970 |
* |
| 971 |
* Note that can_coerce_type will assume that UNKNOWN inputs are coercible to |
| 972 |
* anything, so candidates will not be eliminated on that basis. |
| 973 |
* |
| 974 |
* NB: okay to modify input list structure, as long as we find at least |
| 975 |
* one match. If no match at all, the list must remain unmodified. |
| 976 |
*/ |
| 977 |
int |
| 978 |
func_match_argtypes(int nargs, |
| 979 |
Oid *input_typeids, |
| 980 |
FuncCandidateList raw_candidates, |
| 981 |
FuncCandidateList *candidates) /* return value */ |
| 982 |
{ |
| 983 |
FuncCandidateList current_candidate; |
| 984 |
FuncCandidateList next_candidate; |
| 985 |
int ncandidates = 0; |
| 986 |
|
| 987 |
*candidates = NULL; |
| 988 |
|
| 989 |
for (current_candidate = raw_candidates; |
| 990 |
current_candidate != NULL; |
| 991 |
current_candidate = next_candidate) |
| 992 |
{ |
| 993 |
next_candidate = current_candidate->next; |
| 994 |
if (can_coerce_type(nargs, input_typeids, current_candidate->args, |
| 995 |
COERCION_IMPLICIT)) |
| 996 |
{ |
| 997 |
current_candidate->next = *candidates; |
| 998 |
*candidates = current_candidate; |
| 999 |
ncandidates++; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
return ncandidates; |
| 1004 |
} /* func_match_argtypes() */ |
| 1005 |
|
| 1006 |
|
| 1007 |
/* func_select_candidate() |
| 1008 |
* Given the input argtype array and more than one candidate |
| 1009 |
* for the function, attempt to resolve the conflict. |
| 1010 |
* |
| 1011 |
* Returns the selected candidate if the conflict can be resolved, |
| 1012 |
* otherwise returns NULL. |
| 1013 |
* |
| 1014 |
* Note that the caller has already determined that there is no candidate |
| 1015 |
* exactly matching the input argtypes, and has pruned away any "candidates" |
| 1016 |
* that aren't actually coercion-compatible with the input types. |
| 1017 |
* |
| 1018 |
* This is also used for resolving ambiguous operator references. Formerly |
| 1019 |
* parse_oper.c had its own, essentially duplicate code for the purpose. |
| 1020 |
* The following comments (formerly in parse_oper.c) are kept to record some |
| 1021 |
* of the history of these heuristics. |
| 1022 |
* |
| 1023 |
* OLD COMMENTS: |
| 1024 |
* |
| 1025 |
* This routine is new code, replacing binary_oper_select_candidate() |
| 1026 |
* which dates from v4.2/v1.0.x days. It tries very hard to match up |
| 1027 |
* operators with types, including allowing type coercions if necessary. |
| 1028 |
* The important thing is that the code do as much as possible, |
| 1029 |
* while _never_ doing the wrong thing, where "the wrong thing" would |
| 1030 |
* be returning an operator when other better choices are available, |
| 1031 |
* or returning an operator which is a non-intuitive possibility. |
| 1032 |
* - thomas 1998-05-21 |
| 1033 |
* |
| 1034 |
* The comments below came from binary_oper_select_candidate(), and |
| 1035 |
* illustrate the issues and choices which are possible: |
| 1036 |
* - thomas 1998-05-20 |
| 1037 |
* |
| 1038 |
* current wisdom holds that the default operator should be one in which |
| 1039 |
* both operands have the same type (there will only be one such |
| 1040 |
* operator) |
| 1041 |
* |
| 1042 |
* 7.27.93 - I have decided not to do this; it's too hard to justify, and |
| 1043 |
* it's easy enough to typecast explicitly - avi |
| 1044 |
* [the rest of this routine was commented out since then - ay] |
| 1045 |
* |
| 1046 |
* 6/23/95 - I don't complete agree with avi. In particular, casting |
| 1047 |
* floats is a pain for users. Whatever the rationale behind not doing |
| 1048 |
* this is, I need the following special case to work. |
| 1049 |
* |
| 1050 |
* In the WHERE clause of a query, if a float is specified without |
| 1051 |
* quotes, we treat it as float8. I added the float48* operators so |
| 1052 |
* that we can operate on float4 and float8. But now we have more than |
| 1053 |
* one matching operator if the right arg is unknown (eg. float |
| 1054 |
* specified with quotes). This break some stuff in the regression |
| 1055 |
* test where there are floats in quotes not properly casted. Below is |
| 1056 |
* the solution. In addition to requiring the operator operates on the |
| 1057 |
* same type for both operands [as in the code Avi originally |
| 1058 |
* commented out], we also require that the operators be equivalent in |
| 1059 |
* some sense. (see equivalentOpersAfterPromotion for details.) |
| 1060 |
* - ay 6/95 |
| 1061 |
*/ |
| 1062 |
FuncCandidateList |
| 1063 |
func_select_candidate(int nargs, |
| 1064 |
Oid *input_typeids, |
| 1065 |
FuncCandidateList candidates) |
| 1066 |
{ |
| 1067 |
FuncCandidateList current_candidate, |
| 1068 |
first_candidate, |
| 1069 |
last_candidate; |
| 1070 |
Oid *current_typeids; |
| 1071 |
Oid current_type; |
| 1072 |
int i; |
| 1073 |
int ncandidates; |
| 1074 |
int nbestMatch, |
| 1075 |
nmatch, |
| 1076 |
nunknowns; |
| 1077 |
Oid input_base_typeids[FUNC_MAX_ARGS]; |
| 1078 |
TYPCATEGORY slot_category[FUNC_MAX_ARGS], |
| 1079 |
current_category; |
| 1080 |
bool current_is_preferred; |
| 1081 |
bool slot_has_preferred_type[FUNC_MAX_ARGS]; |
| 1082 |
bool resolved_unknowns; |
| 1083 |
|
| 1084 |
/* protect local fixed-size arrays */ |
| 1085 |
if (nargs > FUNC_MAX_ARGS) |
| 1086 |
ereport(ERROR, |
| 1087 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 1088 |
errmsg_plural("cannot pass more than %d argument to a function", |
| 1089 |
"cannot pass more than %d arguments to a function", |
| 1090 |
FUNC_MAX_ARGS, |
| 1091 |
FUNC_MAX_ARGS))); |
| 1092 |
|
| 1093 |
/* |
| 1094 |
* If any input types are domains, reduce them to their base types. This |
| 1095 |
* ensures that we will consider functions on the base type to be "exact |
| 1096 |
* matches" in the exact-match heuristic; it also makes it possible to do |
| 1097 |
* something useful with the type-category heuristics. Note that this |
| 1098 |
* makes it difficult, but not impossible, to use functions declared to |
| 1099 |
* take a domain as an input datatype. Such a function will be selected |
| 1100 |
* over the base-type function only if it is an exact match at all |
| 1101 |
* argument positions, and so was already chosen by our caller. |
| 1102 |
* |
| 1103 |
* While we're at it, count the number of unknown-type arguments for use |
| 1104 |
* later. |
| 1105 |
*/ |
| 1106 |
nunknowns = 0; |
| 1107 |
for (i = 0; i < nargs; i++) |
| 1108 |
{ |
| 1109 |
if (input_typeids[i] != UNKNOWNOID) |
| 1110 |
input_base_typeids[i] = getBaseType(input_typeids[i]); |
| 1111 |
else |
| 1112 |
{ |
| 1113 |
/* no need to call getBaseType on UNKNOWNOID */ |
| 1114 |
input_base_typeids[i] = UNKNOWNOID; |
| 1115 |
nunknowns++; |
| 1116 |
} |
| 1117 |
} |
| 1118 |
|
| 1119 |
/* |
| 1120 |
* Run through all candidates and keep those with the most matches on |
| 1121 |
* exact types. Keep all candidates if none match. |
| 1122 |
*/ |
| 1123 |
ncandidates = 0; |
| 1124 |
nbestMatch = 0; |
| 1125 |
last_candidate = NULL; |
| 1126 |
for (current_candidate = candidates; |
| 1127 |
current_candidate != NULL; |
| 1128 |
current_candidate = current_candidate->next) |
| 1129 |
{ |
| 1130 |
current_typeids = current_candidate->args; |
| 1131 |
nmatch = 0; |
| 1132 |
for (i = 0; i < nargs; i++) |
| 1133 |
{ |
| 1134 |
if (input_base_typeids[i] != UNKNOWNOID && |
| 1135 |
current_typeids[i] == input_base_typeids[i]) |
| 1136 |
nmatch++; |
| 1137 |
} |
| 1138 |
|
| 1139 |
/* take this one as the best choice so far? */ |
| 1140 |
if ((nmatch > nbestMatch) || (last_candidate == NULL)) |
| 1141 |
{ |
| 1142 |
nbestMatch = nmatch; |
| 1143 |
candidates = current_candidate; |
| 1144 |
last_candidate = current_candidate; |
| 1145 |
ncandidates = 1; |
| 1146 |
} |
| 1147 |
/* no worse than the last choice, so keep this one too? */ |
| 1148 |
else if (nmatch == nbestMatch) |
| 1149 |
{ |
| 1150 |
last_candidate->next = current_candidate; |
| 1151 |
last_candidate = current_candidate; |
| 1152 |
ncandidates++; |
| 1153 |
} |
| 1154 |
/* otherwise, don't bother keeping this one... */ |
| 1155 |
} |
| 1156 |
|
| 1157 |
if (last_candidate) /* terminate rebuilt list */ |
| 1158 |
last_candidate->next = NULL; |
| 1159 |
|
| 1160 |
if (ncandidates == 1) |
| 1161 |
return candidates; |
| 1162 |
|
| 1163 |
/* |
| 1164 |
* Still too many candidates? Now look for candidates which have either |
| 1165 |
* exact matches or preferred types at the args that will require |
| 1166 |
* coercion. (Restriction added in 7.4: preferred type must be of same |
| 1167 |
* category as input type; give no preference to cross-category |
| 1168 |
* conversions to preferred types.) Keep all candidates if none match. |
| 1169 |
*/ |
| 1170 |
for (i = 0; i < nargs; i++) /* avoid multiple lookups */ |
| 1171 |
slot_category[i] = TypeCategory(input_base_typeids[i]); |
| 1172 |
ncandidates = 0; |
| 1173 |
nbestMatch = 0; |
| 1174 |
last_candidate = NULL; |
| 1175 |
for (current_candidate = candidates; |
| 1176 |
current_candidate != NULL; |
| 1177 |
current_candidate = current_candidate->next) |
| 1178 |
{ |
| 1179 |
current_typeids = current_candidate->args; |
| 1180 |
nmatch = 0; |
| 1181 |
for (i = 0; i < nargs; i++) |
| 1182 |
{ |
| 1183 |
if (input_base_typeids[i] != UNKNOWNOID) |
| 1184 |
{ |
| 1185 |
if (current_typeids[i] == input_base_typeids[i] || |
| 1186 |
IsPreferredType(slot_category[i], current_typeids[i])) |
| 1187 |
nmatch++; |
| 1188 |
} |
| 1189 |
} |
| 1190 |
|
| 1191 |
if ((nmatch > nbestMatch) || (last_candidate == NULL)) |
| 1192 |
{ |
| 1193 |
nbestMatch = nmatch; |
| 1194 |
candidates = current_candidate; |
| 1195 |
last_candidate = current_candidate; |
| 1196 |
ncandidates = 1; |
| 1197 |
} |
| 1198 |
else if (nmatch == nbestMatch) |
| 1199 |
{ |
| 1200 |
last_candidate->next = current_candidate; |
| 1201 |
last_candidate = current_candidate; |
| 1202 |
ncandidates++; |
| 1203 |
} |
| 1204 |
} |
| 1205 |
|
| 1206 |
if (last_candidate) /* terminate rebuilt list */ |
| 1207 |
last_candidate->next = NULL; |
| 1208 |
|
| 1209 |
if (ncandidates == 1) |
| 1210 |
return candidates; |
| 1211 |
|
| 1212 |
/* |
| 1213 |
* Still too many candidates? Try assigning types for the unknown inputs. |
| 1214 |
* |
| 1215 |
* If there are no unknown inputs, we have no more heuristics that apply, |
| 1216 |
* and must fail. |
| 1217 |
*/ |
| 1218 |
if (nunknowns == 0) |
| 1219 |
return NULL; /* failed to select a best candidate */ |
| 1220 |
|
| 1221 |
/* |
| 1222 |
* The next step examines each unknown argument position to see if we can |
| 1223 |
* determine a "type category" for it. If any candidate has an input |
| 1224 |
* datatype of STRING category, use STRING category (this bias towards |
| 1225 |
* STRING is appropriate since unknown-type literals look like strings). |
| 1226 |
* Otherwise, if all the candidates agree on the type category of this |
| 1227 |
* argument position, use that category. Otherwise, fail because we |
| 1228 |
* cannot determine a category. |
| 1229 |
* |
| 1230 |
* If we are able to determine a type category, also notice whether any of |
| 1231 |
* the candidates takes a preferred datatype within the category. |
| 1232 |
* |
| 1233 |
* Having completed this examination, remove candidates that accept the |
| 1234 |
* wrong category at any unknown position. Also, if at least one |
| 1235 |
* candidate accepted a preferred type at a position, remove candidates |
| 1236 |
* that accept non-preferred types. If just one candidate remains, return |
| 1237 |
* that one. However, if this rule turns out to reject all candidates, |
| 1238 |
* keep them all instead. |
| 1239 |
*/ |
| 1240 |
resolved_unknowns = false; |
| 1241 |
for (i = 0; i < nargs; i++) |
| 1242 |
{ |
| 1243 |
bool have_conflict; |
| 1244 |
|
| 1245 |
if (input_base_typeids[i] != UNKNOWNOID) |
| 1246 |
continue; |
| 1247 |
resolved_unknowns = true; /* assume we can do it */ |
| 1248 |
slot_category[i] = TYPCATEGORY_INVALID; |
| 1249 |
slot_has_preferred_type[i] = false; |
| 1250 |
have_conflict = false; |
| 1251 |
for (current_candidate = candidates; |
| 1252 |
current_candidate != NULL; |
| 1253 |
current_candidate = current_candidate->next) |
| 1254 |
{ |
| 1255 |
current_typeids = current_candidate->args; |
| 1256 |
current_type = current_typeids[i]; |
| 1257 |
get_type_category_preferred(current_type, |
| 1258 |
¤t_category, |
| 1259 |
¤t_is_preferred); |
| 1260 |
if (slot_category[i] == TYPCATEGORY_INVALID) |
| 1261 |
{ |
| 1262 |
/* first candidate */ |
| 1263 |
slot_category[i] = current_category; |
| 1264 |
slot_has_preferred_type[i] = current_is_preferred; |
| 1265 |
} |
| 1266 |
else if (current_category == slot_category[i]) |
| 1267 |
{ |
| 1268 |
/* more candidates in same category */ |
| 1269 |
slot_has_preferred_type[i] |= current_is_preferred; |
| 1270 |
} |
| 1271 |
else |
| 1272 |
{ |
| 1273 |
/* category conflict! */ |
| 1274 |
if (current_category == TYPCATEGORY_STRING) |
| 1275 |
{ |
| 1276 |
/* STRING always wins if available */ |
| 1277 |
slot_category[i] = current_category; |
| 1278 |
slot_has_preferred_type[i] = current_is_preferred; |
| 1279 |
} |
| 1280 |
else |
| 1281 |
{ |
| 1282 |
/* |
| 1283 |
* Remember conflict, but keep going (might find STRING) |
| 1284 |
*/ |
| 1285 |
have_conflict = true; |
| 1286 |
} |
| 1287 |
} |
| 1288 |
} |
| 1289 |
if (have_conflict && slot_category[i] != TYPCATEGORY_STRING) |
| 1290 |
{ |
| 1291 |
/* Failed to resolve category conflict at this position */ |
| 1292 |
resolved_unknowns = false; |
| 1293 |
break; |
| 1294 |
} |
| 1295 |
} |
| 1296 |
|
| 1297 |
if (resolved_unknowns) |
| 1298 |
{ |
| 1299 |
/* Strip non-matching candidates */ |
| 1300 |
ncandidates = 0; |
| 1301 |
first_candidate = candidates; |
| 1302 |
last_candidate = NULL; |
| 1303 |
for (current_candidate = candidates; |
| 1304 |
current_candidate != NULL; |
| 1305 |
current_candidate = current_candidate->next) |
| 1306 |
{ |
| 1307 |
bool keepit = true; |
| 1308 |
|
| 1309 |
current_typeids = current_candidate->args; |
| 1310 |
for (i = 0; i < nargs; i++) |
| 1311 |
{ |
| 1312 |
if (input_base_typeids[i] != UNKNOWNOID) |
| 1313 |
continue; |
| 1314 |
current_type = current_typeids[i]; |
| 1315 |
get_type_category_preferred(current_type, |
| 1316 |
¤t_category, |
| 1317 |
¤t_is_preferred); |
| 1318 |
if (current_category != slot_category[i]) |
| 1319 |
{ |
| 1320 |
keepit = false; |
| 1321 |
break; |
| 1322 |
} |
| 1323 |
if (slot_has_preferred_type[i] && !current_is_preferred) |
| 1324 |
{ |
| 1325 |
keepit = false; |
| 1326 |
break; |
| 1327 |
} |
| 1328 |
} |
| 1329 |
if (keepit) |
| 1330 |
{ |
| 1331 |
/* keep this candidate */ |
| 1332 |
last_candidate = current_candidate; |
| 1333 |
ncandidates++; |
| 1334 |
} |
| 1335 |
else |
| 1336 |
{ |
| 1337 |
/* forget this candidate */ |
| 1338 |
if (last_candidate) |
| 1339 |
last_candidate->next = current_candidate->next; |
| 1340 |
else |
| 1341 |
first_candidate = current_candidate->next; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
|
| 1345 |
/* if we found any matches, restrict our attention to those */ |
| 1346 |
if (last_candidate) |
| 1347 |
{ |
| 1348 |
candidates = first_candidate; |
| 1349 |
/* terminate rebuilt list */ |
| 1350 |
last_candidate->next = NULL; |
| 1351 |
} |
| 1352 |
|
| 1353 |
if (ncandidates == 1) |
| 1354 |
return candidates; |
| 1355 |
} |
| 1356 |
|
| 1357 |
/* |
| 1358 |
* Last gasp: if there are both known- and unknown-type inputs, and all |
| 1359 |
* the known types are the same, assume the unknown inputs are also that |
| 1360 |
* type, and see if that gives us a unique match. If so, use that match. |
| 1361 |
* |
| 1362 |
* NOTE: for a binary operator with one unknown and one non-unknown input, |
| 1363 |
* we already tried this heuristic in binary_oper_exact(). However, that |
| 1364 |
* code only finds exact matches, whereas here we will handle matches that |
| 1365 |
* involve coercion, polymorphic type resolution, etc. |
| 1366 |
*/ |
| 1367 |
if (nunknowns < nargs) |
| 1368 |
{ |
| 1369 |
Oid known_type = UNKNOWNOID; |
| 1370 |
|
| 1371 |
for (i = 0; i < nargs; i++) |
| 1372 |
{ |
| 1373 |
if (input_base_typeids[i] == UNKNOWNOID) |
| 1374 |
continue; |
| 1375 |
if (known_type == UNKNOWNOID) /* first known arg? */ |
| 1376 |
known_type = input_base_typeids[i]; |
| 1377 |
else if (known_type != input_base_typeids[i]) |
| 1378 |
{ |
| 1379 |
/* oops, not all match */ |
| 1380 |
known_type = UNKNOWNOID; |
| 1381 |
break; |
| 1382 |
} |
| 1383 |
} |
| 1384 |
|
| 1385 |
if (known_type != UNKNOWNOID) |
| 1386 |
{ |
| 1387 |
/* okay, just one known type, apply the heuristic */ |
| 1388 |
for (i = 0; i < nargs; i++) |
| 1389 |
input_base_typeids[i] = known_type; |
| 1390 |
ncandidates = 0; |
| 1391 |
last_candidate = NULL; |
| 1392 |
for (current_candidate = candidates; |
| 1393 |
current_candidate != NULL; |
| 1394 |
current_candidate = current_candidate->next) |
| 1395 |
{ |
| 1396 |
current_typeids = current_candidate->args; |
| 1397 |
if (can_coerce_type(nargs, input_base_typeids, current_typeids, |
| 1398 |
COERCION_IMPLICIT)) |
| 1399 |
{ |
| 1400 |
if (++ncandidates > 1) |
| 1401 |
break; /* not unique, give up */ |
| 1402 |
last_candidate = current_candidate; |
| 1403 |
} |
| 1404 |
} |
| 1405 |
if (ncandidates == 1) |
| 1406 |
{ |
| 1407 |
/* successfully identified a unique match */ |
| 1408 |
last_candidate->next = NULL; |
| 1409 |
return last_candidate; |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
return NULL; /* failed to select a best candidate */ |
| 1415 |
} /* func_select_candidate() */ |
| 1416 |
|
| 1417 |
|
| 1418 |
/* func_get_detail() |
| 1419 |
* |
| 1420 |
* Find the named function in the system catalogs. |
| 1421 |
* |
| 1422 |
* Attempt to find the named function in the system catalogs with |
| 1423 |
* arguments exactly as specified, so that the normal case (exact match) |
| 1424 |
* is as quick as possible. |
| 1425 |
* |
| 1426 |
* If an exact match isn't found: |
| 1427 |
* 1) check for possible interpretation as a type coercion request |
| 1428 |
* 2) apply the ambiguous-function resolution rules |
| 1429 |
* |
| 1430 |
* Return values *funcid through *true_typeids receive info about the function. |
| 1431 |
* If argdefaults isn't NULL, *argdefaults receives a list of any default |
| 1432 |
* argument expressions that need to be added to the given arguments. |
| 1433 |
* |
| 1434 |
* When processing a named- or mixed-notation call (ie, fargnames isn't NIL), |
| 1435 |
* the returned true_typeids and argdefaults are ordered according to the |
| 1436 |
* call's argument ordering: first any positional arguments, then the named |
| 1437 |
* arguments, then defaulted arguments (if needed and allowed by |
| 1438 |
* expand_defaults). Some care is needed if this information is to be compared |
| 1439 |
* to the function's pg_proc entry, but in practice the caller can usually |
| 1440 |
* just work with the call's argument ordering. |
| 1441 |
* |
| 1442 |
* We rely primarily on fargnames/nargs/argtypes as the argument description. |
| 1443 |
* The actual expression node list is passed in fargs so that we can check |
| 1444 |
* for type coercion of a constant. Some callers pass fargs == NIL indicating |
| 1445 |
* they don't need that check made. Note also that when fargnames isn't NIL, |
| 1446 |
* the fargs list must be passed if the caller wants actual argument position |
| 1447 |
* information to be returned into the NamedArgExpr nodes. |
| 1448 |
*/ |
| 1449 |
FuncDetailCode |
| 1450 |
func_get_detail(List *funcname, |
| 1451 |
List *fargs, |
| 1452 |
List *fargnames, |
| 1453 |
int nargs, |
| 1454 |
Oid *argtypes, |
| 1455 |
bool expand_variadic, |
| 1456 |
bool expand_defaults, |
| 1457 |
bool include_out_arguments, |
| 1458 |
Oid *funcid, /* return value */ |
| 1459 |
Oid *rettype, /* return value */ |
| 1460 |
bool *retset, /* return value */ |
| 1461 |
int *nvargs, /* return value */ |
| 1462 |
Oid *vatype, /* return value */ |
| 1463 |
Oid **true_typeids, /* return value */ |
| 1464 |
List **argdefaults) /* optional return value */ |
| 1465 |
{ |
| 1466 |
FuncCandidateList raw_candidates; |
| 1467 |
FuncCandidateList best_candidate; |
| 1468 |
|
| 1469 |
/* initialize output arguments to silence compiler warnings */ |
| 1470 |
*funcid = InvalidOid; |
| 1471 |
*rettype = InvalidOid; |
| 1472 |
*retset = false; |
| 1473 |
*nvargs = 0; |
| 1474 |
*vatype = InvalidOid; |
| 1475 |
*true_typeids = NULL; |
| 1476 |
if (argdefaults) |
| 1477 |
*argdefaults = NIL; |
| 1478 |
|
| 1479 |
/* Get list of possible candidates from namespace search */ |
| 1480 |
raw_candidates = FuncnameGetCandidates(funcname, nargs, fargnames, |
| 1481 |
expand_variadic, expand_defaults, |
| 1482 |
include_out_arguments, false); |
| 1483 |
|
| 1484 |
/* |
| 1485 |
* Quickly check if there is an exact match to the input datatypes (there |
| 1486 |
* can be only one) |
| 1487 |
*/ |
| 1488 |
for (best_candidate = raw_candidates; |
| 1489 |
best_candidate != NULL; |
| 1490 |
best_candidate = best_candidate->next) |
| 1491 |
{ |
| 1492 |
/* if nargs==0, argtypes can be null; don't pass that to memcmp */ |
| 1493 |
if (nargs == 0 || |
| 1494 |
memcmp(argtypes, best_candidate->args, nargs * sizeof(Oid)) == 0) |
| 1495 |
break; |
| 1496 |
} |
| 1497 |
|
| 1498 |
if (best_candidate == NULL) |
| 1499 |
{ |
| 1500 |
/* |
| 1501 |
* If we didn't find an exact match, next consider the possibility |
| 1502 |
* that this is really a type-coercion request: a single-argument |
| 1503 |
* function call where the function name is a type name. If so, and |
| 1504 |
* if the coercion path is RELABELTYPE or COERCEVIAIO, then go ahead |
| 1505 |
* and treat the "function call" as a coercion. |
| 1506 |
* |
| 1507 |
* This interpretation needs to be given higher priority than |
| 1508 |
* interpretations involving a type coercion followed by a function |
| 1509 |
* call, otherwise we can produce surprising results. For example, we |
| 1510 |
* want "text(varchar)" to be interpreted as a simple coercion, not as |
| 1511 |
* "text(name(varchar))" which the code below this point is entirely |
| 1512 |
* capable of selecting. |
| 1513 |
* |
| 1514 |
* We also treat a coercion of a previously-unknown-type literal |
| 1515 |
* constant to a specific type this way. |
| 1516 |
* |
| 1517 |
* The reason we reject COERCION_PATH_FUNC here is that we expect the |
| 1518 |
* cast implementation function to be named after the target type. |
| 1519 |
* Thus the function will be found by normal lookup if appropriate. |
| 1520 |
* |
| 1521 |
* The reason we reject COERCION_PATH_ARRAYCOERCE is mainly that you |
| 1522 |
* can't write "foo[] (something)" as a function call. In theory |
| 1523 |
* someone might want to invoke it as "_foo (something)" but we have |
| 1524 |
* never supported that historically, so we can insist that people |
| 1525 |
* write it as a normal cast instead. |
| 1526 |
* |
| 1527 |
* We also reject the specific case of COERCEVIAIO for a composite |
| 1528 |
* source type and a string-category target type. This is a case that |
| 1529 |
* find_coercion_pathway() allows by default, but experience has shown |
| 1530 |
* that it's too commonly invoked by mistake. So, again, insist that |
| 1531 |
* people use cast syntax if they want to do that. |
| 1532 |
* |
| 1533 |
* NB: it's important that this code does not exceed what coerce_type |
| 1534 |
* can do, because the caller will try to apply coerce_type if we |
| 1535 |
* return FUNCDETAIL_COERCION. If we return that result for something |
| 1536 |
* coerce_type can't handle, we'll cause infinite recursion between |
| 1537 |
* this module and coerce_type! |
| 1538 |
*/ |
| 1539 |
if (nargs == 1 && fargs != NIL && fargnames == NIL) |
| 1540 |
{ |
| 1541 |
Oid targetType = FuncNameAsType(funcname); |
| 1542 |
|
| 1543 |
if (OidIsValid(targetType)) |
| 1544 |
{ |
| 1545 |
Oid sourceType = argtypes[0]; |
| 1546 |
Node *arg1 = linitial(fargs); |
| 1547 |
bool iscoercion; |
| 1548 |
|
| 1549 |
if (sourceType == UNKNOWNOID && IsA(arg1, Const)) |
| 1550 |
{ |
| 1551 |
/* always treat typename('literal') as coercion */ |
| 1552 |
iscoercion = true; |
| 1553 |
} |
| 1554 |
else |
| 1555 |
{ |
| 1556 |
CoercionPathType cpathtype; |
| 1557 |
Oid cfuncid; |
| 1558 |
|
| 1559 |
cpathtype = find_coercion_pathway(targetType, sourceType, |
| 1560 |
COERCION_EXPLICIT, |
| 1561 |
&cfuncid); |
| 1562 |
switch (cpathtype) |
| 1563 |
{ |
| 1564 |
case COERCION_PATH_RELABELTYPE: |
| 1565 |
iscoercion = true; |
| 1566 |
break; |
| 1567 |
case COERCION_PATH_COERCEVIAIO: |
| 1568 |
if ((sourceType == RECORDOID || |
| 1569 |
ISCOMPLEX(sourceType)) && |
| 1570 |
TypeCategory(targetType) == TYPCATEGORY_STRING) |
| 1571 |
iscoercion = false; |
| 1572 |
else |
| 1573 |
iscoercion = true; |
| 1574 |
break; |
| 1575 |
default: |
| 1576 |
iscoercion = false; |
| 1577 |
break; |
| 1578 |
} |
| 1579 |
} |
| 1580 |
|
| 1581 |
if (iscoercion) |
| 1582 |
{ |
| 1583 |
/* Treat it as a type coercion */ |
| 1584 |
*funcid = InvalidOid; |
| 1585 |
*rettype = targetType; |
| 1586 |
*retset = false; |
| 1587 |
*nvargs = 0; |
| 1588 |
*vatype = InvalidOid; |
| 1589 |
*true_typeids = argtypes; |
| 1590 |
return FUNCDETAIL_COERCION; |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
/* |
| 1596 |
* didn't find an exact match, so now try to match up candidates... |
| 1597 |
*/ |
| 1598 |
if (raw_candidates != NULL) |
| 1599 |
{ |
| 1600 |
FuncCandidateList current_candidates; |
| 1601 |
int ncandidates; |
| 1602 |
|
| 1603 |
ncandidates = func_match_argtypes(nargs, |
| 1604 |
argtypes, |
| 1605 |
raw_candidates, |
| 1606 |
¤t_candidates); |
| 1607 |
|
| 1608 |
/* one match only? then run with it... */ |
| 1609 |
if (ncandidates == 1) |
| 1610 |
best_candidate = current_candidates; |
| 1611 |
|
| 1612 |
/* |
| 1613 |
* multiple candidates? then better decide or throw an error... |
| 1614 |
*/ |
| 1615 |
else if (ncandidates > 1) |
| 1616 |
{ |
| 1617 |
best_candidate = func_select_candidate(nargs, |
| 1618 |
argtypes, |
| 1619 |
current_candidates); |
| 1620 |
|
| 1621 |
/* |
| 1622 |
* If we were able to choose a best candidate, we're done. |
| 1623 |
* Otherwise, ambiguous function call. |
| 1624 |
*/ |
| 1625 |
if (!best_candidate) |
| 1626 |
return FUNCDETAIL_MULTIPLE; |
| 1627 |
} |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
if (best_candidate) |
| 1632 |
{ |
| 1633 |
HeapTuple ftup; |
| 1634 |
Form_pg_proc pform; |
| 1635 |
FuncDetailCode result; |
| 1636 |
|
| 1637 |
/* |
| 1638 |
* If processing named args or expanding variadics or defaults, the |
| 1639 |
* "best candidate" might represent multiple equivalently good |
| 1640 |
* functions; treat this case as ambiguous. |
| 1641 |
*/ |
| 1642 |
if (!OidIsValid(best_candidate->oid)) |
| 1643 |
return FUNCDETAIL_MULTIPLE; |
| 1644 |
|
| 1645 |
/* |
| 1646 |
* We disallow VARIADIC with named arguments unless the last argument |
| 1647 |
* (the one with VARIADIC attached) actually matched the variadic |
| 1648 |
* parameter. This is mere pedantry, really, but some folks insisted. |
| 1649 |
*/ |
| 1650 |
if (fargnames != NIL && !expand_variadic && nargs > 0 && |
| 1651 |
best_candidate->argnumbers[nargs - 1] != nargs - 1) |
| 1652 |
return FUNCDETAIL_NOTFOUND; |
| 1653 |
|
| 1654 |
*funcid = best_candidate->oid; |
| 1655 |
*nvargs = best_candidate->nvargs; |
| 1656 |
*true_typeids = best_candidate->args; |
| 1657 |
|
| 1658 |
/* |
| 1659 |
* If processing named args, return actual argument positions into |
| 1660 |
* NamedArgExpr nodes in the fargs list. This is a bit ugly but not |
| 1661 |
* worth the extra notation needed to do it differently. |
| 1662 |
*/ |
| 1663 |
if (best_candidate->argnumbers != NULL) |
| 1664 |
{ |
| 1665 |
int i = 0; |
| 1666 |
ListCell *lc; |
| 1667 |
|
| 1668 |
foreach(lc, fargs) |
| 1669 |
{ |
| 1670 |
NamedArgExpr *na = (NamedArgExpr *) lfirst(lc); |
| 1671 |
|
| 1672 |
if (IsA(na, NamedArgExpr)) |
| 1673 |
na->argnumber = best_candidate->argnumbers[i]; |
| 1674 |
i++; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
ftup = SearchSysCache1(PROCOID, |
| 1679 |
ObjectIdGetDatum(best_candidate->oid)); |
| 1680 |
if (!HeapTupleIsValid(ftup)) /* should not happen */ |
| 1681 |
elog(ERROR, "cache lookup failed for function %u", |
| 1682 |
best_candidate->oid); |
| 1683 |
pform = (Form_pg_proc) GETSTRUCT(ftup); |
| 1684 |
*rettype = pform->prorettype; |
| 1685 |
*retset = pform->proretset; |
| 1686 |
*vatype = pform->provariadic; |
| 1687 |
/* fetch default args if caller wants 'em */ |
| 1688 |
if (argdefaults && best_candidate->ndargs > 0) |
| 1689 |
{ |
| 1690 |
Datum proargdefaults; |
| 1691 |
char *str; |
| 1692 |
List *defaults; |
| 1693 |
|
| 1694 |
/* shouldn't happen, FuncnameGetCandidates messed up */ |
| 1695 |
if (best_candidate->ndargs > pform->pronargdefaults) |
| 1696 |
elog(ERROR, "not enough default arguments"); |
| 1697 |
|
| 1698 |
proargdefaults = SysCacheGetAttrNotNull(PROCOID, ftup, |
| 1699 |
Anum_pg_proc_proargdefaults); |
| 1700 |
str = TextDatumGetCString(proargdefaults); |
| 1701 |
defaults = castNode(List, stringToNode(str)); |
| 1702 |
pfree(str); |
| 1703 |
|
| 1704 |
/* Delete any unused defaults from the returned list */ |
| 1705 |
if (best_candidate->argnumbers != NULL) |
| 1706 |
{ |
| 1707 |
/* |
| 1708 |
* This is a bit tricky in named notation, since the supplied |
| 1709 |
* arguments could replace any subset of the defaults. We |
| 1710 |
* work by making a bitmapset of the argnumbers of defaulted |
| 1711 |
* arguments, then scanning the defaults list and selecting |
| 1712 |
* the needed items. (This assumes that defaulted arguments |
| 1713 |
* should be supplied in their positional order.) |
| 1714 |
*/ |
| 1715 |
Bitmapset *defargnumbers; |
| 1716 |
int *firstdefarg; |
| 1717 |
List *newdefaults; |
| 1718 |
ListCell *lc; |
| 1719 |
int i; |
| 1720 |
|
| 1721 |
defargnumbers = NULL; |
| 1722 |
firstdefarg = &best_candidate->argnumbers[best_candidate->nargs - best_candidate->ndargs]; |
| 1723 |
for (i = 0; i < best_candidate->ndargs; i++) |
| 1724 |
defargnumbers = bms_add_member(defargnumbers, |
| 1725 |
firstdefarg[i]); |
| 1726 |
newdefaults = NIL; |
| 1727 |
i = best_candidate->nominalnargs - pform->pronargdefaults; |
| 1728 |
foreach(lc, defaults) |
| 1729 |
{ |
| 1730 |
if (bms_is_member(i, defargnumbers)) |
| 1731 |
newdefaults = lappend(newdefaults, lfirst(lc)); |
| 1732 |
i++; |
| 1733 |
} |
| 1734 |
Assert(list_length(newdefaults) == best_candidate->ndargs); |
| 1735 |
bms_free(defargnumbers); |
| 1736 |
*argdefaults = newdefaults; |
| 1737 |
} |
| 1738 |
else |
| 1739 |
{ |
| 1740 |
/* |
| 1741 |
* Defaults for positional notation are lots easier; just |
| 1742 |
* remove any unwanted ones from the front. |
| 1743 |
*/ |
| 1744 |
int ndelete; |
| 1745 |
|
| 1746 |
ndelete = list_length(defaults) - best_candidate->ndargs; |
| 1747 |
if (ndelete > 0) |
| 1748 |
defaults = list_delete_first_n(defaults, ndelete); |
| 1749 |
*argdefaults = defaults; |
| 1750 |
} |
| 1751 |
} |
| 1752 |
|
| 1753 |
switch (pform->prokind) |
| 1754 |
{ |
| 1755 |
case PROKIND_AGGREGATE: |
| 1756 |
result = FUNCDETAIL_AGGREGATE; |
| 1757 |
break; |
| 1758 |
case PROKIND_FUNCTION: |
| 1759 |
result = FUNCDETAIL_NORMAL; |
| 1760 |
break; |
| 1761 |
case PROKIND_PROCEDURE: |
| 1762 |
result = FUNCDETAIL_PROCEDURE; |
| 1763 |
break; |
| 1764 |
case PROKIND_WINDOW: |
| 1765 |
result = FUNCDETAIL_WINDOWFUNC; |
| 1766 |
break; |
| 1767 |
default: |
| 1768 |
elog(ERROR, "unrecognized prokind: %c", pform->prokind); |
| 1769 |
result = FUNCDETAIL_NORMAL; /* keep compiler quiet */ |
| 1770 |
break; |
| 1771 |
} |
| 1772 |
|
| 1773 |
ReleaseSysCache(ftup); |
| 1774 |
return result; |
| 1775 |
} |
| 1776 |
|
| 1777 |
return FUNCDETAIL_NOTFOUND; |
| 1778 |
} |
| 1779 |
|
| 1780 |
|
| 1781 |
/* |
| 1782 |
* unify_hypothetical_args() |
| 1783 |
* |
| 1784 |
* Ensure that each hypothetical direct argument of a hypothetical-set |
| 1785 |
* aggregate has the same type as the corresponding aggregated argument. |
| 1786 |
* Modify the expressions in the fargs list, if necessary, and update |
| 1787 |
* actual_arg_types[]. |
| 1788 |
* |
| 1789 |
* If the agg declared its args non-ANY (even ANYELEMENT), we need only a |
| 1790 |
* sanity check that the declared types match; make_fn_arguments will coerce |
| 1791 |
* the actual arguments to match the declared ones. But if the declaration |
| 1792 |
* is ANY, nothing will happen in make_fn_arguments, so we need to fix any |
| 1793 |
* mismatch here. We use the same type resolution logic as UNION etc. |
| 1794 |
*/ |
| 1795 |
static void |
| 1796 |
unify_hypothetical_args(ParseState *pstate, |
| 1797 |
List *fargs, |
| 1798 |
int numAggregatedArgs, |
| 1799 |
Oid *actual_arg_types, |
| 1800 |
Oid *declared_arg_types) |
| 1801 |
{ |
| 1802 |
int numDirectArgs, |
| 1803 |
numNonHypotheticalArgs; |
| 1804 |
int hargpos; |
| 1805 |
|
| 1806 |
numDirectArgs = list_length(fargs) - numAggregatedArgs; |
| 1807 |
numNonHypotheticalArgs = numDirectArgs - numAggregatedArgs; |
| 1808 |
/* safety check (should only trigger with a misdeclared agg) */ |
| 1809 |
if (numNonHypotheticalArgs < 0) |
| 1810 |
elog(ERROR, "incorrect number of arguments to hypothetical-set aggregate"); |
| 1811 |
|
| 1812 |
/* Check each hypothetical arg and corresponding aggregated arg */ |
| 1813 |
for (hargpos = numNonHypotheticalArgs; hargpos < numDirectArgs; hargpos++) |
| 1814 |
{ |
| 1815 |
int aargpos = numDirectArgs + (hargpos - numNonHypotheticalArgs); |
| 1816 |
ListCell *harg = list_nth_cell(fargs, hargpos); |
| 1817 |
ListCell *aarg = list_nth_cell(fargs, aargpos); |
| 1818 |
Oid commontype; |
| 1819 |
int32 commontypmod; |
| 1820 |
|
| 1821 |
/* A mismatch means AggregateCreate didn't check properly ... */ |
| 1822 |
if (declared_arg_types[hargpos] != declared_arg_types[aargpos]) |
| 1823 |
elog(ERROR, "hypothetical-set aggregate has inconsistent declared argument types"); |
| 1824 |
|
| 1825 |
/* No need to unify if make_fn_arguments will coerce */ |
| 1826 |
if (declared_arg_types[hargpos] != ANYOID) |
| 1827 |
continue; |
| 1828 |
|
| 1829 |
/* |
| 1830 |
* Select common type, giving preference to the aggregated argument's |
| 1831 |
* type (we'd rather coerce the direct argument once than coerce all |
| 1832 |
* the aggregated values). |
| 1833 |
*/ |
| 1834 |
commontype = select_common_type(pstate, |
| 1835 |
list_make2(lfirst(aarg), lfirst(harg)), |
| 1836 |
"WITHIN GROUP", |
| 1837 |
NULL); |
| 1838 |
commontypmod = select_common_typmod(pstate, |
| 1839 |
list_make2(lfirst(aarg), lfirst(harg)), |
| 1840 |
commontype); |
| 1841 |
|
| 1842 |
/* |
| 1843 |
* Perform the coercions. We don't need to worry about NamedArgExprs |
| 1844 |
* here because they aren't supported with aggregates. |
| 1845 |
*/ |
| 1846 |
lfirst(harg) = coerce_type(pstate, |
| 1847 |
(Node *) lfirst(harg), |
| 1848 |
actual_arg_types[hargpos], |
| 1849 |
commontype, commontypmod, |
| 1850 |
COERCION_IMPLICIT, |
| 1851 |
COERCE_IMPLICIT_CAST, |
| 1852 |
-1); |
| 1853 |
actual_arg_types[hargpos] = commontype; |
| 1854 |
lfirst(aarg) = coerce_type(pstate, |
| 1855 |
(Node *) lfirst(aarg), |
| 1856 |
actual_arg_types[aargpos], |
| 1857 |
commontype, commontypmod, |
| 1858 |
COERCION_IMPLICIT, |
| 1859 |
COERCE_IMPLICIT_CAST, |
| 1860 |
-1); |
| 1861 |
actual_arg_types[aargpos] = commontype; |
| 1862 |
} |
| 1863 |
} |
| 1864 |
|
| 1865 |
|
| 1866 |
/* |
| 1867 |
* make_fn_arguments() |
| 1868 |
* |
| 1869 |
* Given the actual argument expressions for a function, and the desired |
| 1870 |
* input types for the function, add any necessary typecasting to the |
| 1871 |
* expression tree. Caller should already have verified that casting is |
| 1872 |
* allowed. |
| 1873 |
* |
| 1874 |
* Caution: given argument list is modified in-place. |
| 1875 |
* |
| 1876 |
* As with coerce_type, pstate may be NULL if no special unknown-Param |
| 1877 |
* processing is wanted. |
| 1878 |
*/ |
| 1879 |
void |
| 1880 |
make_fn_arguments(ParseState *pstate, |
| 1881 |
List *fargs, |
| 1882 |
Oid *actual_arg_types, |
| 1883 |
Oid *declared_arg_types) |
| 1884 |
{ |
| 1885 |
ListCell *current_fargs; |
| 1886 |
int i = 0; |
| 1887 |
|
| 1888 |
foreach(current_fargs, fargs) |
| 1889 |
{ |
| 1890 |
/* types don't match? then force coercion using a function call... */ |
| 1891 |
if (actual_arg_types[i] != declared_arg_types[i]) |
| 1892 |
{ |
| 1893 |
Node *node = (Node *) lfirst(current_fargs); |
| 1894 |
|
| 1895 |
/* |
| 1896 |
* If arg is a NamedArgExpr, coerce its input expr instead --- we |
| 1897 |
* want the NamedArgExpr to stay at the top level of the list. |
| 1898 |
*/ |
| 1899 |
if (IsA(node, NamedArgExpr)) |
| 1900 |
{ |
| 1901 |
NamedArgExpr *na = (NamedArgExpr *) node; |
| 1902 |
|
| 1903 |
node = coerce_type(pstate, |
| 1904 |
(Node *) na->arg, |
| 1905 |
actual_arg_types[i], |
| 1906 |
declared_arg_types[i], -1, |
| 1907 |
COERCION_IMPLICIT, |
| 1908 |
COERCE_IMPLICIT_CAST, |
| 1909 |
-1); |
| 1910 |
na->arg = (Expr *) node; |
| 1911 |
} |
| 1912 |
else |
| 1913 |
{ |
| 1914 |
node = coerce_type(pstate, |
| 1915 |
node, |
| 1916 |
actual_arg_types[i], |
| 1917 |
declared_arg_types[i], -1, |
| 1918 |
COERCION_IMPLICIT, |
| 1919 |
COERCE_IMPLICIT_CAST, |
| 1920 |
-1); |
| 1921 |
lfirst(current_fargs) = node; |
| 1922 |
} |
| 1923 |
} |
| 1924 |
i++; |
| 1925 |
} |
| 1926 |
} |
| 1927 |
|
| 1928 |
/* |
| 1929 |
* FuncNameAsType - |
| 1930 |
* convenience routine to see if a function name matches a type name |
| 1931 |
* |
| 1932 |
* Returns the OID of the matching type, or InvalidOid if none. We ignore |
| 1933 |
* shell types and complex types. |
| 1934 |
*/ |
| 1935 |
static Oid |
| 1936 |
FuncNameAsType(List *funcname) |
| 1937 |
{ |
| 1938 |
Oid result; |
| 1939 |
Type typtup; |
| 1940 |
|
| 1941 |
/* |
| 1942 |
* temp_ok=false protects the <refsect1 id="sql-createfunction-security"> |
| 1943 |
* contract for writing SECURITY DEFINER functions safely. |
| 1944 |
*/ |
| 1945 |
typtup = LookupTypeNameExtended(NULL, makeTypeNameFromNameList(funcname), |
| 1946 |
NULL, false, false); |
| 1947 |
if (typtup == NULL) |
| 1948 |
return InvalidOid; |
| 1949 |
|
| 1950 |
if (((Form_pg_type) GETSTRUCT(typtup))->typisdefined && |
| 1951 |
!OidIsValid(typeTypeRelid(typtup))) |
| 1952 |
result = typeTypeId(typtup); |
| 1953 |
else |
| 1954 |
result = InvalidOid; |
| 1955 |
|
| 1956 |
ReleaseSysCache(typtup); |
| 1957 |
return result; |
| 1958 |
} |
| 1959 |
|
| 1960 |
/* |
| 1961 |
* ParseComplexProjection - |
| 1962 |
* handles function calls with a single argument that is of complex type. |
| 1963 |
* If the function call is actually a column projection, return a suitably |
| 1964 |
* transformed expression tree. If not, return NULL. |
| 1965 |
*/ |
| 1966 |
static Node * |
| 1967 |
ParseComplexProjection(ParseState *pstate, const char *funcname, Node *first_arg, |
| 1968 |
int location) |
| 1969 |
{ |
| 1970 |
TupleDesc tupdesc; |
| 1971 |
int i; |
| 1972 |
|
| 1973 |
/* |
| 1974 |
* Special case for whole-row Vars so that we can resolve (foo.*).bar even |
| 1975 |
* when foo is a reference to a subselect, join, or RECORD function. A |
| 1976 |
* bonus is that we avoid generating an unnecessary FieldSelect; our |
| 1977 |
* result can omit the whole-row Var and just be a Var for the selected |
| 1978 |
* field. |
| 1979 |
* |
| 1980 |
* This case could be handled by expandRecordVariable, but it's more |
| 1981 |
* efficient to do it this way when possible. |
| 1982 |
*/ |
| 1983 |
if (IsA(first_arg, Var) && |
| 1984 |
((Var *) first_arg)->varattno == InvalidAttrNumber) |
| 1985 |
{ |
| 1986 |
ParseNamespaceItem *nsitem; |
| 1987 |
|
| 1988 |
nsitem = GetNSItemByVar(pstate, (Var *) first_arg); |
| 1989 |
/* Return a Var if funcname matches a column, else NULL */ |
| 1990 |
return scanNSItemForColumn(pstate, nsitem, |
| 1991 |
((Var *) first_arg)->varlevelsup, |
| 1992 |
funcname, location); |
| 1993 |
} |
| 1994 |
|
| 1995 |
/* |
| 1996 |
* Else do it the hard way with get_expr_result_tupdesc(). |
| 1997 |
* |
| 1998 |
* If it's a Var of type RECORD, we have to work even harder: we have to |
| 1999 |
* find what the Var refers to, and pass that to get_expr_result_tupdesc. |
| 2000 |
* That task is handled by expandRecordVariable(). |
| 2001 |
*/ |
| 2002 |
if (IsA(first_arg, Var) && |
| 2003 |
((Var *) first_arg)->vartype == RECORDOID) |
| 2004 |
tupdesc = expandRecordVariable(pstate, (Var *) first_arg, 0); |
| 2005 |
else |
| 2006 |
tupdesc = get_expr_result_tupdesc(first_arg, true); |
| 2007 |
if (!tupdesc) |
| 2008 |
return NULL; /* unresolvable RECORD type */ |
| 2009 |
|
| 2010 |
for (i = 0; i < tupdesc->natts; i++) |
| 2011 |
{ |
| 2012 |
Form_pg_attribute att = TupleDescAttr(tupdesc, i); |
| 2013 |
|
| 2014 |
if (strcmp(funcname, NameStr(att->attname)) == 0 && |
| 2015 |
!att->attisdropped) |
| 2016 |
{ |
| 2017 |
/* Success, so generate a FieldSelect expression */ |
| 2018 |
FieldSelect *fselect = makeNode(FieldSelect); |
| 2019 |
|
| 2020 |
fselect->arg = (Expr *) first_arg; |
| 2021 |
fselect->fieldnum = i + 1; |
| 2022 |
fselect->resulttype = att->atttypid; |
| 2023 |
fselect->resulttypmod = att->atttypmod; |
| 2024 |
/* save attribute's collation for parse_collate.c */ |
| 2025 |
fselect->resultcollid = att->attcollation; |
| 2026 |
return (Node *) fselect; |
| 2027 |
} |
| 2028 |
} |
| 2029 |
|
| 2030 |
return NULL; /* funcname does not match any column */ |
| 2031 |
} |
| 2032 |
|
| 2033 |
/* |
| 2034 |
* funcname_signature_string |
| 2035 |
* Build a string representing a function name, including arg types. |
| 2036 |
* The result is something like "foo(integer)". |
| 2037 |
* |
| 2038 |
* If argnames isn't NIL, it is a list of C strings representing the actual |
| 2039 |
* arg names for the last N arguments. This must be considered part of the |
| 2040 |
* function signature too, when dealing with named-notation function calls. |
| 2041 |
* |
| 2042 |
* This is typically used in the construction of function-not-found error |
| 2043 |
* messages. |
| 2044 |
*/ |
| 2045 |
const char * |
| 2046 |
funcname_signature_string(const char *funcname, int nargs, |
| 2047 |
List *argnames, const Oid *argtypes) |
| 2048 |
{ |
| 2049 |
StringInfoData argbuf; |
| 2050 |
int numposargs; |
| 2051 |
ListCell *lc; |
| 2052 |
int i; |
| 2053 |
|
| 2054 |
initStringInfo(&argbuf); |
| 2055 |
|
| 2056 |
appendStringInfo(&argbuf, "%s(", funcname); |
| 2057 |
|
| 2058 |
numposargs = nargs - list_length(argnames); |
| 2059 |
lc = list_head(argnames); |
| 2060 |
|
| 2061 |
for (i = 0; i < nargs; i++) |
| 2062 |
{ |
| 2063 |
if (i) |
| 2064 |
appendStringInfoString(&argbuf, ", "); |
| 2065 |
if (i >= numposargs) |
| 2066 |
{ |
| 2067 |
appendStringInfo(&argbuf, "%s => ", (char *) lfirst(lc)); |
| 2068 |
lc = lnext(argnames, lc); |
| 2069 |
} |
| 2070 |
appendStringInfoString(&argbuf, format_type_be(argtypes[i])); |
| 2071 |
} |
| 2072 |
|
| 2073 |
appendStringInfoChar(&argbuf, ')'); |
| 2074 |
|
| 2075 |
return argbuf.data; /* return palloc'd string buffer */ |
| 2076 |
} |
| 2077 |
|
| 2078 |
/* |
| 2079 |
* func_signature_string |
| 2080 |
* As above, but function name is passed as a qualified name list. |
| 2081 |
*/ |
| 2082 |
const char * |
| 2083 |
func_signature_string(List *funcname, int nargs, |
| 2084 |
List *argnames, const Oid *argtypes) |
| 2085 |
{ |
| 2086 |
return funcname_signature_string(NameListToString(funcname), |
| 2087 |
nargs, argnames, argtypes); |
| 2088 |
} |
| 2089 |
|
| 2090 |
/* |
| 2091 |
* LookupFuncNameInternal |
| 2092 |
* Workhorse for LookupFuncName/LookupFuncWithArgs |
| 2093 |
* |
| 2094 |
* In an error situation, e.g. can't find the function, then we return |
| 2095 |
* InvalidOid and set *lookupError to indicate what went wrong. |
| 2096 |
* |
| 2097 |
* Possible errors: |
| 2098 |
* FUNCLOOKUP_NOSUCHFUNC: we can't find a function of this name. |
| 2099 |
* FUNCLOOKUP_AMBIGUOUS: more than one function matches. |
| 2100 |
*/ |
| 2101 |
static Oid |
| 2102 |
LookupFuncNameInternal(ObjectType objtype, List *funcname, |
| 2103 |
int nargs, const Oid *argtypes, |
| 2104 |
bool include_out_arguments, bool missing_ok, |
| 2105 |
FuncLookupError *lookupError) |
| 2106 |
{ |
| 2107 |
Oid result = InvalidOid; |
| 2108 |
FuncCandidateList clist; |
| 2109 |
|
| 2110 |
/* NULL argtypes allowed for nullary functions only */ |
| 2111 |
Assert(argtypes != NULL || nargs == 0); |
| 2112 |
|
| 2113 |
/* Always set *lookupError, to forestall uninitialized-variable warnings */ |
| 2114 |
*lookupError = FUNCLOOKUP_NOSUCHFUNC; |
| 2115 |
|
| 2116 |
/* Get list of candidate objects */ |
| 2117 |
clist = FuncnameGetCandidates(funcname, nargs, NIL, false, false, |
| 2118 |
include_out_arguments, missing_ok); |
| 2119 |
|
| 2120 |
/* Scan list for a match to the arg types (if specified) and the objtype */ |
| 2121 |
for (; clist != NULL; clist = clist->next) |
| 2122 |
{ |
| 2123 |
/* Check arg type match, if specified */ |
| 2124 |
if (nargs >= 0) |
| 2125 |
{ |
| 2126 |
/* if nargs==0, argtypes can be null; don't pass that to memcmp */ |
| 2127 |
if (nargs > 0 && |
| 2128 |
memcmp(argtypes, clist->args, nargs * sizeof(Oid)) != 0) |
| 2129 |
continue; |
| 2130 |
} |
| 2131 |
|
| 2132 |
/* Check for duplicates reported by FuncnameGetCandidates */ |
| 2133 |
if (!OidIsValid(clist->oid)) |
| 2134 |
{ |
| 2135 |
*lookupError = FUNCLOOKUP_AMBIGUOUS; |
| 2136 |
return InvalidOid; |
| 2137 |
} |
| 2138 |
|
| 2139 |
/* Check objtype match, if specified */ |
| 2140 |
switch (objtype) |
| 2141 |
{ |
| 2142 |
case OBJECT_FUNCTION: |
| 2143 |
case OBJECT_AGGREGATE: |
| 2144 |
/* Ignore procedures */ |
| 2145 |
if (get_func_prokind(clist->oid) == PROKIND_PROCEDURE) |
| 2146 |
continue; |
| 2147 |
break; |
| 2148 |
case OBJECT_PROCEDURE: |
| 2149 |
/* Ignore non-procedures */ |
| 2150 |
if (get_func_prokind(clist->oid) != PROKIND_PROCEDURE) |
| 2151 |
continue; |
| 2152 |
break; |
| 2153 |
case OBJECT_ROUTINE: |
| 2154 |
/* no restriction */ |
| 2155 |
break; |
| 2156 |
default: |
| 2157 |
Assert(false); |
| 2158 |
} |
| 2159 |
|
| 2160 |
/* Check for multiple matches */ |
| 2161 |
if (OidIsValid(result)) |
| 2162 |
{ |
| 2163 |
*lookupError = FUNCLOOKUP_AMBIGUOUS; |
| 2164 |
return InvalidOid; |
| 2165 |
} |
| 2166 |
|
| 2167 |
/* OK, we have a candidate */ |
| 2168 |
result = clist->oid; |
| 2169 |
} |
| 2170 |
|
| 2171 |
return result; |
| 2172 |
} |
| 2173 |
|
| 2174 |
/* |
| 2175 |
* LookupFuncName |
| 2176 |
* |
| 2177 |
* Given a possibly-qualified function name and optionally a set of argument |
| 2178 |
* types, look up the function. Pass nargs == -1 to indicate that the number |
| 2179 |
* and types of the arguments are unspecified (this is NOT the same as |
| 2180 |
* specifying that there are no arguments). |
| 2181 |
* |
| 2182 |
* If the function name is not schema-qualified, it is sought in the current |
| 2183 |
* namespace search path. |
| 2184 |
* |
| 2185 |
* If the function is not found, we return InvalidOid if missing_ok is true, |
| 2186 |
* else raise an error. |
| 2187 |
* |
| 2188 |
* If nargs == -1 and multiple functions are found matching this function name |
| 2189 |
* we will raise an ambiguous-function error, regardless of what missing_ok is |
| 2190 |
* set to. |
| 2191 |
* |
| 2192 |
* Only functions will be found; procedures will be ignored even if they |
| 2193 |
* match the name and argument types. (However, we don't trouble to reject |
| 2194 |
* aggregates or window functions here.) |
| 2195 |
*/ |
| 2196 |
Oid |
| 2197 |
LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool missing_ok) |
| 2198 |
{ |
| 2199 |
Oid funcoid; |
| 2200 |
FuncLookupError lookupError; |
| 2201 |
|
| 2202 |
funcoid = LookupFuncNameInternal(OBJECT_FUNCTION, |
| 2203 |
funcname, nargs, argtypes, |
| 2204 |
false, missing_ok, |
| 2205 |
&lookupError); |
| 2206 |
|
| 2207 |
if (OidIsValid(funcoid)) |
| 2208 |
return funcoid; |
| 2209 |
|
| 2210 |
switch (lookupError) |
| 2211 |
{ |
| 2212 |
case FUNCLOOKUP_NOSUCHFUNC: |
| 2213 |
/* Let the caller deal with it when missing_ok is true */ |
| 2214 |
if (missing_ok) |
| 2215 |
return InvalidOid; |
| 2216 |
|
| 2217 |
if (nargs < 0) |
| 2218 |
ereport(ERROR, |
| 2219 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2220 |
errmsg("could not find a function named \"%s\"", |
| 2221 |
NameListToString(funcname)))); |
| 2222 |
else |
| 2223 |
ereport(ERROR, |
| 2224 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2225 |
errmsg("function %s does not exist", |
| 2226 |
func_signature_string(funcname, nargs, |
| 2227 |
NIL, argtypes)))); |
| 2228 |
break; |
| 2229 |
|
| 2230 |
case FUNCLOOKUP_AMBIGUOUS: |
| 2231 |
/* Raise an error regardless of missing_ok */ |
| 2232 |
ereport(ERROR, |
| 2233 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 2234 |
errmsg("function name \"%s\" is not unique", |
| 2235 |
NameListToString(funcname)), |
| 2236 |
errhint("Specify the argument list to select the function unambiguously."))); |
| 2237 |
break; |
| 2238 |
} |
| 2239 |
|
| 2240 |
return InvalidOid; /* Keep compiler quiet */ |
| 2241 |
} |
| 2242 |
|
| 2243 |
/* |
| 2244 |
* LookupFuncWithArgs |
| 2245 |
* |
| 2246 |
* Like LookupFuncName, but the argument types are specified by an |
| 2247 |
* ObjectWithArgs node. Also, this function can check whether the result is a |
| 2248 |
* function, procedure, or aggregate, based on the objtype argument. Pass |
| 2249 |
* OBJECT_ROUTINE to accept any of them. |
| 2250 |
* |
| 2251 |
* For historical reasons, we also accept aggregates when looking for a |
| 2252 |
* function. |
| 2253 |
* |
| 2254 |
* When missing_ok is true we don't generate any error for missing objects and |
| 2255 |
* return InvalidOid. Other types of errors can still be raised, regardless |
| 2256 |
* of the value of missing_ok. |
| 2257 |
*/ |
| 2258 |
Oid |
| 2259 |
LookupFuncWithArgs(ObjectType objtype, ObjectWithArgs *func, bool missing_ok) |
| 2260 |
{ |
| 2261 |
Oid argoids[FUNC_MAX_ARGS]; |
| 2262 |
int argcount; |
| 2263 |
int nargs; |
| 2264 |
int i; |
| 2265 |
ListCell *args_item; |
| 2266 |
Oid oid; |
| 2267 |
FuncLookupError lookupError; |
| 2268 |
|
| 2269 |
Assert(objtype == OBJECT_AGGREGATE || |
| 2270 |
objtype == OBJECT_FUNCTION || |
| 2271 |
objtype == OBJECT_PROCEDURE || |
| 2272 |
objtype == OBJECT_ROUTINE); |
| 2273 |
|
| 2274 |
argcount = list_length(func->objargs); |
| 2275 |
if (argcount > FUNC_MAX_ARGS) |
| 2276 |
{ |
| 2277 |
if (objtype == OBJECT_PROCEDURE) |
| 2278 |
ereport(ERROR, |
| 2279 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 2280 |
errmsg_plural("procedures cannot have more than %d argument", |
| 2281 |
"procedures cannot have more than %d arguments", |
| 2282 |
FUNC_MAX_ARGS, |
| 2283 |
FUNC_MAX_ARGS))); |
| 2284 |
else |
| 2285 |
ereport(ERROR, |
| 2286 |
(errcode(ERRCODE_TOO_MANY_ARGUMENTS), |
| 2287 |
errmsg_plural("functions cannot have more than %d argument", |
| 2288 |
"functions cannot have more than %d arguments", |
| 2289 |
FUNC_MAX_ARGS, |
| 2290 |
FUNC_MAX_ARGS))); |
| 2291 |
} |
| 2292 |
|
| 2293 |
/* |
| 2294 |
* First, perform a lookup considering only input arguments (traditional |
| 2295 |
* Postgres rules). |
| 2296 |
*/ |
| 2297 |
i = 0; |
| 2298 |
foreach(args_item, func->objargs) |
| 2299 |
{ |
| 2300 |
TypeName *t = lfirst_node(TypeName, args_item); |
| 2301 |
|
| 2302 |
argoids[i] = LookupTypeNameOid(NULL, t, missing_ok); |
| 2303 |
if (!OidIsValid(argoids[i])) |
| 2304 |
return InvalidOid; /* missing_ok must be true */ |
| 2305 |
i++; |
| 2306 |
} |
| 2307 |
|
| 2308 |
/* |
| 2309 |
* Set nargs for LookupFuncNameInternal. It expects -1 to mean no args |
| 2310 |
* were specified. |
| 2311 |
*/ |
| 2312 |
nargs = func->args_unspecified ? -1 : argcount; |
| 2313 |
|
| 2314 |
/* |
| 2315 |
* In args_unspecified mode, also tell LookupFuncNameInternal to consider |
| 2316 |
* the object type, since there seems no reason not to. However, if we |
| 2317 |
* have an argument list, disable the objtype check, because we'd rather |
| 2318 |
* complain about "object is of wrong type" than "object doesn't exist". |
| 2319 |
* (Note that with args, FuncnameGetCandidates will have ensured there's |
| 2320 |
* only one argtype match, so we're not risking an ambiguity failure via |
| 2321 |
* this choice.) |
| 2322 |
*/ |
| 2323 |
oid = LookupFuncNameInternal(func->args_unspecified ? objtype : OBJECT_ROUTINE, |
| 2324 |
func->objname, nargs, argoids, |
| 2325 |
false, missing_ok, |
| 2326 |
&lookupError); |
| 2327 |
|
| 2328 |
/* |
| 2329 |
* If PROCEDURE or ROUTINE was specified, and we have an argument list |
| 2330 |
* that contains no parameter mode markers, and we didn't already discover |
| 2331 |
* that there's ambiguity, perform a lookup considering all arguments. |
| 2332 |
* (Note: for a zero-argument procedure, or in args_unspecified mode, the |
| 2333 |
* normal lookup is sufficient; so it's OK to require non-NIL objfuncargs |
| 2334 |
* to perform this lookup.) |
| 2335 |
*/ |
| 2336 |
if ((objtype == OBJECT_PROCEDURE || objtype == OBJECT_ROUTINE) && |
| 2337 |
func->objfuncargs != NIL && |
| 2338 |
lookupError != FUNCLOOKUP_AMBIGUOUS) |
| 2339 |
{ |
| 2340 |
bool have_param_mode = false; |
| 2341 |
|
| 2342 |
/* |
| 2343 |
* Check for non-default parameter mode markers. If there are any, |
| 2344 |
* then the command does not conform to SQL-spec syntax, so we may |
| 2345 |
* assume that the traditional Postgres lookup method of considering |
| 2346 |
* only input parameters is sufficient. (Note that because the spec |
| 2347 |
* doesn't have OUT arguments for functions, we also don't need this |
| 2348 |
* hack in FUNCTION or AGGREGATE mode.) |
| 2349 |
*/ |
| 2350 |
foreach(args_item, func->objfuncargs) |
| 2351 |
{ |
| 2352 |
FunctionParameter *fp = lfirst_node(FunctionParameter, args_item); |
| 2353 |
|
| 2354 |
if (fp->mode != FUNC_PARAM_DEFAULT) |
| 2355 |
{ |
| 2356 |
have_param_mode = true; |
| 2357 |
break; |
| 2358 |
} |
| 2359 |
} |
| 2360 |
|
| 2361 |
if (!have_param_mode) |
| 2362 |
{ |
| 2363 |
Oid poid; |
| 2364 |
|
| 2365 |
/* Without mode marks, objargs surely includes all params */ |
| 2366 |
Assert(list_length(func->objfuncargs) == argcount); |
| 2367 |
|
| 2368 |
/* For objtype == OBJECT_PROCEDURE, we can ignore non-procedures */ |
| 2369 |
poid = LookupFuncNameInternal(objtype, func->objname, |
| 2370 |
argcount, argoids, |
| 2371 |
true, missing_ok, |
| 2372 |
&lookupError); |
| 2373 |
|
| 2374 |
/* Combine results, handling ambiguity */ |
| 2375 |
if (OidIsValid(poid)) |
| 2376 |
{ |
| 2377 |
if (OidIsValid(oid) && oid != poid) |
| 2378 |
{ |
| 2379 |
/* oops, we got hits both ways, on different objects */ |
| 2380 |
oid = InvalidOid; |
| 2381 |
lookupError = FUNCLOOKUP_AMBIGUOUS; |
| 2382 |
} |
| 2383 |
else |
| 2384 |
oid = poid; |
| 2385 |
} |
| 2386 |
else if (lookupError == FUNCLOOKUP_AMBIGUOUS) |
| 2387 |
oid = InvalidOid; |
| 2388 |
} |
| 2389 |
} |
| 2390 |
|
| 2391 |
if (OidIsValid(oid)) |
| 2392 |
{ |
| 2393 |
/* |
| 2394 |
* Even if we found the function, perform validation that the objtype |
| 2395 |
* matches the prokind of the found function. For historical reasons |
| 2396 |
* we allow the objtype of FUNCTION to include aggregates and window |
| 2397 |
* functions; but we draw the line if the object is a procedure. That |
| 2398 |
* is a new enough feature that this historical rule does not apply. |
| 2399 |
* |
| 2400 |
* (This check is partially redundant with the objtype check in |
| 2401 |
* LookupFuncNameInternal; but not entirely, since we often don't tell |
| 2402 |
* LookupFuncNameInternal to apply that check at all.) |
| 2403 |
*/ |
| 2404 |
switch (objtype) |
| 2405 |
{ |
| 2406 |
case OBJECT_FUNCTION: |
| 2407 |
/* Only complain if it's a procedure. */ |
| 2408 |
if (get_func_prokind(oid) == PROKIND_PROCEDURE) |
| 2409 |
ereport(ERROR, |
| 2410 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 2411 |
errmsg("%s is not a function", |
| 2412 |
func_signature_string(func->objname, argcount, |
| 2413 |
NIL, argoids)))); |
| 2414 |
break; |
| 2415 |
|
| 2416 |
case OBJECT_PROCEDURE: |
| 2417 |
/* Reject if found object is not a procedure. */ |
| 2418 |
if (get_func_prokind(oid) != PROKIND_PROCEDURE) |
| 2419 |
ereport(ERROR, |
| 2420 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 2421 |
errmsg("%s is not a procedure", |
| 2422 |
func_signature_string(func->objname, argcount, |
| 2423 |
NIL, argoids)))); |
| 2424 |
break; |
| 2425 |
|
| 2426 |
case OBJECT_AGGREGATE: |
| 2427 |
/* Reject if found object is not an aggregate. */ |
| 2428 |
if (get_func_prokind(oid) != PROKIND_AGGREGATE) |
| 2429 |
ereport(ERROR, |
| 2430 |
(errcode(ERRCODE_WRONG_OBJECT_TYPE), |
| 2431 |
errmsg("function %s is not an aggregate", |
| 2432 |
func_signature_string(func->objname, argcount, |
| 2433 |
NIL, argoids)))); |
| 2434 |
break; |
| 2435 |
|
| 2436 |
default: |
| 2437 |
/* OBJECT_ROUTINE accepts anything. */ |
| 2438 |
break; |
| 2439 |
} |
| 2440 |
|
| 2441 |
return oid; /* All good */ |
| 2442 |
} |
| 2443 |
else |
| 2444 |
{ |
| 2445 |
/* Deal with cases where the lookup failed */ |
| 2446 |
switch (lookupError) |
| 2447 |
{ |
| 2448 |
case FUNCLOOKUP_NOSUCHFUNC: |
| 2449 |
/* Suppress no-such-func errors when missing_ok is true */ |
| 2450 |
if (missing_ok) |
| 2451 |
break; |
| 2452 |
|
| 2453 |
switch (objtype) |
| 2454 |
{ |
| 2455 |
case OBJECT_PROCEDURE: |
| 2456 |
if (func->args_unspecified) |
| 2457 |
ereport(ERROR, |
| 2458 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2459 |
errmsg("could not find a procedure named \"%s\"", |
| 2460 |
NameListToString(func->objname)))); |
| 2461 |
else |
| 2462 |
ereport(ERROR, |
| 2463 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2464 |
errmsg("procedure %s does not exist", |
| 2465 |
func_signature_string(func->objname, argcount, |
| 2466 |
NIL, argoids)))); |
| 2467 |
break; |
| 2468 |
|
| 2469 |
case OBJECT_AGGREGATE: |
| 2470 |
if (func->args_unspecified) |
| 2471 |
ereport(ERROR, |
| 2472 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2473 |
errmsg("could not find an aggregate named \"%s\"", |
| 2474 |
NameListToString(func->objname)))); |
| 2475 |
else if (argcount == 0) |
| 2476 |
ereport(ERROR, |
| 2477 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2478 |
errmsg("aggregate %s(*) does not exist", |
| 2479 |
NameListToString(func->objname)))); |
| 2480 |
else |
| 2481 |
ereport(ERROR, |
| 2482 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2483 |
errmsg("aggregate %s does not exist", |
| 2484 |
func_signature_string(func->objname, argcount, |
| 2485 |
NIL, argoids)))); |
| 2486 |
break; |
| 2487 |
|
| 2488 |
default: |
| 2489 |
/* FUNCTION and ROUTINE */ |
| 2490 |
if (func->args_unspecified) |
| 2491 |
ereport(ERROR, |
| 2492 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2493 |
errmsg("could not find a function named \"%s\"", |
| 2494 |
NameListToString(func->objname)))); |
| 2495 |
else |
| 2496 |
ereport(ERROR, |
| 2497 |
(errcode(ERRCODE_UNDEFINED_FUNCTION), |
| 2498 |
errmsg("function %s does not exist", |
| 2499 |
func_signature_string(func->objname, argcount, |
| 2500 |
NIL, argoids)))); |
| 2501 |
break; |
| 2502 |
} |
| 2503 |
break; |
| 2504 |
|
| 2505 |
case FUNCLOOKUP_AMBIGUOUS: |
| 2506 |
switch (objtype) |
| 2507 |
{ |
| 2508 |
case OBJECT_FUNCTION: |
| 2509 |
ereport(ERROR, |
| 2510 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 2511 |
errmsg("function name \"%s\" is not unique", |
| 2512 |
NameListToString(func->objname)), |
| 2513 |
func->args_unspecified ? |
| 2514 |
errhint("Specify the argument list to select the function unambiguously.") : 0)); |
| 2515 |
break; |
| 2516 |
case OBJECT_PROCEDURE: |
| 2517 |
ereport(ERROR, |
| 2518 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 2519 |
errmsg("procedure name \"%s\" is not unique", |
| 2520 |
NameListToString(func->objname)), |
| 2521 |
func->args_unspecified ? |
| 2522 |
errhint("Specify the argument list to select the procedure unambiguously.") : 0)); |
| 2523 |
break; |
| 2524 |
case OBJECT_AGGREGATE: |
| 2525 |
ereport(ERROR, |
| 2526 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 2527 |
errmsg("aggregate name \"%s\" is not unique", |
| 2528 |
NameListToString(func->objname)), |
| 2529 |
func->args_unspecified ? |
| 2530 |
errhint("Specify the argument list to select the aggregate unambiguously.") : 0)); |
| 2531 |
break; |
| 2532 |
case OBJECT_ROUTINE: |
| 2533 |
ereport(ERROR, |
| 2534 |
(errcode(ERRCODE_AMBIGUOUS_FUNCTION), |
| 2535 |
errmsg("routine name \"%s\" is not unique", |
| 2536 |
NameListToString(func->objname)), |
| 2537 |
func->args_unspecified ? |
| 2538 |
errhint("Specify the argument list to select the routine unambiguously.") : 0)); |
| 2539 |
break; |
| 2540 |
|
| 2541 |
default: |
| 2542 |
Assert(false); /* Disallowed by Assert above */ |
| 2543 |
break; |
| 2544 |
} |
| 2545 |
break; |
| 2546 |
} |
| 2547 |
|
| 2548 |
return InvalidOid; |
| 2549 |
} |
| 2550 |
} |
| 2551 |
|
| 2552 |
/* |
| 2553 |
* check_srf_call_placement |
| 2554 |
* Verify that a set-returning function is called in a valid place, |
| 2555 |
* and throw a nice error if not. |
| 2556 |
* |
| 2557 |
* A side-effect is to set pstate->p_hasTargetSRFs true if appropriate. |
| 2558 |
* |
| 2559 |
* last_srf should be a copy of pstate->p_last_srf from just before we |
| 2560 |
* started transforming the function's arguments. This allows detection |
| 2561 |
* of whether the SRF's arguments contain any SRFs. |
| 2562 |
*/ |
| 2563 |
void |
| 2564 |
check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) |
| 2565 |
{ |
| 2566 |
const char *err; |
| 2567 |
bool errkind; |
| 2568 |
|
| 2569 |
/* |
| 2570 |
* Check to see if the set-returning function is in an invalid place |
| 2571 |
* within the query. Basically, we don't allow SRFs anywhere except in |
| 2572 |
* the targetlist (which includes GROUP BY/ORDER BY expressions), VALUES, |
| 2573 |
* and functions in FROM. |
| 2574 |
* |
| 2575 |
* For brevity we support two schemes for reporting an error here: set |
| 2576 |
* "err" to a custom message, or set "errkind" true if the error context |
| 2577 |
* is sufficiently identified by what ParseExprKindName will return, *and* |
| 2578 |
* what it will return is just a SQL keyword. (Otherwise, use a custom |
| 2579 |
* message to avoid creating translation problems.) |
| 2580 |
*/ |
| 2581 |
err = NULL; |
| 2582 |
errkind = false; |
| 2583 |
switch (pstate->p_expr_kind) |
| 2584 |
{ |
| 2585 |
case EXPR_KIND_NONE: |
| 2586 |
Assert(false); /* can't happen */ |
| 2587 |
break; |
| 2588 |
case EXPR_KIND_OTHER: |
| 2589 |
/* Accept SRF here; caller must throw error if wanted */ |
| 2590 |
break; |
| 2591 |
case EXPR_KIND_JOIN_ON: |
| 2592 |
case EXPR_KIND_JOIN_USING: |
| 2593 |
err = _("set-returning functions are not allowed in JOIN conditions"); |
| 2594 |
break; |
| 2595 |
case EXPR_KIND_FROM_SUBSELECT: |
| 2596 |
/* can't get here, but just in case, throw an error */ |
| 2597 |
errkind = true; |
| 2598 |
break; |
| 2599 |
case EXPR_KIND_FROM_FUNCTION: |
| 2600 |
/* okay, but we don't allow nested SRFs here */ |
| 2601 |
/* errmsg is chosen to match transformRangeFunction() */ |
| 2602 |
/* errposition should point to the inner SRF */ |
| 2603 |
if (pstate->p_last_srf != last_srf) |
| 2604 |
ereport(ERROR, |
| 2605 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2606 |
errmsg("set-returning functions must appear at top level of FROM"), |
| 2607 |
parser_errposition(pstate, |
| 2608 |
exprLocation(pstate->p_last_srf)))); |
| 2609 |
break; |
| 2610 |
case EXPR_KIND_WHERE: |
| 2611 |
errkind = true; |
| 2612 |
break; |
| 2613 |
case EXPR_KIND_POLICY: |
| 2614 |
err = _("set-returning functions are not allowed in policy expressions"); |
| 2615 |
break; |
| 2616 |
case EXPR_KIND_HAVING: |
| 2617 |
errkind = true; |
| 2618 |
break; |
| 2619 |
case EXPR_KIND_FILTER: |
| 2620 |
errkind = true; |
| 2621 |
break; |
| 2622 |
case EXPR_KIND_WINDOW_PARTITION: |
| 2623 |
case EXPR_KIND_WINDOW_ORDER: |
| 2624 |
/* okay, these are effectively GROUP BY/ORDER BY */ |
| 2625 |
pstate->p_hasTargetSRFs = true; |
| 2626 |
break; |
| 2627 |
case EXPR_KIND_WINDOW_FRAME_RANGE: |
| 2628 |
case EXPR_KIND_WINDOW_FRAME_ROWS: |
| 2629 |
case EXPR_KIND_WINDOW_FRAME_GROUPS: |
| 2630 |
err = _("set-returning functions are not allowed in window definitions"); |
| 2631 |
break; |
| 2632 |
case EXPR_KIND_SELECT_TARGET: |
| 2633 |
case EXPR_KIND_INSERT_TARGET: |
| 2634 |
/* okay */ |
| 2635 |
pstate->p_hasTargetSRFs = true; |
| 2636 |
break; |
| 2637 |
case EXPR_KIND_UPDATE_SOURCE: |
| 2638 |
case EXPR_KIND_UPDATE_TARGET: |
| 2639 |
/* disallowed because it would be ambiguous what to do */ |
| 2640 |
errkind = true; |
| 2641 |
break; |
| 2642 |
case EXPR_KIND_GROUP_BY: |
| 2643 |
case EXPR_KIND_ORDER_BY: |
| 2644 |
/* okay */ |
| 2645 |
pstate->p_hasTargetSRFs = true; |
| 2646 |
break; |
| 2647 |
case EXPR_KIND_DISTINCT_ON: |
| 2648 |
/* okay */ |
| 2649 |
pstate->p_hasTargetSRFs = true; |
| 2650 |
break; |
| 2651 |
case EXPR_KIND_LIMIT: |
| 2652 |
case EXPR_KIND_OFFSET: |
| 2653 |
errkind = true; |
| 2654 |
break; |
| 2655 |
case EXPR_KIND_RETURNING: |
| 2656 |
case EXPR_KIND_MERGE_RETURNING: |
| 2657 |
errkind = true; |
| 2658 |
break; |
| 2659 |
case EXPR_KIND_VALUES: |
| 2660 |
/* SRFs are presently not supported by nodeValuesscan.c */ |
| 2661 |
errkind = true; |
| 2662 |
break; |
| 2663 |
case EXPR_KIND_VALUES_SINGLE: |
| 2664 |
/* okay, since we process this like a SELECT tlist */ |
| 2665 |
pstate->p_hasTargetSRFs = true; |
| 2666 |
break; |
| 2667 |
case EXPR_KIND_MERGE_WHEN: |
| 2668 |
err = _("set-returning functions are not allowed in MERGE WHEN conditions"); |
| 2669 |
break; |
| 2670 |
case EXPR_KIND_CHECK_CONSTRAINT: |
| 2671 |
case EXPR_KIND_DOMAIN_CHECK: |
| 2672 |
err = _("set-returning functions are not allowed in check constraints"); |
| 2673 |
break; |
| 2674 |
case EXPR_KIND_COLUMN_DEFAULT: |
| 2675 |
case EXPR_KIND_FUNCTION_DEFAULT: |
| 2676 |
err = _("set-returning functions are not allowed in DEFAULT expressions"); |
| 2677 |
break; |
| 2678 |
case EXPR_KIND_INDEX_EXPRESSION: |
| 2679 |
err = _("set-returning functions are not allowed in index expressions"); |
| 2680 |
break; |
| 2681 |
case EXPR_KIND_INDEX_PREDICATE: |
| 2682 |
err = _("set-returning functions are not allowed in index predicates"); |
| 2683 |
break; |
| 2684 |
case EXPR_KIND_STATS_EXPRESSION: |
| 2685 |
err = _("set-returning functions are not allowed in statistics expressions"); |
| 2686 |
break; |
| 2687 |
case EXPR_KIND_ALTER_COL_TRANSFORM: |
| 2688 |
err = _("set-returning functions are not allowed in transform expressions"); |
| 2689 |
break; |
| 2690 |
case EXPR_KIND_EXECUTE_PARAMETER: |
| 2691 |
err = _("set-returning functions are not allowed in EXECUTE parameters"); |
| 2692 |
break; |
| 2693 |
case EXPR_KIND_TRIGGER_WHEN: |
| 2694 |
err = _("set-returning functions are not allowed in trigger WHEN conditions"); |
| 2695 |
break; |
| 2696 |
case EXPR_KIND_PARTITION_BOUND: |
| 2697 |
err = _("set-returning functions are not allowed in partition bound"); |
| 2698 |
break; |
| 2699 |
case EXPR_KIND_PARTITION_EXPRESSION: |
| 2700 |
err = _("set-returning functions are not allowed in partition key expressions"); |
| 2701 |
break; |
| 2702 |
case EXPR_KIND_CALL_ARGUMENT: |
| 2703 |
err = _("set-returning functions are not allowed in CALL arguments"); |
| 2704 |
break; |
| 2705 |
case EXPR_KIND_COPY_WHERE: |
| 2706 |
err = _("set-returning functions are not allowed in COPY FROM WHERE conditions"); |
| 2707 |
break; |
| 2708 |
case EXPR_KIND_GENERATED_COLUMN: |
| 2709 |
err = _("set-returning functions are not allowed in column generation expressions"); |
| 2710 |
break; |
| 2711 |
case EXPR_KIND_CYCLE_MARK: |
| 2712 |
errkind = true; |
| 2713 |
break; |
| 2714 |
|
| 2715 |
/* |
| 2716 |
* There is intentionally no default: case here, so that the |
| 2717 |
* compiler will warn if we add a new ParseExprKind without |
| 2718 |
* extending this switch. If we do see an unrecognized value at |
| 2719 |
* runtime, the behavior will be the same as for EXPR_KIND_OTHER, |
| 2720 |
* which is sane anyway. |
| 2721 |
*/ |
| 2722 |
} |
| 2723 |
if (err) |
| 2724 |
ereport(ERROR, |
| 2725 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2726 |
errmsg_internal("%s", err), |
| 2727 |
parser_errposition(pstate, location))); |
| 2728 |
if (errkind) |
| 2729 |
ereport(ERROR, |
| 2730 |
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED), |
| 2731 |
/* translator: %s is name of a SQL construct, eg GROUP BY */ |
| 2732 |
errmsg("set-returning functions are not allowed in %s", |
| 2733 |
ParseExprKindName(pstate->p_expr_kind)), |
| 2734 |
parser_errposition(pstate, location))); |
| 2735 |
} |