Home Explore Blog CI



postgresql

14th chunk of `doc/src/sgml/typeconv.sgml`
dec67c32620c050ed6b9c45c7bda51ff4f9812f5d8d4567500000001000008f5
 clauses(s) considered after
    that.  In all other cases, <quote>left to right</quote> means the order
    in which the expressions appear in the query text.
   </para>
  </footnote>
If the candidate type can be implicitly converted to the other type,
but not vice-versa, select the other type as the new candidate type.
Then continue considering the remaining inputs.  If, at any stage of this
process, a preferred type is selected, stop considering additional
inputs.
</para>
</step>

<step performance="required">
<para>
Convert all inputs to the final candidate type.  Fail if there is not an
implicit conversion from a given input type to the candidate type.
</para>
</step>
</procedure>

<para>
Some examples follow.
</para>

<example>
<title>Type Resolution with Underspecified Types in a Union</title>

<para>
<screen>
SELECT text 'a' AS "text" UNION SELECT 'b';

 text
------
 a
 b
(2 rows)
</screen>
Here, the unknown-type literal <literal>'b'</literal> will be resolved to type <type>text</type>.
</para>
</example>

<example>
<title>Type Resolution in a Simple Union</title>

<para>
<screen>
SELECT 1.2 AS "numeric" UNION SELECT 1;

 numeric
---------
       1
     1.2
(2 rows)
</screen>
The literal <literal>1.2</literal> is of type <type>numeric</type>,
and the <type>integer</type> value <literal>1</literal> can be cast implicitly to
<type>numeric</type>, so that type is used.
</para>
</example>

<example>
<title>Type Resolution in a Transposed Union</title>

<para>
<screen>
SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL);

 real
------
    1
  2.2
(2 rows)
</screen>
Here, since type <type>real</type> cannot be implicitly cast to <type>integer</type>,
but <type>integer</type> can be implicitly cast to <type>real</type>, the union
result type is resolved as <type>real</type>.
</para>
</example>

<example>
<title>Type Resolution in a Nested Union</title>

<para>
<screen>
SELECT NULL UNION SELECT NULL UNION SELECT 1;

ERROR:  UNION types text and integer cannot be matched
</screen>
This failure occurs because <productname>PostgreSQL</productname> treats
multiple <literal>UNION</literal>s as a nest of pairwise operations;
that is, this input is the same as
<screen>
(SELECT NULL UNION SELECT NULL) UNION SELECT 1;
</screen>
The inner <literal>UNION</literal>

Title: Type Resolution Examples for UNION Constructs
Summary
The document provides several examples to illustrate the type resolution process for SQL UNION constructs, including cases with underspecified types, simple unions, transposed unions, and nested unions, demonstrating how the system resolves the result type based on implicit conversions and type categories.