]> begriffs open source - ai-pg/blob - full-docs/txt/typeconv-union-case.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / typeconv-union-case.txt
1
2 10.5. UNION, CASE, and Related Constructs #
3
4    SQL UNION constructs must match up possibly dissimilar types to become
5    a single result set. The resolution algorithm is applied separately to
6    each output column of a union query. The INTERSECT and EXCEPT
7    constructs resolve dissimilar types in the same way as UNION. Some
8    other constructs, including CASE, ARRAY, VALUES, and the GREATEST and
9    LEAST functions, use the identical algorithm to match up their
10    component expressions and select a result data type.
11
12    Type Resolution for UNION, CASE, and Related Constructs
13     1. If all inputs are of the same type, and it is not unknown, resolve
14        as that type.
15     2. If any input is of a domain type, treat it as being of the domain's
16        base type for all subsequent steps. ^[12]
17     3. If all inputs are of type unknown, resolve as type text (the
18        preferred type of the string category). Otherwise, unknown inputs
19        are ignored for the purposes of the remaining rules.
20     4. If the non-unknown inputs are not all of the same type category,
21        fail.
22     5. Select the first non-unknown input type as the candidate type, then
23        consider each other non-unknown input type, left to right. ^[13] If
24        the candidate type can be implicitly converted to the other type,
25        but not vice-versa, select the other type as the new candidate
26        type. Then continue considering the remaining inputs. If, at any
27        stage of this process, a preferred type is selected, stop
28        considering additional inputs.
29     6. Convert all inputs to the final candidate type. Fail if there is
30        not an implicit conversion from a given input type to the candidate
31        type.
32
33    Some examples follow.
34
35    Example 10.10. Type Resolution with Underspecified Types in a Union
36
37 SELECT text 'a' AS "text" UNION SELECT 'b';
38
39  text
40 ------
41  a
42  b
43 (2 rows)
44
45    Here, the unknown-type literal 'b' will be resolved to type text.
46
47    Example 10.11. Type Resolution in a Simple Union
48
49 SELECT 1.2 AS "numeric" UNION SELECT 1;
50
51  numeric
52 ---------
53        1
54      1.2
55 (2 rows)
56
57    The literal 1.2 is of type numeric, and the integer value 1 can be cast
58    implicitly to numeric, so that type is used.
59
60    Example 10.12. Type Resolution in a Transposed Union
61
62 SELECT 1 AS "real" UNION SELECT CAST('2.2' AS REAL);
63
64  real
65 ------
66     1
67   2.2
68 (2 rows)
69
70    Here, since type real cannot be implicitly cast to integer, but integer
71    can be implicitly cast to real, the union result type is resolved as
72    real.
73
74    Example 10.13. Type Resolution in a Nested Union
75
76 SELECT NULL UNION SELECT NULL UNION SELECT 1;
77
78 ERROR:  UNION types text and integer cannot be matched
79
80    This failure occurs because PostgreSQL treats multiple UNIONs as a nest
81    of pairwise operations; that is, this input is the same as
82 (SELECT NULL UNION SELECT NULL) UNION SELECT 1;
83
84    The inner UNION is resolved as emitting type text, according to the
85    rules given above. Then the outer UNION has inputs of types text and
86    integer, leading to the observed error. The problem can be fixed by
87    ensuring that the leftmost UNION has at least one input of the desired
88    result type.
89
90    INTERSECT and EXCEPT operations are likewise resolved pairwise.
91    However, the other constructs described in this section consider all of
92    their inputs in one resolution step.
93
94    ^[12] Somewhat like the treatment of domain inputs for operators and
95    functions, this behavior allows a domain type to be preserved through a
96    UNION or similar construct, so long as the user is careful to ensure
97    that all inputs are implicitly or explicitly of that exact type.
98    Otherwise the domain's base type will be used.
99
100    ^[13] For historical reasons, CASE treats its ELSE clause (if any) as
101    the “first” input, with the THEN clauses(s) considered after that. In
102    all other cases, “left to right” means the order in which the
103    expressions appear in the query text.