1 /*-------------------------------------------------------------------------
4 * handle operator things for parser
6 * Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/parse_oper.c,v 1.63 2003/04/29 22:13:10 tgl Exp $
13 *-------------------------------------------------------------------------
18 #include "access/genam.h"
19 #include "access/heapam.h"
20 #include "catalog/catname.h"
21 #include "catalog/indexing.h"
22 #include "catalog/namespace.h"
23 #include "catalog/pg_operator.h"
24 #include "parser/parse_coerce.h"
25 #include "parser/parse_expr.h"
26 #include "parser/parse_func.h"
27 #include "parser/parse_oper.h"
28 #include "parser/parse_type.h"
29 #include "utils/builtins.h"
30 #include "utils/fmgroids.h"
31 #include "utils/lsyscache.h"
32 #include "utils/syscache.h"
35 static Oid binary_oper_exact(Oid arg1, Oid arg2,
36 FuncCandidateList candidates);
37 static Oid oper_select_candidate(int nargs, Oid *input_typeids,
38 FuncCandidateList candidates);
39 static void op_error(List *op, Oid arg1, Oid arg2);
40 static void unary_op_error(List *op, Oid arg, bool is_left_op);
45 * Given a possibly-qualified operator name and exact input datatypes,
46 * look up the operator. Returns InvalidOid if no such operator.
48 * Pass oprleft = InvalidOid for a prefix op, oprright = InvalidOid for
51 * If the operator name is not schema-qualified, it is sought in the current
52 * namespace search path.
55 LookupOperName(List *opername, Oid oprleft, Oid oprright)
57 FuncCandidateList clist;
60 if (!OidIsValid(oprleft))
62 else if (!OidIsValid(oprright))
67 clist = OpernameGetCandidates(opername, oprkind);
71 if (clist->args[0] == oprleft && clist->args[1] == oprright)
80 * LookupOperNameTypeNames
81 * Like LookupOperName, but the argument types are specified by
82 * TypeName nodes. Also, if we fail to find the operator
83 * and caller is not NULL, then an error is reported.
85 * Pass oprleft = NULL for a prefix op, oprright = NULL for a postfix op.
88 LookupOperNameTypeNames(List *opername, TypeName *oprleft,
89 TypeName *oprright, const char *caller)
99 leftoid = LookupTypeName(oprleft);
100 if (!OidIsValid(leftoid))
101 elog(ERROR, "Type \"%s\" does not exist",
102 TypeNameToString(oprleft));
104 if (oprright == NULL)
105 rightoid = InvalidOid;
108 rightoid = LookupTypeName(oprright);
109 if (!OidIsValid(rightoid))
110 elog(ERROR, "Type \"%s\" does not exist",
111 TypeNameToString(oprright));
114 operoid = LookupOperName(opername, leftoid, rightoid);
116 if (!OidIsValid(operoid) && caller != NULL)
119 elog(ERROR, "%s: Prefix operator '%s' for type '%s' does not exist",
120 caller, NameListToString(opername),
121 TypeNameToString(oprright));
122 else if (oprright == NULL)
123 elog(ERROR, "%s: Postfix operator '%s' for type '%s' does not exist",
124 caller, NameListToString(opername),
125 TypeNameToString(oprleft));
127 elog(ERROR, "%s: Operator '%s' for types '%s' and '%s' does not exist",
128 caller, NameListToString(opername),
129 TypeNameToString(oprleft),
130 TypeNameToString(oprright));
137 * equality_oper - identify a suitable equality operator for a datatype
139 * On failure, return NULL if noError, else report a standard error
142 equality_oper(Oid argtype, bool noError)
147 * Look for an "=" operator for the datatype. We require it to be
148 * an exact or binary-compatible match, since most callers are not
149 * prepared to cope with adding any run-time type coercion steps.
151 optup = compatible_oper(makeList1(makeString("=")),
152 argtype, argtype, true);
156 * Only believe that it's equality if it's mergejoinable,
157 * hashjoinable, or uses eqsel() as oprrest.
159 Form_pg_operator pgopform = (Form_pg_operator) GETSTRUCT(optup);
161 if (OidIsValid(pgopform->oprlsortop) ||
162 pgopform->oprcanhash ||
163 pgopform->oprrest == F_EQSEL)
166 ReleaseSysCache(optup);
169 elog(ERROR, "Unable to identify an equality operator for type %s",
170 format_type_be(argtype));
175 * ordering_oper - identify a suitable sorting operator ("<") for a datatype
177 * On failure, return NULL if noError, else report a standard error
180 ordering_oper(Oid argtype, bool noError)
185 * Find the type's equality operator, and use its lsortop (it *must*
186 * be mergejoinable). We use this definition because for sorting and
187 * grouping purposes, it's important that the equality and ordering
188 * operators are consistent.
190 optup = equality_oper(argtype, noError);
193 Oid lsortop = ((Form_pg_operator) GETSTRUCT(optup))->oprlsortop;
195 ReleaseSysCache(optup);
197 if (OidIsValid(lsortop))
199 optup = SearchSysCache(OPEROID,
200 ObjectIdGetDatum(lsortop),
207 elog(ERROR, "Unable to identify an ordering operator for type %s"
208 "\n\tUse an explicit ordering operator or modify the query",
209 format_type_be(argtype));
214 * equality_oper_funcid - convenience routine for oprfuncid(equality_oper())
217 equality_oper_funcid(Oid argtype)
222 optup = equality_oper(argtype, false);
223 result = oprfuncid(optup);
224 ReleaseSysCache(optup);
229 * ordering_oper_opid - convenience routine for oprid(ordering_oper())
231 * This was formerly called any_ordering_op()
234 ordering_oper_opid(Oid argtype)
239 optup = ordering_oper(argtype, false);
240 result = oprid(optup);
241 ReleaseSysCache(optup);
246 /* given operator tuple, return the operator OID */
250 return HeapTupleGetOid(op);
253 /* given operator tuple, return the underlying function's OID */
255 oprfuncid(Operator op)
257 Form_pg_operator pgopform = (Form_pg_operator) GETSTRUCT(op);
259 return pgopform->oprcode;
263 /* binary_oper_exact()
264 * Check for an "exact" match to the specified operand types.
266 * If one operand is an unknown literal, assume it should be taken to be
267 * the same type as the other operand for this purpose.
270 binary_oper_exact(Oid arg1, Oid arg2,
271 FuncCandidateList candidates)
273 /* Unspecified type for one of the arguments? then use the other */
274 if ((arg1 == UNKNOWNOID) && (arg2 != InvalidOid))
276 else if ((arg2 == UNKNOWNOID) && (arg1 != InvalidOid))
279 while (candidates != NULL)
281 if (arg1 == candidates->args[0] &&
282 arg2 == candidates->args[1])
283 return candidates->oid;
284 candidates = candidates->next;
291 /* oper_select_candidate()
292 * Given the input argtype array and one or more candidates
293 * for the function argtype array, attempt to resolve the conflict.
294 * Returns the selected argtype array if the conflict can be resolved,
295 * otherwise returns NULL.
297 * By design, this is pretty similar to func_select_candidate in parse_func.c.
298 * However, we can do a couple of extra things here because we know we can
299 * have no more than two args to deal with. Also, the calling convention
300 * is a little different: we must prune away "candidates" that aren't actually
301 * coercion-compatible with the input types, whereas in parse_func.c that
302 * gets done by match_argtypes before func_select_candidate is called.
304 * This routine is new code, replacing binary_oper_select_candidate()
305 * which dates from v4.2/v1.0.x days. It tries very hard to match up
306 * operators with types, including allowing type coercions if necessary.
307 * The important thing is that the code do as much as possible,
308 * while _never_ doing the wrong thing, where "the wrong thing" would
309 * be returning an operator when other better choices are available,
310 * or returning an operator which is a non-intuitive possibility.
311 * - thomas 1998-05-21
313 * The comments below came from binary_oper_select_candidate(), and
314 * illustrate the issues and choices which are possible:
315 * - thomas 1998-05-20
317 * current wisdom holds that the default operator should be one in which
318 * both operands have the same type (there will only be one such
321 * 7.27.93 - I have decided not to do this; it's too hard to justify, and
322 * it's easy enough to typecast explicitly - avi
323 * [the rest of this routine was commented out since then - ay]
325 * 6/23/95 - I don't complete agree with avi. In particular, casting
326 * floats is a pain for users. Whatever the rationale behind not doing
327 * this is, I need the following special case to work.
329 * In the WHERE clause of a query, if a float is specified without
330 * quotes, we treat it as float8. I added the float48* operators so
331 * that we can operate on float4 and float8. But now we have more than
332 * one matching operator if the right arg is unknown (eg. float
333 * specified with quotes). This break some stuff in the regression
334 * test where there are floats in quotes not properly casted. Below is
335 * the solution. In addition to requiring the operator operates on the
336 * same type for both operands [as in the code Avi originally
337 * commented out], we also require that the operators be equivalent in
338 * some sense. (see equivalentOpersAfterPromotion for details.)
342 oper_select_candidate(int nargs,
344 FuncCandidateList candidates)
346 FuncCandidateList current_candidate;
347 FuncCandidateList last_candidate;
348 Oid *current_typeids;
355 CATEGORY slot_category[FUNC_MAX_ARGS],
357 bool slot_has_preferred_type[FUNC_MAX_ARGS];
358 bool resolved_unknowns;
361 * First, delete any candidates that cannot actually accept the given
362 * input types, whether directly or by coercion. (Note that
363 * can_coerce_type will assume that UNKNOWN inputs are coercible to
364 * anything, so candidates will not be eliminated on that basis.)
367 last_candidate = NULL;
368 for (current_candidate = candidates;
369 current_candidate != NULL;
370 current_candidate = current_candidate->next)
372 if (can_coerce_type(nargs, input_typeids, current_candidate->args,
375 if (last_candidate == NULL)
377 candidates = current_candidate;
378 last_candidate = current_candidate;
383 last_candidate->next = current_candidate;
384 last_candidate = current_candidate;
388 /* otherwise, don't bother keeping this one... */
391 if (last_candidate) /* terminate rebuilt list */
392 last_candidate->next = NULL;
394 /* Done if no candidate or only one candidate survives */
395 if (ncandidates == 0)
397 if (ncandidates == 1)
398 return candidates->oid;
401 * Run through all candidates and keep those with the most matches on
402 * exact types. Keep all candidates if none match.
406 last_candidate = NULL;
407 for (current_candidate = candidates;
408 current_candidate != NULL;
409 current_candidate = current_candidate->next)
411 current_typeids = current_candidate->args;
413 for (i = 0; i < nargs; i++)
415 if (input_typeids[i] != UNKNOWNOID &&
416 current_typeids[i] == input_typeids[i])
420 /* take this one as the best choice so far? */
421 if ((nmatch > nbestMatch) || (last_candidate == NULL))
424 candidates = current_candidate;
425 last_candidate = current_candidate;
428 /* no worse than the last choice, so keep this one too? */
429 else if (nmatch == nbestMatch)
431 last_candidate->next = current_candidate;
432 last_candidate = current_candidate;
435 /* otherwise, don't bother keeping this one... */
438 if (last_candidate) /* terminate rebuilt list */
439 last_candidate->next = NULL;
441 if (ncandidates == 1)
442 return candidates->oid;
445 * Still too many candidates? Run through all candidates and keep
446 * those with the most matches on exact types + binary-compatible
447 * types. Keep all candidates if none match.
451 last_candidate = NULL;
452 for (current_candidate = candidates;
453 current_candidate != NULL;
454 current_candidate = current_candidate->next)
456 current_typeids = current_candidate->args;
458 for (i = 0; i < nargs; i++)
460 if (input_typeids[i] != UNKNOWNOID)
462 if (IsBinaryCoercible(input_typeids[i], current_typeids[i]))
467 /* take this one as the best choice so far? */
468 if ((nmatch > nbestMatch) || (last_candidate == NULL))
471 candidates = current_candidate;
472 last_candidate = current_candidate;
475 /* no worse than the last choice, so keep this one too? */
476 else if (nmatch == nbestMatch)
478 last_candidate->next = current_candidate;
479 last_candidate = current_candidate;
482 /* otherwise, don't bother keeping this one... */
485 if (last_candidate) /* terminate rebuilt list */
486 last_candidate->next = NULL;
488 if (ncandidates == 1)
489 return candidates->oid;
492 * Still too many candidates? Now look for candidates which are
493 * preferred types at the args that will require coercion. Keep all
494 * candidates if none match.
498 last_candidate = NULL;
499 for (current_candidate = candidates;
500 current_candidate != NULL;
501 current_candidate = current_candidate->next)
503 current_typeids = current_candidate->args;
505 for (i = 0; i < nargs; i++)
507 if (input_typeids[i] != UNKNOWNOID)
509 current_category = TypeCategory(current_typeids[i]);
510 if (current_typeids[i] == input_typeids[i] ||
511 IsPreferredType(current_category, current_typeids[i]))
516 if ((nmatch > nbestMatch) || (last_candidate == NULL))
519 candidates = current_candidate;
520 last_candidate = current_candidate;
523 else if (nmatch == nbestMatch)
525 last_candidate->next = current_candidate;
526 last_candidate = current_candidate;
531 if (last_candidate) /* terminate rebuilt list */
532 last_candidate->next = NULL;
534 if (ncandidates == 1)
535 return candidates->oid;
538 * Still too many candidates? Try assigning types for the unknown
541 * First try: if we have an unknown and a non-unknown input, see whether
542 * there is a candidate all of whose input types are the same as the
543 * known input type (there can be at most one such candidate). If so,
544 * use that candidate. NOTE that this is cool only because operators
545 * can't have more than 2 args, so taking the last non-unknown as
546 * current_type can yield only one possibility if there is also an
550 current_type = UNKNOWNOID;
551 for (i = 0; i < nargs; i++)
553 if ((input_typeids[i] != UNKNOWNOID)
554 && (input_typeids[i] != InvalidOid))
555 current_type = input_typeids[i];
560 if (unknownOids && (current_type != UNKNOWNOID))
562 for (current_candidate = candidates;
563 current_candidate != NULL;
564 current_candidate = current_candidate->next)
566 current_typeids = current_candidate->args;
568 for (i = 0; i < nargs; i++)
570 if (current_type == current_typeids[i])
574 return current_candidate->oid;
579 * Second try: same algorithm as for unknown resolution in
582 * We do this by examining each unknown argument position to see if we
583 * can determine a "type category" for it. If any candidate has an
584 * input datatype of STRING category, use STRING category (this bias
585 * towards STRING is appropriate since unknown-type literals look like
586 * strings). Otherwise, if all the candidates agree on the type
587 * category of this argument position, use that category. Otherwise,
588 * fail because we cannot determine a category.
590 * If we are able to determine a type category, also notice whether any
591 * of the candidates takes a preferred datatype within the category.
593 * Having completed this examination, remove candidates that accept the
594 * wrong category at any unknown position. Also, if at least one
595 * candidate accepted a preferred type at a position, remove
596 * candidates that accept non-preferred types.
598 * If we are down to one candidate at the end, we win.
600 resolved_unknowns = false;
601 for (i = 0; i < nargs; i++)
605 if (input_typeids[i] != UNKNOWNOID)
607 resolved_unknowns = true; /* assume we can do it */
608 slot_category[i] = INVALID_TYPE;
609 slot_has_preferred_type[i] = false;
610 have_conflict = false;
611 for (current_candidate = candidates;
612 current_candidate != NULL;
613 current_candidate = current_candidate->next)
615 current_typeids = current_candidate->args;
616 current_type = current_typeids[i];
617 current_category = TypeCategory(current_type);
618 if (slot_category[i] == INVALID_TYPE)
620 /* first candidate */
621 slot_category[i] = current_category;
622 slot_has_preferred_type[i] =
623 IsPreferredType(current_category, current_type);
625 else if (current_category == slot_category[i])
627 /* more candidates in same category */
628 slot_has_preferred_type[i] |=
629 IsPreferredType(current_category, current_type);
633 /* category conflict! */
634 if (current_category == STRING_TYPE)
636 /* STRING always wins if available */
637 slot_category[i] = current_category;
638 slot_has_preferred_type[i] =
639 IsPreferredType(current_category, current_type);
644 * Remember conflict, but keep going (might find
647 have_conflict = true;
651 if (have_conflict && slot_category[i] != STRING_TYPE)
653 /* Failed to resolve category conflict at this position */
654 resolved_unknowns = false;
659 if (resolved_unknowns)
661 /* Strip non-matching candidates */
663 last_candidate = NULL;
664 for (current_candidate = candidates;
665 current_candidate != NULL;
666 current_candidate = current_candidate->next)
670 current_typeids = current_candidate->args;
671 for (i = 0; i < nargs; i++)
673 if (input_typeids[i] != UNKNOWNOID)
675 current_type = current_typeids[i];
676 current_category = TypeCategory(current_type);
677 if (current_category != slot_category[i])
682 if (slot_has_preferred_type[i] &&
683 !IsPreferredType(current_category, current_type))
691 /* keep this candidate */
692 last_candidate = current_candidate;
697 /* forget this candidate */
699 last_candidate->next = current_candidate->next;
701 candidates = current_candidate->next;
704 if (last_candidate) /* terminate rebuilt list */
705 last_candidate->next = NULL;
708 if (ncandidates == 1)
709 return candidates->oid;
711 return InvalidOid; /* failed to determine a unique candidate */
712 } /* oper_select_candidate() */
715 /* oper() -- search for a binary operator
716 * Given operator name, types of arg1 and arg2, return oper struct.
718 * IMPORTANT: the returned operator (if any) is only promised to be
719 * coercion-compatible with the input datatypes. Do not use this if
720 * you need an exact- or binary-compatible match; see compatible_oper.
722 * If no matching operator found, return NULL if noError is true,
723 * raise an error if it is false.
725 * NOTE: on success, the returned object is a syscache entry. The caller
726 * must ReleaseSysCache() the entry when done with it.
729 oper(List *opname, Oid ltypeId, Oid rtypeId, bool noError)
731 FuncCandidateList clist;
734 HeapTuple tup = NULL;
736 /* Get binary operators of given name */
737 clist = OpernameGetCandidates(opname, 'b');
739 /* No operators found? Then fail... */
743 * Check for an "exact" match.
745 operOid = binary_oper_exact(ltypeId, rtypeId, clist);
746 if (!OidIsValid(operOid))
749 * Otherwise, search for the most suitable candidate.
753 * Unspecified type for one of the arguments? then use the
756 if (rtypeId == InvalidOid)
758 else if (ltypeId == InvalidOid)
760 inputOids[0] = ltypeId;
761 inputOids[1] = rtypeId;
762 operOid = oper_select_candidate(2, inputOids, clist);
764 if (OidIsValid(operOid))
765 tup = SearchSysCache(OPEROID,
766 ObjectIdGetDatum(operOid),
770 if (!HeapTupleIsValid(tup) && !noError)
771 op_error(opname, ltypeId, rtypeId);
773 return (Operator) tup;
777 * given an opname and input datatypes, find a compatible binary operator
779 * This is tighter than oper() because it will not return an operator that
780 * requires coercion of the input datatypes (but binary-compatible operators
781 * are accepted). Otherwise, the semantics are the same.
784 compatible_oper(List *op, Oid arg1, Oid arg2, bool noError)
787 Form_pg_operator opform;
789 /* oper() will find the best available match */
790 optup = oper(op, arg1, arg2, noError);
791 if (optup == (Operator) NULL)
792 return (Operator) NULL; /* must be noError case */
794 /* but is it good enough? */
795 opform = (Form_pg_operator) GETSTRUCT(optup);
796 if (IsBinaryCoercible(arg1, opform->oprleft) &&
797 IsBinaryCoercible(arg2, opform->oprright))
801 ReleaseSysCache(optup);
804 op_error(op, arg1, arg2);
806 return (Operator) NULL;
809 /* compatible_oper_opid() -- get OID of a binary operator
811 * This is a convenience routine that extracts only the operator OID
812 * from the result of compatible_oper(). InvalidOid is returned if the
813 * lookup fails and noError is true.
816 compatible_oper_opid(List *op, Oid arg1, Oid arg2, bool noError)
821 optup = compatible_oper(op, arg1, arg2, noError);
824 result = oprid(optup);
825 ReleaseSysCache(optup);
832 /* right_oper() -- search for a unary right operator (operator on right)
833 * Given operator name and type of arg, return oper struct.
835 * IMPORTANT: the returned operator (if any) is only promised to be
836 * coercion-compatible with the input datatype. Do not use this if
837 * you need an exact- or binary-compatible match.
839 * If no matching operator found, return NULL if noError is true,
840 * raise an error if it is false.
842 * NOTE: on success, the returned object is a syscache entry. The caller
843 * must ReleaseSysCache() the entry when done with it.
846 right_oper(List *op, Oid arg, bool noError)
848 FuncCandidateList clist;
849 Oid operOid = InvalidOid;
850 HeapTuple tup = NULL;
852 /* Find candidates */
853 clist = OpernameGetCandidates(op, 'r');
858 * First, quickly check to see if there is an exactly matching
859 * operator (there can be only one such entry in the list).
861 FuncCandidateList clisti;
863 for (clisti = clist; clisti != NULL; clisti = clisti->next)
865 if (arg == clisti->args[0])
867 operOid = clisti->oid;
872 if (!OidIsValid(operOid))
875 * We must run oper_select_candidate even if only one
876 * candidate, otherwise we may falsely return a
877 * non-type-compatible operator.
879 operOid = oper_select_candidate(1, &arg, clist);
881 if (OidIsValid(operOid))
882 tup = SearchSysCache(OPEROID,
883 ObjectIdGetDatum(operOid),
887 if (!HeapTupleIsValid(tup) && !noError)
888 unary_op_error(op, arg, FALSE);
890 return (Operator) tup;
894 /* left_oper() -- search for a unary left operator (operator on left)
895 * Given operator name and type of arg, return oper struct.
897 * IMPORTANT: the returned operator (if any) is only promised to be
898 * coercion-compatible with the input datatype. Do not use this if
899 * you need an exact- or binary-compatible match.
901 * If no matching operator found, return NULL if noError is true,
902 * raise an error if it is false.
904 * NOTE: on success, the returned object is a syscache entry. The caller
905 * must ReleaseSysCache() the entry when done with it.
908 left_oper(List *op, Oid arg, bool noError)
910 FuncCandidateList clist;
911 Oid operOid = InvalidOid;
912 HeapTuple tup = NULL;
914 /* Find candidates */
915 clist = OpernameGetCandidates(op, 'l');
920 * First, quickly check to see if there is an exactly matching
921 * operator (there can be only one such entry in the list).
923 * The returned list has args in the form (0, oprright). Move the
924 * useful data into args[0] to keep oper_select_candidate simple.
925 * XXX we are assuming here that we may scribble on the list!
927 FuncCandidateList clisti;
929 for (clisti = clist; clisti != NULL; clisti = clisti->next)
931 clisti->args[0] = clisti->args[1];
932 if (arg == clisti->args[0])
934 operOid = clisti->oid;
939 if (!OidIsValid(operOid))
942 * We must run oper_select_candidate even if only one
943 * candidate, otherwise we may falsely return a
944 * non-type-compatible operator.
946 operOid = oper_select_candidate(1, &arg, clist);
948 if (OidIsValid(operOid))
949 tup = SearchSysCache(OPEROID,
950 ObjectIdGetDatum(operOid),
954 if (!HeapTupleIsValid(tup) && !noError)
955 unary_op_error(op, arg, TRUE);
957 return (Operator) tup;
962 * Give a somewhat useful error message when the operator for two types
966 op_error(List *op, Oid arg1, Oid arg2)
968 if (!typeidIsValid(arg1))
969 elog(ERROR, "Left hand side of operator '%s' has an unknown type"
970 "\n\tProbably a bad attribute name",
971 NameListToString(op));
973 if (!typeidIsValid(arg2))
974 elog(ERROR, "Right hand side of operator %s has an unknown type"
975 "\n\tProbably a bad attribute name",
976 NameListToString(op));
978 elog(ERROR, "Unable to identify an operator '%s' for types '%s' and '%s'"
979 "\n\tYou will have to retype this query using an explicit cast",
980 NameListToString(op),
981 format_type_be(arg1), format_type_be(arg2));
985 * Give a somewhat useful error message when the operator for one type
989 unary_op_error(List *op, Oid arg, bool is_left_op)
991 if (!typeidIsValid(arg))
994 elog(ERROR, "operand of prefix operator '%s' has an unknown type"
995 "\n\t(probably an invalid column reference)",
996 NameListToString(op));
998 elog(ERROR, "operand of postfix operator '%s' has an unknown type"
999 "\n\t(probably an invalid column reference)",
1000 NameListToString(op));
1005 elog(ERROR, "Unable to identify a prefix operator '%s' for type '%s'"
1006 "\n\tYou may need to add parentheses or an explicit cast",
1007 NameListToString(op), format_type_be(arg));
1009 elog(ERROR, "Unable to identify a postfix operator '%s' for type '%s'"
1010 "\n\tYou may need to add parentheses or an explicit cast",
1011 NameListToString(op), format_type_be(arg));
1018 * Operator expression construction.
1020 * Transform operator expression ensuring type compatibility.
1021 * This is where some type conversion happens.
1023 * As with coerce_type, pstate may be NULL if no special unknown-Param
1024 * processing is wanted.
1027 make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree)
1034 /* Select the operator */
1037 /* right operator */
1038 ltypeId = exprType(ltree);
1039 rtypeId = InvalidOid;
1040 tup = right_oper(opname, ltypeId, false);
1042 else if (ltree == NULL)
1045 rtypeId = exprType(rtree);
1046 ltypeId = InvalidOid;
1047 tup = left_oper(opname, rtypeId, false);
1051 /* otherwise, binary operator */
1052 ltypeId = exprType(ltree);
1053 rtypeId = exprType(rtree);
1054 tup = oper(opname, ltypeId, rtypeId, false);
1057 /* Do typecasting and build the expression tree */
1058 result = make_op_expr(pstate, tup, ltree, rtree, ltypeId, rtypeId);
1060 ReleaseSysCache(tup);
1068 * Build operator expression using an already-looked-up operator.
1070 * As with coerce_type, pstate may be NULL if no special unknown-Param
1071 * processing is wanted.
1074 make_op_expr(ParseState *pstate, Operator op,
1075 Node *ltree, Node *rtree,
1076 Oid ltypeId, Oid rtypeId)
1078 Form_pg_operator opform = (Form_pg_operator) GETSTRUCT(op);
1079 Oid actual_arg_types[2];
1080 Oid declared_arg_types[2];
1088 /* right operator */
1089 args = makeList1(ltree);
1090 actual_arg_types[0] = ltypeId;
1091 declared_arg_types[0] = opform->oprleft;
1094 else if (ltree == NULL)
1097 args = makeList1(rtree);
1098 actual_arg_types[0] = rtypeId;
1099 declared_arg_types[0] = opform->oprright;
1104 /* otherwise, binary operator */
1105 args = makeList2(ltree, rtree);
1106 actual_arg_types[0] = ltypeId;
1107 actual_arg_types[1] = rtypeId;
1108 declared_arg_types[0] = opform->oprleft;
1109 declared_arg_types[1] = opform->oprright;
1114 * enforce consistency with ANYARRAY and ANYELEMENT argument and
1115 * return types, possibly adjusting return type or declared_arg_types
1116 * (which will be used as the cast destination by make_fn_arguments)
1118 rettype = enforce_generic_type_consistency(actual_arg_types,
1123 /* perform the necessary typecasting of arguments */
1124 make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types);
1126 /* and build the expression node */
1127 result = makeNode(OpExpr);
1128 result->opno = oprid(op);
1129 result->opfuncid = InvalidOid;
1130 result->opresulttype = rettype;
1131 result->opretset = get_func_retset(opform->oprcode);
1132 result->args = args;
1134 return (Expr *) result;