Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitee4dcf1

Browse files
committed
Update/improve documentation about creating aggregate functions.
1 parent0a27641 commitee4dcf1

File tree

2 files changed

+165
-117
lines changed

2 files changed

+165
-117
lines changed

‎doc/src/sgml/ref/create_aggregate.sgml

Lines changed: 107 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.7 1999/07/22 15:09:07 thomas Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/create_aggregate.sgml,v 1.8 2000/03/26 19:45:21 tgl Exp $
33
Postgres documentation
44
-->
55

@@ -24,9 +24,9 @@ Postgres documentation
2424
<date>1999-07-20</date>
2525
</refsynopsisdivinfo>
2626
<synopsis>
27-
CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASETYPE = <replaceable class="PARAMETER">data_type</replaceable>
28-
[ , SFUNC1 = <replaceable class="PARAMETER">sfunc1</replaceable>, STYPE1 = <replaceable class="PARAMETER">sfunc1_return_type</replaceable> ]
29-
[ , SFUNC2 = <replaceable class="PARAMETER">sfunc2</replaceable>, STYPE2 = <replaceable class="PARAMETER">sfunc2_return_type</replaceable> ]
27+
CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASETYPE = <replaceable class="PARAMETER">input_data_type</replaceable>
28+
[ , SFUNC1 = <replaceable class="PARAMETER">sfunc1</replaceable>, STYPE1 = <replaceable class="PARAMETER">state1_type</replaceable> ]
29+
[ , SFUNC2 = <replaceable class="PARAMETER">sfunc2</replaceable>, STYPE2 = <replaceable class="PARAMETER">state2_type</replaceable> ]
3030
[ , FINALFUNC = <replaceable class="PARAMETER">ffunc</replaceable> ]
3131
[ , INITCOND1 = <replaceable class="PARAMETER">initial_condition1</replaceable> ]
3232
[ , INITCOND2 = <replaceable class="PARAMETER">initial_condition2</replaceable> ] )
@@ -51,10 +51,10 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
5151
</varlistentry>
5252

5353
<varlistentry>
54-
<term><replaceable class="PARAMETER">data_type</replaceable></term>
54+
<term><replaceable class="PARAMETER">input_data_type</replaceable></term>
5555
<listitem>
5656
<para>
57-
Thefundamental data type on which this aggregate function operates.
57+
Theinput data type on which this aggregate function operates.
5858
</para>
5959
</listitem>
6060
</varlistentry>
@@ -63,21 +63,25 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
6363
<term><replaceable class="PARAMETER">sfunc1</replaceable></term>
6464
<listitem>
6565
<para>
66-
The state transition function
67-
to be called for every non-NULL field from the source column.
68-
It takes a variable of
69-
type <replaceable class="PARAMETER">sfunc1_return_type</replaceable> as
70-
the first argument and that field as the
71-
second argument.
66+
A state transition function
67+
to be called for every non-NULL input data value.
68+
This must be a function of two arguments, the first being of
69+
type <replaceable class="PARAMETER">state1_type</replaceable>
70+
and the second of
71+
type <replaceable class="PARAMETER">input_data_type</replaceable>.
72+
The function must return a value of
73+
type <replaceable class="PARAMETER">state1_type</replaceable>.
74+
This function takes the current state value 1 and the current
75+
input data item, and returns the next state value 1.
7276
</para>
7377
</listitem>
7478
</varlistentry>
7579

7680
<varlistentry>
77-
<term><replaceable class="PARAMETER">sfunc1_return_type</replaceable></term>
81+
<term><replaceable class="PARAMETER">state1_type</replaceable></term>
7882
<listitem>
7983
<para>
80-
Thereturn typeof the firsttransition function.
84+
Thedata typefor the firststate value of the aggregate.
8185
</para>
8286
</listitem>
8387
</varlistentry>
@@ -86,20 +90,22 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
8690
<term><replaceable class="PARAMETER">sfunc2</replaceable></term>
8791
<listitem>
8892
<para>
89-
The state transition function
90-
to be called for every non-NULL field from the source column.
91-
It takes a variable
92-
of type <replaceable class="PARAMETER">sfunc2_return_type</replaceable>
93-
as the only argument and returns a variable of the same type.
93+
A state transition function
94+
to be called for every non-NULL input data value.
95+
This must be a function of one argument of
96+
type <replaceable class="PARAMETER">state2_type</replaceable>,
97+
returning a value of the same type.
98+
This function takes the current state value 2 and
99+
returns the next state value 2.
94100
</para>
95101
</listitem>
96102
</varlistentry>
97103

