Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 517ae40

Browse files
committed
Code review for function default parameters patch. Fix numerous problems as
per recent discussions. In passing this also fixes a couple of bugs in the previous variadic-parameters patch.
1 parent cee63ea commit 517ae40

27 files changed

+797
-439
lines changed

doc/src/sgml/catalogs.sgml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.183 2008/11/21 19:31:58 mha Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/catalogs.sgml,v 2.184 2008/12/18 18:20:33 tgl Exp $ -->
22
<!--
33
Documentation of the system catalogs, directed toward PostgreSQL developers
44
-->
@@ -3653,7 +3653,14 @@
36533653
<entry><structfield>pronargs</structfield></entry>
36543654
<entry><type>int2</type></entry>
36553655
<entry></entry>
3656-
<entry>Number of arguments</entry>
3656+
<entry>Number of input arguments</entry>
3657+
</row>
3658+
3659+
<row>
3660+
<entry><structfield>pronargdefaults</structfield></entry>
3661+
<entry><type>int2</type></entry>
3662+
<entry></entry>
3663+
<entry>Number of arguments that have defaults</entry>
36573664
</row>
36583665

36593666
<row>
@@ -3720,6 +3727,20 @@
37203727
</entry>
37213728
</row>
37223729

3730+
<row>
3731+
<entry><structfield>proargdefaults</structfield></entry>
3732+
<entry><type>text</type></entry>
3733+
<entry></entry>
3734+
<entry>
3735+
Expression trees (in <function>nodeToString()</function> representation)
3736+
for default values. This is a list with
3737+
<structfield>pronargdefaults</> elements, corresponding to the last
3738+
<replaceable>N</> <emphasis>input</> arguments (i.e., the last
3739+
<replaceable>N</> <structfield>proargtypes</> positions).
3740+
If none of the arguments have defaults, this field will be null
3741+
</entry>
3742+
</row>
3743+
37233744
<row>
37243745
<entry><structfield>prosrc</structfield></entry>
37253746
<entry><type>text</type></entry>

doc/src/sgml/func.sgml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.461 2008/12/04 17:51:26 petere Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.462 2008/12/18 18:20:33 tgl Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -11808,7 +11808,7 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1180811808
<row>
1180911809
<entry><literal><function>pg_get_function_identity_arguments</function>(<parameter>func_oid</parameter>)</literal></entry>
1181011810
<entry><type>text</type></entry>
11811-
<entry>get argument list to identify a function (without argument names, default values)</entry>
11811+
<entry>get argument list to identify a function (without default values)</entry>
1181211812
</row>
1181311813
<row>
1181411814
<entry><literal><function>pg_get_function_result</function>(<parameter>func_oid</parameter>)</literal></entry>
@@ -11929,12 +11929,11 @@ SELECT pg_type_is_visible('myschema.widget'::regtype);
1192911929
of a function, in the form it would need to appear in within
1193011930
<command>CREATE FUNCTION</>.
1193111931
<function>pg_get_function_result</function> similarly returns the
11932-
appropriate <literal>RETURNS</> clause for the function.
11932+
appropriate <literal>RETURNS</> clause for the function.
1193311933
<function>pg_get_function_identity_arguments</function> returns the
1193411934
argument list necessary to identify a function, in the form it
1193511935
would need to appear in within <command>ALTER FUNCTION</>, for
11936-
instance. This form omits default values and argument names, for
11937-
example.
11936+
instance. This form omits default values.
1193811937
</para>
1193911938

1194011939
<para>

doc/src/sgml/ref/create_function.sgml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.82 2008/12/04 17:51:26 petere Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.83 2008/12/18 18:20:33 tgl Exp $
33
-->
44

55
<refentry id="SQL-CREATEFUNCTION">
@@ -21,7 +21,7 @@ $PostgreSQL: pgsql/doc/src/sgml/ref/create_function.sgml,v 1.82 2008/12/04 17:51
2121
<refsynopsisdiv>
2222
<synopsis>
2323
CREATE [ OR REPLACE ] FUNCTION
24-
<replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [ { DEFAULT | = } <replaceable class="parameter">defexpr</replaceable>] [, ...] ] )
24+
<replaceable class="parameter">name</replaceable> ( [ [ <replaceable class="parameter">argmode</replaceable> ] [ <replaceable class="parameter">argname</replaceable> ] <replaceable class="parameter">argtype</replaceable> [ { DEFAULT | = } <replaceable class="parameter">defexpr</replaceable> ] [, ...] ] )
2525
[ RETURNS <replaceable class="parameter">rettype</replaceable>
2626
| RETURNS TABLE ( <replaceable class="parameter">colname</replaceable> <replaceable class="parameter">coltype</replaceable> [, ...] ) ]
2727
{ LANGUAGE <replaceable class="parameter">langname</replaceable>
@@ -37,7 +37,7 @@ CREATE [ OR REPLACE ] FUNCTION
3737
[ WITH ( <replaceable class="parameter">attribute</replaceable> [, ...] ) ]
3838
</synopsis>
3939
</refsynopsisdiv>
40-
40+
4141
<refsect1 id="sql-createfunction-description">
4242
<title>Description</title>
4343

@@ -133,7 +133,7 @@ CREATE [ OR REPLACE ] FUNCTION
133133

134134
<listitem>
135135
<para>
136-
The data type(s) of the function's arguments (optionally
136+
The data type(s) of the function's arguments (optionally
137137
schema-qualified), if any. The argument types can be base, composite,
138138
or domain types, or can reference the type of a table column.
139139
</para>
@@ -160,10 +160,11 @@ CREATE [ OR REPLACE ] FUNCTION
160160
<listitem>
161161
<para>
162162
An expression to be used as default value if the parameter is
163-
not specified. The expression has to be convertable to the
164-
argument type of the parameter. All parameters after a
165-
parameter with default value have to be parameters with default
166-
values as well.
163+
not specified. The expression has to be coercible to the
164+
argument type of the parameter.
165+
Only input (including <literal>INOUT</>) parameters can have a default
166+
value. All input parameters following a
167+
parameter with a default value must have default values as well.
167168
</para>
168169
</listitem>
169170
</varlistentry>
@@ -173,7 +174,7 @@ CREATE [ OR REPLACE ] FUNCTION
173174

