⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse_func.c

📁 PostgreSQL 8.1.4的源码 适用于Linux下的开源数据库系统
💻 C
📖 第 1 页 / 共 3 页
字号:
/*------------------------------------------------------------------------- * * parse_func.c *		handle function calls in parser * * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION *	  $PostgreSQL: pgsql/src/backend/parser/parse_func.c,v 1.182.2.1 2005/11/22 18:23:14 momjian Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/heapam.h"#include "catalog/pg_inherits.h"#include "catalog/pg_proc.h"#include "funcapi.h"#include "lib/stringinfo.h"#include "nodes/makefuncs.h"#include "parser/parse_agg.h"#include "parser/parse_coerce.h"#include "parser/parse_expr.h"#include "parser/parse_func.h"#include "parser/parse_relation.h"#include "parser/parse_target.h"#include "parser/parse_type.h"#include "utils/builtins.h"#include "utils/fmgroids.h"#include "utils/lsyscache.h"#include "utils/syscache.h"static Node *ParseComplexProjection(ParseState *pstate, char *funcname,					   Node *first_arg);static void unknown_attribute(ParseState *pstate, Node *relref, char *attname);/* *	Parse a function call * *	For historical reasons, Postgres tries to treat the notations tab.col *	and col(tab) as equivalent: if a single-argument function call has an *	argument of complex type and the (unqualified) function name matches *	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. * *	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 *	a single argument (the putative table), unqualified function name *	equal to the column name, and no aggregate decoration. * *	The argument expressions (in fargs) must have been transformed already. */Node *ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,				  bool agg_star, bool agg_distinct, bool is_column){	Oid			rettype;	Oid			funcid;	ListCell   *l;	ListCell   *nextl;	Node	   *first_arg = NULL;	int			nargs;	Oid			actual_arg_types[FUNC_MAX_ARGS];	Oid		   *declared_arg_types;	Node	   *retval;	bool		retset;	FuncDetailCode fdresult;	/*	 * 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.	 */	if (list_length(fargs) > FUNC_MAX_ARGS)		ereport(ERROR,				(errcode(ERRCODE_TOO_MANY_ARGUMENTS),				 errmsg("cannot pass more than %d arguments to a function",						FUNC_MAX_ARGS)));	/*	 * Extract arg type info in preparation for function lookup.	 *	 * 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 ...	 */	nargs = 0;	for (l = list_head(fargs); l != NULL; l = nextl)	{		Node	   *arg = lfirst(l);		Oid			argtype = exprType(arg);		nextl = lnext(l);		if (argtype == VOIDOID && IsA(arg, Param) &&!is_column)		{			fargs = list_delete_ptr(fargs, arg);			continue;		}		actual_arg_types[nargs++] = argtype;	}	if (fargs)	{		first_arg = linitial(fargs);		Assert(first_arg != NULL);	}	/*	 * Check for column projection: if function has one argument, and that	 * 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.	 */	if (nargs == 1 && !agg_star && !agg_distinct && list_length(funcname) == 1)	{		Oid			argtype = actual_arg_types[0];		if (argtype == RECORDOID || ISCOMPLEX(argtype))		{			retval = ParseComplexProjection(pstate,											strVal(linitial(funcname)),											first_arg);			if (retval)				return retval;			/*			 * If ParseComplexProjection doesn't recognize it as a projection,			 * just press on.			 */		}	}	/*	 * Okay, it's not a column projection, so it must really be a function.	 * func_get_detail looks up the function in the catalogs, does	 * disambiguation for polymorphic functions, handles inheritance, and	 * returns the funcid and type and set or singleton status of the	 * function's return value.  it also returns the true argument types to	 * the function.	 */	fdresult = func_get_detail(funcname, fargs, nargs, actual_arg_types,							   &funcid, &rettype, &retset,							   &declared_arg_types);	if (fdresult == FUNCDETAIL_COERCION)	{		/*		 * We can do it as a trivial coercion. coerce_type can handle these		 * cases, so why duplicate code...		 */		return coerce_type(pstate, linitial(fargs),						   actual_arg_types[0], rettype, -1,						   COERCION_EXPLICIT, COERCE_EXPLICIT_CALL);	}	else if (fdresult == FUNCDETAIL_NORMAL)	{		/*		 * Normal function found; was there anything indicating it must be an		 * aggregate?		 */		if (agg_star)			ereport(ERROR,					(errcode(ERRCODE_WRONG_OBJECT_TYPE),			   errmsg("%s(*) specified, but %s is not an aggregate function",					  NameListToString(funcname),					  NameListToString(funcname))));		if (agg_distinct)			ereport(ERROR,					(errcode(ERRCODE_WRONG_OBJECT_TYPE),			errmsg("DISTINCT specified, but %s is not an aggregate function",				   NameListToString(funcname))));	}	else if (fdresult != FUNCDETAIL_AGGREGATE)	{		/*		 * Oops.  Time to die.		 *		 * If we are dealing with the attribute notation rel.function, give an		 * error message that is appropriate for that case.		 */		if (is_column)		{			Assert(nargs == 1);			Assert(list_length(funcname) == 1);			unknown_attribute(pstate, first_arg, strVal(linitial(funcname)));		}		/*		 * Else generate a detailed complaint for a function		 */		if (fdresult == FUNCDETAIL_MULTIPLE)			ereport(ERROR,					(errcode(ERRCODE_AMBIGUOUS_FUNCTION),					 errmsg("function %s is not unique",							func_signature_string(funcname, nargs,												  actual_arg_types)),					 errhint("Could not choose a best candidate function. "							 "You may need to add explicit type casts.")));		else			ereport(ERROR,					(errcode(ERRCODE_UNDEFINED_FUNCTION),					 errmsg("function %s does not exist",							func_signature_string(funcname, nargs,												  actual_arg_types)),			errhint("No function matches the given name and argument types. "					"You may need to add explicit type casts.")));	}	/*	 * enforce consistency with ANYARRAY and ANYELEMENT argument and return	 * types, possibly adjusting return type or declared_arg_types (which will	 * be used as the cast destination by make_fn_arguments)	 */	rettype = enforce_generic_type_consistency(actual_arg_types,											   declared_arg_types,											   nargs,											   rettype);	/* perform the necessary typecasting of arguments */	make_fn_arguments(pstate, fargs, actual_arg_types, declared_arg_types);	/* build the appropriate output structure */	if (fdresult == FUNCDETAIL_NORMAL)	{		FuncExpr   *funcexpr = makeNode(FuncExpr);		funcexpr->funcid = funcid;		funcexpr->funcresulttype = rettype;		funcexpr->funcretset = retset;		funcexpr->funcformat = COERCE_EXPLICIT_CALL;		funcexpr->args = fargs;		retval = (Node *) funcexpr;	}	else	{		/* aggregate function */		Aggref	   *aggref = makeNode(Aggref);		aggref->aggfnoid = funcid;		aggref->aggtype = rettype;		aggref->target = linitial(fargs);		aggref->aggstar = agg_star;		aggref->aggdistinct = agg_distinct;		/* parse_agg.c does additional aggregate-specific processing */		transformAggregateCall(pstate, aggref);		retval = (Node *) aggref;		if (retset)			ereport(ERROR,					(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),					 errmsg("aggregates may not return sets")));	}	return retval;}/* func_match_argtypes() * * 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. * * NB: okay to modify input list structure, as long as we find at least * one match.  If no match at all, the list must remain unmodified. */intfunc_match_argtypes(int nargs,					Oid *input_typeids,					FuncCandidateList raw_candidates,					FuncCandidateList *candidates)		/* return value */{	FuncCandidateList current_candidate;	FuncCandidateList next_candidate;	int			ncandidates = 0;	*candidates = NULL;	for (current_candidate = raw_candidates;		 current_candidate != NULL;		 current_candidate = next_candidate)	{		next_candidate = current_candidate->next;		if (can_coerce_type(nargs, input_typeids, current_candidate->args,							COERCION_IMPLICIT))		{			current_candidate->next = *candidates;			*candidates = current_candidate;			ncandidates++;		}	}	return ncandidates;}	/* func_match_argtypes() *//* func_select_candidate() *		Given the input argtype array and more than one candidate *		for the function, attempt to resolve the conflict. * * Returns the selected candidate if the conflict can be resolved, * otherwise returns NULL. * * 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 */FuncCandidateListfunc_select_candidate(int nargs,					  Oid *input_typeids,					  FuncCandidateList candidates){	FuncCandidateList current_candidate;	FuncCandidateList last_candidate;	Oid		   *current_typeids;	Oid			current_type;	int			i;	int			ncandidates;	int			nbestMatch,				nmatch;	Oid			input_base_typeids[FUNC_MAX_ARGS];	CATEGORY	slot_category[FUNC_MAX_ARGS],				current_category;	bool		slot_has_preferred_type[FUNC_MAX_ARGS];	bool		resolved_unknowns;	/* 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)));	/*	 * 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.	 */	for (i = 0; i < nargs; i++)		input_base_typeids[i] = getBaseType(input_typeids[i]);	/*	 * Run through all candidates and keep those with the most matches on	 * exact types. Keep all candidates if none match.	 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -