Skip to content

Commit d4fb509

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 12fd81c commit d4fb509

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

12020+
<para>
12021+
The arguments must all be convertible to a common data type, which
12022+
will be the type of the result (see
12023+
<xref linkend="typeconv-union-case"> for details).
12024+
</para>
12025+
1202012026
<para>
1202112027
Like a <token>CASE</token> expression, <function>COALESCE</function> only
1202212028
evaluates the arguments that are needed to determine the result;
@@ -12047,13 +12053,30 @@ SELECT COALESCE(description, short_description, '(none)') ...
1204712053
<programlisting>
1204812054
SELECT NULLIF(value, '(none)') ...
1204912055
</programlisting>
12050-
</para>
12051-
<para>
1205212056
In this example, if <literal>value</literal> is <literal>(none)</>,
1205312057
null is returned, otherwise the value of <literal>value</literal>
1205412058
is returned.
1205512059
</para>
1205612060

12061+
<para>
12062+
The two arguments must be of comparable types.
12063+
To be specific, they are compared exactly as if you had
12064+
written <literal><replaceable>value1</replaceable>
12065+
= <replaceable>value2</replaceable></literal>, so there must be a
12066+
suitable <literal>=</literal> operator available.
12067+
</para>
12068+
12069+
<para>
12070+
The result has the same type as the first argument &mdash; but there is
12071+
a subtlety. What is actually returned is the first argument of the
12072+
implied <literal>=</literal> operator, and in some cases that will have
12073+
been promoted to match the second argument's type. For
12074+
example, <literal>NULLIF(1, 2.2)</literal> yields <type>numeric</type>,
12075+
because there is no <type>integer</type> <literal>=</literal>
12076+
<type>numeric</type> operator,
12077+
only <type>numeric</type> <literal>=</literal> <type>numeric</type>.
12078+
</para>
12079+
1205712080
</sect2>
1205812081

1205912082
<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)