10000 Doc: add example of type resolution in nested UNIONs. · tomdcc/postgres@b666c55 · GitHub
[go: up one dir, main page]

Skip to content

Commit b666c55

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 675001d commit b666c55

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
@@ -1076,5 +1076,34 @@ result type is resolved as <type>real</>.
10761076
</para>
10771077
</example>
10781078

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

0 commit comments

Comments
 (0)
0