1 /*-------------------------------------------------------------------------
4 * handle type coercions/conversions for parser
6 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.72 2002/05/12 23:43:03 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_proc.h"
18 #include "nodes/makefuncs.h"
19 #include "optimizer/clauses.h"
20 #include "parser/parse_coerce.h"
21 #include "parser/parse_expr.h"
22 #include "parser/parse_func.h"
23 #include "parser/parse_type.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 #include "utils/syscache.h"
29 Oid DemoteType(Oid inType);
30 Oid PromoteTypeToNext(Oid inType);
32 static Oid PreferredType(CATEGORY category, Oid type);
33 static Node *build_func_call(Oid funcid, Oid rettype, List *args);
34 static Oid find_coercion_function(Oid targetTypeId, Oid inputTypeId,
35 Oid secondArgType, bool isExplicit);
39 * Convert a function argument to a different type.
42 coerce_type(ParseState *pstate, Node *node, Oid inputTypeId,
43 Oid targetTypeId, int32 atttypmod, bool isExplicit)
47 if (targetTypeId == inputTypeId ||
48 targetTypeId == InvalidOid ||
51 /* no conversion needed */
54 else if (inputTypeId == UNKNOWNOID && IsA(node, Const))
57 * Input is a string constant with previously undetermined type.
58 * Apply the target type's typinput function to it to produce a
59 * constant of the target type.
61 * NOTE: this case cannot be folded together with the other
62 * constant-input case, since the typinput function does not
63 * necessarily behave the same as a type conversion function. For
64 * example, int4's typinput function will reject "1.2", whereas
65 * float-to-int type conversion will round to integer.
67 * XXX if the typinput function is not cachable, we really ought to
68 * postpone evaluation of the function call until runtime. But
69 * there is no way to represent a typinput function call as an
70 * expression tree, because C-string values are not Datums.
72 Const *con = (Const *) node;
73 Const *newcon = makeNode(Const);
74 Type targetType = typeidType(targetTypeId);
76 newcon->consttype = targetTypeId;
77 newcon->constlen = typeLen(targetType);
78 newcon->constbyval = typeByVal(targetType);
79 newcon->constisnull = con->constisnull;
80 newcon->constisset = false;
82 if (!con->constisnull)
84 char *val = DatumGetCString(DirectFunctionCall1(unknownout,
87 newcon->constvalue = stringTypeDatum(targetType, val, atttypmod);
91 ReleaseSysCache(targetType);
93 result = (Node *) newcon;
95 else if (IsBinaryCompatible(inputTypeId, targetTypeId))
98 * We don't really need to do a conversion, but we do need to
99 * attach a RelabelType node so that the expression will be seen
100 * to have the intended type when inspected by higher-level code.
102 * XXX could we label result with exprTypmod(node) instead of
103 * default -1 typmod, to save a possible length-coercion later?
104 * Would work if both types have same interpretation of typmod,
105 * which is likely but not certain.
107 result = (Node *) makeRelabelType(node, targetTypeId, -1);
109 else if (typeInheritsFrom(inputTypeId, targetTypeId))
112 * Input class type is a subclass of target, so nothing to do
113 * --- except relabel the type. This is binary compatibility
116 result = (Node *) makeRelabelType(node, targetTypeId, -1);
121 * Otherwise, find the appropriate type conversion function
122 * (caller should have determined that there is one), and generate
123 * an expression tree representing run-time application of the
124 * conversion function.
126 * For domains, we use the coercion function for the base type.
128 Oid baseTypeId = getBaseType(targetTypeId);
131 funcId = find_coercion_function(baseTypeId,
132 getBaseType(inputTypeId),
135 if (!OidIsValid(funcId))
136 elog(ERROR, "coerce_type: no conversion function from %s to %s",
137 format_type_be(inputTypeId), format_type_be(targetTypeId));
139 result = build_func_call(funcId, baseTypeId, makeList1(node));
141 /* if domain, relabel with domain type ID */
142 if (targetTypeId != baseTypeId)
143 result = (Node *) makeRelabelType(result, targetTypeId, -1);
146 * If the input is a constant, apply the type conversion function
147 * now instead of delaying to runtime. (We could, of course, just
148 * leave this to be done during planning/optimization; but it's a
149 * very frequent special case, and we save cycles in the rewriter
150 * if we fold the expression now.)
152 * Note that no folding will occur if the conversion function is not
153 * marked 'iscachable'.
155 * HACK: if constant is NULL, don't fold it here. This is needed by
156 * make_subplan(), which calls this routine on placeholder Const
157 * nodes that mustn't be collapsed. (It'd be a lot cleaner to
158 * make a separate node type for that purpose...)
160 if (IsA(node, Const) &&
161 !((Const *) node)->constisnull)
162 result = eval_const_expressions(result);
170 * Can input_typeids be coerced to func_typeids?
172 * There are a few types which are known apriori to be convertible.
173 * We will check for those cases first, and then look for possible
174 * conversion functions.
176 * We must be told whether this is an implicit or explicit coercion
177 * (explicit being a CAST construct, explicit function call, etc).
178 * We will accept a wider set of coercion cases for an explicit coercion.
181 * This uses the same mechanism as the CAST() SQL construct in gram.y.
184 can_coerce_type(int nargs, Oid *input_typeids, Oid *func_typeids,
189 /* run through argument list... */
190 for (i = 0; i < nargs; i++)
192 Oid inputTypeId = input_typeids[i];
193 Oid targetTypeId = func_typeids[i];
196 /* no problem if same type */
197 if (inputTypeId == targetTypeId)
201 * one of the known-good transparent conversions? then drop
204 if (IsBinaryCompatible(inputTypeId, targetTypeId))
207 /* don't know what to do for the output type? then quit... */
208 if (targetTypeId == InvalidOid)
210 /* don't know what to do for the input type? then quit... */
211 if (inputTypeId == InvalidOid)
215 * If input is an untyped string constant, assume we can convert
216 * it to anything except a class type.
218 if (inputTypeId == UNKNOWNOID)
220 if (ISCOMPLEX(targetTypeId))
226 * If input is a class type that inherits from target, no problem
228 if (typeInheritsFrom(inputTypeId, targetTypeId))
231 /* don't choke on references to no-longer-existing types */
232 if (!typeidIsValid(inputTypeId))
234 if (!typeidIsValid(targetTypeId))
238 * Else, try for run-time conversion using functions: look for a
239 * single-argument function named with the target type name and
240 * accepting the source type.
242 * If either type is a domain, use its base type instead.
244 funcId = find_coercion_function(getBaseType(targetTypeId),
245 getBaseType(inputTypeId),
248 if (!OidIsValid(funcId))
255 /* coerce_type_typmod()
256 * Force a value to a particular typmod, if meaningful and possible.
258 * This is applied to values that are going to be stored in a relation
259 * (where we have an atttypmod for the column) as well as values being
260 * explicitly CASTed (where the typmod comes from the target type spec).
262 * The caller must have already ensured that the value is of the correct
263 * type, typically by applying coerce_type.
265 * If the target column type possesses a function named for the type
266 * and having parameter signature (columntype, int4), we assume that
267 * the type requires coercion to its own length and that the said
268 * function should be invoked to do that.
270 * "bpchar" (ie, char(N)) and "numeric" are examples of such types.
273 coerce_type_typmod(ParseState *pstate, Node *node,
274 Oid targetTypeId, int32 atttypmod)
280 * A negative typmod is assumed to mean that no coercion is wanted.
282 if (atttypmod < 0 || atttypmod == exprTypmod(node))
285 /* If given type is a domain, use base type instead */
286 baseTypeId = getBaseType(targetTypeId);
288 /* Note this is always implicit coercion */
289 funcId = find_coercion_function(baseTypeId, baseTypeId, INT4OID, false);
291 if (OidIsValid(funcId))
295 cons = makeConst(INT4OID,
297 Int32GetDatum(atttypmod),
303 node = build_func_call(funcId, baseTypeId, makeList2(node, cons));
305 /* relabel if it's domain case */
306 if (targetTypeId != baseTypeId)
307 node = (Node *) makeRelabelType(node, targetTypeId, atttypmod);
314 /* coerce_to_boolean()
315 * Coerce an argument of a construct that requires boolean input
316 * (AND, OR, NOT, etc). Also check that input is not a set.
318 * Returns the possibly-transformed node tree.
321 coerce_to_boolean(Node *node, const char *constructName)
323 Oid inputTypeId = exprType(node);
326 if (inputTypeId != BOOLOID)
328 targetTypeId = BOOLOID;
329 if (!can_coerce_type(1, &inputTypeId, &targetTypeId, false))
331 /* translator: first %s is name of a SQL construct, eg WHERE */
332 elog(ERROR, "Argument of %s must be type boolean, not type %s",
333 constructName, format_type_be(inputTypeId));
335 node = coerce_type(NULL, node, inputTypeId, targetTypeId, -1,
339 if (expression_returns_set(node))
341 /* translator: %s is name of a SQL construct, eg WHERE */
342 elog(ERROR, "Argument of %s must not be a set function",
350 /* select_common_type()
351 * Determine the common supertype of a list of input expression types.
352 * This is used for determining the output type of CASE and UNION
355 * typeids is a nonempty integer list of type OIDs. Note that earlier items
356 * in the list will be preferred if there is doubt.
357 * 'context' is a phrase to use in the error message if we fail to select
360 * XXX this code is WRONG, since (for example) given the input (int4,int8)
361 * it will select int4, whereas according to SQL92 clause 9.3 the correct
362 * answer is clearly int8. To fix this we need a notion of a promotion
363 * hierarchy within type categories --- something more complete than
364 * just a single preferred type.
367 select_common_type(List *typeids, const char *context)
373 Assert(typeids != NIL);
374 ptype = (Oid) lfirsti(typeids);
375 pcategory = TypeCategory(ptype);
376 foreach(l, lnext(typeids))
378 Oid ntype = (Oid) lfirsti(l);
380 /* move on to next one if no new information... */
381 if ((ntype != InvalidOid) && (ntype != UNKNOWNOID) && (ntype != ptype))
383 if ((ptype == InvalidOid) || ptype == UNKNOWNOID)
385 /* so far, only nulls so take anything... */
387 pcategory = TypeCategory(ptype);
389 else if (TypeCategory(ntype) != pcategory)
392 * both types in different categories? then not much
395 elog(ERROR, "%s types \"%s\" and \"%s\" not matched",
396 context, typeidTypeName(ptype), typeidTypeName(ntype));
398 else if (IsPreferredType(pcategory, ntype)
399 && !IsPreferredType(pcategory, ptype)
400 && can_coerce_type(1, &ptype, &ntype, false))
403 * new one is preferred and can convert? then take it...
406 pcategory = TypeCategory(ptype);
412 * If all the inputs were UNKNOWN type --- ie, unknown-type literals
413 * --- then resolve as type TEXT. This situation comes up with
414 * constructs like SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END);
415 * SELECT 'foo' UNION SELECT 'bar'; It might seem desirable to leave
416 * the construct's output type as UNKNOWN, but that really doesn't
417 * work, because we'd probably end up needing a runtime coercion from
418 * UNKNOWN to something else, and we usually won't have it. We need
419 * to coerce the unknown literals while they are still literals, so a
420 * decision has to be made now.
422 if (ptype == UNKNOWNOID)
428 /* coerce_to_common_type()
429 * Coerce an expression to the given type.
431 * This is used following select_common_type() to coerce the individual
432 * expressions to the desired type. 'context' is a phrase to use in the
433 * error message if we fail to coerce.
435 * NOTE: pstate may be NULL.
438 coerce_to_common_type(ParseState *pstate, Node *node,
442 Oid inputTypeId = exprType(node);
444 if (inputTypeId == targetTypeId)
445 return node; /* no work */
446 if (can_coerce_type(1, &inputTypeId, &targetTypeId, false))
447 node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1,
451 elog(ERROR, "%s unable to convert to type \"%s\"",
452 context, typeidTypeName(targetTypeId));
459 * Assign a category to the specified OID.
460 * XXX This should be moved to system catalog lookups
461 * to allow for better type extensibility.
462 * - thomas 2001-09-30
465 TypeCategory(Oid inType)
472 result = BOOLEAN_TYPE;
480 result = STRING_TYPE;
485 result = BITSTRING_TYPE;
490 case (REGPROCEDUREOID):
492 case (REGOPERATOROID):
502 result = NUMERIC_TYPE;
510 case (TIMESTAMPTZOID):
511 result = DATETIME_TYPE;
517 result = TIMESPAN_TYPE;
527 result = GEOMETRIC_TYPE;
532 result = NETWORK_TYPE;
537 result = UNKNOWN_TYPE;
545 } /* TypeCategory() */
548 /* IsBinaryCompatible()
549 * Check if two types are binary-compatible.
551 * This notion allows us to cheat and directly exchange values without
552 * going through the trouble of calling a conversion function.
554 * XXX This should be moved to system catalog lookups
555 * to allow for better type extensibility.
558 #define TypeIsTextGroup(t) \
560 (t) == BPCHAROID || \
563 /* Notice OidGroup is a subset of Int4GroupA */
564 #define TypeIsOidGroup(t) \
566 (t) == REGPROCOID || \
567 (t) == REGPROCEDUREOID || \
568 (t) == REGOPEROID || \
569 (t) == REGOPERATOROID || \
570 (t) == REGCLASSOID || \
574 * INT4 is binary-compatible with many types, but we don't want to allow
575 * implicit coercion directly between, say, OID and AbsTime. So we subdivide
578 #define TypeIsInt4GroupA(t) \
582 #define TypeIsInt4GroupB(t) \
586 #define TypeIsInt4GroupC(t) \
590 #define TypeIsInetGroup(t) \
594 #define TypeIsBitGroup(t) \
600 DirectlyBinaryCompatible(Oid type1, Oid type2)
604 if (TypeIsTextGroup(type1) && TypeIsTextGroup(type2))
606 if (TypeIsInt4GroupA(type1) && TypeIsInt4GroupA(type2))
608 if (TypeIsInt4GroupB(type1) && TypeIsInt4GroupB(type2))
610 if (TypeIsInt4GroupC(type1) && TypeIsInt4GroupC(type2))
612 if (TypeIsInetGroup(type1) && TypeIsInetGroup(type2))
614 if (TypeIsBitGroup(type1) && TypeIsBitGroup(type2))
621 IsBinaryCompatible(Oid type1, Oid type2)
623 if (DirectlyBinaryCompatible(type1, type2))
626 * Perhaps the types are domains; if so, look at their base types
628 if (OidIsValid(type1))
629 type1 = getBaseType(type1);
630 if (OidIsValid(type2))
631 type2 = getBaseType(type2);
632 if (DirectlyBinaryCompatible(type1, type2))
639 * Check if this type is a preferred type.
640 * XXX This should be moved to system catalog lookups
641 * to allow for better type extensibility.
642 * - thomas 2001-09-30
645 IsPreferredType(CATEGORY category, Oid type)
647 return (type == PreferredType(category, type));
648 } /* IsPreferredType() */
652 * Return the preferred type OID for the specified category.
653 * XXX This should be moved to system catalog lookups
654 * to allow for better type extensibility.
655 * - thomas 2001-09-30
658 PreferredType(CATEGORY category, Oid type)
672 case (BITSTRING_TYPE):
677 if (TypeIsOidGroup(type))
679 else if (type == NUMERICOID)
685 case (DATETIME_TYPE):
687 result = TIMESTAMPOID;
689 result = TIMESTAMPTZOID;
692 case (TIMESPAN_TYPE):
693 result = INTERVALOID;
700 case (GEOMETRIC_TYPE):
710 } /* PreferredType() */
713 * find_coercion_function
714 * Look for a coercion function between two types.
716 * A coercion function must be named after (the internal name of) its
717 * result type, and must accept exactly the specified input type. We
718 * also require it to be defined in the same namespace as its result type.
719 * Furthermore, unless we are doing explicit coercion the function must
720 * be marked as usable for implicit coercion --- this allows coercion
721 * functions to be provided that aren't implicitly invokable.
723 * This routine is also used to look for length-coercion functions, which
724 * are similar but accept a second argument. secondArgType is the type
725 * of the second argument (normally INT4OID), or InvalidOid if we are
726 * looking for a regular coercion function.
728 * If a function is found, return its pg_proc OID; else return InvalidOid.
731 find_coercion_function(Oid targetTypeId, Oid inputTypeId, Oid secondArgType,
734 Oid funcid = InvalidOid;
738 Oid oid_array[FUNC_MAX_ARGS];
742 targetType = typeidType(targetTypeId);
743 typname = NameStr(((Form_pg_type) GETSTRUCT(targetType))->typname);
744 typnamespace = ((Form_pg_type) GETSTRUCT(targetType))->typnamespace;
746 MemSet(oid_array, 0, FUNC_MAX_ARGS * sizeof(Oid));
747 oid_array[0] = inputTypeId;
748 if (OidIsValid(secondArgType))
750 oid_array[1] = secondArgType;
756 ftup = SearchSysCache(PROCNAMENSP,
757 CStringGetDatum(typname),
758 Int16GetDatum(nargs),
759 PointerGetDatum(oid_array),
760 ObjectIdGetDatum(typnamespace));
761 if (HeapTupleIsValid(ftup))
763 Form_pg_proc pform = (Form_pg_proc) GETSTRUCT(ftup);
765 /* Make sure the function's result type is as expected */
766 if (pform->prorettype == targetTypeId && !pform->proretset &&
769 /* If needed, make sure it can be invoked implicitly */
770 if (isExplicit || pform->proimplicit)
773 funcid = ftup->t_data->t_oid;
776 ReleaseSysCache(ftup);
779 ReleaseSysCache(targetType);
784 * Build an expression tree representing a function call.
786 * The argument expressions must have been transformed already.
789 build_func_call(Oid funcid, Oid rettype, List *args)
794 funcnode = makeNode(Func);
795 funcnode->funcid = funcid;
796 funcnode->funcresulttype = rettype;
797 funcnode->funcretset = false; /* only possible case here */
798 funcnode->func_fcache = NULL;
800 expr = makeNode(Expr);
801 expr->typeOid = rettype;
802 expr->opType = FUNC_EXPR;
803 expr->oper = (Node *) funcnode;
806 return (Node *) expr;