8000 Doc: add example of type resolution in nested UNIONs. · itsmadness55/postgres@356f85f · GitHub
[go: up one dir, main page]

Skip to content

Commit 356f85f

Browse files
committed
Doc: add example of type resolution in nested UNIONs.
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
1 parent 7b5
10000
5a3b commit 356f85f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

doc/src/sgml/typeconv.sgml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,5 +1077,34 @@ result type is resolved as <type>real</>.
10771077
</para>
10781078
</example>
10791079

1080+
<example>
1081+
<title>Type Resolution in a Nested Union</title>
1082+
1083+
<para>
1084+
<screen>
1085+
SELECT NULL UNION SELECT NULL UNION SELECT 1;
1086+
1087+
ERROR: UNION types text and integer cannot be matched
1088+
</screen>
1089+
This failure occurs because <productname>PostgreSQL</productname> treats
1090+
multiple <literal>UNION</literal>s as a nest of pairwise operations;
1091+
that is, this input is the same as
1092+
<screen>
1093+
(SELECT NULL UNION SELECT NULL) UNION SELECT 1;
1094+
</screen>
1095+
The inner <literal>UNION</literal> is resolved as emitting
1096+
type <type>text</type>, according to the rules given above. Then the
1097+
outer <literal>UNION</literal> has inputs of types <type>text</type>
1098+
and <type>integer</type>, leading to the observed error. The problem
1099+
can be fixed by ensuring that the leftmost <literal>UNION</literal>
1100+
has at least one input of the desired result type.
1101+
</para>
1102+
1103+
<para>
1104+
<literal>INTERSECT</literal> and <literal>EXCEPT</literal> operations are
1105+
likewise resolved pairwise. However, the other constructs described in this
1106+
section consider all of their inputs in one resolution step.
1107+
</para>
1108+
</example>
10801109
</sect1>
10811110
</chapter>

0 commit comments

Comments
 (0)
0