]> begriffs open source - ai-pg/blob - full-docs/src/sgml/html/typeconv-union-case.html
PG 18 docs from https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0-docs...
[ai-pg] / full-docs / src / sgml / html / typeconv-union-case.html
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>10.5. UNION, CASE, and Related Constructs</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="typeconv-query.html" title="10.4. Value Storage" /><link rel="next" href="typeconv-select.html" title="10.6. SELECT Output Columns" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">10.5. <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="typeconv-query.html" title="10.4. Value Storage">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><th width="60%" align="center">Chapter 10. Type Conversion</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="typeconv-select.html" title="10.6. SELECT Output Columns">Next</a></td></tr></table><hr /></div><div class="sect1" id="TYPECONV-UNION-CASE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">10.5. <code class="literal">UNION</code>, <code class="literal">CASE</code>, and Related Constructs <a href="#TYPECONV-UNION-CASE" class="id_link">#</a></h2></div></div></div><a id="id-1.5.9.10.2" class="indexterm"></a><a id="id-1.5.9.10.3" class="indexterm"></a><a id="id-1.5.9.10.4" class="indexterm"></a><a id="id-1.5.9.10.5" class="indexterm"></a><a id="id-1.5.9.10.6" class="indexterm"></a><a id="id-1.5.9.10.7" class="indexterm"></a><p>
3 SQL <code class="literal">UNION</code> constructs must match up possibly dissimilar
4 types to become a single result set.  The resolution algorithm is
5 applied separately to each output column of a union query.  The
6 <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> constructs resolve
7 dissimilar types in the same way as <code class="literal">UNION</code>.
8 Some other constructs, including
9 <code class="literal">CASE</code>, <code class="literal">ARRAY</code>, <code class="literal">VALUES</code>,
10 and the <code class="function">GREATEST</code> and <code class="function">LEAST</code>
11 functions, use the identical
12 algorithm to match up their component expressions and select a result
13 data type.
14 </p><div class="procedure" id="id-1.5.9.10.9"><p class="title"><strong>Type Resolution for <code class="literal">UNION</code>, <code class="literal">CASE</code>,
15 and Related Constructs</strong></p><ol class="procedure" type="1"><li class="step"><p>
16 If all inputs are of the same type, and it is not <code class="type">unknown</code>,
17 resolve as that type.
18 </p></li><li class="step"><p>
19 If any input is of a domain type, treat it as being of the
20 domain's base type for all subsequent steps.
21   <a href="#ftn.id-1.5.9.10.9.3.1.1" class="footnote"><sup class="footnote" id="id-1.5.9.10.9.3.1.1">[12]</sup></a>
22 </p></li><li class="step"><p>
23 If all inputs are of type <code class="type">unknown</code>, resolve as type
24 <code class="type">text</code> (the preferred type of the string category).
25 Otherwise, <code class="type">unknown</code> inputs are ignored for the purposes
26 of the remaining rules.
27 </p></li><li class="step"><p>
28 If the non-unknown inputs are not all of the same type category, fail.
29 </p></li><li class="step"><p>
30 Select the first non-unknown input type as the candidate type,
31 then consider each other non-unknown input type, left to right.
32   <a href="#ftn.id-1.5.9.10.9.6.1.1" class="footnote"><sup class="footnote" id="id-1.5.9.10.9.6.1.1">[13]</sup></a>
33 If the candidate type can be implicitly converted to the other type,
34 but not vice-versa, select the other type as the new candidate type.
35 Then continue considering the remaining inputs.  If, at any stage of this
36 process, a preferred type is selected, stop considering additional
37 inputs.
38 </p></li><li class="step"><p>
39 Convert all inputs to the final candidate type.  Fail if there is not an
40 implicit conversion from a given input type to the candidate type.
41 </p></li></ol></div><p>
42 Some examples follow.
43 </p><div class="example" id="id-1.5.9.10.11"><p class="title"><strong>Example 10.10. Type Resolution with Underspecified Types in a Union</strong></p><div class="example-contents"><p>
44 </p><pre class="screen">
45 SELECT text 'a' AS "text" UNION SELECT 'b';
46
47  text
48 ------
49  a
50  b
51 (2 rows)
52 </pre><p>
53 Here, the unknown-type literal <code class="literal">'b'</code> will be resolved to type <code class="type">text</code>.
54 </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.12"><p class="title"><strong>Example 10.11. Type Resolution in a Simple Union</strong></p><div class="example-contents"><p>
55 </p><pre class="screen">
56 SELECT 1.2 AS "numeric" UNION SELECT 1;
57
58  numeric
59 ---------
60        1
61      1.2
62 (2 rows)
63 </pre><p>
64 The literal <code class="literal">1.2</code> is of type <code class="type">numeric</code>,
65 and the <code class="type">integer</code> value <code class="literal">1</code> can be cast implicitly to
66 <code class="type">numeric</code>, so that type is used.
67 </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.13"><p class="title"><strong>Example 10.12. Type Resolution in a Transposed Union</strong></p><div class="example-contents"><p>
68 </p><pre class="screen">
69 SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL);
70
71  real
72 ------
73     1
74   2.2
75 (2 rows)
76 </pre><p>
77 Here, since type <code class="type">real</code> cannot be implicitly cast to <code class="type">integer</code>,
78 but <code class="type">integer</code> can be implicitly cast to <code class="type">real</code>, the union
79 result type is resolved as <code class="type">real</code>.
80 </p></div></div><br class="example-break" /><div class="example" id="id-1.5.9.10.14"><p class="title"><strong>Example 10.13. Type Resolution in a Nested Union</strong></p><div class="example-contents"><p>
81 </p><pre class="screen">
82 SELECT NULL UNION SELECT NULL UNION SELECT 1;
83
84 ERROR:  UNION types text and integer cannot be matched
85 </pre><p>
86 This failure occurs because <span class="productname">PostgreSQL</span> treats
87 multiple <code class="literal">UNION</code>s as a nest of pairwise operations;
88 that is, this input is the same as
89 </p><pre class="screen">
90 (SELECT NULL UNION SELECT NULL) UNION SELECT 1;
91 </pre><p>
92 The inner <code class="literal">UNION</code> is resolved as emitting
93 type <code class="type">text</code>, according to the rules given above.  Then the
94 outer <code class="literal">UNION</code> has inputs of types <code class="type">text</code>
95 and <code class="type">integer</code>, leading to the observed error.  The problem
96 can be fixed by ensuring that the leftmost <code class="literal">UNION</code>
97 has at least one input of the desired result type.
98 </p><p>
99 <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> operations are
100 likewise resolved pairwise.  However, the other constructs described in this
101 section consider all of their inputs in one resolution step.
102 </p></div></div><br class="example-break" /><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.id-1.5.9.10.9.3.1.1" class="footnote"><p><a href="#id-1.5.9.10.9.3.1.1" class="para"><sup class="para">[12] </sup></a>
103     Somewhat like the treatment of domain inputs for operators and
104     functions, this behavior allows a domain type to be preserved through
105     a <code class="literal">UNION</code> or similar construct, so long as the user is
106     careful to ensure that all inputs are implicitly or explicitly of that
107     exact type.  Otherwise the domain's base type will be used.
108    </p></div><div id="ftn.id-1.5.9.10.9.6.1.1" class="footnote"><p><a href="#id-1.5.9.10.9.6.1.1" class="para"><sup class="para">[13] </sup></a>
109     For historical reasons, <code class="literal">CASE</code> treats
110     its <code class="literal">ELSE</code> clause (if any) as the <span class="quote">“<span class="quote">first</span>”</span>
111     input, with the <code class="literal">THEN</code> clauses(s) considered after
112     that.  In all other cases, <span class="quote">“<span class="quote">left to right</span>”</span> means the order
113     in which the expressions appear in the query text.
114    </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="typeconv-query.html" title="10.4. Value Storage">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="typeconv.html" title="Chapter 10. Type Conversion">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="typeconv-select.html" title="10.6. SELECT Output Columns">Next</a></td></tr><tr><td width="40%" align="left" valign="top">10.4. Value Storage </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 10.6. <code class="literal">SELECT</code> Output Columns</td></tr></table></div></body></html>