Doc: add example of type resolution in nested UNIONs.
authorTom Lane
Sun, 25 Mar 2018 20:15:16 +0000 (16:15 -0400)
committerTom Lane
Sun, 25 Mar 2018 20:15:16 +0000 (16:15 -0400)
Section 10.5 didn't say explicitly that multiple UNIONs are resolved
pairwise.  Since the resolution algorithm is described as taking any
number of inputs, readers might well think that a query like
"select x union select y union select z" would be resolved by
considering x, y, and z in one resolution step.  But that's not what
happens (and I think that behavior is per SQL spec).  Add an example
clarifying this point.

Per bug #15129 from Philippe Beaudoin.

Discussion: https://postgr.es/m/152196085023.32649.9916472370480121694@wrigleys.postgresql.org

doc/src/sgml/typeconv.sgml

index c031c01ed35a91ea01c93c6a3434f8c7aba2f310..aa367bb0eb06aa5d705f1b429b9a491b9e770c9d 100644 (file)
@@ -1077,5 +1077,34 @@ result type is resolved as real.
 
 
 
+
+Type Resolution in a Nested Union
+
+
+
+SELECT NULL UNION SELECT NULL UNION SELECT 1;
+
+ERROR:  UNION types text and integer cannot be matched
+
+This failure occurs because PostgreSQL treats
+multiple UNIONs as a nest of pairwise operations;
+that is, this input is the same as
+
+(SELECT NULL UNION SELECT NULL) UNION SELECT 1;
+
+The inner UNION is resolved as emitting
+type text, according to the rules given above.  Then the
+outer UNION has inputs of types text
+and integer, leading to the observed error.  The problem
+can be fixed by ensuring that the leftmost UNION
+has at least one input of the desired result type.
+
+
+
+INTERSECT and EXCEPT operations are
+likewise resolved pairwise.  However, the other constructs described in this
+section consider all of their inputs in one resolution step.
+
+