Skip to content

Commit f47841f

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 6702984 commit f47841f

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

17288+
<para>
17289+
The arguments must all be convertible to a common data type, which
17290+
will be the type of the result (see
17291+
<xref linkend="typeconv-union-case"/> for details).
17292+
</para>
17293+
1728817294
<para>
1728917295
Like a <token>CASE</token> expression, <function>COALESCE</function> only
1729017296
evaluates the arguments that are needed to determine the result;
@@ -17315,13 +17321,30 @@ SELECT COALESCE(description, short_description, '(none)') ...
1731517321
<programlisting>
1731617322
SELECT NULLIF(value, '(none)') ...
1731717323
</programlisting>
17318-
</para>
17319-
<para>
1732017324
In this example, if <literal>value</literal> is <literal>(none)</literal>,
1732117325
null is returned, otherwise the value of <literal>value</literal>
1732217326
is returned.
1732317327
</para>
1732417328

17329+
<para>
17330+
The two arguments must be of comparable types.
17331+
To be specific, they are compared exactly as if you had
17332+
written <literal><replaceable>value1</replaceable>
17333+
= <replaceable>value2</replaceable></literal>, so there must be a
17334+
suitable <literal>=</literal> operator available.
17335+
</para>
17336+
17337+
<para>
17338+
The result has the same type as the first argument &mdash; but there is
17339+
a subtlety. What is actually returned is the first argument of the
17340+
implied <literal>=</literal> operator, and in some cases that will have
17341+
been promoted to match the second argument's type. For
17342+
example, <literal>NULLIF(1, 2.2)</literal> yields <type>numeric</type>,
17343+
because there is no <type>integer</type> <literal>=</literal>
17344+
<type>numeric</type> operator,
17345+
only <type>numeric</type> <literal>=</literal> <type>numeric</type>.
17346+
</para>
17347+
1732517348
</sect2>
1732617349

1732717350
<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>
@@ -1040,9 +1042,11 @@ SQL <literal>UNION</literal> constructs must match up possibly dissimilar
10401042
types to become a single result set. The resolution algorithm is
10411043
applied separately to each output column of a union query. The
10421044
<literal>INTERSECT</literal> and <literal>EXCEPT</literal> constructs resolve
1043-
dissimilar types in the same way as <literal>UNION</literal>. The
1045+
dissimilar types in the same way as <literal>UNION</literal>.
1046+
Some other constructs, including
10441047
<literal>CASE</literal>, <literal>ARRAY</literal>, <literal>VALUES</literal>,
1045-
<function>GREATEST</function> and <function>LEAST</function> constructs use the identical
1048+
and the <function>GREATEST</function> and <function>LEAST</function>
1049+
functions, use the identical
10461050
algorithm to match up their component expressions and select a result
10471051
data type.
10481052
</para>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy