????
Current Path : C:/opt/pgsql/doc/postgresql/html/ |
Current File : C:/opt/pgsql/doc/postgresql/html/typeconv-union-case.html |
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!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 16.3 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> SQL <code class="literal">UNION</code> 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 <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> constructs resolve dissimilar types in the same way as <code class="literal">UNION</code>. Some other constructs, including <code class="literal">CASE</code>, <code class="literal">ARRAY</code>, <code class="literal">VALUES</code>, and the <code class="function">GREATEST</code> and <code class="function">LEAST</code> functions, use the identical algorithm to match up their component expressions and select a result data type. </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>, and Related Constructs</strong></p><ol class="procedure" type="1"><li class="step"><p> If all inputs are of the same type, and it is not <code class="type">unknown</code>, resolve as that type. </p></li><li class="step"><p> If any input is of a domain type, treat it as being of the domain's base type for all subsequent steps. <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> </p></li><li class="step"><p> If all inputs are of type <code class="type">unknown</code>, resolve as type <code class="type">text</code> (the preferred type of the string category). Otherwise, <code class="type">unknown</code> inputs are ignored for the purposes of the remaining rules. </p></li><li class="step"><p> If the non-unknown inputs are not all of the same type category, fail. </p></li><li class="step"><p> Select the first non-unknown input type as the candidate type, then consider each other non-unknown input type, left to right. <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> 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. </p></li><li class="step"><p> 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. </p></li></ol></div><p> Some examples follow. </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> </p><pre class="screen"> SELECT text 'a' AS "text" UNION SELECT 'b'; text ------ a b (2 rows) </pre><p> Here, the unknown-type literal <code class="literal">'b'</code> will be resolved to type <code class="type">text</code>. </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> </p><pre class="screen"> SELECT 1.2 AS "numeric" UNION SELECT 1; numeric --------- 1 1.2 (2 rows) </pre><p> The literal <code class="literal">1.2</code> is of type <code class="type">numeric</code>, and the <code class="type">integer</code> value <code class="literal">1</code> can be cast implicitly to <code class="type">numeric</code>, so that type is used. </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> </p><pre class="screen"> SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL); real ------ 1 2.2 (2 rows) </pre><p> Here, since type <code class="type">real</code> cannot be implicitly cast to <code class="type">integer</code>, but <code class="type">integer</code> can be implicitly cast to <code class="type">real</code>, the union result type is resolved as <code class="type">real</code>. </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> </p><pre class="screen"> SELECT NULL UNION SELECT NULL UNION SELECT 1; ERROR: UNION types text and integer cannot be matched </pre><p> This failure occurs because <span class="productname">PostgreSQL</span> treats multiple <code class="literal">UNION</code>s as a nest of pairwise operations; that is, this input is the same as </p><pre class="screen"> (SELECT NULL UNION SELECT NULL) UNION SELECT 1; </pre><p> The inner <code class="literal">UNION</code> is resolved as emitting type <code class="type">text</code>, according to the rules given above. Then the outer <code class="literal">UNION</code> has inputs of types <code class="type">text</code> and <code class="type">integer</code>, leading to the observed error. The problem can be fixed by ensuring that the leftmost <code class="literal">UNION</code> has at least one input of the desired result type. </p><p> <code class="literal">INTERSECT</code> and <code class="literal">EXCEPT</code> operations are likewise resolved pairwise. However, the other constructs described in this section consider all of their inputs in one resolution step. </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> Somewhat like the treatment of domain inputs for operators and functions, this behavior allows a domain type to be preserved through a <code class="literal">UNION</code> or similar construct, so long as the user is careful to ensure that all inputs are implicitly or explicitly of that exact type. Otherwise the domain's base type will be used. </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> For historical reasons, <code class="literal">CASE</code> treats its <code class="literal">ELSE</code> clause (if any) as the <span class="quote">“<span class="quote">first</span>”</span> input, with the <code class="literal">THEN</code> clauses(s) considered after that. In all other cases, <span class="quote">“<span class="quote">left to right</span>”</span> means the order in which the expressions appear in the query text. </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 16.3 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>