1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Fix concat() and format() to handle VARIADIC-labeled arguments correctly.

Previously, the VARIADIC labeling was effectively ignored, but now these
functions act as though the array elements had all been given as separate
arguments.

Pavel Stehule
This commit is contained in:
Tom Lane
2013-01-25 00:19:18 -05:00
parent 56a6317bf5
commit 760f3c043a
5 changed files with 310 additions and 35 deletions

View File

@ -1394,7 +1394,8 @@
</entry>
<entry><type>text</type></entry>
<entry>
Concatenate all arguments. NULL arguments are ignored.
Concatenate the text representations of all the arguments.
NULL arguments are ignored.
</entry>
<entry><literal>concat('abcde', 2, NULL, 22)</literal></entry>
<entry><literal>abcde222</literal></entry>
@ -1411,8 +1412,8 @@
</entry>
<entry><type>text</type></entry>
<entry>
Concatenate all but first arguments with separators. The first
parameter is used as a separator. NULL arguments are ignored.
Concatenate all but the first argument with separators. The first
argument is used as the separator string. NULL arguments are ignored.
</entry>
<entry><literal>concat_ws(',', 'abcde', 2, NULL, 22)</literal></entry>
<entry><literal>abcde,2,22</literal></entry>
@ -1522,8 +1523,9 @@
</entry>
<entry><type>text</type></entry>
<entry>
Format a string. This function is similar to the C function
<function>sprintf</>; but only the following conversion specifications
Format arguments according to a format string.
This function is similar to the C function
<function>sprintf</>, but only the following conversion specifications
are recognized: <literal>%s</literal> interpolates the corresponding
argument as a string; <literal>%I</literal> escapes its argument as
an SQL identifier; <literal>%L</literal> escapes its argument as an
@ -2033,6 +2035,18 @@
</tgroup>
</table>
<para>
The <function>concat</function>, <function>concat_ws</function> and
<function>format</function> functions are variadic, so it is possible to
pass the values to be concatenated or formatted as an array marked with
the <literal>VARIADIC</literal> keyword (see <xref
linkend="xfunc-sql-variadic-functions">). The array's elements are
treated as if they were separate ordinary arguments to the function.
If the variadic array argument is NULL, <function>concat</function>
and <function>concat_ws</function> return NULL, but
<function>format</function> treats a NULL as a zero-element array.
</para>
<para>
See also the aggregate function <function>string_agg</function> in
<xref linkend="functions-aggregate">.

View File

@ -3153,6 +3153,10 @@ CREATE OR REPLACE FUNCTION retcomposite(IN integer, IN integer,
<literal>fcinfo-&gt;flinfo</>. The parameter <literal>argnum</>
is zero based. <function>get_call_result_type</> can also be used
as an alternative to <function>get_fn_expr_rettype</>.
There is also <function>get_fn_expr_variadic</>, which can be used to
find out whether the call contained an explicit <literal>VARIADIC</>
keyword. This is primarily useful for <literal>VARIADIC "any"</>
functions, as described below.
</para>
<para>
@ -3229,7 +3233,12 @@ CREATE FUNCTION make_array(anyelement) RETURNS anyarray
as happens with normal variadic functions; they will just be passed to
the function separately. The <function>PG_NARGS()</> macro and the
methods described above must be used to determine the number of actual
arguments and their types when using this feature.
arguments and their types when using this feature. Also, users of such
a function might wish to use the <literal>VARIADIC</> keyword in their
function call, with the expectation that the function would treat the
array elements as separate arguments. The function itself must implement
that behavior if wanted, after using <function>get_fn_expr_variadic</> to
detect that the actual argument was marked with <literal>VARIADIC</>.
</para>
</sect2>