98104
<varlistentry>
99-
<term><replaceable class="PARAMETER">sfunc2_return_type</replaceable></term>
105+
<term><replaceable class="PARAMETER">state2_type</replaceable></term>
100106
<listitem>
101107
<para>
102-
Thereturn typeof the secondtransition function.
108+
Thedata typefor the secondstate value of the aggregate.
103109
</para>
104110
</listitem>
105111
</varlistentry>
@@ -108,12 +114,17 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
108114
<term><replaceable class="PARAMETER">ffunc</replaceable></term>
109115
<listitem>
110116
<para>
111-
The final function
112-
called after traversing all input fields. This function must
117+
The final function called to compute the aggregate's result
118+
after all input data has been traversed.
119+
If both state values are used, the final function must
113120
take two arguments of types
114-
<replaceable class="PARAMETER">sfunc1_return_type</replaceable>
121+
<replaceable class="PARAMETER">state1_type</replaceable>
115122
and
116-
<replaceable class="PARAMETER">sfunc2_return_type</replaceable>.
123+
<replaceable class="PARAMETER">state2_type</replaceable>.
124+
If only one state value is used, the final function must
125+
take a single argument of that state value's type.
126+
The output datatype of the aggregate is defined as the return
127+
type of this function.
117128
</para>
118129
</listitem>
119130
</varlistentry>
@@ -122,7 +133,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
122133
<term><replaceable class="PARAMETER">initial_condition1</replaceable></term>
123134
<listitem>
124135
<para>
125-
The initial value forthe first transition function argument.
136+
The initial value forstate value 1.
126137
</para>
127138
</listitem>
128139
</varlistentry>
@@ -131,7 +142,7 @@ CREATE AGGREGATE <replaceable class="PARAMETER">name</replaceable> [ AS ] ( BASE
131142
<term><replaceable class="PARAMETER">initial_condition2</replaceable></term>
132143
<listitem>
133144
<para>
134-
The initial value forthe second transition function argument.
145+
The initial value forstate value 2.
135146
</para>
136147
</listitem>
137148
</varlistentry>
@@ -182,84 +193,66 @@ CREATE
182193
can be used to provide the desired features.
183194
</para>
184195
<para>
185-
An aggregate function can require up to three functions, two
186-
state transition functions,
196+
An aggregate function is identified by its name and input data type.
197+
Two aggregates can have the same name if they operate on different
198+
input types. To avoid confusion, do not make an ordinary function
199+
of the same name and input data type as an aggregate.
200+
</para>
201+
<para>
202+
An aggregate function is made from between one and three ordinary
203+
functions:
204+
two state transition functions,
187205
<replaceable class="PARAMETER">sfunc1</replaceable>
188-
and <replaceable class="PARAMETER">sfunc2</replaceable>:
189-
<programlisting>
190-
<replaceable class="PARAMETER">sfunc1</replaceable>( internal-state1, next-data_item ) ---> next-internal-state1 <replaceable class="PARAMETER">sfunc2</replaceable>( internal-state2 ) ---> next-internal-state2
191-
</programlisting>
206+
and <replaceable class="PARAMETER">sfunc2</replaceable>,
192207
and a final calculation function,
193-
<replaceable class="PARAMETER">ffunc</replaceable>:
208+
<replaceable class="PARAMETER">ffunc</replaceable>.
209+
These are used as follows:
194210
<programlisting>
211+
<replaceable class="PARAMETER">sfunc1</replaceable>( internal-state1, next-data-item ) ---> next-internal-state1
212+
<replaceable class="PARAMETER">sfunc2</replaceable>( internal-state2 ) ---> next-internal-state2
195213
<replaceable class="PARAMETER">ffunc</replaceable>(internal-state1, internal-state2) ---> aggregate-value
196214
</programlisting>
197215
</para>
198216
<para>
199-
<productname>Postgres</productname> creates up to two temporary variables
200-
(referred to here as <replaceable class="PARAMETER">temp1</replaceable>
201-
and <replaceable class="PARAMETER">temp2</replaceable>)
202-
to hold intermediate results used as arguments to the transition functions.
217+
<productname>Postgres</productname> creates one or two temporary variables
218+
(of data types <replaceable class="PARAMETER">stype1</replaceable> and/or
219+
<replaceable class="PARAMETER">stype2</replaceable>) to hold the
220+
current internal states of the aggregate. At each input data item,
221+
the state transition function(s) are invoked to calculate new values
222+
for the internal state values. After all the data has been processed,
223+
the final function is invoked once to calculate the aggregate's output
224+
value.
203225
</para>
204226
<para>
205-
These transition functions are required to have the following properties:
206-
<itemizedlist>
207-
<listitem>
208-
<para>
209-
The arguments to
210-
<replaceable class="PARAMETER">sfunc1</replaceable>
211-
must be
212-
<replaceable class="PARAMETER">temp1</replaceable>
213-
of type
214-
<replaceable class="PARAMETER">sfunc1_return_type</replaceable>
215-
and
216-
<replaceable class="PARAMETER">column_value</replaceable>
217-
of type <replaceable class="PARAMETER">data_type</replaceable>.
218-
The return value must be of type
219-
<replaceable class="PARAMETER">sfunc1_return_type</replaceable>
220-
and will be used as the first argument in the next call to
221-
<replaceable class="PARAMETER">sfunc1</replaceable>.
222-
</para>
223-
</listitem>
224-
225-
<listitem>
226-
<para>
227-
The argument and return value of
228-
<replaceable class="PARAMETER">sfunc2</replaceable>
229-
must be
230-
<replaceable class="PARAMETER">temp2</replaceable>
231-
of type
232-
<replaceable class="PARAMETER">sfunc2_return_type</replaceable>.
233-
</para>
234-
</listitem>
235-
<listitem>
236-
<para>
237-
The arguments to the final-calculation-function
238-
must be
239-
<replaceable class="PARAMETER">temp1</replaceable>
240-
and
241-
<replaceable class="PARAMETER">temp2</replaceable>
242-
and its return value must
243-
be a <productname>Postgres</productname>
244-
base type (not necessarily
245-
<replaceable class="PARAMETER">data_type</replaceable>
246-
which had been specified for BASETYPE).
247-
</para>
248-
</listitem>
249-
<listitem>
250-
<para>
251-
FINALFUNC should be specified
252-
if and only if both state-transition functions are
253-
specified.
254-
</para></listitem>
255-
</itemizedlist>
227+
<replaceable class="PARAMETER">ffunc</replaceable> must be specified if
228+
both transition functions are specified. If only one transition function
229+
is used, then <replaceable class="PARAMETER">ffunc</replaceable> is
230+
optional. The default behavior when
231+
<replaceable class="PARAMETER">ffunc</replaceable> is not provided is
232+
to return the ending value of the internal state value being used
233+
(and, therefore, the aggregate's output type is the same as that
234+
state value's type).
256235
</para>
257236

258237
<para>
259-
An aggregate function may also require one or two initial conditions,
260-
one for
261-
each transition function. These are specified and stored
262-
in the database as fields of type <type>text</type>.
238+
An aggregate function may also provide one or two initial conditions,
239+
that is, initial values for the internal state values being used.
240+
These are specified and stored in the database as fields of type
241+
<type>text</type>, but they must be valid external representations
242+
of constants of the state value datatypes. If
243+
<replaceable class="PARAMETER">sfunc1</replaceable> is specified
244+
without an <replaceable class="PARAMETER">initcond1</replaceable> value,
245+
then the system does not call
246+
<replaceable class="PARAMETER">sfunc1</replaceable>
247+
at the first input item; instead, the internal state value 1 is
248+
initialized with the first input value, and
249+
<replaceable class="PARAMETER">sfunc1</replaceable> is called beginning
250+
at the second input item. This is useful for aggregates like MIN and
251+
MAX. Note that an aggregate using this feature will return NULL when
252+
called with no input values. There is no comparable provision for
253+
state value 2; if <replaceable class="PARAMETER">sfunc2</replaceable> is
254+
specified then an <replaceable class="PARAMETER">initcond2</replaceable> is
255+
required.
263256
</para>
264257

265258
<refsect2 id="R2-SQL-CREATEAGGREGATE-3">
@@ -274,18 +267,32 @@ CREATE
274267
to drop aggregate functions.
275268
</para>
276269

270+
<para>
271+
The parameters of <command>CREATE AGGREGATE</command> can be written
272+
in any order, not just the order illustrated above.
273+
</para>
274+
277275
<para>
278276
It is possible to specify aggregate functions
279277
that have varying combinations of state and final functions.
280-
For example, the <function>count</function> aggregate requires SFUNC2
281-
(an incrementing function) but not SFUNC1 or FINALFUNC,
282-
whereas the <function>sum</function> aggregate requires SFUNC1 (an addition
283-
function) but not SFUNC2 or FINALFUNC and the <function>avg</function>
278+
For example, the <function>count</function> aggregate requires
279+
<replaceable class="PARAMETER">sfunc2</replaceable>
280+
(an incrementing function) but not
281+
<replaceable class="PARAMETER">sfunc1</replaceable> or
282+
<replaceable class="PARAMETER">ffunc</replaceable>,
283+
whereas the <function>sum</function> aggregate requires
284+
<replaceable class="PARAMETER">sfunc1</replaceable> (an addition
285+
function) but not <replaceable class="PARAMETER">sfunc2</replaceable> or
286+
<replaceable class="PARAMETER">ffunc</replaceable>, and the
287+
<function>avg</function>
284288
aggregate requires
285-
both of the above state functions as
286-
well as a FINALFUNC (a division function) to produce its
289+
both state functions as
290+
well as a <replaceable class="PARAMETER">ffunc</replaceable> (a division
291+
function) to produce its
287292
answer. In any case, at least one state function must be
288-
defined, and any SFUNC2 must have a corresponding INITCOND2.
293+
defined, and any <replaceable class="PARAMETER">sfunc2</replaceable> must
294+
have a corresponding
295+
<replaceable class="PARAMETER">initcond2</replaceable>.
289296
</para>
290297

291298
</refsect2>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp