OSDN Git Service

Add separate type category for bit string types, allowing mixed bit/varbit
[pg-rex/syncrep.git] / src / backend / parser / parse_coerce.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_coerce.c
4  *              handle type coercions/conversions for parser
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        $Header: /cvsroot/pgsql/src/backend/parser/parse_coerce.c,v 2.50 2000/11/17 19:57:47 petere Exp $
12  *
13  *-------------------------------------------------------------------------
14  */
15 #include "postgres.h"
16
17 #include "catalog/pg_proc.h"
18 #include "optimizer/clauses.h"
19 #include "parser/parse_coerce.h"
20 #include "parser/parse_expr.h"
21 #include "parser/parse_func.h"
22 #include "parser/parse_type.h"
23 #include "utils/builtins.h"
24 #include "utils/syscache.h"
25
26 Oid                     DemoteType(Oid inType);
27 Oid                     PromoteTypeToNext(Oid inType);
28
29 static Oid      PreferredType(CATEGORY category, Oid type);
30
31
32 /* coerce_type()
33  * Convert a function argument to a different type.
34  */
35 Node *
36 coerce_type(ParseState *pstate, Node *node, Oid inputTypeId,
37                         Oid targetTypeId, int32 atttypmod)
38 {
39         Node       *result;
40
41         if (targetTypeId == inputTypeId ||
42                 targetTypeId == InvalidOid ||
43                 node == NULL)
44         {
45                 /* no conversion needed */
46                 result = node;
47         }
48         else if (inputTypeId == UNKNOWNOID && IsA(node, Const))
49         {
50
51                 /*
52                  * Input is a string constant with previously undetermined type.
53                  * Apply the target type's typinput function to it to produce a
54                  * constant of the target type.
55                  *
56                  * NOTE: this case cannot be folded together with the other
57                  * constant-input case, since the typinput function does not
58                  * necessarily behave the same as a type conversion function. For
59                  * example, int4's typinput function will reject "1.2", whereas
60                  * float-to-int type conversion will round to integer.
61                  *
62                  * XXX if the typinput function is not cachable, we really ought to
63                  * postpone evaluation of the function call until runtime. But
64                  * there is no way to represent a typinput function call as an
65                  * expression tree, because C-string values are not Datums.
66                  */
67                 Const      *con = (Const *) node;
68                 Const      *newcon = makeNode(Const);
69                 Type            targetType = typeidType(targetTypeId);
70
71                 newcon->consttype = targetTypeId;
72                 newcon->constlen = typeLen(targetType);
73                 newcon->constbyval = typeByVal(targetType);
74                 newcon->constisnull = con->constisnull;
75                 newcon->constisset = false;
76
77                 if (!con->constisnull)
78                 {
79                         /* We know the source constant is really of type 'text' */
80                         char       *val = DatumGetCString(DirectFunctionCall1(textout,
81                                                                                                                 con->constvalue));
82
83                         newcon->constvalue = stringTypeDatum(targetType, val, atttypmod);
84                         pfree(val);
85                 }
86
87                 ReleaseSysCache(targetType);
88
89                 result = (Node *) newcon;
90         }
91         else if (IS_BINARY_COMPATIBLE(inputTypeId, targetTypeId))
92         {
93
94                 /*
95                  * We don't really need to do a conversion, but we do need to
96                  * attach a RelabelType node so that the expression will be seen
97                  * to have the intended type when inspected by higher-level code.
98                  */
99                 RelabelType *relabel = makeNode(RelabelType);
100
101                 relabel->arg = node;
102                 relabel->resulttype = targetTypeId;
103
104                 /*
105                  * XXX could we label result with exprTypmod(node) instead of
106                  * default -1 typmod, to save a possible length-coercion later?
107                  * Would work if both types have same interpretation of typmod,
108                  * which is likely but not certain.
109                  */
110                 relabel->resulttypmod = -1;
111
112                 result = (Node *) relabel;
113         }
114         else if (typeInheritsFrom(inputTypeId, targetTypeId))
115         {
116                 /* Input class type is a subclass of target, so nothing to do */
117                 result = node;
118         }
119         else
120         {
121
122                 /*
123                  * Otherwise, find the appropriate type conversion function
124                  * (caller should have determined that there is one), and generate
125                  * an expression tree representing run-time application of the
126                  * conversion function.
127                  */
128                 FuncCall   *n = makeNode(FuncCall);
129
130                 n->funcname = typeidTypeName(targetTypeId);
131                 n->args = lcons(node, NIL);
132                 n->agg_star = false;
133                 n->agg_distinct = false;
134
135                 result = transformExpr(pstate, (Node *) n, EXPR_COLUMN_FIRST);
136
137                 /* safety check that we got the right thing */
138                 if (exprType(result) != targetTypeId)
139                         elog(ERROR, "coerce_type: conversion function %s produced %s",
140                                  typeidTypeName(targetTypeId),
141                                  typeidTypeName(exprType(result)));
142
143                 /*
144                  * If the input is a constant, apply the type conversion function
145                  * now instead of delaying to runtime.  (We could, of course, just
146                  * leave this to be done during planning/optimization; but it's a
147                  * very frequent special case, and we save cycles in the rewriter
148                  * if we fold the expression now.)
149                  *
150                  * Note that no folding will occur if the conversion function is not
151                  * marked 'iscachable'.
152                  *
153                  * HACK: if constant is NULL, don't fold it here.  This is needed by
154                  * make_subplan(), which calls this routine on placeholder Const
155                  * nodes that mustn't be collapsed.  (It'd be a lot cleaner to
156                  * make a separate node type for that purpose...)
157                  */
158                 if (IsA(node, Const) &&!((Const *) node)->constisnull)
159                         result = eval_const_expressions(result);
160         }
161
162         return result;
163 }
164
165
166 /* can_coerce_type()
167  * Can input_typeids be coerced to func_typeids?
168  *
169  * There are a few types which are known apriori to be convertible.
170  * We will check for those cases first, and then look for possible
171  *      conversion functions.
172  *
173  * Notes:
174  * This uses the same mechanism as the CAST() SQL construct in gram.y.
175  * We should also check the function return type on candidate conversion
176  *      routines just to be safe but we do not do that yet...
177  * We need to have a zero-filled OID array here, otherwise the cache lookup fails.
178  * - thomas 1998-03-31
179  */
180 bool
181 can_coerce_type(int nargs, Oid *input_typeids, Oid *func_typeids)
182 {
183         int                     i;
184         HeapTuple       ftup;
185         Form_pg_proc pform;
186         Oid                     oid_array[FUNC_MAX_ARGS];
187
188         /* run through argument list... */
189         for (i = 0; i < nargs; i++)
190         {
191                 Oid                     inputTypeId = input_typeids[i];
192                 Oid                     targetTypeId = func_typeids[i];
193
194                 /* no problem if same type */
195                 if (inputTypeId == targetTypeId)
196                         continue;
197
198                 /*
199                  * one of the known-good transparent conversions? then drop
200                  * through...
201                  */
202                 if (IS_BINARY_COMPATIBLE(inputTypeId, targetTypeId))
203                         continue;
204
205                 /* don't know what to do for the output type? then quit... */
206                 if (targetTypeId == InvalidOid)
207                         return false;
208                 /* don't know what to do for the input type? then quit... */
209                 if (inputTypeId == InvalidOid)
210                         return false;
211
212                 /*
213                  * If input is an untyped string constant, assume we can convert
214                  * it to anything except a class type.
215                  */
216                 if (inputTypeId == UNKNOWNOID)
217                 {
218                         if (ISCOMPLEX(targetTypeId))
219                                 return false;
220                         continue;
221                 }
222
223                 /*
224                  * If input is a class type that inherits from target, no problem
225                  */
226                 if (typeInheritsFrom(inputTypeId, targetTypeId))
227                         continue;
228
229                 /*
230                  * Else, try for explicit conversion using functions: look for a
231                  * single-argument function named with the target type name and
232                  * accepting the source type.
233                  */
234                 MemSet(oid_array, 0, FUNC_MAX_ARGS * sizeof(Oid));
235                 oid_array[0] = inputTypeId;
236
237                 ftup = SearchSysCache(PROCNAME,
238                                                           PointerGetDatum(typeidTypeName(targetTypeId)),
239                                                           Int32GetDatum(1),
240                                                           PointerGetDatum(oid_array),
241                                                           0);
242                 if (!HeapTupleIsValid(ftup))
243                         return false;
244                 /* Make sure the function's result type is as expected, too */
245                 pform = (Form_pg_proc) GETSTRUCT(ftup);
246                 if (pform->prorettype != targetTypeId)
247                 {
248                         ReleaseSysCache(ftup);
249                         return false;
250                 }
251                 ReleaseSysCache(ftup);
252         }
253
254         return true;
255 }
256
257 /* coerce_type_typmod()
258  * Force a value to a particular typmod, if meaningful and possible.
259  *
260  * This is applied to values that are going to be stored in a relation
261  * (where we have an atttypmod for the column) as well as values being
262  * explicitly CASTed (where the typmod comes from the target type spec).
263  *
264  * The caller must have already ensured that the value is of the correct
265  * type, typically by applying coerce_type.
266  *
267  * If the target column type possesses a function named for the type
268  * and having parameter signature (columntype, int4), we assume that
269  * the type requires coercion to its own length and that the said
270  * function should be invoked to do that.
271  *
272  * "bpchar" (ie, char(N)) and "numeric" are examples of such types.
273  */
274 Node *
275 coerce_type_typmod(ParseState *pstate, Node *node,
276                                    Oid targetTypeId, int32 atttypmod)
277 {
278         char       *funcname;
279         Oid                     oid_array[FUNC_MAX_ARGS];
280
281         /*
282          * We assume that only typmod values greater than 0 indicate a forced
283          * conversion is necessary.
284          */
285         if (atttypmod <= 0 ||
286                 atttypmod == exprTypmod(node))
287                 return node;
288
289         funcname = typeidTypeName(targetTypeId);
290         MemSet(oid_array, 0, FUNC_MAX_ARGS * sizeof(Oid));
291         oid_array[0] = targetTypeId;
292         oid_array[1] = INT4OID;
293
294         /* attempt to find with arguments exactly as specified... */
295         if (SearchSysCacheExists(PROCNAME,
296                                                          PointerGetDatum(funcname),
297                                                          Int32GetDatum(2),
298                                                          PointerGetDatum(oid_array),
299                                                          0))
300         {
301                 A_Const    *cons = makeNode(A_Const);
302                 FuncCall   *func = makeNode(FuncCall);
303
304                 cons->val.type = T_Integer;
305                 cons->val.val.ival = atttypmod;
306
307                 func->funcname = funcname;
308                 func->args = lappend(lcons(node, NIL), cons);
309                 func->agg_star = false;
310                 func->agg_distinct = false;
311
312                 node = transformExpr(pstate, (Node *) func, EXPR_COLUMN_FIRST);
313         }
314
315         return node;
316 }
317
318
319 /* select_common_type()
320  *              Determine the common supertype of a list of input expression types.
321  *              This is used for determining the output type of CASE and UNION
322  *              constructs.
323  *
324  * typeids is a nonempty integer list of type OIDs.  Note that earlier items
325  * in the list will be preferred if there is doubt.
326  * 'context' is a phrase to use in the error message if we fail to select
327  * a usable type.
328  *
329  * XXX this code is WRONG, since (for example) given the input (int4,int8)
330  * it will select int4, whereas according to SQL92 clause 9.3 the correct
331  * answer is clearly int8.  To fix this we need a notion of a promotion
332  * hierarchy within type categories --- something more complete than
333  * just a single preferred type.
334  */
335 Oid
336 select_common_type(List *typeids, const char *context)
337 {
338         Oid                     ptype;
339         CATEGORY        pcategory;
340         List       *l;
341
342         Assert(typeids != NIL);
343         ptype = (Oid) lfirsti(typeids);
344         pcategory = TypeCategory(ptype);
345         foreach(l, lnext(typeids))
346         {
347                 Oid             ntype = (Oid) lfirsti(l);
348
349                 /* move on to next one if no new information... */
350                 if (ntype && (ntype != UNKNOWNOID) && (ntype != ptype))
351                 {
352                         if (!ptype || ptype == UNKNOWNOID)
353                         {
354                                 /* so far, only nulls so take anything... */
355                                 ptype = ntype;
356                                 pcategory = TypeCategory(ptype);
357                         }
358                         else if (TypeCategory(ntype) != pcategory)
359                         {
360                                 /*
361                                  * both types in different categories? then
362                                  * not much hope...
363                                  */
364                                 elog(ERROR, "%s types \"%s\" and \"%s\" not matched",
365                                          context, typeidTypeName(ptype), typeidTypeName(ntype));
366                         }
367                         else if (IsPreferredType(pcategory, ntype)
368                                          && can_coerce_type(1, &ptype, &ntype))
369                         {
370                                 /*
371                                  * new one is preferred and can convert? then
372                                  * take it...
373                                  */
374                                 ptype = ntype;
375                                 pcategory = TypeCategory(ptype);
376                         }
377                 }
378         }
379
380         /*
381          * If all the inputs were UNKNOWN type --- ie, unknown-type literals ---
382          * then resolve as type TEXT.  This situation comes up with constructs
383          * like
384          *                      SELECT (CASE WHEN foo THEN 'bar' ELSE 'baz' END);
385          *                      SELECT 'foo' UNION SELECT 'bar';
386          * It might seem desirable to leave the construct's output type as
387          * UNKNOWN, but that really doesn't work, because we'd probably end up
388          * needing a runtime coercion from UNKNOWN to something else, and we
389          * usually won't have it.  We need to coerce the unknown literals while
390          * they are still literals, so a decision has to be made now.
391          */
392         if (ptype == UNKNOWNOID)
393                 ptype = TEXTOID;
394
395         return ptype;
396 }
397
398 /* coerce_to_common_type()
399  *              Coerce an expression to the given type.
400  *
401  * This is used following select_common_type() to coerce the individual
402  * expressions to the desired type.  'context' is a phrase to use in the
403  * error message if we fail to coerce.
404  *
405  * NOTE: pstate may be NULL.
406  */
407 Node *
408 coerce_to_common_type(ParseState *pstate, Node *node,
409                                           Oid targetTypeId,
410                                           const char *context)
411 {
412         Oid                     inputTypeId = exprType(node);
413
414         if (inputTypeId == targetTypeId)
415                 return node;                    /* no work */
416         if (can_coerce_type(1, &inputTypeId, &targetTypeId))
417         {
418                 node = coerce_type(pstate, node, inputTypeId, targetTypeId, -1);
419         }
420         else
421         {
422                 elog(ERROR, "%s unable to convert to type \"%s\"",
423                          context, typeidTypeName(targetTypeId));
424         }
425         return node;
426 }
427
428
429 /* TypeCategory()
430  * Assign a category to the specified OID.
431  */
432 CATEGORY
433 TypeCategory(Oid inType)
434 {
435         CATEGORY        result;
436
437         switch (inType)
438         {
439                 case (BOOLOID):
440                         result = BOOLEAN_TYPE;
441                         break;
442
443                 case (CHAROID):
444                 case (NAMEOID):
445                 case (BPCHAROID):
446                 case (VARCHAROID):
447                 case (TEXTOID):
448                         result = STRING_TYPE;
449                         break;
450
451                         /*
452                          * Kluge added 4/8/00 by tgl: treat the new BIT types as
453                          * strings, so that 'unknown' || 'unknown' continues to
454                          * resolve as textcat rather than generating an
455                          * ambiguous-operator error.  Probably BIT types should have
456                          * their own type category, or maybe they should be numeric?
457                          * Need a better way of handling unknown types first.
458                          */
459                 case (ZPBITOID):
460                 case (VARBITOID):
461                         result = BITSTRING_TYPE;
462                         break;
463
464                 case (OIDOID):
465                 case (REGPROCOID):
466                 case (INT2OID):
467                 case (INT4OID):
468                 case (INT8OID):
469                 case (FLOAT4OID):
470                 case (FLOAT8OID):
471                 case (NUMERICOID):
472                 case (CASHOID):
473                         result = NUMERIC_TYPE;
474                         break;
475
476                 case (DATEOID):
477                 case (TIMEOID):
478                 case (TIMETZOID):
479                 case (ABSTIMEOID):
480                 case (TIMESTAMPOID):
481                         result = DATETIME_TYPE;
482                         break;
483
484                 case (RELTIMEOID):
485                 case (TINTERVALOID):
486                 case (INTERVALOID):
487                         result = TIMESPAN_TYPE;
488                         break;
489
490                 case (POINTOID):
491                 case (LSEGOID):
492                 case (PATHOID):
493                 case (BOXOID):
494                 case (POLYGONOID):
495                 case (LINEOID):
496                 case (CIRCLEOID):
497                         result = GEOMETRIC_TYPE;
498                         break;
499
500                 case (INETOID):
501                 case (CIDROID):
502                         result = NETWORK_TYPE;
503                         break;
504
505                 case (UNKNOWNOID):
506                 case (InvalidOid):
507                         result = UNKNOWN_TYPE;
508                         break;
509
510                 default:
511                         result = USER_TYPE;
512                         break;
513         }
514         return result;
515 }       /* TypeCategory() */
516
517
518 /* IsPreferredType()
519  * Check if this type is a preferred type.
520  */
521 bool
522 IsPreferredType(CATEGORY category, Oid type)
523 {
524         return type == PreferredType(category, type);
525 }       /* IsPreferredType() */
526
527
528 /* PreferredType()
529  * Return the preferred type OID for the specified category.
530  */
531 static Oid
532 PreferredType(CATEGORY category, Oid type)
533 {
534         Oid                     result;
535
536         switch (category)
537         {
538                 case (BOOLEAN_TYPE):
539                         result = BOOLOID;
540                         break;
541
542                 case (STRING_TYPE):
543                         result = TEXTOID;
544                         break;
545
546                 case (BITSTRING_TYPE):
547                         result = VARBITOID;
548                         break;
549
550                 case (NUMERIC_TYPE):
551                         if (type == OIDOID)
552                                 result = OIDOID;
553                         else if (type == NUMERICOID)
554                                 result = NUMERICOID;
555                         else
556                                 result = FLOAT8OID;
557                         break;
558
559                 case (DATETIME_TYPE):
560                         result = TIMESTAMPOID;
561                         break;
562
563                 case (TIMESPAN_TYPE):
564                         result = INTERVALOID;
565                         break;
566
567                 case (NETWORK_TYPE):
568                         result = INETOID;
569                         break;
570
571                 case (GEOMETRIC_TYPE):
572                 case (USER_TYPE):
573                         result = type;
574                         break;
575
576                 default:
577                         result = UNKNOWNOID;
578                         break;
579         }
580         return result;
581 }       /* PreferredType() */
582
583
584 #ifdef NOT_USED
585 Oid
586 PromoteTypeToNext(Oid inType)
587 {
588         Oid                     result;
589
590         switch (inType)
591         {
592                 case (CHAROID):
593                 case (BPCHAROID):
594                         result = VARCHAROID;
595                         break;
596
597                 case (VARCHAROID):
598                         result = TEXTOID;
599                         break;
600
601                 case (INT2OID):
602                 case (CASHOID):
603                         result = INT4OID;
604                         break;
605
606                 case (INT4OID):
607                 case (INT8OID):
608                 case (FLOAT4OID):
609                         result = FLOAT8OID;
610                         break;
611
612                 case (NUMERICOID):
613                         result = NUMERICOID;
614                         break;
615
616                 case (DATEOID):
617                 case (ABSTIMEOID):
618                         result = TIMESTAMPOID;
619                         break;
620
621                 case (TIMEOID):
622                 case (RELTIMEOID):
623                         result = INTERVALOID;
624                         break;
625
626                 case (BOOLOID):
627                 case (TEXTOID):
628                 case (FLOAT8OID):
629                 case (TIMESTAMPOID):
630                 case (INTERVALOID):
631                 default:
632                         result = inType;
633                         break;
634         }
635         return result;
636 }       /* PromoteTypeToNext() */
637
638
639 Oid
640 DemoteType(Oid inType)
641 {
642         Oid                     result;
643
644         switch (inType)
645         {
646                 case (FLOAT4OID):
647                 case (FLOAT8OID):
648                         result = INT4OID;
649                         break;
650
651                 default:
652                         result = inType;
653                         break;
654         }
655         return result;
656 }       /* DemoteType() */
657
658
659 Oid
660 PromoteLesserType(Oid inType1, Oid inType2, Oid *newType1, Oid *newType2)
661 {
662         Oid                     result;
663
664         if (inType1 == inType2)
665         {
666                 result = PromoteTypeToNext(inType1);
667                 inType1 = result;
668                 *arg2 = result;
669                 return result;
670         }
671
672         kind1 = ClassifyType(inType1);
673         kind2 = ClassifyType(*arg2);
674         if (kind1 != kind2)
675         {
676                 *newType1 = inType1;
677                 *newType2 = inType2;
678                 result = InvalidOid;
679         }
680
681         isBuiltIn1 = IS_BUILTIN_TYPE(inType1);
682         isBuiltIn2 = IS_BUILTIN_TYPE(*arg2);
683
684         if (isBuiltIn1 && isBuiltIn2)
685         {
686                 switch (*arg1)
687                 {
688                         case (CHAROID):
689                                 switch (*arg2)
690                                 {
691                                         case (BPCHAROID):
692                                         case (VARCHAROID):
693                                         case (TEXTOID):
694
695                                         case (INT2OID):
696                                         case (INT4OID):
697                                         case (FLOAT4OID):
698                                         case (FLOAT8OID):
699                                         case (CASHOID):
700
701                                         case (POINTOID):
702                                         case (LSEGOID):
703                                         case (LINEOID):
704                                         case (BOXOID):
705                                         case (PATHOID):
706                                         case (CIRCLEOID):
707                                         case (POLYGONOID):
708
709                                         case (InvalidOid):
710                                         case (UNKNOWNOID):
711                                         case (BOOLOID):
712                                         default:
713                                                 *arg1 = InvalidOid;
714                                                 *arg2 = InvalidOid;
715                                                 result = InvalidOid;
716                                 }
717                 }
718         }
719         else if (isBuiltIn1 && !isBuiltIn2)
720         {
721                 if ((promotedType = PromoteBuiltInType(*arg1)) != *arg1)
722                 {
723                         *arg1 = promotedType;
724                         return promotedType;
725                 }
726                 else if (CanCoerceType(*arg1, *arg2))
727                 {
728                         *arg1 = *arg2;
729                         return *arg2;
730                 }
731         }
732         else if (!isBuiltIn1 && isBuiltIn2)
733         {
734                 if ((promotedType = PromoteBuiltInType(*arg2)) != *arg2)
735                 {
736                         *arg2 = promotedType;
737                         return promotedType;
738                 }
739                 else if (CanCoerceType(*arg2, *arg1))
740                 {
741                         *arg2 = *arg1;
742                         return *arg1;
743                 }
744         }
745
746
747         if (*arg2 == InvalidOid)
748                 return InvalidOid;
749
750         switch (*arg1)
751         {
752                 case (CHAROID):
753                         switch (*arg2)
754                         {
755                                 case (BPCHAROID):
756                                 case (VARCHAROID):
757                                 case (TEXTOID):
758
759                                 case (INT2OID):
760                                 case (INT4OID):
761                                 case (FLOAT4OID):
762                                 case (FLOAT8OID):
763                                 case (CASHOID):
764
765                                 case (POINTOID):
766                                 case (LSEGOID):
767                                 case (LINEOID):
768                                 case (BOXOID):
769                                 case (PATHOID):
770                                 case (CIRCLEOID):
771                                 case (POLYGONOID):
772
773                                 case (InvalidOid):
774                                 case (UNKNOWNOID):
775                                 case (BOOLOID):
776                                 default:
777                                         *arg1 = InvalidOid;
778                                         *arg2 = InvalidOid;
779                                         result = InvalidOid;
780                         }
781         }
782 }
783
784 #endif