1997-11-25 23:07:18 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2000-02-27 03:48:15 +01:00
|
|
|
* parse_oper.c
|
1997-11-25 23:07:18 +01:00
|
|
|
* handle operator things for parser
|
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, 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
|
2001-01-24 20:43:33 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/parser/parse_oper.c,v 1.46 2001/01/24 19:43:02 momjian Exp $
|
1997-11-25 23:07:18 +01:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-11-26 02:14:33 +01:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "postgres.h"
|
1998-04-27 06:08:07 +02:00
|
|
|
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "catalog/catname.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
#include "catalog/pg_operator.h"
|
1999-07-16 07:00:38 +02:00
|
|
|
#include "parser/parse_coerce.h"
|
|
|
|
#include "parser/parse_func.h"
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "parser/parse_oper.h"
|
|
|
|
#include "parser/parse_type.h"
|
2000-05-28 19:56:29 +02:00
|
|
|
#include "utils/fmgroids.h"
|
1997-11-26 02:14:33 +01:00
|
|
|
#include "utils/syscache.h"
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
static Oid *oper_select_candidate(int nargs, Oid *input_typeids,
|
|
|
|
CandidateList candidates);
|
1999-08-24 01:48:39 +02:00
|
|
|
static Operator oper_exact(char *op, Oid arg1, Oid arg2);
|
|
|
|
static Operator oper_inexact(char *op, Oid arg1, Oid arg2);
|
1998-09-01 06:40:42 +02:00
|
|
|
static int binary_oper_get_candidates(char *opname,
|
2000-04-12 19:17:23 +02:00
|
|
|
CandidateList *candidates);
|
2000-03-19 01:19:39 +01:00
|
|
|
static int unary_oper_get_candidates(char *opname,
|
2000-04-12 19:17:23 +02:00
|
|
|
CandidateList *candidates,
|
|
|
|
char rightleft);
|
1998-09-16 16:22:22 +02:00
|
|
|
static void op_error(char *op, Oid arg1, Oid arg2);
|
2000-02-27 03:48:15 +01:00
|
|
|
static void unary_op_error(char *op, Oid arg, bool is_left_op);
|
|
|
|
|
1998-02-26 05:46:47 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* Select an ordering operator for the given datatype */
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
1999-12-12 21:51:29 +01:00
|
|
|
any_ordering_op(Oid restype)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
|
|
|
Oid order_opid;
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
order_opid = oper_oid("<", restype, restype, true);
|
|
|
|
if (!OidIsValid(order_opid))
|
1998-12-14 00:54:40 +01:00
|
|
|
elog(ERROR, "Unable to identify an ordering operator '%s' for type '%s'"
|
|
|
|
"\n\tUse an explicit ordering operator or modify the query",
|
1998-09-01 06:40:42 +02:00
|
|
|
"<", typeidTypeName(restype));
|
1997-11-25 23:07:18 +01:00
|
|
|
return order_opid;
|
|
|
|
}
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* given operator tuple, return the operator OID */
|
1997-11-25 23:07:18 +01:00
|
|
|
Oid
|
|
|
|
oprid(Operator op)
|
|
|
|
{
|
1998-11-27 20:52:36 +01:00
|
|
|
return op->t_data->t_oid;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
/* binary_oper_get_candidates()
|
1999-08-26 06:59:15 +02:00
|
|
|
* given opname, find all possible input type pairs for which an operator
|
2000-03-19 01:19:39 +01:00
|
|
|
* named opname exists.
|
|
|
|
* Build a list of the candidate input types.
|
1999-08-26 06:59:15 +02:00
|
|
|
* Returns number of candidates found.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1997-11-26 04:43:18 +01:00
|
|
|
static int
|
1997-11-25 23:07:18 +01:00
|
|
|
binary_oper_get_candidates(char *opname,
|
|
|
|
CandidateList *candidates)
|
|
|
|
{
|
|
|
|
CandidateList current_candidate;
|
|
|
|
Relation pg_operator_desc;
|
|
|
|
HeapScanDesc pg_operator_scan;
|
|
|
|
HeapTuple tup;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_operator oper;
|
1997-11-25 23:07:18 +01:00
|
|
|
int ncandidates = 0;
|
2000-03-19 01:19:39 +01:00
|
|
|
ScanKeyData opKey[2];
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
*candidates = NULL;
|
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&opKey[0], 0,
|
|
|
|
Anum_pg_operator_oprname,
|
1998-04-27 06:08:07 +02:00
|
|
|
F_NAMEEQ,
|
1997-11-25 23:07:18 +01:00
|
|
|
NameGetDatum(opname));
|
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&opKey[1], 0,
|
|
|
|
Anum_pg_operator_oprkind,
|
1998-04-27 06:08:07 +02:00
|
|
|
F_CHAREQ,
|
1997-11-25 23:07:18 +01:00
|
|
|
CharGetDatum('b'));
|
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_operator_desc = heap_openr(OperatorRelationName, AccessShareLock);
|
1998-05-10 01:31:34 +02:00
|
|
|
pg_operator_scan = heap_beginscan(pg_operator_desc,
|
|
|
|
0,
|
1998-09-01 06:40:42 +02:00
|
|
|
SnapshotSelf, /* ??? */
|
1999-09-18 21:08:25 +02:00
|
|
|
2,
|
1998-05-10 01:31:34 +02:00
|
|
|
opKey);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tup = heap_getnext(pg_operator_scan, 0)))
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
oper = (Form_pg_operator) GETSTRUCT(tup);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
current_candidate = (CandidateList) palloc(sizeof(struct _CandidateList));
|
|
|
|
current_candidate->args = (Oid *) palloc(2 * sizeof(Oid));
|
|
|
|
|
|
|
|
current_candidate->args[0] = oper->oprleft;
|
|
|
|
current_candidate->args[1] = oper->oprright;
|
|
|
|
current_candidate->next = *candidates;
|
|
|
|
*candidates = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
heap_endscan(pg_operator_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_operator_desc, AccessShareLock);
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
return ncandidates;
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* binary_oper_get_candidates() */
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
/* oper_select_candidate()
|
|
|
|
* Given the input argtype array and more than one candidate
|
|
|
|
* for the function argtype array, attempt to resolve the conflict.
|
2000-03-19 01:19:39 +01:00
|
|
|
* Returns the selected argtype array if the conflict can be resolved,
|
1998-05-29 16:00:24 +02:00
|
|
|
* otherwise returns NULL.
|
|
|
|
*
|
2000-03-19 01:19:39 +01:00
|
|
|
* By design, this is pretty similar to func_select_candidate in parse_func.c.
|
|
|
|
* However, we can do a couple of extra things here because we know we can
|
|
|
|
* have no more than two args to deal with. Also, the calling convention
|
|
|
|
* is a little different: we must prune away "candidates" that aren't actually
|
|
|
|
* coercion-compatible with the input types, whereas in parse_func.c that
|
|
|
|
* gets done by match_argtypes before func_select_candidate is called.
|
|
|
|
*
|
1998-05-29 16:00:24 +02:00
|
|
|
* 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
|
2000-03-12 00:17:47 +01:00
|
|
|
* operators with types, including allowing type coercions if necessary.
|
1998-05-29 16:00:24 +02:00
|
|
|
* 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
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
1998-10-08 20:30:52 +02:00
|
|
|
static Oid *
|
1998-05-29 16:00:24 +02:00
|
|
|
oper_select_candidate(int nargs,
|
|
|
|
Oid *input_typeids,
|
|
|
|
CandidateList candidates)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
CandidateList current_candidate;
|
|
|
|
CandidateList last_candidate;
|
|
|
|
Oid *current_typeids;
|
2000-12-15 20:22:03 +01:00
|
|
|
Oid current_type;
|
1998-09-01 06:40:42 +02:00
|
|
|
int unknownOids;
|
|
|
|
int i;
|
|
|
|
int ncandidates;
|
|
|
|
int nbestMatch,
|
|
|
|
nmatch;
|
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;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
|
|
|
* First, delete any candidates that cannot actually accept the given
|
|
|
|
* input types, whether directly or by coercion. (Note that
|
|
|
|
* can_coerce_type will assume that UNKNOWN inputs are coercible to
|
|
|
|
* anything, so candidates will not be eliminated on that basis.)
|
|
|
|
*/
|
|
|
|
ncandidates = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
|
|
|
{
|
|
|
|
if (can_coerce_type(nargs, input_typeids, current_candidate->args))
|
|
|
|
{
|
|
|
|
if (last_candidate == NULL)
|
|
|
|
{
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* otherwise, don't bother keeping this one... */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
|
|
|
/* Done if no candidate or only one candidate survives */
|
|
|
|
if (ncandidates == 0)
|
|
|
|
return NULL;
|
|
|
|
if (ncandidates == 1)
|
|
|
|
return candidates->args;
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +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-29 16:00:24 +02:00
|
|
|
ncandidates = 0;
|
|
|
|
nbestMatch = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
1998-05-10 01:31:34 +02:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
if (input_typeids[i] != UNKNOWNOID &&
|
|
|
|
current_typeids[i] == input_typeids[i])
|
1998-05-29 16:00:24 +02:00
|
|
|
nmatch++;
|
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1998-12-08 07:19:15 +01:00
|
|
|
/* take this one as the best choice so far? */
|
1998-05-29 16:00:24 +02:00
|
|
|
if ((nmatch > nbestMatch) || (last_candidate == NULL))
|
|
|
|
{
|
|
|
|
nbestMatch = nmatch;
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
1998-12-08 07:19:15 +01:00
|
|
|
/* no worse than the last choice, so keep this one too? */
|
1998-05-29 16:00:24 +02:00
|
|
|
else if (nmatch == nbestMatch)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
1998-12-08 07:19:15 +01:00
|
|
|
/* otherwise, don't bother keeping this one... */
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
1999-08-26 06:59:15 +02:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
if (ncandidates == 1)
|
|
|
|
return candidates->args;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Still too many candidates? Run through all candidates and keep
|
|
|
|
* those with the most matches on exact types + binary-compatible
|
|
|
|
* types. Keep all candidates if none match.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
1998-05-29 16:00:24 +02:00
|
|
|
ncandidates = 0;
|
|
|
|
nbestMatch = 0;
|
|
|
|
last_candidate = NULL;
|
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
if (input_typeids[i] != UNKNOWNOID)
|
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
if (current_typeids[i] == input_typeids[i] ||
|
|
|
|
IS_BINARY_COMPATIBLE(current_typeids[i],
|
|
|
|
input_typeids[i]))
|
1998-05-29 16:00:24 +02:00
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/* take this one as the best choice so far? */
|
1998-05-29 16:00:24 +02:00
|
|
|
if ((nmatch > nbestMatch) || (last_candidate == NULL))
|
|
|
|
{
|
|
|
|
nbestMatch = nmatch;
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
2000-03-19 01:19:39 +01:00
|
|
|
/* no worse than the last choice, so keep this one too? */
|
1998-05-29 16:00:24 +02:00
|
|
|
else if (nmatch == nbestMatch)
|
|
|
|
{
|
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
1997-11-25 23:07:18 +01:00
|
|
|
ncandidates++;
|
|
|
|
}
|
2000-03-19 01:19:39 +01:00
|
|
|
/* otherwise, don't bother keeping this one... */
|
1998-05-29 16:00:24 +02:00
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1999-08-26 06:59:15 +02:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
if (ncandidates == 1)
|
|
|
|
return candidates->args;
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Still too many candidates? Now look for candidates which are
|
|
|
|
* preferred types at the args that will require coercion. Keep all
|
|
|
|
* candidates if none match.
|
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)
|
1998-05-29 16:00:24 +02:00
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
if (input_typeids[i] != UNKNOWNOID)
|
|
|
|
{
|
|
|
|
current_category = TypeCategory(current_typeids[i]);
|
|
|
|
if (current_typeids[i] == input_typeids[i] ||
|
|
|
|
IsPreferredType(current_category, current_typeids[i]))
|
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nmatch > nbestMatch) || (last_candidate == NULL))
|
|
|
|
{
|
|
|
|
nbestMatch = nmatch;
|
|
|
|
candidates = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates = 1;
|
|
|
|
}
|
|
|
|
else if (nmatch == nbestMatch)
|
2000-02-27 03:48:15 +01:00
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
last_candidate->next = current_candidate;
|
|
|
|
last_candidate = current_candidate;
|
|
|
|
ncandidates++;
|
2000-02-27 03:48:15 +01:00
|
|
|
}
|
1998-05-29 16:00:24 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
|
|
|
|
|
|
|
if (ncandidates == 1)
|
|
|
|
return candidates->args;
|
|
|
|
|
|
|
|
/*
|
2000-04-12 19:17:23 +02:00
|
|
|
* Still too many candidates? Try assigning types for the unknown
|
|
|
|
* columns.
|
2000-03-19 01:19:39 +01:00
|
|
|
*
|
|
|
|
* First try: if we have an unknown and a non-unknown input, see whether
|
2000-04-12 19:17:23 +02:00
|
|
|
* there is a candidate all of whose input types are the same as the
|
|
|
|
* known input type (there can be at most one such candidate). If so,
|
|
|
|
* use that candidate. NOTE that this is cool only because operators
|
|
|
|
* can't have more than 2 args, so taking the last non-unknown as
|
|
|
|
* current_type can yield only one possibility if there is also an
|
|
|
|
* unknown.
|
2000-03-19 01:19:39 +01:00
|
|
|
*/
|
1998-05-29 16:00:24 +02:00
|
|
|
unknownOids = FALSE;
|
|
|
|
current_type = UNKNOWNOID;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
if ((input_typeids[i] != UNKNOWNOID)
|
1998-09-01 06:40:42 +02:00
|
|
|
&& (input_typeids[i] != InvalidOid))
|
1998-05-29 16:00:24 +02:00
|
|
|
current_type = input_typeids[i];
|
|
|
|
else
|
|
|
|
unknownOids = TRUE;
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
if (unknownOids && (current_type != UNKNOWNOID))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
for (current_candidate = candidates;
|
|
|
|
current_candidate != NULL;
|
|
|
|
current_candidate = current_candidate->next)
|
|
|
|
{
|
1999-08-26 06:59:15 +02:00
|
|
|
current_typeids = current_candidate->args;
|
1998-05-29 16:00:24 +02:00
|
|
|
nmatch = 0;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2000-03-12 00:17:47 +01:00
|
|
|
if (current_type == current_typeids[i])
|
1998-05-29 16:00:24 +02:00
|
|
|
nmatch++;
|
|
|
|
}
|
|
|
|
if (nmatch == nargs)
|
2000-03-19 01:19:39 +01:00
|
|
|
return current_typeids;
|
1998-05-29 16:00:24 +02:00
|
|
|
}
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
/*
|
2000-12-15 20:22:03 +01:00
|
|
|
* Second try: same algorithm as for unknown resolution in parse_func.c.
|
2000-03-19 01:19:39 +01:00
|
|
|
*
|
2000-12-15 20:22:03 +01: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.
|
|
|
|
*
|
|
|
|
* If we are able to determine a type category, also notice whether
|
|
|
|
* any of the candidates takes a preferred datatype within the category.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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;
|
1998-05-29 16:00:24 +02:00
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
bool have_conflict;
|
|
|
|
|
|
|
|
if (input_typeids[i] != UNKNOWNOID)
|
|
|
|
continue;
|
|
|
|
resolved_unknowns = true; /* assume we can do it */
|
|
|
|
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)
|
1997-11-25 23:07:18 +01: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-29 16:00:24 +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)
|
1998-05-29 16:00:24 +02: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);
|
1998-05-29 16:00:24 +02:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
else
|
2000-03-12 00:17:47 +01:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
/* Remember conflict, but keep going (might find STRING) */
|
|
|
|
have_conflict = true;
|
2000-03-12 00:17:47 +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)
|
|
|
|
{
|
|
|
|
bool keepit = true;
|
|
|
|
|
|
|
|
current_typeids = current_candidate->args;
|
|
|
|
for (i = 0; i < nargs; i++)
|
|
|
|
{
|
|
|
|
if (input_typeids[i] != UNKNOWNOID)
|
|
|
|
continue;
|
|
|
|
current_type = current_typeids[i];
|
|
|
|
current_category = TypeCategory(current_type);
|
|
|
|
if (current_category != slot_category[i])
|
1998-05-29 16:00:24 +02:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
keepit = false;
|
|
|
|
break;
|
2000-03-12 00:17:47 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
if (slot_has_preferred_type[i] &&
|
|
|
|
!IsPreferredType(current_category, current_type))
|
2000-03-12 00:17:47 +01:00
|
|
|
{
|
2000-12-15 20:22:03 +01:00
|
|
|
keepit = false;
|
|
|
|
break;
|
1998-05-29 16:00:24 +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;
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-12-15 20:22:03 +01:00
|
|
|
if (last_candidate) /* terminate rebuilt list */
|
|
|
|
last_candidate->next = NULL;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2000-12-15 20:22:03 +01:00
|
|
|
if (ncandidates == 1)
|
|
|
|
return candidates->args;
|
|
|
|
|
|
|
|
return NULL; /* failed to determine a unique candidate */
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* oper_select_candidate() */
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
/* oper_exact()
|
2000-11-16 23:30:52 +01:00
|
|
|
* Given operator, types of arg1 and arg2, return oper struct or NULL.
|
|
|
|
*
|
|
|
|
* NOTE: on success, the returned object is a syscache entry. The caller
|
|
|
|
* must ReleaseSysCache() the entry when done with it.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1999-08-24 01:48:39 +02:00
|
|
|
static Operator
|
|
|
|
oper_exact(char *op, Oid arg1, Oid arg2)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-05-29 16:00:24 +02:00
|
|
|
HeapTuple tup;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
/* Unspecified type for one of the arguments? then use the other */
|
1998-09-01 06:40:42 +02:00
|
|
|
if ((arg1 == UNKNOWNOID) && (arg2 != InvalidOid))
|
|
|
|
arg1 = arg2;
|
|
|
|
else if ((arg2 == UNKNOWNOID) && (arg1 != InvalidOid))
|
|
|
|
arg2 = arg1;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(arg1),
|
|
|
|
ObjectIdGetDatum(arg2),
|
|
|
|
CharGetDatum('b'));
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-08-24 01:48:39 +02:00
|
|
|
return (Operator) tup;
|
2000-11-16 23:30:52 +01:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
|
|
|
|
/* oper_inexact()
|
2000-11-16 23:30:52 +01:00
|
|
|
* Given operator, types of arg1 and arg2, return oper struct or NULL.
|
|
|
|
*
|
|
|
|
* NOTE: on success, the returned object is a syscache entry. The caller
|
|
|
|
* must ReleaseSysCache() the entry when done with it.
|
1998-05-10 01:31:34 +02:00
|
|
|
*/
|
1999-08-24 01:48:39 +02:00
|
|
|
static Operator
|
|
|
|
oper_inexact(char *op, Oid arg1, Oid arg2)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
CandidateList candidates;
|
|
|
|
int ncandidates;
|
|
|
|
Oid *targetOids;
|
|
|
|
Oid inputOids[2];
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
/* Unspecified type for one of the arguments? then use the other */
|
|
|
|
if (arg2 == InvalidOid)
|
1997-11-25 23:07:18 +01:00
|
|
|
arg2 = arg1;
|
1998-05-10 01:31:34 +02:00
|
|
|
if (arg1 == InvalidOid)
|
1997-11-25 23:07:18 +01:00
|
|
|
arg1 = arg2;
|
|
|
|
|
1999-08-26 06:59:15 +02:00
|
|
|
ncandidates = binary_oper_get_candidates(op, &candidates);
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1999-08-24 01:48:39 +02:00
|
|
|
/* No operators found? Then return null... */
|
1998-05-29 16:00:24 +02:00
|
|
|
if (ncandidates == 0)
|
1998-09-01 05:29:17 +02:00
|
|
|
return NULL;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
/* Or found exactly one? Then proceed... */
|
|
|
|
else if (ncandidates == 1)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(candidates->args[0]),
|
|
|
|
ObjectIdGetDatum(candidates->args[1]),
|
|
|
|
CharGetDatum('b'));
|
1998-05-29 16:00:24 +02:00
|
|
|
Assert(HeapTupleIsValid(tup));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, multiple operators of the desired types found... */
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inputOids[0] = arg1;
|
|
|
|
inputOids[1] = arg2;
|
|
|
|
targetOids = oper_select_candidate(2, inputOids, candidates);
|
|
|
|
if (targetOids != NULL)
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(targetOids[0]),
|
|
|
|
ObjectIdGetDatum(targetOids[1]),
|
|
|
|
CharGetDatum('b'));
|
1998-05-29 16:00:24 +02:00
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
else
|
1998-05-29 16:00:24 +02:00
|
|
|
tup = NULL;
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Operator) tup;
|
2000-11-16 23:30:52 +01:00
|
|
|
}
|
1998-05-29 16:00:24 +02:00
|
|
|
|
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* oper() -- search for a binary operator
|
|
|
|
* Given operator name, types of arg1 and arg2, return oper struct.
|
|
|
|
*
|
|
|
|
* If no matching operator found, return NULL if noError is true,
|
|
|
|
* raise an error if it is false.
|
|
|
|
*
|
|
|
|
* NOTE: on success, the returned object is a syscache entry. The caller
|
|
|
|
* must ReleaseSysCache() the entry when done with it.
|
1998-05-29 16:00:24 +02:00
|
|
|
*/
|
|
|
|
Operator
|
2000-11-16 23:30:52 +01:00
|
|
|
oper(char *opname, Oid ltypeId, Oid rtypeId, bool noError)
|
1998-05-29 16:00:24 +02:00
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
HeapTuple tup;
|
1998-05-29 16:00:24 +02:00
|
|
|
|
|
|
|
/* check for exact match on this operator... */
|
1999-08-24 01:48:39 +02:00
|
|
|
if (HeapTupleIsValid(tup = oper_exact(opname, ltypeId, rtypeId)))
|
2000-11-16 23:30:52 +01:00
|
|
|
return (Operator) tup;
|
|
|
|
|
1998-05-29 16:00:24 +02:00
|
|
|
/* try to find a match on likely candidates... */
|
2000-11-16 23:30:52 +01:00
|
|
|
if (HeapTupleIsValid(tup = oper_inexact(opname, ltypeId, rtypeId)))
|
|
|
|
return (Operator) tup;
|
|
|
|
|
|
|
|
if (!noError)
|
1999-08-24 01:48:39 +02:00
|
|
|
op_error(opname, ltypeId, rtypeId);
|
1998-05-29 16:00:24 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
return (Operator) NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* oper_oid() -- get OID of a binary operator
|
|
|
|
*
|
|
|
|
* This is a convenience routine that extracts only the operator OID
|
|
|
|
* from the result of oper(). InvalidOid is returned if the lookup
|
|
|
|
* fails and noError is true.
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
oper_oid(char *op, Oid arg1, Oid arg2, bool noError)
|
|
|
|
{
|
|
|
|
Operator optup;
|
|
|
|
Oid result;
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
optup = oper(op, arg1, arg2, noError);
|
|
|
|
if (optup != NULL)
|
|
|
|
{
|
|
|
|
result = oprid(optup);
|
|
|
|
ReleaseSysCache(optup);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return InvalidOid;
|
|
|
|
}
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
/* unary_oper_get_candidates()
|
2000-03-19 01:19:39 +01:00
|
|
|
* given opname, find all possible types for which
|
|
|
|
* a right/left unary operator named opname exists.
|
|
|
|
* Build a list of the candidate input types.
|
|
|
|
* Returns number of candidates found.
|
1997-11-25 23:07:18 +01:00
|
|
|
*/
|
1997-11-26 04:43:18 +01:00
|
|
|
static int
|
2000-03-19 01:19:39 +01:00
|
|
|
unary_oper_get_candidates(char *opname,
|
1997-11-25 23:07:18 +01:00
|
|
|
CandidateList *candidates,
|
|
|
|
char rightleft)
|
|
|
|
{
|
|
|
|
CandidateList current_candidate;
|
|
|
|
Relation pg_operator_desc;
|
|
|
|
HeapScanDesc pg_operator_scan;
|
|
|
|
HeapTuple tup;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_operator oper;
|
1997-11-25 23:07:18 +01:00
|
|
|
int ncandidates = 0;
|
2000-03-19 01:19:39 +01:00
|
|
|
ScanKeyData opKey[2];
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
*candidates = NULL;
|
|
|
|
|
2000-03-19 01:19:39 +01:00
|
|
|
ScanKeyEntryInitialize(&opKey[0], 0,
|
|
|
|
Anum_pg_operator_oprname,
|
|
|
|
F_NAMEEQ,
|
|
|
|
NameGetDatum(opname));
|
|
|
|
|
|
|
|
ScanKeyEntryInitialize(&opKey[1], 0,
|
|
|
|
Anum_pg_operator_oprkind,
|
|
|
|
F_CHAREQ,
|
|
|
|
CharGetDatum(rightleft));
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-09-18 21:08:25 +02:00
|
|
|
pg_operator_desc = heap_openr(OperatorRelationName, AccessShareLock);
|
1997-11-25 23:07:18 +01:00
|
|
|
pg_operator_scan = heap_beginscan(pg_operator_desc,
|
|
|
|
0,
|
1998-09-01 06:40:42 +02:00
|
|
|
SnapshotSelf, /* ??? */
|
1997-11-25 23:07:18 +01:00
|
|
|
2,
|
|
|
|
opKey);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
while (HeapTupleIsValid(tup = heap_getnext(pg_operator_scan, 0)))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-03-19 01:19:39 +01:00
|
|
|
oper = (Form_pg_operator) GETSTRUCT(tup);
|
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
current_candidate = (CandidateList) palloc(sizeof(struct _CandidateList));
|
|
|
|
current_candidate->args = (Oid *) palloc(sizeof(Oid));
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-08-19 04:04:17 +02:00
|
|
|
if (rightleft == 'r')
|
|
|
|
current_candidate->args[0] = oper->oprleft;
|
|
|
|
else
|
|
|
|
current_candidate->args[0] = oper->oprright;
|
|
|
|
current_candidate->next = *candidates;
|
|
|
|
*candidates = current_candidate;
|
|
|
|
ncandidates++;
|
|
|
|
}
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
heap_endscan(pg_operator_scan);
|
1999-09-18 21:08:25 +02:00
|
|
|
heap_close(pg_operator_desc, AccessShareLock);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
|
|
|
return ncandidates;
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* unary_oper_get_candidates() */
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* Given unary right operator (operator on right), return oper struct
|
|
|
|
*
|
|
|
|
* Always raises error on failure.
|
|
|
|
*
|
|
|
|
* NOTE: on success, the returned object is a syscache entry. The caller
|
|
|
|
* must ReleaseSysCache() the entry when done with it.
|
|
|
|
*/
|
1997-11-25 23:07:18 +01:00
|
|
|
Operator
|
|
|
|
right_oper(char *op, Oid arg)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
CandidateList candidates;
|
|
|
|
int ncandidates;
|
|
|
|
Oid *targetOid;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2000-02-27 03:48:15 +01:00
|
|
|
/* Try for exact match */
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(arg),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
CharGetDatum('r'));
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-02-27 03:48:15 +01:00
|
|
|
/* Try for inexact matches */
|
2000-03-19 01:19:39 +01:00
|
|
|
ncandidates = unary_oper_get_candidates(op, &candidates, 'r');
|
1997-11-25 23:07:18 +01:00
|
|
|
if (ncandidates == 0)
|
2000-02-27 03:48:15 +01:00
|
|
|
unary_op_error(op, arg, FALSE);
|
1997-11-25 23:07:18 +01:00
|
|
|
else if (ncandidates == 1)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(candidates->args[0]),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
CharGetDatum('r'));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-09-16 16:22:22 +02:00
|
|
|
targetOid = oper_select_candidate(1, &arg, candidates);
|
1998-05-10 01:31:34 +02:00
|
|
|
if (targetOid != NULL)
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(targetOid[0]),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
CharGetDatum('r'));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-02-27 03:48:15 +01:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
unary_op_error(op, arg, FALSE);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-02-27 03:48:15 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Operator) tup;
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* right_oper() */
|
1998-05-10 01:31:34 +02:00
|
|
|
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
/* Given unary left operator (operator on left), return oper struct
|
|
|
|
*
|
|
|
|
* Always raises error on failure.
|
|
|
|
*
|
|
|
|
* NOTE: on success, the returned object is a syscache entry. The caller
|
|
|
|
* must ReleaseSysCache() the entry when done with it.
|
|
|
|
*/
|
1997-11-25 23:07:18 +01:00
|
|
|
Operator
|
|
|
|
left_oper(char *op, Oid arg)
|
|
|
|
{
|
1998-09-01 06:40:42 +02:00
|
|
|
HeapTuple tup;
|
|
|
|
CandidateList candidates;
|
|
|
|
int ncandidates;
|
|
|
|
Oid *targetOid;
|
1998-05-10 01:31:34 +02:00
|
|
|
|
2000-02-27 03:48:15 +01:00
|
|
|
/* Try for exact match */
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
ObjectIdGetDatum(arg),
|
|
|
|
CharGetDatum('l'));
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
1997-11-25 23:07:18 +01:00
|
|
|
{
|
2000-02-27 03:48:15 +01:00
|
|
|
/* Try for inexact matches */
|
2000-03-19 01:19:39 +01:00
|
|
|
ncandidates = unary_oper_get_candidates(op, &candidates, 'l');
|
1997-11-25 23:07:18 +01:00
|
|
|
if (ncandidates == 0)
|
2000-02-27 03:48:15 +01:00
|
|
|
unary_op_error(op, arg, TRUE);
|
1997-11-25 23:07:18 +01:00
|
|
|
else if (ncandidates == 1)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
ObjectIdGetDatum(candidates->args[0]),
|
|
|
|
CharGetDatum('l'));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-09-16 16:22:22 +02:00
|
|
|
targetOid = oper_select_candidate(1, &arg, candidates);
|
|
|
|
if (targetOid != NULL)
|
2000-11-16 23:30:52 +01:00
|
|
|
tup = SearchSysCache(OPERNAME,
|
|
|
|
PointerGetDatum(op),
|
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
ObjectIdGetDatum(targetOid[0]),
|
|
|
|
CharGetDatum('l'));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-02-27 03:48:15 +01:00
|
|
|
|
|
|
|
if (!HeapTupleIsValid(tup))
|
|
|
|
unary_op_error(op, arg, TRUE);
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-02-27 03:48:15 +01:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Operator) tup;
|
1998-09-01 06:40:42 +02:00
|
|
|
} /* left_oper() */
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1998-05-10 01:31:34 +02:00
|
|
|
|
|
|
|
/* op_error()
|
1997-11-25 23:07:18 +01:00
|
|
|
* Give a somewhat useful error message when the operator for two types
|
|
|
|
* is not found.
|
|
|
|
*/
|
1997-11-26 04:43:18 +01:00
|
|
|
static void
|
1997-11-25 23:07:18 +01:00
|
|
|
op_error(char *op, Oid arg1, Oid arg2)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!typeidIsValid(arg1))
|
1998-05-10 01:31:34 +02:00
|
|
|
elog(ERROR, "Left hand side of operator '%s' has an unknown type"
|
1998-05-29 16:00:24 +02:00
|
|
|
"\n\tProbably a bad attribute name", op);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!typeidIsValid(arg2))
|
1998-05-10 01:31:34 +02:00
|
|
|
elog(ERROR, "Right hand side of operator %s has an unknown type"
|
1998-05-29 16:00:24 +02:00
|
|
|
"\n\tProbably a bad attribute name", op);
|
1997-11-25 23:07:18 +01:00
|
|
|
|
1999-08-24 01:48:39 +02:00
|
|
|
elog(ERROR, "Unable to identify an operator '%s' for types '%s' and '%s'"
|
|
|
|
"\n\tYou will have to retype this query using an explicit cast",
|
2000-11-16 23:30:52 +01:00
|
|
|
op, typeidTypeName(arg1), typeidTypeName(arg2));
|
1997-11-25 23:07:18 +01:00
|
|
|
}
|
2000-02-27 03:48:15 +01:00
|
|
|
|
|
|
|
/* unary_op_error()
|
|
|
|
* Give a somewhat useful error message when the operator for one type
|
|
|
|
* is not found.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
unary_op_error(char *op, Oid arg, bool is_left_op)
|
|
|
|
{
|
2000-11-16 23:30:52 +01:00
|
|
|
if (!typeidIsValid(arg))
|
2000-02-27 03:48:15 +01:00
|
|
|
elog(ERROR, "Argument of %s operator '%s' has an unknown type"
|
|
|
|
"\n\tProbably a bad attribute name",
|
|
|
|
(is_left_op ? "left" : "right"),
|
|
|
|
op);
|
|
|
|
|
|
|
|
elog(ERROR, "Unable to identify a %s operator '%s' for type '%s'"
|
2000-03-18 20:53:54 +01:00
|
|
|
"\n\tYou may need to add parentheses or an explicit cast",
|
2000-02-27 03:48:15 +01:00
|
|
|
(is_left_op ? "left" : "right"),
|
2000-11-16 23:30:52 +01:00
|
|
|
op, typeidTypeName(arg));
|
2000-02-27 03:48:15 +01:00
|
|
|
}
|