00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "postgres.h"
00016
00017 #include "access/htup_details.h"
00018 #include "catalog/pg_proc.h"
00019 #include "catalog/pg_type.h"
00020 #include "funcapi.h"
00021 #include "lib/stringinfo.h"
00022 #include "nodes/makefuncs.h"
00023 #include "nodes/nodeFuncs.h"
00024 #include "parser/parse_agg.h"
00025 #include "parser/parse_coerce.h"
00026 #include "parser/parse_func.h"
00027 #include "parser/parse_relation.h"
00028 #include "parser/parse_target.h"
00029 #include "parser/parse_type.h"
00030 #include "utils/builtins.h"
00031 #include "utils/lsyscache.h"
00032 #include "utils/syscache.h"
00033
00034
00035 static Oid FuncNameAsType(List *funcname);
00036 static Node *ParseComplexProjection(ParseState *pstate, char *funcname,
00037 Node *first_arg, int location);
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062 Node *
00063 ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs,
00064 List *agg_order, bool agg_star, bool agg_distinct,
00065 bool func_variadic,
00066 WindowDef *over, bool is_column, int location)
00067 {
00068 Oid rettype;
00069 Oid funcid;
00070 ListCell *l;
00071 ListCell *nextl;
00072 Node *first_arg = NULL;
00073 int nargs;
00074 int nargsplusdefs;
00075 Oid actual_arg_types[FUNC_MAX_ARGS];
00076 Oid *declared_arg_types;
00077 List *argnames;
00078 List *argdefaults;
00079 Node *retval;
00080 bool retset;
00081 int nvargs;
00082 FuncDetailCode fdresult;
00083
00084
00085
00086
00087
00088
00089
00090 if (list_length(fargs) > FUNC_MAX_ARGS)
00091 ereport(ERROR,
00092 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
00093 errmsg_plural("cannot pass more than %d argument to a function",
00094 "cannot pass more than %d arguments to a function",
00095 FUNC_MAX_ARGS,
00096 FUNC_MAX_ARGS),
00097 parser_errposition(pstate, location)));
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 nargs = 0;
00109 for (l = list_head(fargs); l != NULL; l = nextl)
00110 {
00111 Node *arg = lfirst(l);
00112 Oid argtype = exprType(arg);
00113
00114 nextl = lnext(l);
00115
00116 if (argtype == VOIDOID && IsA(arg, Param) &&!is_column)
00117 {
00118 fargs = list_delete_ptr(fargs, arg);
00119 continue;
00120 }
00121
00122 actual_arg_types[nargs++] = argtype;
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133 argnames = NIL;
00134 foreach(l, fargs)
00135 {
00136 Node *arg = lfirst(l);
00137
00138 if (IsA(arg, NamedArgExpr))
00139 {
00140 NamedArgExpr *na = (NamedArgExpr *) arg;
00141 ListCell *lc;
00142
00143
00144 foreach(lc, argnames)
00145 {
00146 if (strcmp(na->name, (char *) lfirst(lc)) == 0)
00147 ereport(ERROR,
00148 (errcode(ERRCODE_SYNTAX_ERROR),
00149 errmsg("argument name \"%s\" used more than once",
00150 na->name),
00151 parser_errposition(pstate, na->location)));
00152 }
00153 argnames = lappend(argnames, na->name);
00154 }
00155 else
00156 {
00157 if (argnames != NIL)
00158 ereport(ERROR,
00159 (errcode(ERRCODE_SYNTAX_ERROR),
00160 errmsg("positional argument cannot follow named argument"),
00161 parser_errposition(pstate, exprLocation(arg))));
00162 }
00163 }
00164
00165 if (fargs)
00166 {
00167 first_arg = linitial(fargs);
00168 Assert(first_arg != NULL);
00169 }
00170
00171
00172
00173
00174
00175
00176
00177 if (nargs == 1 && agg_order == NIL && !agg_star && !agg_distinct &&
00178 over == NULL && !func_variadic && argnames == NIL &&
00179 list_length(funcname) == 1)
00180 {
00181 Oid argtype = actual_arg_types[0];
00182
00183 if (argtype == RECORDOID || ISCOMPLEX(argtype))
00184 {
00185 retval = ParseComplexProjection(pstate,
00186 strVal(linitial(funcname)),
00187 first_arg,
00188 location);
00189 if (retval)
00190 return retval;
00191
00192
00193
00194
00195
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 fdresult = func_get_detail(funcname, fargs, argnames, nargs,
00215 actual_arg_types,
00216 !func_variadic, true,
00217 &funcid, &rettype, &retset, &nvargs,
00218 &declared_arg_types, &argdefaults);
00219 if (fdresult == FUNCDETAIL_COERCION)
00220 {
00221
00222
00223
00224
00225 return coerce_type(pstate, linitial(fargs),
00226 actual_arg_types[0], rettype, -1,
00227 COERCION_EXPLICIT, COERCE_EXPLICIT_CALL, location);
00228 }
00229 else if (fdresult == FUNCDETAIL_NORMAL)
00230 {
00231
00232
00233
00234
00235 if (agg_star)
00236 ereport(ERROR,
00237 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00238 errmsg("%s(*) specified, but %s is not an aggregate function",
00239 NameListToString(funcname),
00240 NameListToString(funcname)),
00241 parser_errposition(pstate, location)));
00242 if (agg_distinct)
00243 ereport(ERROR,
00244 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00245 errmsg("DISTINCT specified, but %s is not an aggregate function",
00246 NameListToString(funcname)),
00247 parser_errposition(pstate, location)));
00248 if (agg_order != NIL)
00249 ereport(ERROR,
00250 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00251 errmsg("ORDER BY specified, but %s is not an aggregate function",
00252 NameListToString(funcname)),
00253 parser_errposition(pstate, location)));
00254 if (over)
00255 ereport(ERROR,
00256 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00257 errmsg("OVER specified, but %s is not a window function nor an aggregate function",
00258 NameListToString(funcname)),
00259 parser_errposition(pstate, location)));
00260 }
00261 else if (!(fdresult == FUNCDETAIL_AGGREGATE ||
00262 fdresult == FUNCDETAIL_WINDOWFUNC))
00263 {
00264
00265
00266
00267
00268
00269
00270 if (is_column)
00271 return NULL;
00272
00273
00274
00275
00276 if (fdresult == FUNCDETAIL_MULTIPLE)
00277 ereport(ERROR,
00278 (errcode(ERRCODE_AMBIGUOUS_FUNCTION),
00279 errmsg("function %s is not unique",
00280 func_signature_string(funcname, nargs, argnames,
00281 actual_arg_types)),
00282 errhint("Could not choose a best candidate function. "
00283 "You might need to add explicit type casts."),
00284 parser_errposition(pstate, location)));
00285 else if (list_length(agg_order) > 1)
00286 {
00287
00288 ereport(ERROR,
00289 (errcode(ERRCODE_UNDEFINED_FUNCTION),
00290 errmsg("function %s does not exist",
00291 func_signature_string(funcname, nargs, argnames,
00292 actual_arg_types)),
00293 errhint("No aggregate function matches the given name and argument types. "
00294 "Perhaps you misplaced ORDER BY; ORDER BY must appear "
00295 "after all regular arguments of the aggregate."),
00296 parser_errposition(pstate, location)));
00297 }
00298 else
00299 ereport(ERROR,
00300 (errcode(ERRCODE_UNDEFINED_FUNCTION),
00301 errmsg("function %s does not exist",
00302 func_signature_string(funcname, nargs, argnames,
00303 actual_arg_types)),
00304 errhint("No function matches the given name and argument types. "
00305 "You might need to add explicit type casts."),
00306 parser_errposition(pstate, location)));
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316 nargsplusdefs = nargs;
00317 foreach(l, argdefaults)
00318 {
00319 Node *expr = (Node *) lfirst(l);
00320
00321
00322 if (nargsplusdefs >= FUNC_MAX_ARGS)
00323 ereport(ERROR,
00324 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
00325 errmsg_plural("cannot pass more than %d argument to a function",
00326 "cannot pass more than %d arguments to a function",
00327 FUNC_MAX_ARGS,
00328 FUNC_MAX_ARGS),
00329 parser_errposition(pstate, location)));
00330
00331 actual_arg_types[nargsplusdefs++] = exprType(expr);
00332 }
00333
00334
00335
00336
00337
00338
00339 rettype = enforce_generic_type_consistency(actual_arg_types,
00340 declared_arg_types,
00341 nargsplusdefs,
00342 rettype,
00343 false);
00344
00345
00346 make_fn_arguments(pstate, fargs, actual_arg_types, declared_arg_types);
00347
00348
00349
00350
00351
00352 if (nvargs > 0 && declared_arg_types[nargs - 1] != ANYOID)
00353 {
00354 ArrayExpr *newa = makeNode(ArrayExpr);
00355 int non_var_args = nargs - nvargs;
00356 List *vargs;
00357
00358 Assert(non_var_args >= 0);
00359 vargs = list_copy_tail(fargs, non_var_args);
00360 fargs = list_truncate(fargs, non_var_args);
00361
00362 newa->elements = vargs;
00363
00364 newa->element_typeid = exprType((Node *) linitial(vargs));
00365 newa->array_typeid = get_array_type(newa->element_typeid);
00366 if (!OidIsValid(newa->array_typeid))
00367 ereport(ERROR,
00368 (errcode(ERRCODE_UNDEFINED_OBJECT),
00369 errmsg("could not find array type for data type %s",
00370 format_type_be(newa->element_typeid)),
00371 parser_errposition(pstate, exprLocation((Node *) vargs))));
00372
00373 newa->multidims = false;
00374 newa->location = exprLocation((Node *) vargs);
00375
00376 fargs = lappend(fargs, newa);
00377 }
00378
00379
00380 if (fdresult == FUNCDETAIL_NORMAL)
00381 {
00382 FuncExpr *funcexpr = makeNode(FuncExpr);
00383
00384 funcexpr->funcid = funcid;
00385 funcexpr->funcresulttype = rettype;
00386 funcexpr->funcretset = retset;
00387 funcexpr->funcvariadic = func_variadic;
00388 funcexpr->funcformat = COERCE_EXPLICIT_CALL;
00389
00390 funcexpr->args = fargs;
00391 funcexpr->location = location;
00392
00393 retval = (Node *) funcexpr;
00394 }
00395 else if (fdresult == FUNCDETAIL_AGGREGATE && !over)
00396 {
00397
00398 Aggref *aggref = makeNode(Aggref);
00399
00400 aggref->aggfnoid = funcid;
00401 aggref->aggtype = rettype;
00402
00403
00404 aggref->aggstar = agg_star;
00405
00406 aggref->location = location;
00407
00408
00409
00410
00411
00412 if (fargs == NIL && !agg_star)
00413 ereport(ERROR,
00414 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00415 errmsg("%s(*) must be used to call a parameterless aggregate function",
00416 NameListToString(funcname)),
00417 parser_errposition(pstate, location)));
00418
00419 if (retset)
00420 ereport(ERROR,
00421 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
00422 errmsg("aggregates cannot return sets"),
00423 parser_errposition(pstate, location)));
00424
00425
00426
00427
00428
00429
00430
00431 if (argnames != NIL)
00432 ereport(ERROR,
00433 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
00434 errmsg("aggregates cannot use named arguments"),
00435 parser_errposition(pstate, location)));
00436
00437
00438 transformAggregateCall(pstate, aggref, fargs, agg_order, agg_distinct);
00439
00440 retval = (Node *) aggref;
00441 }
00442 else
00443 {
00444
00445 WindowFunc *wfunc = makeNode(WindowFunc);
00446
00447
00448
00449
00450 if (!over)
00451 ereport(ERROR,
00452 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00453 errmsg("window function call requires an OVER clause"),
00454 parser_errposition(pstate, location)));
00455
00456 wfunc->winfnoid = funcid;
00457 wfunc->wintype = rettype;
00458
00459 wfunc->args = fargs;
00460
00461 wfunc->winstar = agg_star;
00462 wfunc->winagg = (fdresult == FUNCDETAIL_AGGREGATE);
00463 wfunc->location = location;
00464
00465
00466
00467
00468 if (agg_distinct)
00469 ereport(ERROR,
00470 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
00471 errmsg("DISTINCT is not implemented for window functions"),
00472 parser_errposition(pstate, location)));
00473
00474
00475
00476
00477
00478 if (wfunc->winagg && fargs == NIL && !agg_star)
00479 ereport(ERROR,
00480 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
00481 errmsg("%s(*) must be used to call a parameterless aggregate function",
00482 NameListToString(funcname)),
00483 parser_errposition(pstate, location)));
00484
00485
00486
00487
00488 if (agg_order != NIL)
00489 ereport(ERROR,
00490 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
00491 errmsg("aggregate ORDER BY is not implemented for window functions"),
00492 parser_errposition(pstate, location)));
00493
00494 if (retset)
00495 ereport(ERROR,
00496 (errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
00497 errmsg("window functions cannot return sets"),
00498 parser_errposition(pstate, location)));
00499
00500
00501
00502
00503
00504
00505 if (argnames != NIL)
00506 ereport(ERROR,
00507 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
00508 errmsg("window functions cannot use named arguments"),
00509 parser_errposition(pstate, location)));
00510
00511
00512 transformWindowFuncCall(pstate, wfunc, over);
00513
00514 retval = (Node *) wfunc;
00515 }
00516
00517 return retval;
00518 }
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534 int
00535 func_match_argtypes(int nargs,
00536 Oid *input_typeids,
00537 FuncCandidateList raw_candidates,
00538 FuncCandidateList *candidates)
00539 {
00540 FuncCandidateList current_candidate;
00541 FuncCandidateList next_candidate;
00542 int ncandidates = 0;
00543
00544 *candidates = NULL;
00545
00546 for (current_candidate = raw_candidates;
00547 current_candidate != NULL;
00548 current_candidate = next_candidate)
00549 {
00550 next_candidate = current_candidate->next;
00551 if (can_coerce_type(nargs, input_typeids, current_candidate->args,
00552 COERCION_IMPLICIT))
00553 {
00554 current_candidate->next = *candidates;
00555 *candidates = current_candidate;
00556 ncandidates++;
00557 }
00558 }
00559
00560 return ncandidates;
00561 }
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619 FuncCandidateList
00620 func_select_candidate(int nargs,
00621 Oid *input_typeids,
00622 FuncCandidateList candidates)
00623 {
00624 FuncCandidateList current_candidate,
00625 first_candidate,
00626 last_candidate;
00627 Oid *current_typeids;
00628 Oid current_type;
00629 int i;
00630 int ncandidates;
00631 int nbestMatch,
00632 nmatch,
00633 nunknowns;
00634 Oid input_base_typeids[FUNC_MAX_ARGS];
00635 TYPCATEGORY slot_category[FUNC_MAX_ARGS],
00636 current_category;
00637 bool current_is_preferred;
00638 bool slot_has_preferred_type[FUNC_MAX_ARGS];
00639 bool resolved_unknowns;
00640
00641
00642 if (nargs > FUNC_MAX_ARGS)
00643 ereport(ERROR,
00644 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
00645 errmsg_plural("cannot pass more than %d argument to a function",
00646 "cannot pass more than %d arguments to a function",
00647 FUNC_MAX_ARGS,
00648 FUNC_MAX_ARGS)));
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 nunknowns = 0;
00664 for (i = 0; i < nargs; i++)
00665 {
00666 if (input_typeids[i] != UNKNOWNOID)
00667 input_base_typeids[i] = getBaseType(input_typeids[i]);
00668 else
00669 {
00670
00671 input_base_typeids[i] = UNKNOWNOID;
00672 nunknowns++;
00673 }
00674 }
00675
00676
00677
00678
00679
00680 ncandidates = 0;
00681 nbestMatch = 0;
00682 last_candidate = NULL;
00683 for (current_candidate = candidates;
00684 current_candidate != NULL;
00685 current_candidate = current_candidate->next)
00686 {
00687 current_typeids = current_candidate->args;
00688 nmatch = 0;
00689 for (i = 0; i < nargs; i++)
00690 {
00691 if (input_base_typeids[i] != UNKNOWNOID &&
00692 current_typeids[i] == input_base_typeids[i])
00693 nmatch++;
00694 }
00695
00696
00697 if ((nmatch > nbestMatch) || (last_candidate == NULL))
00698 {
00699 nbestMatch = nmatch;
00700 candidates = current_candidate;
00701 last_candidate = current_candidate;
00702 ncandidates = 1;
00703 }
00704
00705 else if (nmatch == nbestMatch)
00706 {
00707 last_candidate->next = current_candidate;
00708 last_candidate = current_candidate;
00709 ncandidates++;
00710 }
00711
00712 }
00713
00714 if (last_candidate)
00715 last_candidate->next = NULL;
00716
00717 if (ncandidates == 1)
00718 return candidates;
00719
00720
00721
00722
00723
00724
00725
00726
00727 for (i = 0; i < nargs; i++)
00728 slot_category[i] = TypeCategory(input_base_typeids[i]);
00729 ncandidates = 0;
00730 nbestMatch = 0;
00731 last_candidate = NULL;
00732 for (current_candidate = candidates;
00733 current_candidate != NULL;
00734 current_candidate = current_candidate->next)
00735 {
00736 current_typeids = current_candidate->args;
00737 nmatch = 0;
00738 for (i = 0; i < nargs; i++)
00739 {
00740 if (input_base_typeids[i] != UNKNOWNOID)
00741 {
00742 if (current_typeids[i] == input_base_typeids[i] ||
00743 IsPreferredType(slot_category[i], current_typeids[i]))
00744 nmatch++;
00745 }
00746 }
00747
00748 if ((nmatch > nbestMatch) || (last_candidate == NULL))
00749 {
00750 nbestMatch = nmatch;
00751 candidates = current_candidate;
00752 last_candidate = current_candidate;
00753 ncandidates = 1;
00754 }
00755 else if (nmatch == nbestMatch)
00756 {
00757 last_candidate->next = current_candidate;
00758 last_candidate = current_candidate;
00759 ncandidates++;
00760 }
00761 }
00762
00763 if (last_candidate)
00764 last_candidate->next = NULL;
00765
00766 if (ncandidates == 1)
00767 return candidates;
00768
00769
00770
00771
00772
00773
00774
00775 if (nunknowns == 0)
00776 return NULL;
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797 resolved_unknowns = false;
00798 for (i = 0; i < nargs; i++)
00799 {
00800 bool have_conflict;
00801
00802 if (input_base_typeids[i] != UNKNOWNOID)
00803 continue;
00804 resolved_unknowns = true;
00805 slot_category[i] = TYPCATEGORY_INVALID;
00806 slot_has_preferred_type[i] = false;
00807 have_conflict = false;
00808 for (current_candidate = candidates;
00809 current_candidate != NULL;
00810 current_candidate = current_candidate->next)
00811 {
00812 current_typeids = current_candidate->args;
00813 current_type = current_typeids[i];
00814 get_type_category_preferred(current_type,
00815 ¤t_category,
00816 ¤t_is_preferred);
00817 if (slot_category[i] == TYPCATEGORY_INVALID)
00818 {
00819
00820 slot_category[i] = current_category;
00821 slot_has_preferred_type[i] = current_is_preferred;
00822 }
00823 else if (current_category == slot_category[i])
00824 {
00825
00826 slot_has_preferred_type[i] |= current_is_preferred;
00827 }
00828 else
00829 {
00830
00831 if (current_category == TYPCATEGORY_STRING)
00832 {
00833
00834 slot_category[i] = current_category;
00835 slot_has_preferred_type[i] = current_is_preferred;
00836 }
00837 else
00838 {
00839
00840
00841
00842 have_conflict = true;
00843 }
00844 }
00845 }
00846 if (have_conflict && slot_category[i] != TYPCATEGORY_STRING)
00847 {
00848
00849 resolved_unknowns = false;
00850 break;
00851 }
00852 }
00853
00854 if (resolved_unknowns)
00855 {
00856
00857 ncandidates = 0;
00858 first_candidate = candidates;
00859 last_candidate = NULL;
00860 for (current_candidate = candidates;
00861 current_candidate != NULL;
00862 current_candidate = current_candidate->next)
00863 {
00864 bool keepit = true;
00865
00866 current_typeids = current_candidate->args;
00867 for (i = 0; i < nargs; i++)
00868 {
00869 if (input_base_typeids[i] != UNKNOWNOID)
00870 continue;
00871 current_type = current_typeids[i];
00872 get_type_category_preferred(current_type,
00873 ¤t_category,
00874 ¤t_is_preferred);
00875 if (current_category != slot_category[i])
00876 {
00877 keepit = false;
00878 break;
00879 }
00880 if (slot_has_preferred_type[i] && !current_is_preferred)
00881 {
00882 keepit = false;
00883 break;
00884 }
00885 }
00886 if (keepit)
00887 {
00888
00889 last_candidate = current_candidate;
00890 ncandidates++;
00891 }
00892 else
00893 {
00894
00895 if (last_candidate)
00896 last_candidate->next = current_candidate->next;
00897 else
00898 first_candidate = current_candidate->next;
00899 }
00900 }
00901
00902
00903 if (last_candidate)
00904 {
00905 candidates = first_candidate;
00906
00907 last_candidate->next = NULL;
00908 }
00909
00910 if (ncandidates == 1)
00911 return candidates;
00912 }
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924 if (nunknowns < nargs)
00925 {
00926 Oid known_type = UNKNOWNOID;
00927
00928 for (i = 0; i < nargs; i++)
00929 {
00930 if (input_base_typeids[i] == UNKNOWNOID)
00931 continue;
00932 if (known_type == UNKNOWNOID)
00933 known_type = input_base_typeids[i];
00934 else if (known_type != input_base_typeids[i])
00935 {
00936
00937 known_type = UNKNOWNOID;
00938 break;
00939 }
00940 }
00941
00942 if (known_type != UNKNOWNOID)
00943 {
00944
00945 for (i = 0; i < nargs; i++)
00946 input_base_typeids[i] = known_type;
00947 ncandidates = 0;
00948 last_candidate = NULL;
00949 for (current_candidate = candidates;
00950 current_candidate != NULL;
00951 current_candidate = current_candidate->next)
00952 {
00953 current_typeids = current_candidate->args;
00954 if (can_coerce_type(nargs, input_base_typeids, current_typeids,
00955 COERCION_IMPLICIT))
00956 {
00957 if (++ncandidates > 1)
00958 break;
00959 last_candidate = current_candidate;
00960 }
00961 }
00962 if (ncandidates == 1)
00963 {
00964
00965 last_candidate->next = NULL;
00966 return last_candidate;
00967 }
00968 }
00969 }
00970
00971 return NULL;
00972 }
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006 FuncDetailCode
01007 func_get_detail(List *funcname,
01008 List *fargs,
01009 List *fargnames,
01010 int nargs,
01011 Oid *argtypes,
01012 bool expand_variadic,
01013 bool expand_defaults,
01014 Oid *funcid,
01015 Oid *rettype,
01016 bool *retset,
01017 int *nvargs,
01018 Oid **true_typeids,
01019 List **argdefaults)
01020 {
01021 FuncCandidateList raw_candidates;
01022 FuncCandidateList best_candidate;
01023
01024
01025 *funcid = InvalidOid;
01026 *rettype = InvalidOid;
01027 *retset = false;
01028 *nvargs = 0;
01029 *true_typeids = NULL;
01030 if (argdefaults)
01031 *argdefaults = NIL;
01032
01033
01034 raw_candidates = FuncnameGetCandidates(funcname, nargs, fargnames,
01035 expand_variadic, expand_defaults);
01036
01037
01038
01039
01040
01041 for (best_candidate = raw_candidates;
01042 best_candidate != NULL;
01043 best_candidate = best_candidate->next)
01044 {
01045 if (memcmp(argtypes, best_candidate->args, nargs * sizeof(Oid)) == 0)
01046 break;
01047 }
01048
01049 if (best_candidate == NULL)
01050 {
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078
01079
01080
01081
01082
01083
01084
01085
01086
01087
01088
01089
01090 if (nargs == 1 && fargs != NIL && fargnames == NIL)
01091 {
01092 Oid targetType = FuncNameAsType(funcname);
01093
01094 if (OidIsValid(targetType))
01095 {
01096 Oid sourceType = argtypes[0];
01097 Node *arg1 = linitial(fargs);
01098 bool iscoercion;
01099
01100 if (sourceType == UNKNOWNOID && IsA(arg1, Const))
01101 {
01102
01103 iscoercion = true;
01104 }
01105 else
01106 {
01107 CoercionPathType cpathtype;
01108 Oid cfuncid;
01109
01110 cpathtype = find_coercion_pathway(targetType, sourceType,
01111 COERCION_EXPLICIT,
01112 &cfuncid);
01113 switch (cpathtype)
01114 {
01115 case COERCION_PATH_RELABELTYPE:
01116 iscoercion = true;
01117 break;
01118 case COERCION_PATH_COERCEVIAIO:
01119 if ((sourceType == RECORDOID ||
01120 ISCOMPLEX(sourceType)) &&
01121 TypeCategory(targetType) == TYPCATEGORY_STRING)
01122 iscoercion = false;
01123 else
01124 iscoercion = true;
01125 break;
01126 default:
01127 iscoercion = false;
01128 break;
01129 }
01130 }
01131
01132 if (iscoercion)
01133 {
01134
01135 *funcid = InvalidOid;
01136 *rettype = targetType;
01137 *retset = false;
01138 *nvargs = 0;
01139 *true_typeids = argtypes;
01140 return FUNCDETAIL_COERCION;
01141 }
01142 }
01143 }
01144
01145
01146
01147
01148 if (raw_candidates != NULL)
01149 {
01150 FuncCandidateList current_candidates;
01151 int ncandidates;
01152
01153 ncandidates = func_match_argtypes(nargs,
01154 argtypes,
01155 raw_candidates,
01156 ¤t_candidates);
01157
01158
01159 if (ncandidates == 1)
01160 best_candidate = current_candidates;
01161
01162
01163
01164
01165 else if (ncandidates > 1)
01166 {
01167 best_candidate = func_select_candidate(nargs,
01168 argtypes,
01169 current_candidates);
01170
01171
01172
01173
01174
01175 if (!best_candidate)
01176 return FUNCDETAIL_MULTIPLE;
01177 }
01178 }
01179 }
01180
01181 if (best_candidate)
01182 {
01183 HeapTuple ftup;
01184 Form_pg_proc pform;
01185 FuncDetailCode result;
01186
01187
01188
01189
01190
01191
01192 if (!OidIsValid(best_candidate->oid))
01193 return FUNCDETAIL_MULTIPLE;
01194
01195
01196
01197
01198
01199
01200 if (fargnames != NIL && !expand_variadic && nargs > 0 &&
01201 best_candidate->argnumbers[nargs - 1] != nargs - 1)
01202 return FUNCDETAIL_NOTFOUND;
01203
01204 *funcid = best_candidate->oid;
01205 *nvargs = best_candidate->nvargs;
01206 *true_typeids = best_candidate->args;
01207
01208
01209
01210
01211
01212
01213 if (best_candidate->argnumbers != NULL)
01214 {
01215 int i = 0;
01216 ListCell *lc;
01217
01218 foreach(lc, fargs)
01219 {
01220 NamedArgExpr *na = (NamedArgExpr *) lfirst(lc);
01221
01222 if (IsA(na, NamedArgExpr))
01223 na->argnumber = best_candidate->argnumbers[i];
01224 i++;
01225 }
01226 }
01227
01228 ftup = SearchSysCache1(PROCOID,
01229 ObjectIdGetDatum(best_candidate->oid));
01230 if (!HeapTupleIsValid(ftup))
01231 elog(ERROR, "cache lookup failed for function %u",
01232 best_candidate->oid);
01233 pform = (Form_pg_proc) GETSTRUCT(ftup);
01234 *rettype = pform->prorettype;
01235 *retset = pform->proretset;
01236
01237 if (argdefaults && best_candidate->ndargs > 0)
01238 {
01239 Datum proargdefaults;
01240 bool isnull;
01241 char *str;
01242 List *defaults;
01243
01244
01245 if (best_candidate->ndargs > pform->pronargdefaults)
01246 elog(ERROR, "not enough default arguments");
01247
01248 proargdefaults = SysCacheGetAttr(PROCOID, ftup,
01249 Anum_pg_proc_proargdefaults,
01250 &isnull);
01251 Assert(!isnull);
01252 str = TextDatumGetCString(proargdefaults);
01253 defaults = (List *) stringToNode(str);
01254 Assert(IsA(defaults, List));
01255 pfree(str);
01256
01257
01258 if (best_candidate->argnumbers != NULL)
01259 {
01260
01261
01262
01263
01264
01265
01266
01267
01268 Bitmapset *defargnumbers;
01269 int *firstdefarg;
01270 List *newdefaults;
01271 ListCell *lc;
01272 int i;
01273
01274 defargnumbers = NULL;
01275 firstdefarg = &best_candidate->argnumbers[best_candidate->nargs - best_candidate->ndargs];
01276 for (i = 0; i < best_candidate->ndargs; i++)
01277 defargnumbers = bms_add_member(defargnumbers,
01278 firstdefarg[i]);
01279 newdefaults = NIL;
01280 i = pform->pronargs - pform->pronargdefaults;
01281 foreach(lc, defaults)
01282 {
01283 if (bms_is_member(i, defargnumbers))
01284 newdefaults = lappend(newdefaults, lfirst(lc));
01285 i++;
01286 }
01287 Assert(list_length(newdefaults) == best_candidate->ndargs);
01288 bms_free(defargnumbers);
01289 *argdefaults = newdefaults;
01290 }
01291 else
01292 {
01293
01294
01295
01296
01297 int ndelete;
01298
01299 ndelete = list_length(defaults) - best_candidate->ndargs;
01300 while (ndelete-- > 0)
01301 defaults = list_delete_first(defaults);
01302 *argdefaults = defaults;
01303 }
01304 }
01305 if (pform->proisagg)
01306 result = FUNCDETAIL_AGGREGATE;
01307 else if (pform->proiswindow)
01308 result = FUNCDETAIL_WINDOWFUNC;
01309 else
01310 result = FUNCDETAIL_NORMAL;
01311 ReleaseSysCache(ftup);
01312 return result;
01313 }
01314
01315 return FUNCDETAIL_NOTFOUND;
01316 }
01317
01318
01319
01320
01321
01322
01323
01324
01325
01326
01327
01328
01329
01330
01331
01332 void
01333 make_fn_arguments(ParseState *pstate,
01334 List *fargs,
01335 Oid *actual_arg_types,
01336 Oid *declared_arg_types)
01337 {
01338 ListCell *current_fargs;
01339 int i = 0;
01340
01341 foreach(current_fargs, fargs)
01342 {
01343
01344 if (actual_arg_types[i] != declared_arg_types[i])
01345 {
01346 Node *node = (Node *) lfirst(current_fargs);
01347
01348
01349
01350
01351
01352 if (IsA(node, NamedArgExpr))
01353 {
01354 NamedArgExpr *na = (NamedArgExpr *) node;
01355
01356 node = coerce_type(pstate,
01357 (Node *) na->arg,
01358 actual_arg_types[i],
01359 declared_arg_types[i], -1,
01360 COERCION_IMPLICIT,
01361 COERCE_IMPLICIT_CAST,
01362 -1);
01363 na->arg = (Expr *) node;
01364 }
01365 else
01366 {
01367 node = coerce_type(pstate,
01368 node,
01369 actual_arg_types[i],
01370 declared_arg_types[i], -1,
01371 COERCION_IMPLICIT,
01372 COERCE_IMPLICIT_CAST,
01373 -1);
01374 lfirst(current_fargs) = node;
01375 }
01376 }
01377 i++;
01378 }
01379 }
01380
01381
01382
01383
01384
01385
01386
01387
01388 static Oid
01389 FuncNameAsType(List *funcname)
01390 {
01391 Oid result;
01392 Type typtup;
01393
01394 typtup = LookupTypeName(NULL, makeTypeNameFromNameList(funcname), NULL);
01395 if (typtup == NULL)
01396 return InvalidOid;
01397
01398 if (((Form_pg_type) GETSTRUCT(typtup))->typisdefined &&
01399 !OidIsValid(typeTypeRelid(typtup)))
01400 result = typeTypeId(typtup);
01401 else
01402 result = InvalidOid;
01403
01404 ReleaseSysCache(typtup);
01405 return result;
01406 }
01407
01408
01409
01410
01411
01412
01413
01414 static Node *
01415 ParseComplexProjection(ParseState *pstate, char *funcname, Node *first_arg,
01416 int location)
01417 {
01418 TupleDesc tupdesc;
01419 int i;
01420
01421
01422
01423
01424
01425
01426
01427
01428
01429
01430
01431 if (IsA(first_arg, Var) &&
01432 ((Var *) first_arg)->varattno == InvalidAttrNumber)
01433 {
01434 RangeTblEntry *rte;
01435
01436 rte = GetRTEByRangeTablePosn(pstate,
01437 ((Var *) first_arg)->varno,
01438 ((Var *) first_arg)->varlevelsup);
01439
01440 return scanRTEForColumn(pstate, rte, funcname, location);
01441 }
01442
01443
01444
01445
01446
01447
01448
01449
01450 if (IsA(first_arg, Var) &&
01451 ((Var *) first_arg)->vartype == RECORDOID)
01452 tupdesc = expandRecordVariable(pstate, (Var *) first_arg, 0);
01453 else if (get_expr_result_type(first_arg, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
01454 return NULL;
01455 Assert(tupdesc);
01456
01457 for (i = 0; i < tupdesc->natts; i++)
01458 {
01459 Form_pg_attribute att = tupdesc->attrs[i];
01460
01461 if (strcmp(funcname, NameStr(att->attname)) == 0 &&
01462 !att->attisdropped)
01463 {
01464
01465 FieldSelect *fselect = makeNode(FieldSelect);
01466
01467 fselect->arg = (Expr *) first_arg;
01468 fselect->fieldnum = i + 1;
01469 fselect->resulttype = att->atttypid;
01470 fselect->resulttypmod = att->atttypmod;
01471
01472 fselect->resultcollid = att->attcollation;
01473 return (Node *) fselect;
01474 }
01475 }
01476
01477 return NULL;
01478 }
01479
01480
01481
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492 const char *
01493 funcname_signature_string(const char *funcname, int nargs,
01494 List *argnames, const Oid *argtypes)
01495 {
01496 StringInfoData argbuf;
01497 int numposargs;
01498 ListCell *lc;
01499 int i;
01500
01501 initStringInfo(&argbuf);
01502
01503 appendStringInfo(&argbuf, "%s(", funcname);
01504
01505 numposargs = nargs - list_length(argnames);
01506 lc = list_head(argnames);
01507
01508 for (i = 0; i < nargs; i++)
01509 {
01510 if (i)
01511 appendStringInfoString(&argbuf, ", ");
01512 if (i >= numposargs)
01513 {
01514 appendStringInfo(&argbuf, "%s := ", (char *) lfirst(lc));
01515 lc = lnext(lc);
01516 }
01517 appendStringInfoString(&argbuf, format_type_be(argtypes[i]));
01518 }
01519
01520 appendStringInfoChar(&argbuf, ')');
01521
01522 return argbuf.data;
01523 }
01524
01525
01526
01527
01528
01529 const char *
01530 func_signature_string(List *funcname, int nargs,
01531 List *argnames, const Oid *argtypes)
01532 {
01533 return funcname_signature_string(NameListToString(funcname),
01534 nargs, argnames, argtypes);
01535 }
01536
01537
01538
01539
01540
01541
01542
01543
01544
01545
01546
01547
01548 Oid
01549 LookupFuncName(List *funcname, int nargs, const Oid *argtypes, bool noError)
01550 {
01551 FuncCandidateList clist;
01552
01553 clist = FuncnameGetCandidates(funcname, nargs, NIL, false, false);
01554
01555 while (clist)
01556 {
01557 if (memcmp(argtypes, clist->args, nargs * sizeof(Oid)) == 0)
01558 return clist->oid;
01559 clist = clist->next;
01560 }
01561
01562 if (!noError)
01563 ereport(ERROR,
01564 (errcode(ERRCODE_UNDEFINED_FUNCTION),
01565 errmsg("function %s does not exist",
01566 func_signature_string(funcname, nargs,
01567 NIL, argtypes))));
01568
01569 return InvalidOid;
01570 }
01571
01572
01573
01574
01575
01576 static Oid
01577 LookupTypeNameOid(const TypeName *typename)
01578 {
01579 Oid result;
01580 Type typtup;
01581
01582 typtup = LookupTypeName(NULL, typename, NULL);
01583 if (typtup == NULL)
01584 ereport(ERROR,
01585 (errcode(ERRCODE_UNDEFINED_OBJECT),
01586 errmsg("type \"%s\" does not exist",
01587 TypeNameToString(typename))));
01588 result = typeTypeId(typtup);
01589 ReleaseSysCache(typtup);
01590 return result;
01591 }
01592
01593
01594
01595
01596
01597
01598 Oid
01599 LookupFuncNameTypeNames(List *funcname, List *argtypes, bool noError)
01600 {
01601 Oid argoids[FUNC_MAX_ARGS];
01602 int argcount;
01603 int i;
01604 ListCell *args_item;
01605
01606 argcount = list_length(argtypes);
01607 if (argcount > FUNC_MAX_ARGS)
01608 ereport(ERROR,
01609 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
01610 errmsg_plural("functions cannot have more than %d argument",
01611 "functions cannot have more than %d arguments",
01612 FUNC_MAX_ARGS,
01613 FUNC_MAX_ARGS)));
01614
01615 args_item = list_head(argtypes);
01616 for (i = 0; i < argcount; i++)
01617 {
01618 TypeName *t = (TypeName *) lfirst(args_item);
01619
01620 argoids[i] = LookupTypeNameOid(t);
01621 args_item = lnext(args_item);
01622 }
01623
01624 return LookupFuncName(funcname, argcount, argoids, noError);
01625 }
01626
01627
01628
01629
01630
01631
01632
01633
01634
01635 Oid
01636 LookupAggNameTypeNames(List *aggname, List *argtypes, bool noError)
01637 {
01638 Oid argoids[FUNC_MAX_ARGS];
01639 int argcount;
01640 int i;
01641 ListCell *lc;
01642 Oid oid;
01643 HeapTuple ftup;
01644 Form_pg_proc pform;
01645
01646 argcount = list_length(argtypes);
01647 if (argcount > FUNC_MAX_ARGS)
01648 ereport(ERROR,
01649 (errcode(ERRCODE_TOO_MANY_ARGUMENTS),
01650 errmsg_plural("functions cannot have more than %d argument",
01651 "functions cannot have more than %d arguments",
01652 FUNC_MAX_ARGS,
01653 FUNC_MAX_ARGS)));
01654
01655 i = 0;
01656 foreach(lc, argtypes)
01657 {
01658 TypeName *t = (TypeName *) lfirst(lc);
01659
01660 argoids[i] = LookupTypeNameOid(t);
01661 i++;
01662 }
01663
01664 oid = LookupFuncName(aggname, argcount, argoids, true);
01665
01666 if (!OidIsValid(oid))
01667 {
01668 if (noError)
01669 return InvalidOid;
01670 if (argcount == 0)
01671 ereport(ERROR,
01672 (errcode(ERRCODE_UNDEFINED_FUNCTION),
01673 errmsg("aggregate %s(*) does not exist",
01674 NameListToString(aggname))));
01675 else
01676 ereport(ERROR,
01677 (errcode(ERRCODE_UNDEFINED_FUNCTION),
01678 errmsg("aggregate %s does not exist",
01679 func_signature_string(aggname, argcount,
01680 NIL, argoids))));
01681 }
01682
01683
01684 ftup = SearchSysCache1(PROCOID, ObjectIdGetDatum(oid));
01685 if (!HeapTupleIsValid(ftup))
01686 elog(ERROR, "cache lookup failed for function %u", oid);
01687 pform = (Form_pg_proc) GETSTRUCT(ftup);
01688
01689 if (!pform->proisagg)
01690 {
01691 ReleaseSysCache(ftup);
01692 if (noError)
01693 return InvalidOid;
01694
01695 ereport(ERROR,
01696 (errcode(ERRCODE_WRONG_OBJECT_TYPE),
01697 errmsg("function %s is not an aggregate",
01698 func_signature_string(aggname, argcount,
01699 NIL, argoids))));
01700 }
01701
01702 ReleaseSysCache(ftup);
01703
01704 return oid;
01705 }