Skip to content

Commit 7b9cee4

Browse files
committed
Doc: clarify data type behavior of COALESCE and NULLIF.
After studying the code, NULLIF is a lot more subtle than you might have guessed. Discussion: https://postgr.es/m/160486028730.25500.15740897403028593550@wrigleys.postgresql.org
1 parent a54dfbe commit 7b9cee4

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
lines changed

doc/src/sgml/func.sgml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11577,6 +11577,12 @@ SELECT COALESCE(description, short_description, '(none)') ...
1157711577
<varname>short_description</> if it is not null, otherwise <literal>(none)</>.
1157811578
</para>
1157911579

11580+
<para>
11581+
The arguments must all be convertible to a common data type, which
11582+
will be the type of the result (see
11583+
<xref linkend="typeconv-union-case"> for details).
11584+
</para>
11585+
1158011586
<para>
1158111587
Like a <token>CASE</token> expression, <function>COALESCE</function> only
1158211588
evaluates the arguments that are needed to determine the result;
@@ -11607,13 +11613,30 @@ SELECT COALESCE(description, short_description, '(none)') ...
1160711613
<programlisting>
1160811614
SELECT NULLIF(value, '(none)') ...
1160911615
</programlisting>
11610-
</para>
11611-
<para>
1161211616
In this example, if <literal>value</literal> is <literal>(none)</>,
1161311617
null is returned, otherwise the value of <literal>value</literal>
1161411618
is returned.
1161511619
</para>
1161611620

11621+
<para>
11622+
The two arguments must be of comparable types.
11623+
To be specific, they are compared exactly as if you had
11624+
written <literal><replaceable>value1</replaceable>
11625+
= <replaceable>value2</replaceable></literal>, so there must be a
11626+
suitable <literal>=</literal> operator available.
11627+
</para>
11628+
11629+
<para>
11630+
The result has the same type as the first argument &mdash; but there is
11631+
a subtlety. What is actually returned is the first argument of the
11632+
implied <literal>=</literal> operator, and in some cases that will have
11633+
been promoted to match the second argument's type. For
11634+
example, <literal>NULLIF(1, 2.2)</literal> yields <type>numeric</type>,
11635+
because there is no <type>integer</type> <literal>=</literal>
11636+
<type>numeric</type> operator,
11637+
only <type>numeric</type> <literal>=</literal> <type>numeric</type>.
11638+
</para>
11639+
1161711640
</sect2>
1161811641

1161911642
<sect2 id="functions-greatest-least">

doc/src/sgml/typeconv.sgml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ must appear in a single set of columns, the types of the results of each
127127
<command>SELECT</> clause must be matched up and converted to a uniform set.
128128
Similarly, the result expressions of a <literal>CASE</> construct must be
129129
converted to a common type so that the <literal>CASE</> expression as a whole
130-
has a known output type. The same holds for <literal>ARRAY</> constructs,
131-
and for the <function>GREATEST</> and <function>LEAST</> functions.
130+
has a known output type. Some other constructs, such
131+
as <literal>ARRAY[]</literal> and the <function>GREATEST</function>
132+
and <function>LEAST</function> functions, likewise require determination of a
133+
common type for several subexpressions.
132134
</para>
133135
</listitem>
134136
</varlistentry>
@@ -1041,9 +1043,11 @@ SQL <literal>UNION</> constructs must match up possibly dissimilar
10411043
types to become a single result set. The resolution algorithm is
10421044
applied separately to each output column of a union query. The
10431045
<literal>INTERSECT</> and <literal>EXCEPT</> constructs resolve
1044-
dissimilar types in the same way as <literal>UNION</>. The
1046+
dissimilar types in the same way as <literal>UNION</>.
1047+
Some other constructs, including
10451048
<literal>CASE</>, <literal>ARRAY</>, <literal>VALUES</>,
1046-
<function>GREATEST</> and <function>LEAST</> constructs use the identical
1049+
and the <function>GREATEST</> and <function>LEAST</>
1050+
functions, use the identical
10471051
algorithm to match up their component expressions and select a result
10481052
data type.
10491053
</para>

0 commit comments

Comments
 (0)