1997-11-25 23:07:18 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* parse_func.c
|
|
|
|
* handle function calls in parser
|
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-11-15 22:14:46 +01:00
|
|
|
* $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.199 2007/11/15 21:14:37 momjian Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/pg_inherits.h"
|
|
|
|
#include "catalog/pg_proc.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "catalog/pg_type.h"
|
2005-04-01 00:46:33 +02:00
|
|
|
#include "funcapi.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "nodes/makefuncs.h"
|
2003-06-06 17:04:03 +02:00
|
|
|
#include "parser/parse_agg.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "parser/parse_expr.h"
|
|
|
|
#include "parser/parse_func.h"
|
|
|
|
#include "parser/parse_relation.h"
|
2005-05-31 03:03:23 +02:00
|
|
|
#include "parser/parse_target.h"
|
2000-06-15 05:33:12 +02:00
|
|
|
#include "parser/parse_type.h"
|
2001-08-09 20:28:18 +02:00
|
|
|
#include "utils/builtins.h"
|
2000-05-28 19:56:29 +02:00
|
|
|
#include "utils/fmgroids.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "utils/lsyscache.h"
|
|
|
|
#include "utils/syscache.h"
|
|
|
|
|
2002-04-11 22:00:18 +02:00
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
static Oid FuncNameAsType(List *funcname);
|
2004-04-02 21:07:02 +02:00
|
|
|
static Node *ParseComplexProjection(ParseState *pstate, char *funcname,
|
2006-03-14 23:48:25 +01:00
|
|
|
Node *first_arg, int location);
|
|
|
|
static void unknown_attribute(ParseState *pstate, Node *relref, char *attname,
|
2006-10-04 02:30:14 +02:00
|
|
|
int location);
|
1997-11-26 04:43:18 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
/*
|
2002-03-21 17:02:16 +01:00
|
|
|
* Parse a function call
|
New comment. This func/column things has always confused me.
/*
* parse function
* This code is confusing because the database can accept
* relation.column, column.function, or relation.column.function.
* In these cases, funcname is the last parameter, and fargs are
* the rest.
*
* It can also be called as func(col) or func(col,col).
* In this case, Funcname is the part before parens, and fargs
* are the part in parens.
*
*/
Node *
ParseFuncOrColumn(ParseState *pstate, char *funcname, List *fargs,
bool agg_star, bool agg_distinct,
int precedence)
2001-05-19 02:33:20 +02:00
|
|
|
*
|
2002-03-21 17:02:16 +01:00
|
|
|
* For historical reasons, Postgres tries to treat the notations tab.col
|
|
|
|
* and col(tab) as equivalent: if a single-argument function call has an
|
2002-04-09 22:35:55 +02:00
|
|
|
* argument of complex type and the (unqualified) function name matches
|
2004-04-02 21:07:02 +02:00
|
|
|
* any attribute of the type, we take it as a column projection. Conversely
|
|
|
|
* a function of a single complex-type argument can be written like a
|
|
|
|
* column reference, allowing functions to act like computed columns.
|
2001-05-19 00:54:23 +02:00
|
|
|
*
|
2002-03-21 17:02:16 +01:00
|
|
|
* Hence, both cases come through here. The is_column parameter tells us
|
|
|
|
* which syntactic construct is actually being dealt with, but this is
|
|
|
|
* intended to be used only to deliver an appropriate error message,
|
|
|
|
* not to affect the semantics. When is_column is true, we should have
|
2002-04-09 22:35:55 +02:00
|
|
|
* a single argument (the putative table), unqualified function name
|
|
|
|
* equal to the column name, and no aggregate decoration.
|
2001-05-19 03:57:11 +02:00
|
|
|
*
|
2004-04-02 21:07:02 +02:00
|
|
|
* The argument expressions (in fargs) must have been transformed already.
|
1998-01-20 06:05:08 +01:00
|
|
|
*/
|
1997-11-25 23:07:18 +01:00
|
|
|
Node *
|
2002-04-09 22:35:55 +02:00
|
|
|
ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
|
2006-03-14 23:48:25 +01:00
|
|
|
bool agg_star, bool agg_distinct, bool is_column,
|
|
|
|
int location)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-03-21 17:02:16 +01:00
|
|
|
Oid rettype;
|
|
|
|
Oid funcid;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *l;
|
2005-06-22 17:19:43 +02:00
|
|
|
ListCell *nextl;
|
1997-11-25 23:07:18 +01:00
|
|
|
Node *first_arg = NULL;
|
2005-06-22 17:19:43 +02:00
|
|
|
int nargs;
|
2003-04-09 01:20:04 +02:00
|
|
|
Oid actual_arg_types[FUNC_MAX_ARGS];
|
|
|
|
Oid *declared_arg_types;
|
1997-11-25 23:07:18 +01:00
|
|
|
Node *retval;
|
|
|
|
bool retset;
|
2002-03-21 17:02:16 +01:00
|
|
|
FuncDetailCode fdresult;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2001-04-19 00:25:31 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Most of the rest of the parser just assumes that functions do not have
|
|
|
|
* more than FUNC_MAX_ARGS parameters. We have to test here to protect
|
|
|
|
* against array overruns, etc. Of course, this may not be a function,
|
|
|
|
* but the test doesn't hurt.
|
2001-04-19 00:25:31 +02:00
|
|
|
*/
|
2005-06-22 17:19:43 +02:00
|
|
|
if (list_length(fargs) > FUNC_MAX_ARGS)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
|
2005-03-29 05:01:32 +02:00
|
|
|
errmsg("cannot pass more than %d arguments to a function",
|
2006-03-14 23:48:25 +01:00
|
|
|
FUNC_MAX_ARGS),
|
|
|
|
parser_errposition(pstate, location)));
|
2001-04-19 00:25:31 +02:00
|
|
|
|
2005-06-22 17:19:43 +02:00
|
|
|
/*
|
|
|
|
* Extract arg type info in preparation for function lookup.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If any arguments are Param markers of type VOID, we discard them from
|
|
|
|
* the parameter list. This is a hack to allow the JDBC driver to not
|
|
|
|
* have to distinguish "input" and "output" parameter symbols while
|
|
|
|
* parsing function-call constructs. We can't use foreach() because we
|
|
|
|
* may modify the list ...
|
2005-06-22 17:19:43 +02:00
|
|
|
*/
|
|
|
|
nargs = 0;
|
|
|
|
for (l = list_head(fargs); l != NULL; l = nextl)
|
|
|
|
{
|
|
|
|
Node *arg = lfirst(l);
|
|
|
|
Oid argtype = exprType(arg);
|
|
|
|
|
|
|
|
nextl = lnext(l);
|
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
if (argtype == VOIDOID && IsA(arg, Param) &&!is_column)
|
2005-06-22 17:19:43 +02:00
|
|
|
{
|
|
|
|
fargs = list_delete_ptr(fargs, arg);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
actual_arg_types[nargs++] = argtype;
|
|
|
|
}
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
if (fargs)
|
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
first_arg = linitial(fargs);
|
2003-07-19 01:20:33 +02:00
|
|
|
Assert(first_arg != NULL);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-04-02 21:07:02 +02:00
|
|
|
* Check for column projection: if function has one argument, and that
|
2005-10-15 04:49:52 +02:00
|
|
|
* argument is of complex type, and function name is not qualified, then
|
|
|
|
* the "function call" could be a projection. We also check that there
|
|
|
|
* wasn't any aggregate decoration.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2004-05-31 01:40:41 +02:00
|
|
|
if (nargs == 1 && !agg_star && !agg_distinct && list_length(funcname) == 1)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2005-06-22 17:19:43 +02:00
|
|
|
Oid argtype = actual_arg_types[0];
|
2002-04-09 22:35:55 +02:00
|
|
|
|
2004-04-02 21:07:02 +02:00
|
|
|
if (argtype == RECORDOID || ISCOMPLEX(argtype))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2004-04-02 21:07:02 +02:00
|
|
|
retval = ParseComplexProjection(pstate,
|
2004-05-26 06:41:50 +02:00
|
|
|
strVal(linitial(funcname)),
|
2006-03-14 23:48:25 +01:00
|
|
|
first_arg,
|
|
|
|
location);
|
2000-09-12 23:07:18 +02:00
|
|
|
if (retval)
|
|
|
|
return retval;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If ParseComplexProjection doesn't recognize it as a projection,
|
|
|
|
* just press on.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
|
|
|
}
|
1999-12-10 08:37:35 +01:00
|
|
|
}
|
1999-05-25 18:15:34 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
2005-06-22 17:19:43 +02:00
|
|
|
* Okay, it's not a column projection, so it must really be a function.
|
2002-03-21 17:02:16 +01:00
|
|
|
* func_get_detail looks up the function in the catalogs, does
|
2002-09-04 22:31:48 +02:00
|
|
|
* disambiguation for polymorphic functions, handles inheritance, and
|
|
|
|
* returns the funcid and type and set or singleton status of the
|
2005-10-15 04:49:52 +02:00
|
|
|
* function's return value. it also returns the true argument types to
|
|
|
|
* the function.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2003-04-09 01:20:04 +02:00
|
|
|
fdresult = func_get_detail(funcname, fargs, nargs, actual_arg_types,
|
2002-03-21 17:02:16 +01:00
|
|
|
&funcid, &rettype, &retset,
|
2003-04-09 01:20:04 +02:00
|
|
|
&declared_arg_types);
|
2002-03-21 17:02:16 +01:00
|
|
|
if (fdresult == FUNCDETAIL_COERCION)
|
|
|
|
{
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
2007-06-05 23:31:09 +02:00
|
|
|
* We interpreted it as a type coercion. coerce_type can handle these
|
2005-10-15 04:49:52 +02:00
|
|
|
* cases, so why duplicate code...
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
return coerce_type(pstate, linitial(fargs),
|
2004-06-16 03:27:00 +02:00
|
|
|
actual_arg_types[0], rettype, -1,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
COERCION_EXPLICIT, COERCE_EXPLICIT_CALL);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2002-04-11 22:00:18 +02:00
|
|
|
else if (fdresult == FUNCDETAIL_NORMAL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Normal function found; was there anything indicating it must be an
|
|
|
|
* aggregate?
|
2002-04-11 22:00:18 +02:00
|
|
|
*/
|
|
|
|
if (agg_star)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("%s(*) specified, but %s is not an aggregate function",
|
|
|
|
NameListToString(funcname),
|
2006-03-14 23:48:25 +01:00
|
|
|
NameListToString(funcname)),
|
|
|
|
parser_errposition(pstate, location)));
|
2002-04-11 22:00:18 +02:00
|
|
|
if (agg_distinct)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("DISTINCT specified, but %s is not an aggregate function",
|
2006-03-14 23:48:25 +01:00
|
|
|
NameListToString(funcname)),
|
|
|
|
parser_errposition(pstate, location)));
|
2002-04-11 22:00:18 +02:00
|
|
|
}
|
|
|
|
else if (fdresult != FUNCDETAIL_AGGREGATE)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2001-10-05 00:06:46 +02:00
|
|
|
/*
|
2002-03-21 17:02:16 +01:00
|
|
|
* Oops. Time to die.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we are dealing with the attribute notation rel.function, give an
|
|
|
|
* error message that is appropriate for that case.
|
2001-10-05 00:06:46 +02:00
|
|
|
*/
|
2002-03-21 17:02:16 +01:00
|
|
|
if (is_column)
|
2002-05-18 00:35:13 +02:00
|
|
|
{
|
|
|
|
Assert(nargs == 1);
|
2004-05-31 01:40:41 +02:00
|
|
|
Assert(list_length(funcname) == 1);
|
2006-03-14 23:48:25 +01:00
|
|
|
unknown_attribute(pstate, first_arg, strVal(linitial(funcname)),
|
|
|
|
location);
|
2002-05-18 00:35:13 +02:00
|
|
|
}
|
2002-09-04 22:31:48 +02:00
|
|
|
|
2002-05-18 00:35:13 +02:00
|
|
|
/*
|
|
|
|
* Else generate a detailed complaint for a function
|
|
|
|
*/
|
2003-07-04 04:51:34 +02:00
|
|
|
if (fdresult == FUNCDETAIL_MULTIPLE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_AMBIGUOUS_FUNCTION),
|
|
|
|
errmsg("function %s is not unique",
|
|
|
|
func_signature_string(funcname, nargs,
|
|
|
|
actual_arg_types)),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Could not choose a best candidate function. "
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
"You might need to add explicit type casts."),
|
2006-03-14 23:48:25 +01:00
|
|
|
parser_errposition(pstate, location)));
|
2003-07-04 04:51:34 +02:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
|
|
|
func_signature_string(funcname, nargs,
|
|
|
|
actual_arg_types)),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("No function matches the given name and argument types. "
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
"You might need to add explicit type casts."),
|
2006-03-14 23:48:25 +01:00
|
|
|
parser_errposition(pstate, location)));
|
1998-07-08 16:04:11 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/*
|
2007-06-07 01:00:50 +02:00
|
|
|
* enforce consistency with polymorphic argument and return types,
|
|
|
|
* possibly adjusting return type or declared_arg_types (which will be
|
|
|
|
* used as the cast destination by make_fn_arguments)
|
2003-04-09 01:20:04 +02:00
|
|
|
*/
|
|
|
|
rettype = enforce_generic_type_consistency(actual_arg_types,
|
|
|
|
declared_arg_types,
|
|
|
|
nargs,
|
|
|
|
rettype);
|
|
|
|
|
2000-08-08 17:43:12 +02:00
|
|
|
/* perform the necessary typecasting of arguments */
|
2003-04-30 00:13:11 +02:00
|
|
|
make_fn_arguments(pstate, fargs, actual_arg_types, declared_arg_types);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-04-11 22:00:18 +02:00
|
|
|
/* build the appropriate output structure */
|
|
|
|
if (fdresult == FUNCDETAIL_NORMAL)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-12-12 16:49:42 +01:00
|
|
|
FuncExpr *funcexpr = makeNode(FuncExpr);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
funcexpr->funcid = funcid;
|
|
|
|
funcexpr->funcresulttype = rettype;
|
|
|
|
funcexpr->funcretset = retset;
|
|
|
|
funcexpr->funcformat = COERCE_EXPLICIT_CALL;
|
|
|
|
funcexpr->args = fargs;
|
2001-05-19 02:37:45 +02:00
|
|
|
|
2002-12-12 16:49:42 +01:00
|
|
|
retval = (Node *) funcexpr;
|
2001-05-19 02:37:45 +02:00
|
|
|
}
|
2002-04-11 22:00:18 +02:00
|
|
|
else
|
2001-05-19 02:37:45 +02:00
|
|
|
{
|
2002-04-11 22:00:18 +02:00
|
|
|
/* aggregate function */
|
|
|
|
Aggref *aggref = makeNode(Aggref);
|
2001-05-19 02:37:45 +02:00
|
|
|
|
2002-04-11 22:00:18 +02:00
|
|
|
aggref->aggfnoid = funcid;
|
|
|
|
aggref->aggtype = rettype;
|
2006-07-27 21:52:07 +02:00
|
|
|
aggref->args = fargs;
|
2002-04-11 22:00:18 +02:00
|
|
|
aggref->aggstar = agg_star;
|
|
|
|
aggref->aggdistinct = agg_distinct;
|
2001-05-19 02:37:45 +02:00
|
|
|
|
2006-07-27 21:52:07 +02:00
|
|
|
/*
|
|
|
|
* Reject attempt to call a parameterless aggregate without (*)
|
2006-10-04 02:30:14 +02:00
|
|
|
* syntax. This is mere pedantry but some folks insisted ...
|
2006-07-27 21:52:07 +02:00
|
|
|
*/
|
|
|
|
if (fargs == NIL && !agg_star)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
2006-10-04 02:30:14 +02:00
|
|
|
errmsg("%s(*) must be used to call a parameterless aggregate function",
|
|
|
|
NameListToString(funcname)),
|
2006-07-27 21:52:07 +02:00
|
|
|
parser_errposition(pstate, location)));
|
|
|
|
|
2003-06-06 17:04:03 +02:00
|
|
|
/* parse_agg.c does additional aggregate-specific processing */
|
|
|
|
transformAggregateCall(pstate, aggref);
|
|
|
|
|
2002-04-11 22:00:18 +02:00
|
|
|
retval = (Node *) aggref;
|
2001-05-19 02:37:45 +02:00
|
|
|
|
2002-04-11 22:00:18 +02:00
|
|
|
if (retset)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
errmsg("aggregates cannot return sets"),
|
2006-03-14 23:48:25 +01:00
|
|
|
parser_errposition(pstate, location)));
|
2002-04-11 22:00:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2001-05-19 02:37:45 +02:00
|
|
|
|
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
/* func_match_argtypes()
|
2002-04-06 08:59:25 +02:00
|
|
|
*
|
2003-05-26 02:11:29 +02:00
|
|
|
* Given a list of candidate functions (having the right name and number
|
|
|
|
* of arguments) and an array of input datatype OIDs, produce a shortlist of
|
|
|
|
* those candidates that actually accept the input datatypes (either exactly
|
|
|
|
* or by coercion), and return the number of such candidates.
|
|
|
|
*
|
|
|
|
* Note that can_coerce_type will assume that UNKNOWN inputs are coercible to
|
|
|
|
* anything, so candidates will not be eliminated on that basis.
|
2002-04-06 08:59:25 +02:00
|
|
|
*
|
|
|
|
* NB: okay to modify input list structure, as long as we find at least
|
2003-05-26 02:11:29 +02:00
|
|
|
* one match. If no match at all, the list must remain unmodified.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2003-05-26 02:11:29 +02:00
|
|
|
int
|
|
|
|
func_match_argtypes(int nargs,
|
|
|
|
Oid *input_typeids,
|
|
|
|
FuncCandidateList raw_candidates,
|
2003-08-04 02:43:34 +02:00
|
|
|
FuncCandidateList *candidates) /* return value */
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
FuncCandidateList current_candidate;
|
|
|
|
FuncCandidateList next_candidate;
|
1997-11-25 23:07:18 +01:00
|
|
|
int ncandidates = 0;
|
|
|
|
|
|
|
|
*candidates = NULL;
|
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
for (current_candidate = raw_candidates;
|
1997-11-25 23:07:18 +01:00
|
|
|
current_candidate != NULL;
|
2002-04-06 08:59:25 +02:00
|
|
|
current_candidate = next_candidate)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
next_candidate = current_candidate->next;
|
2002-04-11 22:00:18 +02:00
|
|
|
if (can_coerce_type(nargs, input_typeids, current_candidate->args,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
COERCION_IMPLICIT))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
current_candidate->next = *candidates;
|
|
|
|
*candidates = current_candidate;
|
1997-11-25 23:07:18 +01:00
|
|
|
ncandidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ncandidates;
|
2003-05-26 02:11:29 +02:00
|
|
|
} /* func_match_argtypes() */
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
/* func_select_candidate()
|
2003-05-26 02:11:29 +02:00
|
|
|
* Given the input argtype array and more than one candidate
|
|
|
|
* for the function, attempt to resolve the conflict.
|
|
|
|
*
|
2002-04-06 08:59:25 +02:00
|
|
|
* Returns the selected candidate if the conflict can be resolved,
|
1998-05-10 01:31:34 +02:00
|
|
|
* otherwise returns NULL.
|
2000-03-19 01:19:39 +01:00
|
|
|
*
|
2003-05-26 02:11:29 +02:00
|
|
|
* Note that the caller has already determined that there is no candidate
|
|
|
|
* exactly matching the input argtypes, and has pruned away any "candidates"
|
|
|
|
* that aren't actually coercion-compatible with the input types.
|
|
|
|
*
|
|
|
|
* This is also used for resolving ambiguous operator references. Formerly
|
|
|
|
* parse_oper.c had its own, essentially duplicate code for the purpose.
|
|
|
|
* The following comments (formerly in parse_oper.c) are kept to record some
|
|
|
|
* of the history of these heuristics.
|
|
|
|
*
|
|
|
|
* OLD COMMENTS:
|
|
|
|
*
|
|
|
|
* This routine is new code, replacing binary_oper_select_candidate()
|
|
|
|
* which dates from v4.2/v1.0.x days. It tries very hard to match up
|
|
|
|
* operators with types, including allowing type coercions if necessary.
|
|
|
|
* The important thing is that the code do as much as possible,
|
|
|
|
* while _never_ doing the wrong thing, where "the wrong thing" would
|
|
|
|
* be returning an operator when other better choices are available,
|
|
|
|
* or returning an operator which is a non-intuitive possibility.
|
|
|
|
* - thomas 1998-05-21
|
|
|
|
*
|
|
|
|
* The comments below came from binary_oper_select_candidate(), and
|
|
|
|
* illustrate the issues and choices which are possible:
|
|
|
|
* - thomas 1998-05-20
|
|
|
|
*
|
|
|
|
* current wisdom holds that the default operator should be one in which
|
|
|
|
* both operands have the same type (there will only be one such
|
|
|
|
* operator)
|
|
|
|
*
|
|
|
|
* 7.27.93 - I have decided not to do this; it's too hard to justify, and
|
|
|
|
* it's easy enough to typecast explicitly - avi
|
|
|
|
* [the rest of this routine was commented out since then - ay]
|
|
|
|
*
|
|
|
|
* 6/23/95 - I don't complete agree with avi. In particular, casting
|
|
|
|
* floats is a pain for users. Whatever the rationale behind not doing
|
|
|
|
* this is, I need the following special case to work.
|
|
|
|
*
|
|
|
|
* In the WHERE clause of a query, if a float is specified without
|
|
|
|
* quotes, we treat it as float8. I added the float48* operators so
|
|
|
|
* that we can operate on float4 and float8. But now we have more than
|
|
|
|
* one matching operator if the right arg is unknown (eg. float
|
|
|
|
* specified with quotes). This break some stuff in the regression
|
|
|
|
* test where there are floats in quotes not properly casted. Below is
|
|
|
|
* the solution. In addition to requiring the operator operates on the
|
|
|
|
* same type for both operands [as in the code Avi originally
|
|
|
|
* commented out], we also require that the operators be equivalent in
|
|
|
|
* some sense. (see equivalentOpersAfterPromotion for details.)
|
|
|
|
* - ay 6/95
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2003-05-26 02:11:29 +02:00
|
|
|
FuncCandidateList
|
1997-11-25 23:07:18 +01:00
|
|
|
func_select_candidate(int nargs,
|
|
|
|
Oid *input_typeids,
|
2002-04-06 08:59:25 +02:00
|
|
|
FuncCandidateList candidates)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
FuncCandidateList current_candidate;
|
|
|
|
FuncCandidateList last_candidate;
|
1998-09-01 06:40:42 +02:00
|
|
|
Oid *current_typeids;
|
2000-12-15 20:22:03 +01:00
|
|
|
Oid current_type;
|
1998-09-01 06:40:42 +02:00
|
|
|
int i;
|
|
|
|
int ncandidates;
|
|
|
|
int nbestMatch,
|
2000-03-19 01:19:39 +01:00
|
|
|
nmatch;
|
2003-05-26 02:11:29 +02:00
|
|
|
Oid input_base_typeids[FUNC_MAX_ARGS];
|
2000-12-15 20:22:03 +01:00
|
|
|
CATEGORY slot_category[FUNC_MAX_ARGS],
|
1998-09-01 06:40:42 +02:00
|
|
|
current_category;
|
2000-12-15 20:22:03 +01:00
|
|
|
bool slot_has_preferred_type[FUNC_MAX_ARGS];
|
|
|
|
bool resolved_unknowns;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2005-03-29 05:01:32 +02:00
|
|
|
/* protect local fixed-size arrays */
|
|
|
|
if (nargs > FUNC_MAX_ARGS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
|
|
|
|
errmsg("cannot pass more than %d arguments to a function",
|
|
|
|
FUNC_MAX_ARGS)));
|
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If any input types are domains, reduce them to their base types. This
|
|
|
|
* ensures that we will consider functions on the base type to be "exact
|
|
|
|
* matches" in the exact-match heuristic; it also makes it possible to do
|
|
|
|
* something useful with the type-category heuristics. Note that this
|
|
|
|
* makes it difficult, but not impossible, to use functions declared to
|
|
|
|
* take a domain as an input datatype. Such a function will be selected
|
|
|
|
* over the base-type function only if it is an exact match at all
|
|
|
|
* argument positions, and so was already chosen by our caller.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
2003-05-26 02:11:29 +02:00
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
input_base_typeids[i] = getBaseType(input_typeids[i]);
|
2000-03-19 01:19:39 +01:00
|
|
|
|
|
|
|
/*
|
2003-05-26 02:11:29 +02:00
|
|
|
* Run through all candidates and keep those with the most matches on
|
|
|
|
* exact types. Keep all candidates if none match.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
1998-05-10 01:31:34 +02:00
|
|
|
ncandidates = 0;
|
|
|
|
nbestMatch = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
|
|
|
{
|
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
if (input_base_typeids[i] != UNKNOWNOID &&
|
|
|
|
current_typeids[i] == input_base_typeids[i])
|
|
|
|
nmatch++;
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
|
|
|
|
2000-02-20 07:35:08 +01:00
|
|
|
/* take this one as the best choice so far? */
|
1998-05-10 01:31:34 +02:00
|
|
|
if ((nmatch > nbestMatch) || (last_candidate == NULL))
|
|
|
|
{
|
|
|
|
nbestMatch = nmatch;
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
2000-02-20 07:35:08 +01:00
|
|
|
/* no worse than the last choice, so keep this one too? */
|
1998-05-10 01:31:34 +02:00
|
|
|
else if (nmatch == nbestMatch)
|
|
|
|
{
|
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
2000-02-20 07:35:08 +01:00
|
|
|
/* otherwise, don't bother keeping this one... */
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
|
|
|
|
2000-02-20 07:35:08 +01:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
1999-02-23 08:51:53 +01:00
|
|
|
if (ncandidates == 1)
|
2002-04-06 08:59:25 +02:00
|
|
|
return candidates;
|
1999-02-23 08:51:53 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Still too many candidates? Now look for candidates which have either
|
|
|
|
* exact matches or preferred types at the args that will require
|
|
|
|
* coercion. (Restriction added in 7.4: preferred type must be of same
|
|
|
|
* category as input type; give no preference to cross-category
|
|
|
|
* conversions to preferred types.) Keep all candidates if none match.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
2003-08-04 02:43:34 +02:00
|
|
|
for (i = 0; i < nargs; i++) /* avoid multiple lookups */
|
2003-05-26 02:11:29 +02:00
|
|
|
slot_category[i] = TypeCategory(input_base_typeids[i]);
|
2000-03-19 01:19:39 +01:00
|
|
|
ncandidates = 0;
|
|
|
|
nbestMatch = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
|
|
|
{
|
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
if (input_base_typeids[i] != UNKNOWNOID)
|
2000-03-19 01:19:39 +01:00
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
if (current_typeids[i] == input_base_typeids[i] ||
|
|
|
|
IsPreferredType(slot_category[i], current_typeids[i]))
|
2000-03-19 01:19:39 +01:00
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nmatch > nbestMatch) || (last_candidate == NULL))
|
|
|
|
{
|
|
|
|
nbestMatch = nmatch;
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
|
|
|
else if (nmatch == nbestMatch)
|
|
|
|
{
|
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
|
|
|
if (ncandidates == 1)
|
2002-04-06 08:59:25 +02:00
|
|
|
return candidates;
|
2000-03-19 01:19:39 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Still too many candidates? Try assigning types for the unknown columns.
|
2000-03-19 01:19:39 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* NOTE: for a binary operator with one unknown and one non-unknown input,
|
|
|
|
* we already tried the heuristic of looking for a candidate with the
|
|
|
|
* known input type on both sides (see binary_oper_exact()). That's
|
|
|
|
* essentially a special case of the general algorithm we try next.
|
2003-05-26 02:11:29 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We do this by examining each unknown argument position to see if we can
|
|
|
|
* determine a "type category" for it. If any candidate has an input
|
|
|
|
* datatype of STRING category, use STRING category (this bias towards
|
|
|
|
* STRING is appropriate since unknown-type literals look like strings).
|
|
|
|
* Otherwise, if all the candidates agree on the type category of this
|
|
|
|
* argument position, use that category. Otherwise, fail because we
|
|
|
|
* cannot determine a category.
|
2000-03-19 01:19:39 +01:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we are able to determine a type category, also notice whether any of
|
|
|
|
* the candidates takes a preferred datatype within the category.
|
2000-12-15 20:22:03 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Having completed this examination, remove candidates that accept the
|
|
|
|
* wrong category at any unknown position. Also, if at least one
|
|
|
|
* candidate accepted a preferred type at a position, remove candidates
|
|
|
|
* that accept non-preferred types.
|
2000-12-15 20:22:03 +01:00
|
|
|
*
|
|
|
|
* If we are down to one candidate at the end, we win.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
2000-12-15 20:22:03 +01:00
|
|
|
resolved_unknowns = false;
|
1999-02-23 08:51:53 +01:00
|
|
|
for (i = 0; i < nargs; i++)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
bool have_conflict;
|
2000-12-15 20:22:03 +01:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
if (input_base_typeids[i] != UNKNOWNOID)
|
2000-12-15 20:22:03 +01:00
|
|
|
continue;
|
2001-03-22 05:01:46 +01:00
|
|
|
resolved_unknowns = true; /* assume we can do it */
|
2000-12-15 20:22:03 +01:00
|
|
|
slot_category[i] = INVALID_TYPE;
|
|
|
|
slot_has_preferred_type[i] = false;
|
|
|
|
have_conflict = false;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
current_type = current_typeids[i];
|
|
|
|
current_category = TypeCategory(current_type);
|
|
|
|
if (slot_category[i] == INVALID_TYPE)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
/* first candidate */
|
|
|
|
slot_category[i] = current_category;
|
|
|
|
slot_has_preferred_type[i] =
|
|
|
|
IsPreferredType(current_category, current_type);
|
|
|
|
}
|
|
|
|
else if (current_category == slot_category[i])
|
|
|
|
{
|
|
|
|
/* more candidates in same category */
|
|
|
|
slot_has_preferred_type[i] |=
|
|
|
|
IsPreferredType(current_category, current_type);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* category conflict! */
|
|
|
|
if (current_category == STRING_TYPE)
|
1999-02-23 08:51:53 +01:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
/* STRING always wins if available */
|
|
|
|
slot_category[i] = current_category;
|
|
|
|
slot_has_preferred_type[i] =
|
|
|
|
IsPreferredType(current_category, current_type);
|
1999-02-23 08:51:53 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
else
|
2000-02-20 07:35:08 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Remember conflict, but keep going (might find STRING)
|
2001-03-22 05:01:46 +01:00
|
|
|
*/
|
2000-12-15 20:22:03 +01:00
|
|
|
have_conflict = true;
|
2000-02-20 07:35:08 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (have_conflict && slot_category[i] != STRING_TYPE)
|
|
|
|
{
|
|
|
|
/* Failed to resolve category conflict at this position */
|
|
|
|
resolved_unknowns = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolved_unknowns)
|
|
|
|
{
|
|
|
|
/* Strip non-matching candidates */
|
|
|
|
ncandidates = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
bool keepit = true;
|
2000-12-15 20:22:03 +01:00
|
|
|
|
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
if (input_base_typeids[i] != UNKNOWNOID)
|
2000-12-15 20:22:03 +01:00
|
|
|
continue;
|
|
|
|
current_type = current_typeids[i];
|
|
|
|
current_category = TypeCategory(current_type);
|
|
|
|
if (current_category != slot_category[i])
|
1999-02-23 08:51:53 +01:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
keepit = false;
|
|
|
|
break;
|
2000-02-20 07:35:08 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
if (slot_has_preferred_type[i] &&
|
|
|
|
!IsPreferredType(current_category, current_type))
|
2000-02-20 07:35:08 +01:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
keepit = false;
|
|
|
|
break;
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
if (keepit)
|
|
|
|
{
|
|
|
|
/* keep this candidate */
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* forget this candidate */
|
|
|
|
if (last_candidate)
|
|
|
|
last_candidate->next = current_candidate->next;
|
|
|
|
else
|
|
|
|
candidates = current_candidate->next;
|
|
|
|
}
|
1999-02-23 08:51:53 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
|
|
|
|
2000-12-15 20:22:03 +01:00
|
|
|
if (ncandidates == 1)
|
2002-04-06 08:59:25 +02:00
|
|
|
return candidates;
|
2000-11-06 16:42:30 +01:00
|
|
|
|
2003-05-26 02:11:29 +02:00
|
|
|
return NULL; /* failed to select a best candidate */
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* func_select_candidate() */
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* func_get_detail()
|
2001-10-05 00:06:46 +02:00
|
|
|
*
|
1998-05-10 01:31:34 +02:00
|
|
|
* Find the named function in the system catalogs.
|
|
|
|
*
|
|
|
|
* Attempt to find the named function in the system catalogs with
|
1998-09-01 06:40:42 +02:00
|
|
|
* arguments exactly as specified, so that the normal case
|
|
|
|
* (exact match) is as quick as possible.
|
1998-05-10 01:31:34 +02:00
|
|
|
*
|
|
|
|
* If an exact match isn't found:
|
2007-06-05 23:31:09 +02:00
|
|
|
* 1) check for possible interpretation as a type coercion request
|
2001-10-05 00:06:46 +02:00
|
|
|
* 2) get a vector of all possible input arg type arrays constructed
|
1998-09-01 06:40:42 +02:00
|
|
|
* from the superclasses of the original input arg types
|
2001-10-05 00:06:46 +02:00
|
|
|
* 3) get a list of all possible argument type arrays to the function
|
1998-09-01 06:40:42 +02:00
|
|
|
* with given name and number of arguments
|
2001-10-05 00:06:46 +02:00
|
|
|
* 4) for each input arg type array from vector #1:
|
1998-09-01 06:40:42 +02:00
|
|
|
* a) find how many of the function arg type arrays from list #2
|
|
|
|
* it can be coerced to
|
|
|
|
* b) if the answer is one, we have our function
|
|
|
|
* c) if the answer is more than one, attempt to resolve the conflict
|
|
|
|
* d) if the answer is zero, try the next array from vector #1
|
2002-05-03 22:15:02 +02:00
|
|
|
*
|
|
|
|
* Note: we rely primarily on nargs/argtypes as the argument description.
|
|
|
|
* The actual expression node list is passed in fargs so that we can check
|
|
|
|
* for type coercion of a constant. Some callers pass fargs == NIL
|
|
|
|
* indicating they don't want that check made.
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
2001-10-05 00:06:46 +02:00
|
|
|
FuncDetailCode
|
2002-04-09 22:35:55 +02:00
|
|
|
func_get_detail(List *funcname,
|
2001-10-05 00:06:46 +02:00
|
|
|
List *fargs,
|
1997-11-25 23:07:18 +01:00
|
|
|
int nargs,
|
2000-08-20 02:44:19 +02:00
|
|
|
Oid *argtypes,
|
1998-09-01 06:40:42 +02:00
|
|
|
Oid *funcid, /* return value */
|
|
|
|
Oid *rettype, /* return value */
|
|
|
|
bool *retset, /* return value */
|
|
|
|
Oid **true_typeids) /* return value */
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2003-05-26 02:11:29 +02:00
|
|
|
FuncCandidateList raw_candidates;
|
2002-04-06 08:59:25 +02:00
|
|
|
FuncCandidateList best_candidate;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-04-06 08:59:25 +02:00
|
|
|
/* Get list of possible candidates from namespace search */
|
2003-05-26 02:11:29 +02:00
|
|
|
raw_candidates = FuncnameGetCandidates(funcname, nargs);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-04-06 08:59:25 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Quickly check if there is an exact match to the input datatypes (there
|
|
|
|
* can be only one)
|
2002-04-06 08:59:25 +02:00
|
|
|
*/
|
2003-05-26 02:11:29 +02:00
|
|
|
for (best_candidate = raw_candidates;
|
2002-04-06 08:59:25 +02:00
|
|
|
best_candidate != NULL;
|
|
|
|
best_candidate = best_candidate->next)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
if (memcmp(argtypes, best_candidate->args, nargs * sizeof(Oid)) == 0)
|
|
|
|
break;
|
2000-03-19 01:19:39 +01:00
|
|
|
}
|
2002-04-06 08:59:25 +02:00
|
|
|
|
|
|
|
if (best_candidate == NULL)
|
2000-03-19 01:19:39 +01:00
|
|
|
{
|
2001-10-05 00:06:46 +02:00
|
|
|
/*
|
|
|
|
* If we didn't find an exact match, next consider the possibility
|
|
|
|
* that this is really a type-coercion request: a single-argument
|
2005-10-15 04:49:52 +02:00
|
|
|
* function call where the function name is a type name. If so, and
|
2007-06-05 23:31:09 +02:00
|
|
|
* if the coercion path is RELABELTYPE or COERCEVIAIO, then go ahead
|
|
|
|
* and treat the "function call" as a coercion.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This interpretation needs to be given higher priority than
|
|
|
|
* interpretations involving a type coercion followed by a function
|
|
|
|
* call, otherwise we can produce surprising results. For example, we
|
2007-11-15 22:14:46 +01:00
|
|
|
* want "text(varchar)" to be interpreted as a simple coercion, not as
|
|
|
|
* "text(name(varchar))" which the code below this point is entirely
|
|
|
|
* capable of selecting.
|
2001-10-05 00:06:46 +02:00
|
|
|
*
|
2007-06-05 23:31:09 +02:00
|
|
|
* We also treat a coercion of a previously-unknown-type literal
|
|
|
|
* constant to a specific type this way.
|
|
|
|
*
|
|
|
|
* The reason we reject COERCION_PATH_FUNC here is that we expect the
|
|
|
|
* cast implementation function to be named after the target type.
|
|
|
|
* Thus the function will be found by normal lookup if appropriate.
|
2001-10-05 00:06:46 +02:00
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* The reason we reject COERCION_PATH_ARRAYCOERCE is mainly that you
|
|
|
|
* can't write "foo[] (something)" as a function call. In theory
|
2007-06-05 23:31:09 +02:00
|
|
|
* someone might want to invoke it as "_foo (something)" but we have
|
|
|
|
* never supported that historically, so we can insist that people
|
|
|
|
* write it as a normal cast instead. Lack of historical support is
|
|
|
|
* also the reason for not considering composite-type casts here.
|
2002-10-25 00:09:00 +02:00
|
|
|
*
|
2007-06-05 23:31:09 +02:00
|
|
|
* NB: it's important that this code does not exceed what coerce_type
|
|
|
|
* can do, because the caller will try to apply coerce_type if we
|
2007-11-15 22:14:46 +01:00
|
|
|
* return FUNCDETAIL_COERCION. If we return that result for something
|
2007-06-05 23:31:09 +02:00
|
|
|
* coerce_type can't handle, we'll cause infinite recursion between
|
|
|
|
* this module and coerce_type!
|
2001-10-05 00:06:46 +02:00
|
|
|
*/
|
2002-05-03 22:15:02 +02:00
|
|
|
if (nargs == 1 && fargs != NIL)
|
2001-10-05 00:06:46 +02:00
|
|
|
{
|
2007-11-11 20:22:49 +01:00
|
|
|
Oid targetType = FuncNameAsType(funcname);
|
2001-10-05 00:06:46 +02:00
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
if (OidIsValid(targetType))
|
2001-10-05 00:06:46 +02:00
|
|
|
{
|
|
|
|
Oid sourceType = argtypes[0];
|
2004-05-26 06:41:50 +02:00
|
|
|
Node *arg1 = linitial(fargs);
|
2007-06-05 23:31:09 +02:00
|
|
|
bool iscoercion;
|
|
|
|
|
|
|
|
if (sourceType == UNKNOWNOID && IsA(arg1, Const))
|
|
|
|
{
|
|
|
|
/* always treat typename('literal') as coercion */
|
|
|
|
iscoercion = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CoercionPathType cpathtype;
|
|
|
|
Oid cfuncid;
|
|
|
|
|
|
|
|
cpathtype = find_coercion_pathway(targetType, sourceType,
|
|
|
|
COERCION_EXPLICIT,
|
|
|
|
&cfuncid);
|
|
|
|
iscoercion = (cpathtype == COERCION_PATH_RELABELTYPE ||
|
|
|
|
cpathtype == COERCION_PATH_COERCEVIAIO);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iscoercion)
|
2001-10-05 00:06:46 +02:00
|
|
|
{
|
2007-06-05 23:31:09 +02:00
|
|
|
/* Treat it as a type coercion */
|
2001-10-05 00:06:46 +02:00
|
|
|
*funcid = InvalidOid;
|
|
|
|
*rettype = targetType;
|
|
|
|
*retset = false;
|
|
|
|
*true_typeids = argtypes;
|
|
|
|
return FUNCDETAIL_COERCION;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-12 19:17:23 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* didn't find an exact match, so now try to match up candidates...
|
2000-04-12 19:17:23 +02:00
|
|
|
*/
|
2003-05-26 02:11:29 +02:00
|
|
|
if (raw_candidates != NULL)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2005-04-24 00:09:58 +02:00
|
|
|
FuncCandidateList current_candidates;
|
|
|
|
int ncandidates;
|
|
|
|
|
|
|
|
ncandidates = func_match_argtypes(nargs,
|
|
|
|
argtypes,
|
|
|
|
raw_candidates,
|
|
|
|
¤t_candidates);
|
|
|
|
|
|
|
|
/* one match only? then run with it... */
|
|
|
|
if (ncandidates == 1)
|
|
|
|
best_candidate = current_candidates;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
2005-04-24 00:09:58 +02:00
|
|
|
* multiple candidates? then better decide or throw an error...
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
2005-04-24 00:09:58 +02:00
|
|
|
else if (ncandidates > 1)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2005-04-24 00:09:58 +02:00
|
|
|
best_candidate = func_select_candidate(nargs,
|
|
|
|
argtypes,
|
|
|
|
current_candidates);
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1998-09-01 06:40:42 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we were able to choose a best candidate, we're done.
|
|
|
|
* Otherwise, ambiguous function call.
|
1998-09-01 06:40:42 +02:00
|
|
|
*/
|
2005-04-24 00:09:58 +02:00
|
|
|
if (!best_candidate)
|
2003-07-04 04:51:34 +02:00
|
|
|
return FUNCDETAIL_MULTIPLE;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-06 08:59:25 +02:00
|
|
|
if (best_candidate)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-04-06 08:59:25 +02:00
|
|
|
HeapTuple ftup;
|
|
|
|
Form_pg_proc pform;
|
2002-04-11 22:00:18 +02:00
|
|
|
FuncDetailCode result;
|
2002-04-06 08:59:25 +02:00
|
|
|
|
|
|
|
*funcid = best_candidate->oid;
|
|
|
|
*true_typeids = best_candidate->args;
|
|
|
|
|
|
|
|
ftup = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(best_candidate->oid),
|
|
|
|
0, 0, 0);
|
2002-09-04 22:31:48 +02:00
|
|
|
if (!HeapTupleIsValid(ftup)) /* should not happen */
|
2003-07-19 01:20:33 +02:00
|
|
|
elog(ERROR, "cache lookup failed for function %u",
|
2003-07-04 04:51:34 +02:00
|
|
|
best_candidate->oid);
|
2002-04-06 08:59:25 +02:00
|
|
|
pform = (Form_pg_proc) GETSTRUCT(ftup);
|
1997-11-25 23:07:18 +01:00
|
|
|
*rettype = pform->prorettype;
|
|
|
|
*retset = pform->proretset;
|
2002-04-11 22:00:18 +02:00
|
|
|
result = pform->proisagg ? FUNCDETAIL_AGGREGATE : FUNCDETAIL_NORMAL;
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(ftup);
|
2002-04-11 22:00:18 +02:00
|
|
|
return result;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2001-10-05 00:06:46 +02:00
|
|
|
|
|
|
|
return FUNCDETAIL_NOTFOUND;
|
2003-07-04 04:51:34 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
|
2003-06-25 22:07:39 +02:00
|
|
|
/*
|
2005-04-24 00:09:58 +02:00
|
|
|
* Given two type OIDs, determine whether the first is a complex type
|
|
|
|
* (class type) that inherits from the second.
|
2003-06-25 22:07:39 +02:00
|
|
|
*/
|
2005-04-24 00:09:58 +02:00
|
|
|
bool
|
|
|
|
typeInheritsFrom(Oid subclassTypeId, Oid superclassTypeId)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2005-04-24 00:09:58 +02:00
|
|
|
bool result = false;
|
|
|
|
Oid relid;
|
1997-11-25 23:07:18 +01:00
|
|
|
Relation inhrel;
|
1999-12-07 05:09:39 +01:00
|
|
|
List *visited,
|
1997-11-25 23:07:18 +01:00
|
|
|
*queue;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *queue_item;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2005-04-24 00:09:58 +02:00
|
|
|
if (!ISCOMPLEX(subclassTypeId) || !ISCOMPLEX(superclassTypeId))
|
|
|
|
return false;
|
|
|
|
relid = typeidTypeRelid(subclassTypeId);
|
|
|
|
if (relid == InvalidOid)
|
|
|
|
return false;
|
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Begin the search at the relation itself, so add relid to the queue.
|
2004-05-26 06:41:50 +02:00
|
|
|
*/
|
|
|
|
queue = list_make1_oid(relid);
|
1999-12-07 05:09:39 +01:00
|
|
|
visited = NIL;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2005-04-14 22:03:27 +02:00
|
|
|
inhrel = heap_open(InheritsRelationId, AccessShareLock);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Use queue to do a breadth-first traversal of the inheritance graph from
|
|
|
|
* the relid supplied up to the root. Notice that we append to the queue
|
|
|
|
* inside the loop --- this is okay because the foreach() macro doesn't
|
|
|
|
* advance queue_item until the next loop iteration begins.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2004-05-26 06:41:50 +02:00
|
|
|
foreach(queue_item, queue)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
Oid this_relid = lfirst_oid(queue_item);
|
|
|
|
ScanKeyData skey;
|
|
|
|
HeapScanDesc inhscan;
|
|
|
|
HeapTuple inhtup;
|
2004-05-26 06:41:50 +02:00
|
|
|
|
|
|
|
/* If we've seen this relid already, skip it */
|
|
|
|
if (list_member_oid(visited, this_relid))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay, this is a not-yet-seen relid. Add it to the list of
|
2005-10-15 04:49:52 +02:00
|
|
|
* already-visited OIDs, then find all the types this relid inherits
|
|
|
|
* from and add them to the queue. The one exception is we don't add
|
|
|
|
* the original relation to 'visited'.
|
2004-05-26 06:41:50 +02:00
|
|
|
*/
|
|
|
|
if (queue_item != list_head(queue))
|
|
|
|
visited = lappend_oid(visited, this_relid);
|
1999-12-07 05:09:39 +01:00
|
|
|
|
2003-11-12 22:15:59 +01:00
|
|
|
ScanKeyInit(&skey,
|
|
|
|
Anum_pg_inherits_inhrelid,
|
|
|
|
BTEqualStrategyNumber, F_OIDEQ,
|
2004-05-26 06:41:50 +02:00
|
|
|
ObjectIdGetDatum(this_relid));
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
inhscan = heap_beginscan(inhrel, SnapshotNow, 1, &skey);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2002-05-21 01:51:44 +02:00
|
|
|
while ((inhtup = heap_getnext(inhscan, ForwardScanDirection)) != NULL)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1999-12-07 05:09:39 +01:00
|
|
|
Form_pg_inherits inh = (Form_pg_inherits) GETSTRUCT(inhtup);
|
2005-10-15 04:49:52 +02:00
|
|
|
Oid inhparent = inh->inhparent;
|
2005-04-24 00:09:58 +02:00
|
|
|
|
|
|
|
/* If this is the target superclass, we're done */
|
|
|
|
if (get_rel_type_id(inhparent) == superclassTypeId)
|
|
|
|
{
|
|
|
|
result = true;
|
|
|
|
break;
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2005-04-24 00:09:58 +02:00
|
|
|
/* Else add to queue */
|
|
|
|
queue = lappend_oid(queue, inhparent);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
heap_endscan(inhscan);
|
2005-04-24 00:09:58 +02:00
|
|
|
|
|
|
|
if (result)
|
|
|
|
break;
|
2004-05-26 06:41:50 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(inhrel, AccessShareLock);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
list_free(visited);
|
|
|
|
list_free(queue);
|
1999-12-07 05:09:39 +01:00
|
|
|
|
2000-03-16 07:35:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
/*
|
|
|
|
* make_fn_arguments()
|
|
|
|
*
|
|
|
|
* Given the actual argument expressions for a function, and the desired
|
|
|
|
* input types for the function, add any necessary typecasting to the
|
|
|
|
* expression tree. Caller should already have verified that casting is
|
|
|
|
* allowed.
|
|
|
|
*
|
|
|
|
* Caution: given argument list is modified in-place.
|
2003-04-30 00:13:11 +02:00
|
|
|
*
|
|
|
|
* As with coerce_type, pstate may be NULL if no special unknown-Param
|
|
|
|
* processing is wanted.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2003-04-09 01:20:04 +02:00
|
|
|
void
|
2003-04-30 00:13:11 +02:00
|
|
|
make_fn_arguments(ParseState *pstate,
|
|
|
|
List *fargs,
|
2003-04-09 01:20:04 +02:00
|
|
|
Oid *actual_arg_types,
|
|
|
|
Oid *declared_arg_types)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *current_fargs;
|
2003-04-09 01:20:04 +02:00
|
|
|
int i = 0;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2003-04-09 01:20:04 +02:00
|
|
|
foreach(current_fargs, fargs)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-02-20 22:32:16 +01:00
|
|
|
/* types don't match? then force coercion using a function call... */
|
2003-04-09 01:20:04 +02:00
|
|
|
if (actual_arg_types[i] != declared_arg_types[i])
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2003-04-30 00:13:11 +02:00
|
|
|
lfirst(current_fargs) = coerce_type(pstate,
|
|
|
|
lfirst(current_fargs),
|
2003-04-09 01:20:04 +02:00
|
|
|
actual_arg_types[i],
|
2004-06-16 03:27:00 +02:00
|
|
|
declared_arg_types[i], -1,
|
Extend pg_cast castimplicit column to a three-way value; this allows us
to be flexible about assignment casts without introducing ambiguity in
operator/function resolution. Introduce a well-defined promotion hierarchy
for numeric datatypes (int2->int4->int8->numeric->float4->float8).
Change make_const to initially label numeric literals as int4, int8, or
numeric (never float8 anymore).
Explicitly mark Func and RelabelType nodes to indicate whether they came
from a function call, explicit cast, or implicit cast; use this to do
reverse-listing more accurately and without so many heuristics.
Explicit casts to char, varchar, bit, varbit will truncate or pad without
raising an error (the pre-7.2 behavior), while assigning to a column without
any explicit cast will still raise an error for wrong-length data like 7.3.
This more nearly follows the SQL spec than 7.2 behavior (we should be
reporting a 'completion condition' in the explicit-cast cases, but we have
no mechanism for that, so just do silent truncation).
Fix some problems with enforcement of typmod for array elements;
it didn't work at all in 'UPDATE ... SET array[n] = foo', for example.
Provide a generalized array_length_coerce() function to replace the
specialized per-array-type functions that used to be needed (and were
missing for NUMERIC as well as all the datetime types).
Add missing conversions int8<->float4, text<->numeric, oid<->int8.
initdb forced.
2002-09-18 23:35:25 +02:00
|
|
|
COERCION_IMPLICIT,
|
|
|
|
COERCE_IMPLICIT_CAST);
|
1998-05-10 01:31:34 +02:00
|
|
|
}
|
2003-04-09 01:20:04 +02:00
|
|
|
i++;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
/*
|
|
|
|
* FuncNameAsType -
|
|
|
|
* convenience routine to see if a function name matches a type name
|
|
|
|
*
|
|
|
|
* Returns the OID of the matching type, or InvalidOid if none. We ignore
|
|
|
|
* shell types and complex types.
|
|
|
|
*/
|
|
|
|
static Oid
|
|
|
|
FuncNameAsType(List *funcname)
|
|
|
|
{
|
|
|
|
Oid result;
|
|
|
|
Type typtup;
|
|
|
|
|
|
|
|
typtup = LookupTypeName(NULL, makeTypeNameFromNameList(funcname), NULL);
|
|
|
|
if (typtup == NULL)
|
|
|
|
return InvalidOid;
|
|
|
|
|
|
|
|
if (((Form_pg_type) GETSTRUCT(typtup))->typisdefined &&
|
|
|
|
!OidIsValid(typeTypeRelid(typtup)))
|
|
|
|
result = typeTypeId(typtup);
|
|
|
|
else
|
|
|
|
result = InvalidOid;
|
|
|
|
|
|
|
|
ReleaseSysCache(typtup);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
|
|
|
* ParseComplexProjection -
|
|
|
|
* handles function calls with a single argument that is of complex type.
|
2002-03-21 17:02:16 +01:00
|
|
|
* If the function call is actually a column projection, return a suitably
|
2002-09-04 22:31:48 +02:00
|
|
|
* transformed expression tree. If not, return NULL.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1997-11-26 04:43:18 +01:00
|
|
|
static Node *
|
2006-03-14 23:48:25 +01:00
|
|
|
ParseComplexProjection(ParseState *pstate, char *funcname, Node *first_arg,
|
|
|
|
int location)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2005-04-01 00:46:33 +02:00
|
|
|
TupleDesc tupdesc;
|
|
|
|
int i;
|
2002-03-21 17:02:16 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Special case for whole-row Vars so that we can resolve (foo.*).bar even
|
|
|
|
* when foo is a reference to a subselect, join, or RECORD function. A
|
|
|
|
* bonus is that we avoid generating an unnecessary FieldSelect; our
|
|
|
|
* result can omit the whole-row Var and just be a Var for the selected
|
|
|
|
* field.
|
2005-05-31 03:03:23 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* This case could be handled by expandRecordVariable, but it's more
|
|
|
|
* efficient to do it this way when possible.
|
2002-03-21 17:02:16 +01:00
|
|
|
*/
|
2004-04-02 21:07:02 +02:00
|
|
|
if (IsA(first_arg, Var) &&
|
|
|
|
((Var *) first_arg)->varattno == InvalidAttrNumber)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2004-04-02 21:07:02 +02:00
|
|
|
RangeTblEntry *rte;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2004-04-02 21:07:02 +02:00
|
|
|
rte = GetRTEByRangeTablePosn(pstate,
|
|
|
|
((Var *) first_arg)->varno,
|
|
|
|
((Var *) first_arg)->varlevelsup);
|
|
|
|
/* Return a Var if funcname matches a column, else NULL */
|
2006-03-14 23:48:25 +01:00
|
|
|
return scanRTEForColumn(pstate, rte, funcname, location);
|
2004-04-02 21:07:02 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2004-04-02 21:07:02 +02:00
|
|
|
/*
|
2005-05-31 03:03:23 +02:00
|
|
|
* Else do it the hard way with get_expr_result_type().
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If it's a Var of type RECORD, we have to work even harder: we have to
|
|
|
|
* find what the Var refers to, and pass that to get_expr_result_type.
|
|
|
|
* That task is handled by expandRecordVariable().
|
2004-04-02 21:07:02 +02:00
|
|
|
*/
|
2005-05-31 03:03:23 +02:00
|
|
|
if (IsA(first_arg, Var) &&
|
|
|
|
((Var *) first_arg)->vartype == RECORDOID)
|
|
|
|
tupdesc = expandRecordVariable(pstate, (Var *) first_arg, 0);
|
|
|
|
else if (get_expr_result_type(first_arg, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
2005-04-01 00:46:33 +02:00
|
|
|
return NULL; /* unresolvable RECORD type */
|
2005-05-31 03:03:23 +02:00
|
|
|
Assert(tupdesc);
|
2005-04-01 00:46:33 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tupdesc->natts; i++)
|
|
|
|
{
|
|
|
|
Form_pg_attribute att = tupdesc->attrs[i];
|
|
|
|
|
|
|
|
if (strcmp(funcname, NameStr(att->attname)) == 0 &&
|
|
|
|
!att->attisdropped)
|
|
|
|
{
|
|
|
|
/* Success, so generate a FieldSelect expression */
|
|
|
|
FieldSelect *fselect = makeNode(FieldSelect);
|
|
|
|
|
|
|
|
fselect->arg = (Expr *) first_arg;
|
|
|
|
fselect->fieldnum = i + 1;
|
|
|
|
fselect->resulttype = att->atttypid;
|
|
|
|
fselect->resulttypmod = att->atttypmod;
|
|
|
|
return (Node *) fselect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL; /* funcname does not match any column */
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2002-08-08 03:44:31 +02:00
|
|
|
/*
|
2004-04-02 21:07:02 +02:00
|
|
|
* helper routine for delivering "column does not exist" error message
|
2002-08-08 03:44:31 +02:00
|
|
|
*/
|
|
|
|
static void
|
2006-03-14 23:48:25 +01:00
|
|
|
unknown_attribute(ParseState *pstate, Node *relref, char *attname,
|
|
|
|
int location)
|
2002-08-08 03:44:31 +02:00
|
|
|
{
|
2004-04-02 21:07:02 +02:00
|
|
|
RangeTblEntry *rte;
|
|
|
|
|
2004-04-02 23:30:44 +02:00
|
|
|
if (IsA(relref, Var) &&
|
|
|
|
((Var *) relref)->varattno == InvalidAttrNumber)
|
2004-04-02 21:07:02 +02:00
|
|
|
{
|
|
|
|
/* Reference the RTE by alias not by actual table name */
|
|
|
|
rte = GetRTEByRangeTablePosn(pstate,
|
|
|
|
((Var *) relref)->varno,
|
|
|
|
((Var *) relref)->varlevelsup);
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("column %s.%s does not exist",
|
2006-03-14 23:48:25 +01:00
|
|
|
rte->eref->aliasname, attname),
|
|
|
|
parser_errposition(pstate, location)));
|
2004-04-02 21:07:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Have to do it by reference to the type of the expression */
|
|
|
|
Oid relTypeId = exprType(relref);
|
|
|
|
|
|
|
|
if (ISCOMPLEX(relTypeId))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
|
|
|
errmsg("column \"%s\" not found in data type %s",
|
2006-03-14 23:48:25 +01:00
|
|
|
attname, format_type_be(relTypeId)),
|
|
|
|
parser_errposition(pstate, location)));
|
2004-04-02 21:07:02 +02:00
|
|
|
else if (relTypeId == RECORDOID)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_COLUMN),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not identify column \"%s\" in record data type",
|
2006-03-14 23:48:25 +01:00
|
|
|
attname),
|
|
|
|
parser_errposition(pstate, location)));
|
2004-04-02 21:07:02 +02:00
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("column notation .%s applied to type %s, "
|
|
|
|
"which is not a composite type",
|
2006-03-14 23:48:25 +01:00
|
|
|
attname, format_type_be(relTypeId)),
|
|
|
|
parser_errposition(pstate, location)));
|
2004-04-02 21:07:02 +02:00
|
|
|
}
|
2002-08-08 03:44:31 +02:00
|
|
|
}
|
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
/*
|
2003-07-20 23:56:35 +02:00
|
|
|
* funcname_signature_string
|
2003-07-04 04:51:34 +02:00
|
|
|
* Build a string representing a function name, including arg types.
|
|
|
|
* The result is something like "foo(integer)".
|
|
|
|
*
|
|
|
|
* This is typically used in the construction of function-not-found error
|
|
|
|
* messages.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
2003-07-04 04:51:34 +02:00
|
|
|
const char *
|
2003-07-20 23:56:35 +02:00
|
|
|
funcname_signature_string(const char *funcname,
|
|
|
|
int nargs, const Oid *argtypes)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2002-05-18 00:35:13 +02:00
|
|
|
StringInfoData argbuf;
|
1997-11-25 23:07:18 +01:00
|
|
|
int i;
|
|
|
|
|
2002-05-18 00:35:13 +02:00
|
|
|
initStringInfo(&argbuf);
|
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
appendStringInfo(&argbuf, "%s(", funcname);
|
2003-07-04 04:51:34 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
if (i)
|
2003-04-24 23:16:45 +02:00
|
|
|
appendStringInfoString(&argbuf, ", ");
|
|
|
|
appendStringInfoString(&argbuf, format_type_be(argtypes[i]));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
appendStringInfoChar(&argbuf, ')');
|
|
|
|
|
|
|
|
return argbuf.data; /* return palloc'd string buffer */
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2002-04-09 22:35:55 +02:00
|
|
|
|
2003-07-20 23:56:35 +02:00
|
|
|
/*
|
|
|
|
* func_signature_string
|
|
|
|
* As above, but function name is passed as a qualified name list.
|
|
|
|
*/
|
|
|
|
const char *
|
|
|
|
func_signature_string(List *funcname, int nargs, const Oid *argtypes)
|
|
|
|
{
|
|
|
|
return funcname_signature_string(NameListToString(funcname),
|
|
|
|
nargs, argtypes);
|
|
|
|
}
|
|
|
|
|
2002-04-09 22:35:55 +02:00
|
|
|
/*
|
|
|
|
* LookupFuncName
|
|
|
|
* Given a possibly-qualified function name and a set of argument types,
|
2003-07-04 04:51:34 +02:00
|
|
|
* look up the function.
|
2002-04-09 22:35:55 +02:00
|
|
|
*
|
|
|
|
* If the function name is not schema-qualified, it is sought in the current
|
|
|
|
* namespace search path.
|
2003-07-04 04:51:34 +02:00
|
|
|
*
|
|
|
|
* If the function is not found, we return InvalidOid if noError is true,
|
|
|
|
* else raise an error.
|
2002-04-09 22:35:55 +02:00
|
|
|
*/
|
|
|
|
Oid
|
2003-07-04 04:51:34 +02:00
|
|
|
LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
|
2002-04-09 22:35:55 +02:00
|
|
|
{
|
|
|
|
FuncCandidateList clist;
|
|
|
|
|
|
|
|
clist = FuncnameGetCandidates(funcname, nargs);
|
|
|
|
|
|
|
|
while (clist)
|
|
|
|
{
|
|
|
|
if (memcmp(argtypes, clist->args, nargs * sizeof(Oid)) == 0)
|
|
|
|
return clist->oid;
|
|
|
|
clist = clist->next;
|
|
|
|
}
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
if (!noError)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("function %s does not exist",
|
2005-10-15 04:49:52 +02:00
|
|
|
func_signature_string(funcname, nargs, argtypes))));
|
2003-07-04 04:51:34 +02:00
|
|
|
|
2002-04-09 22:35:55 +02:00
|
|
|
return InvalidOid;
|
|
|
|
}
|
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
/*
|
|
|
|
* LookupTypeNameOid
|
|
|
|
* Convenience routine to look up a type, silently accepting shell types
|
|
|
|
*/
|
|
|
|
static Oid
|
|
|
|
LookupTypeNameOid(const TypeName *typename)
|
|
|
|
{
|
|
|
|
Oid result;
|
|
|
|
Type typtup;
|
|
|
|
|
|
|
|
typtup = LookupTypeName(NULL, typename, NULL);
|
|
|
|
if (typtup == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_OBJECT),
|
|
|
|
errmsg("type \"%s\" does not exist",
|
|
|
|
TypeNameToString(typename))));
|
|
|
|
result = typeTypeId(typtup);
|
|
|
|
ReleaseSysCache(typtup);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-04-09 22:35:55 +02:00
|
|
|
/*
|
|
|
|
* LookupFuncNameTypeNames
|
|
|
|
* Like LookupFuncName, but the argument types are specified by a
|
2003-07-04 04:51:34 +02:00
|
|
|
* list of TypeName nodes.
|
2002-04-09 22:35:55 +02:00
|
|
|
*/
|
|
|
|
Oid
|
2003-07-04 04:51:34 +02:00
|
|
|
LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError)
|
2002-04-09 22:35:55 +02:00
|
|
|
{
|
2002-09-04 22:31:48 +02:00
|
|
|
Oid argoids[FUNC_MAX_ARGS];
|
|
|
|
int argcount;
|
|
|
|
int i;
|
2004-05-26 06:41:50 +02:00
|
|
|
ListCell *args_item;
|
2002-04-09 22:35:55 +02:00
|
|
|
|
2004-05-31 01:40:41 +02:00
|
|
|
argcount = list_length(argtypes);
|
2002-04-09 22:35:55 +02:00
|
|
|
if (argcount > FUNC_MAX_ARGS)
|
2003-07-19 01:20:33 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
|
|
|
|
errmsg("functions cannot have more than %d arguments",
|
|
|
|
FUNC_MAX_ARGS)));
|
2002-04-09 22:35:55 +02:00
|
|
|
|
2004-05-26 06:41:50 +02:00
|
|
|
args_item = list_head(argtypes);
|
2002-04-09 22:35:55 +02:00
|
|
|
for (i = 0; i < argcount; i++)
|
|
|
|
{
|
2004-05-26 06:41:50 +02:00
|
|
|
TypeName *t = (TypeName *) lfirst(args_item);
|
2002-04-09 22:35:55 +02:00
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
argoids[i] = LookupTypeNameOid(t);
|
2004-05-26 06:41:50 +02:00
|
|
|
args_item = lnext(args_item);
|
2002-04-09 22:35:55 +02:00
|
|
|
}
|
|
|
|
|
2003-07-04 04:51:34 +02:00
|
|
|
return LookupFuncName(funcname, argcount, argoids, noError);
|
2002-04-09 22:35:55 +02:00
|
|
|
}
|
2006-04-15 19:45:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* LookupAggNameTypeNames
|
|
|
|
* Find an aggregate function given a name and list of TypeName nodes.
|
|
|
|
*
|
|
|
|
* This is almost like LookupFuncNameTypeNames, but the error messages refer
|
|
|
|
* to aggregates rather than plain functions, and we verify that the found
|
2006-07-27 21:52:07 +02:00
|
|
|
* function really is an aggregate.
|
2006-04-15 19:45:46 +02:00
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
LookupAggNameTypeNames(List *aggname, List *argtypes, bool noError)
|
|
|
|
{
|
|
|
|
Oid argoids[FUNC_MAX_ARGS];
|
|
|
|
int argcount;
|
|
|
|
int i;
|
2006-07-27 21:52:07 +02:00
|
|
|
ListCell *lc;
|
2006-04-15 19:45:46 +02:00
|
|
|
Oid oid;
|
|
|
|
HeapTuple ftup;
|
|
|
|
Form_pg_proc pform;
|
|
|
|
|
|
|
|
argcount = list_length(argtypes);
|
|
|
|
if (argcount > FUNC_MAX_ARGS)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
|
|
|
|
errmsg("functions cannot have more than %d arguments",
|
|
|
|
FUNC_MAX_ARGS)));
|
|
|
|
|
2006-07-27 21:52:07 +02:00
|
|
|
i = 0;
|
|
|
|
foreach(lc, argtypes)
|
2006-04-15 19:45:46 +02:00
|
|
|
{
|
2006-07-27 21:52:07 +02:00
|
|
|
TypeName *t = (TypeName *) lfirst(lc);
|
2006-04-15 19:45:46 +02:00
|
|
|
|
2007-11-11 20:22:49 +01:00
|
|
|
argoids[i] = LookupTypeNameOid(t);
|
2006-07-27 21:52:07 +02:00
|
|
|
i++;
|
2006-04-15 19:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
oid = LookupFuncName(aggname, argcount, argoids, true);
|
|
|
|
|
|
|
|
if (!OidIsValid(oid))
|
|
|
|
{
|
|
|
|
if (noError)
|
|
|
|
return InvalidOid;
|
2006-07-27 21:52:07 +02:00
|
|
|
if (argcount == 0)
|
2006-04-15 19:45:46 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("aggregate %s(*) does not exist",
|
|
|
|
NameListToString(aggname))));
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_UNDEFINED_FUNCTION),
|
|
|
|
errmsg("aggregate %s does not exist",
|
|
|
|
func_signature_string(aggname,
|
|
|
|
argcount, argoids))));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure it's an aggregate */
|
|
|
|
ftup = SearchSysCache(PROCOID,
|
|
|
|
ObjectIdGetDatum(oid),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(ftup)) /* should not happen */
|
|
|
|
elog(ERROR, "cache lookup failed for function %u", oid);
|
|
|
|
pform = (Form_pg_proc) GETSTRUCT(ftup);
|
|
|
|
|
|
|
|
if (!pform->proisagg)
|
|
|
|
{
|
|
|
|
ReleaseSysCache(ftup);
|
|
|
|
if (noError)
|
|
|
|
return InvalidOid;
|
|
|
|
/* we do not use the (*) notation for functions... */
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("function %s is not an aggregate",
|
|
|
|
func_signature_string(aggname,
|
|
|
|
argcount, argoids))));
|
|
|
|
}
|
|
|
|
|
|
|
|
ReleaseSysCache(ftup);
|
|
|
|
|
|
|
|
return oid;
|
|
|
|
}
|