-
+
- Two types can be binary compatible, which
- means that they can be converted into one another for
- free without invoking any function. This requires that
- corresponding values use the same internal representation. For
- instance, the types text and varchar are
- binary compatible.
+ Two types can be binary coercible, which
+ means that the conversion can be performed for free
+ without invoking any function. This requires that corresponding
+ values use the same internal representation. For instance, the
+ types text and varchar are binary
+ coercible both ways. Binary coercibility is not necessarily a
+ symmetric relationship. For example, the cast
+ from xml to text can be performed for
+ free in the present implementation, but the reverse direction
+ requires a function that performs at least a syntax check. (Two
+ types that are binary coercible both ways are also referred to as
+ binary compatible.)
To be able to create a cast, you must own the source or the target
- data type. To create a binary-compatible cast, you must be superuser.
- (This restriction is made because an erroneous binary-compatible cast
+ data type. To create a binary-coercible cast, you must be superuser.
+ (This restriction is made because an erroneous binary-coercible cast
conversion can easily crash the server.)
Indicates that the source type and the target type are binary
- compatible, so no function is required to perform the cast.
+ coercible, so no function is required to perform the cast.
Cast implementation functions can have one to three arguments.
- The first argument type must be identical to the cast's source type.
- The second argument,
+ The first argument type must be identical to or binary-coercible from
+ the cast's source type. The second argument,
if present, must be type integer>; it receives the type
modifier associated with the destination type, or -1>
if there is none. The third argument,
your own data types so that this matters.)
+ The return type of a cast function must be identical to or
+ binary-coercible to the cast's target type.
+
+
Ordinarily a cast must have different source and target data types.
However, it is allowed to declare a cast with identical source and
request without having matched it to an actual function.
If a function call name>(x>) does not
exactly match any existing function, but name> is the name
- of a data type and pg_cast> provides a binary-compatible cast
+ of a data type and pg_cast> provides a binary-coercible cast
to this type from the type of x>, then the call will be
construed as a binary-compatible cast. This exception is made so that
- binary-compatible casts can be invoked using functional syntax, even
+ binary-coercible casts can be invoked using functional syntax, even
though they lack any function. Likewise, if there is no
pg_cast> entry but the cast would be to or from a string
type, the call will be construed as an I/O conversion cast. This
The CREATE CAST command conforms to the
- except that SQL does not make provisions for binary-compatible
+ except that SQL does not make provisions for binary-coercible
types or extra arguments to implementation functions.
AS IMPLICIT> is a PostgreSQL
extension, too.
-
+
Type Conversion
to be a special type conversion request. This happens if the function call
has just one argument and the function name is the same as the (internal)
name of some data type. Furthermore, the function argument must be either
-an unknown-type literal, or a type that is binary-compatible with the named
+an unknown-type literal, or a type that is binary-coercible to the named
data type, or a type that could be converted to the named data type by
applying that type's I/O functions (that is, the conversion is either to or
from one of the standard string types). When these conditions are met,
to be resolved as text concatenation. Then the text
result of the operator is converted to bpchar (blank-padded
char>, the internal name of the character data type) to match the target
-column type. (Since the types text and
-bpchar are binary-compatible, this conversion does
+column type. (Since the conversion from text to
+bpchar is binary-coercible, this conversion does
not insert any real function call.) Finally, the sizing function
bpchar(bpchar, integer) is found in the system catalog
and applied to the operator's result and the stored column length. This
*
*
* IDENTIFICATION
- * $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.93 2008/06/19 00:46:04 alvherre Exp $
+ * $PostgreSQL: pgsql/src/backend/commands/functioncmds.c,v 1.94 2008/07/11 07:02:43 petere Exp $
*
* DESCRIPTION
* These routines take the parse tree and pick out the
#include "commands/defrem.h"
#include "commands/proclang.h"
#include "miscadmin.h"
+#include "parser/parse_coerce.h"
#include "parser/parse_func.h"
#include "parser/parse_type.h"
#include "utils/acl.h"
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("cast function must take one to three arguments")));
- if (procstruct->proargtypes.values[0] != sourcetypeid)
+ if (!IsBinaryCoercible(sourcetypeid, procstruct->proargtypes.values[0]))
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
- errmsg("argument of cast function must match source data type")));
+ errmsg("argument of cast function must match or be binary-compatible with source data type")));
if (nargs > 1 && procstruct->proargtypes.values[1] != INT4OID)
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
errmsg("third argument of cast function must be type boolean")));
- if (procstruct->prorettype != targettypeid)
+ if (!IsBinaryCoercible(procstruct->prorettype, targettypeid))
ereport(ERROR,
(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
- errmsg("return data type of cast function must match target data type")));
+ errmsg("return data type of cast function must match or be binary-compatible with target data type")));
/*
* Restricting the volatility of a cast function may or may not be a