174175
<listitem>
175176
<para>
176-
The return data type (optionally schema-qualified). The return type
177+
The return data type (optionally schema-qualified). The return type
177178
can be a base, composite, or domain type,
178179
or can reference the type of a table column.
179180
Depending on the implementation language it might also be allowed
@@ -496,6 +497,18 @@ CREATE FUNCTION foo(int, out text) ...
496497
</programlisting>
497498
</para>
498499

500+
<para>
501+
Functions that have different argument type lists will not be considered
502+
to conflict at creation time, but if defaults are provided they might
503+
conflict in use. For example, consider
504+
<programlisting>
505+
CREATE FUNCTION foo(int) ...
506+
CREATE FUNCTION foo(int, int default 42) ...
507+
</programlisting>
508+
A call <literal>foo(10)</> will fail due to the ambiguity about which
509+
function should be called.
510+
</para>
511+
499512
<para>
500513
When repeated <command>CREATE FUNCTION</command> calls refer to
501514
the same object file, the file is only loaded once per session.
@@ -664,7 +677,6 @@ COMMIT;
664677

665678
</refsect1>
666679

667-
668680
<refsect1 id="sql-createfunction-compat">
669681
<title>Compatibility</title>
670682

doc/src/sgml/typeconv.sgml

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/typeconv.sgml,v 1.57 2008/07/30 19:35:12 tgl Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/typeconv.sgml,v 1.58 2008/12/18 18:20:33 tgl Exp $ -->
22

33
<chapter Id="typeconv">
44
<title>Type Conversion</title>
@@ -496,7 +496,20 @@ of its element type, as needed to match the call. After such expansion the
496496
function might have effective argument types identical to some non-variadic
497497
function. In that case the function appearing earlier in the search path is
498498
used, or if the two functions are in the same schema, the non-variadic one is
499-
selected.
499+
preferred.
500+
</para>
501+
</step>
502+
<step performance="optional">
503+
<para>
504+
Functions that have default values for parameters are considered to match any
505+
call that omits zero or more of the defaultable parameter positions. If more
506+
than one such function matches a call, the one appearing earliest in the
507+
search path is used. If there are two or more such functions in the same
508+
schema with identical parameter types in the non-defaulted positions (which is
509+
possible if they have different sets of defaultable parameters), the system
510+
will not be able to determine which to prefer, and so an <quote>ambiguous
511+
function call</> error will result if no better match to the call can be
512+
found.
500513
</para>
501514
</step>
502515
</substeps>

doc/src/sgml/xfunc.sgml

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.135 2008/12/07 23:46:39 alvherre Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/xfunc.sgml,v 1.136 2008/12/18 18:20:33 tgl Exp $ -->
22

33
<sect1 id="xfunc">
44
<title>User-Defined Functions</title>
@@ -663,25 +663,27 @@ SELECT mleast(VARIADIC ARRAY[10, -1, 5, 4.4]);
663663
</para>
664664
</sect2>
665665

666-
<sect2 id="xfunc-parameter-defaults">
667-
<title><acronym>SQL</> Functions with Parameters Default Values</title>
666+
<sect2 id="xfunc-sql-parameter-defaults">
667+
<title><acronym>SQL</> Functions with Default Values for Arguments</title>
668668

669669
<indexterm>
670-
<primary>default values</primary>
670+
<primary>function</primary>
671+
<secondary>default values for arguments</secondary>
671672
</indexterm>
672673

673674
<para>
674-
Functions can be declared with parameters with default values or
675-
expressions. The default expressions are used as parameter value
676-
if the parameter is not explicitly specified in a function call.
677-
All parameters after a a parameter with default value have to be
678-
parameters with default values as well.
675+
Functions can be declared with default values for some or all input
676+
arguments. The default values are inserted whenever the function is
677+
called with insufficiently many actual arguments. Since arguments
678+
can only be omitted from the end of the actual argument list, all
679+
parameters after a parameter with a default value have to have
680+
default values as well.
679681
</para>
680682

681683
<para>
682684
For example:
683685
<screen>
684-
CREATE FUNCTION foo(a int DEFAULT 1, b int DEFAULT 2, c int DEFAULT 3)
686+
CREATE FUNCTION foo(a int, b int DEFAULT 2, c int DEFAULT 3)
685687
RETURNS int
686688
LANGUAGE SQL
687689
AS $$
@@ -706,14 +708,11 @@ SELECT foo(10);
706708
15
707709
(1 row)
708710

709-
SELECT foo();
710-
foo
711-
-----
712-
6
713-
(1 row)
711+
SELECT foo(); -- fails since there is no default for the first argument
712+
ERROR: function foo() does not exist
714713
</screen>
715-
Instead of the key word <literal>DEFAULT</literal>,
716-
the <literal>=</literal> sign can also be used.
714+
The <literal>=</literal> sign can also be used in place of the
715+
key word <literal>DEFAULT</literal>,
717716
</para>
718717
</sect2>
719718

0 commit comments

Comments
 (0)