]> granicus.if.org Git - postgresql/blob - src/include/parser/parse_coerce.h
f0b0607cd6c8e6b4a81c0265530fb2834d6eecd9
[postgresql] / src / include / parser / parse_coerce.h
1 /*-------------------------------------------------------------------------
2  *
3  * parse_coerce.h
4  *
5  *      Routines for type coercion.
6  *
7  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
8  * Portions Copyright (c) 1994, Regents of the University of California
9  *
10  * $Id: parse_coerce.h,v 1.30 2001/06/23 22:23:49 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #ifndef PARSE_COERCE_H
15 #define PARSE_COERCE_H
16
17 #include "catalog/pg_type.h"
18 #include "parser/parse_node.h"
19
20 typedef enum CATEGORY
21 {
22         INVALID_TYPE,
23         UNKNOWN_TYPE,
24         BOOLEAN_TYPE,
25         STRING_TYPE,
26         BITSTRING_TYPE,
27         NUMERIC_TYPE,
28         DATETIME_TYPE,
29         TIMESPAN_TYPE,
30         GEOMETRIC_TYPE,
31         NETWORK_TYPE,
32         USER_TYPE,
33         MIXED_TYPE
34 } CATEGORY;
35
36
37 /* IS_BUILTIN_TYPE()
38  * Check for types which are in the core distribution.
39  * The built-in types can have more explicit support for type coersion, etc,
40  *      since we know apriori how they should behave.
41  * - thomas 1998-05-13
42  */
43 #define IS_BUILTIN_TYPE(t) \
44                   (((t) == OIDOID) \
45                 || ((t) == BOOLOID) \
46                 || ((t) == BPCHAROID) \
47                 || ((t) == VARCHAROID) \
48                 || ((t) == TEXTOID) \
49                 || ((t) == BYTEAOID) \
50                 || ((t) == INT4OID) \
51                 || ((t) == INT8OID) \
52                 || ((t) == FLOAT8OID) \
53                 || ((t) == NUMERICOID) \
54                 || ((t) == TIMESTAMPOID) \
55                 || ((t) == INTERVALOID) \
56                 || ((t) == ABSTIMEOID) \
57                 || ((t) == RELTIMEOID) \
58                 || ((t) == DATEOID) \
59                 || ((t) == TIMEOID) \
60                 || ((t) == TIMETZOID) \
61                 || ((t) == CHAROID) \
62                 || ((t) == NAMEOID) \
63                 || ((t) == CASHOID) \
64                 || ((t) == POINTOID) \
65                 || ((t) == LSEGOID) \
66                 || ((t) == LINEOID) \
67                 || ((t) == BOXOID) \
68                 || ((t) == PATHOID) \
69                 || ((t) == POLYGONOID) \
70                 || ((t) == CIRCLEOID) \
71                 || ((t) == INETOID) \
72                 || ((t) == CIDROID) \
73                 || ((t) == BITOID) \
74                 || ((t) == VARBITOID) )
75
76
77 /* IS_BINARY_COMPATIBLE()
78  * Check for types with the same underlying binary representation.
79  * This allows us to cheat and directly exchange values without
80  *      going through the trouble of calling a conversion function.
81  *
82  * Remove equivalencing of FLOAT8 and TIMESTAMP. They really are not
83  *      close enough in behavior, with the TIMESTAMP reserved values
84  *      and special formatting. - thomas 1999-01-24
85  */
86 #define IS_BINARY_COMPATIBLE(a,b) \
87                   (((a) == BPCHAROID && (b) == TEXTOID) \
88                 || ((a) == BPCHAROID && (b) == VARCHAROID) \
89                 || ((a) == BPCHAROID && (b) == BYTEAOID) \
90                 || ((a) == VARCHAROID && (b) == TEXTOID) \
91                 || ((a) == VARCHAROID && (b) == BPCHAROID) \
92                 || ((a) == VARCHAROID && (b) == BYTEAOID) \
93                 || ((a) == TEXTOID && (b) == BPCHAROID) \
94                 || ((a) == TEXTOID && (b) == VARCHAROID) \
95                 || ((a) == TEXTOID && (b) == BYTEAOID) \
96                 || ((a) == BYTEAOID && (b) == BPCHAROID) \
97                 || ((a) == BYTEAOID && (b) == VARCHAROID) \
98                 || ((a) == BYTEAOID && (b) == TEXTOID) \
99                 || ((a) == OIDOID && (b) == INT4OID) \
100                 || ((a) == OIDOID && (b) == REGPROCOID) \
101                 || ((a) == INT4OID && (b) == OIDOID) \
102                 || ((a) == INT4OID && (b) == REGPROCOID) \
103                 || ((a) == REGPROCOID && (b) == OIDOID) \
104                 || ((a) == REGPROCOID && (b) == INT4OID) \
105                 || ((a) == ABSTIMEOID && (b) == INT4OID) \
106                 || ((a) == INT4OID && (b) == ABSTIMEOID) \
107                 || ((a) == RELTIMEOID && (b) == INT4OID) \
108                 || ((a) == INT4OID && (b) == RELTIMEOID) \
109                 || ((a) == INETOID && (b) == CIDROID) \
110                 || ((a) == CIDROID && (b) == INETOID) \
111                 || ((a) == BITOID && (b) == VARBITOID) \
112                 || ((a) == VARBITOID && (b) == BITOID))
113
114 /* IS_HIGHER_TYPE()
115  * These types are the most general in each of the type categories.
116  */
117 #define IS_HIGHER_TYPE(t) \
118                   (((t) == TEXTOID) \
119                 || ((t) == FLOAT8OID) \
120                 || ((t) == INTERVALOID) \
121                 || ((t) == TIMESTAMPOID) \
122                 || ((t) == POLYGONOID) \
123                 || ((t) == INETOID) )
124
125 /* IS_HIGHEST_TYPE()
126  * These types are the most general in each of the type categories.
127  * Since interval and timestamp overload so many functions, let's
128  *      give timestamp the preference.
129  * Since text is a generic string type let's leave it out too.
130  */
131 #define IS_HIGHEST_TYPE(t) \
132                   (((t) == FLOAT8OID) \
133                 || ((t) == TIMESTAMPOID) \
134                 || ((t) == INTERVALOID))
135
136
137 extern bool IsPreferredType(CATEGORY category, Oid type);
138 extern CATEGORY TypeCategory(Oid type);
139
140 extern bool can_coerce_type(int nargs, Oid *input_typeids, Oid *func_typeids);
141 extern Node *coerce_type(ParseState *pstate, Node *node, Oid inputTypeId,
142                         Oid targetTypeId, int32 atttypmod);
143 extern Node *coerce_type_typmod(ParseState *pstate, Node *node,
144                                    Oid targetTypeId, int32 atttypmod);
145
146 extern bool coerce_to_boolean(ParseState *pstate, Node **pnode);
147
148 extern Oid      select_common_type(List *typeids, const char *context);
149 extern Node *coerce_to_common_type(ParseState *pstate, Node *node,
150                                           Oid targetTypeId,
151                                           const char *context);
152
153 #endif   /* PARSE_COERCE_H */