Home Explore Blog CI



postgresql

12th chunk of `doc/src/sgml/typeconv.sgml`
16f4bea41745245735df3e74735794c1c61b037dbb9b12310000000100000fa7
 linkend="sql-createcast"/>).
Alternatively, if the expression is an unknown-type literal, the contents of
the literal string will be fed to the input conversion routine for the target
type.
</para>
</step>

<step performance="required">
<para>
Check to see if there is a sizing cast for the target type.  A sizing
cast is a cast from that type to itself.  If one is found in the
<structname>pg_cast</structname> catalog, apply it to the expression before storing
into the destination column.  The implementation function for such a cast
always takes an extra parameter of type <type>integer</type>, which receives
the destination column's <structfield>atttypmod</structfield> value (typically its
declared length, although the interpretation of <structfield>atttypmod</structfield>
varies for different data types), and it may take a third <type>boolean</type>
parameter that says whether the cast is explicit or implicit.  The cast
function
is responsible for applying any length-dependent semantics such as size
checking or truncation.
</para>
</step>

</procedure>

<example>
<title><type>character</type> Storage Type Conversion</title>

<para>
For a target column declared as <type>character(20)</type> the following
statement shows that the stored value is sized correctly:

<screen>
CREATE TABLE vv (v character(20));
INSERT INTO vv SELECT 'abc' || 'def';
SELECT v, octet_length(v) FROM vv;

          v           | octet_length
----------------------+--------------
 abcdef               |           20
(1 row)
</screen>
</para>

<para>
What has really happened here is that the two unknown literals are resolved
to <type>text</type> by default, allowing the <literal>||</literal> operator
to be resolved as <type>text</type> concatenation.  Then the <type>text</type>
result of the operator is converted to <type>bpchar</type> (<quote>blank-padded
char</quote>, the internal name of the <type>character</type> data type) to match the target
column type.  (Since the conversion from <type>text</type> to
<type>bpchar</type> is binary-coercible, this conversion does
not insert any real function call.)  Finally, the sizing function
<literal>bpchar(bpchar, integer, boolean)</literal> is found in the system catalog
and applied to the operator's result and the stored column length.  This
type-specific function performs the required length check and addition of
padding spaces.
</para>
</example>
</sect1>

<sect1 id="typeconv-union-case">
<title><literal>UNION</literal>, <literal>CASE</literal>, and Related Constructs</title>

<indexterm zone="typeconv-union-case">
 <primary>UNION</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<indexterm zone="typeconv-union-case">
 <primary>CASE</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<indexterm zone="typeconv-union-case">
 <primary>ARRAY</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<indexterm zone="typeconv-union-case">
 <primary>VALUES</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<indexterm zone="typeconv-union-case">
 <primary>GREATEST</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<indexterm zone="typeconv-union-case">
 <primary>LEAST</primary>
 <secondary>determination of result type</secondary>
</indexterm>

<para>
SQL <literal>UNION</literal> constructs must match up possibly dissimilar
types to become a single result set.  The resolution algorithm is
applied separately to each output column of a union query.  The
<literal>INTERSECT</literal> and <literal>EXCEPT</literal> constructs resolve
dissimilar types in the same way as <literal>UNION</literal>.
Some other constructs, including
<literal>CASE</literal>, <literal>ARRAY</literal>, <literal>VALUES</literal>,
and the <function>GREATEST</function> and <function>LEAST</function>
functions, use the identical
algorithm to match up their component expressions and select a result
data type.
</para>

<procedure>
<title>Type

Title: Type Conversion for Union, Case, and Related Constructs
Summary
The system explains how SQL Union constructs and other related constructs such as Case, Array, Values, GREATEST, and LEAST resolve dissimilar types to become a single result set, using a resolution algorithm applied separately to each output column, to match up possibly dissimilar types and select a result data type.