Skip to content

Commit a24cda2

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 7b356c7 commit a24cda2

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
@@ -12638,6 +12638,12 @@ SELECT COALESCE(description, short_description, '(none)') ...
1263812638
<varname>short_description</varname> if it is not null, otherwise <literal>(none)</literal>.
1263912639
</para>
1264012640

12641+
<para>
12642+
The arguments must all be convertible to a common data type, which
12643+
will be the type of the result (see
12644+
<xref linkend="typeconv-union-case"/> for details).
12645+
</para>
12646+
1264112647
<para>
1264212648
Like a <token>CASE</token> expression, <function>COALESCE</function> only
1264312649
evaluates the arguments that are needed to determine the result;
@@ -12668,13 +12674,30 @@ SELECT COALESCE(description, short_description, '(none)') ...
1266812674
<programlisting>
1266912675
SELECT NULLIF(value, '(none)') ...
1267012676
</programlisting>
12671-
</para>
12672-
<para>
1267312677
In this example, if <literal>value</literal> is <literal>(none)</literal>,
1267412678
null is returned, otherwise the value of <literal>value</literal>
1267512679
is returned.
1267612680
</para>
1267712681

12682+
<para>
12683+
The two arguments must be of comparable types.
12684+
To be specific, they are compared exactly as if you had
12685+
written <literal><replaceable>value1</replaceable>
12686+
= <replaceable>value2</replaceable></literal>, so there must be a
12687+
suitable <literal>=</literal> operator available.
12688+
</para>
12689+
12690+
<para>
12691+
The result has the same type as the first argument &mdash; but there is
12692+
a subtlety. What is actually returned is the first argument of the
12693+
implied <literal>=</literal> operator, and in some cases that will have
12694+
been promoted to match the second argument's type. For
12695+
example, <literal>NULLIF(1, 2.2)</literal> yields <type>numeric</type>,
12696+
because there is no <type>integer</type> <literal>=</literal>
12697+
<type>numeric</type> operator,
12698+
only <type>numeric</type> <literal>=</literal> <type>numeric</type>.
12699+
</para>
12700+
1267812701
</sect2>
1267912702

1268012703
<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</command> clause must be matched up and converted to a uniform set.
128128
Similarly, the result expressions of a <literal>CASE</literal> construct must be
129129
converted to a common type so that the <literal>CASE</literal> expression as a whole
130-
has a known output type. The same holds for <literal>ARRAY</literal> constructs,
131-
and for the <function>GREATEST</function> and <function>LEAST</function> 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</literal> 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</literal> and <literal>EXCEPT</literal> constructs resolve
1044-
dissimilar types in the same way as <literal>UNION</literal>. The
1046+
dissimilar types in the same way as <literal>UNION</literal>.
1047+
Some other constructs, including
10451048
<literal>CASE</literal>, <literal>ARRAY</literal>, <literal>VALUES</literal>,
1046-
<function>GREATEST</function> and <function>LEAST</function> constructs use the identical
1049+
and the <function>GREATEST</function> and <function>LEAST</function>
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)