parse_func.c
来自「PostgreSQL7.4.6 for Linux」· C语言 代码 · 共 1,534 行 · 第 1/3 页
C
1,534 行
/*------------------------------------------------------------------------- * * parse_func.c * handle function calls in parser * * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION * $Header: /cvsroot/pgsql/src/backend/parser/parse_func.c,v 1.161 2003/09/29 00:05:25 petere Exp $ * *------------------------------------------------------------------------- */#include "postgres.h"#include "access/heapam.h"#include "catalog/catname.h"#include "catalog/pg_inherits.h"#include "catalog/pg_proc.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_type.h"#include "utils/builtins.h"#include "utils/fmgroids.h"#include "utils/lsyscache.h"#include "utils/syscache.h"static Node *ParseComplexProjection(char *funcname, Node *first_arg);static Oid **argtype_inherit(int nargs, Oid *argtypes);static int find_inheritors(Oid relid, Oid **supervec);static Oid **gen_cross_product(InhPaths *arginh, int nargs);static FieldSelect *setup_field_select(Node *input, char *attname, Oid relid);static void unknown_attribute(const char *schemaname, const char *relname, const 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. * * 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. * * In the function-call case, the argument expressions have been transformed * already. In the column case, we may get either a transformed expression * or a RangeVar node as argument. */Node *ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, bool agg_star, bool agg_distinct, bool is_column){ Oid rettype; Oid funcid; List *i; Node *first_arg = NULL; int nargs = length(fargs); int argn; 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 (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 (fargs) { first_arg = lfirst(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 && length(funcname) == 1) { char *cname = strVal(lfirst(funcname)); /* Is it a not-yet-transformed RangeVar node? */ if (IsA(first_arg, RangeVar)) { /* First arg is a relation. This could be a projection. */ retval = qualifiedNameToVar(pstate, ((RangeVar *) first_arg)->schemaname, ((RangeVar *) first_arg)->relname, cname, true); if (retval) return retval; } else if (ISCOMPLEX(exprType(first_arg))) { /* * Attempt to handle projection of a complex argument. If * ParseComplexProjection can't handle the projection, we have * to keep going. */ retval = ParseComplexProjection(cname, first_arg); if (retval) return retval; } } /* * Okay, it's not a column projection, so it must really be a * function. Extract arg type info and transform RangeVar arguments * into varnodes of the appropriate form. */ MemSet(actual_arg_types, 0, FUNC_MAX_ARGS * sizeof(Oid)); argn = 0; foreach(i, fargs) { Node *arg = lfirst(i); Oid toid; if (IsA(arg, RangeVar)) { char *schemaname; char *relname; RangeTblEntry *rte; int vnum; int sublevels_up; /* * a relation: look it up in the range table, or add if needed */ schemaname = ((RangeVar *) arg)->schemaname; relname = ((RangeVar *) arg)->relname; rte = refnameRangeTblEntry(pstate, schemaname, relname, &sublevels_up); if (rte == NULL) rte = addImplicitRTE(pstate, (RangeVar *) arg); vnum = RTERangeTablePosn(pstate, rte, &sublevels_up); /* * The parameter to be passed to the function is the whole * tuple from the relation. We build a special VarNode to * reflect this -- it has varno set to the correct range table * entry, but has varattno == 0 to signal that the whole tuple * is the argument. Also, it has typmod set to * sizeof(Pointer) to signal that the runtime representation * will be a pointer not an Oid. */ switch (rte->rtekind) { case RTE_RELATION: toid = get_rel_type_id(rte->relid); if (!OidIsValid(toid)) elog(ERROR, "could not find type OID for relation %u", rte->relid); /* replace RangeVar in the arg list */ lfirst(i) = makeVar(vnum, InvalidAttrNumber, toid, sizeof(Pointer), sublevels_up); break; case RTE_FUNCTION: toid = exprType(rte->funcexpr); if (get_typtype(toid) == 'c') { /* func returns composite; same as relation case */ lfirst(i) = makeVar(vnum, InvalidAttrNumber, toid, sizeof(Pointer), sublevels_up); } else { /* func returns scalar; use attno 1 instead */ lfirst(i) = makeVar(vnum, 1, toid, -1, sublevels_up); } break; default: /* * RTE is a join or subselect; must fail for lack of a * named tuple type */ if (is_column) unknown_attribute(schemaname, relname, strVal(lfirst(funcname))); else ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot pass result of subquery or join \"%s\" to a function", relname))); toid = InvalidOid; /* keep compiler quiet */ break; } } else toid = exprType(arg); actual_arg_types[argn++] = toid; } /* * 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, lfirst(fargs), actual_arg_types[0], rettype, 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) { char *colname = strVal(lfirst(funcname)); Oid relTypeId; Assert(nargs == 1); if (IsA(first_arg, RangeVar)) unknown_attribute(((RangeVar *) first_arg)->schemaname, ((RangeVar *) first_arg)->relname, colname); relTypeId = exprType(first_arg); if (!ISCOMPLEX(relTypeId)) ereport(ERROR, (errcode(ERRCODE_WRONG_OBJECT_TYPE), errmsg("attribute notation .%s applied to type %s, which is not a complex type", colname, format_type_be(relTypeId)))); else ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("attribute \"%s\" not found in data type %s", colname, format_type_be(relTypeId)))); } /* * 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 = lfirst(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; /* * 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 + =
减小字号Ctrl + -
显示快捷键?