CREATE AGGREGATE 中文man頁面
NAME
CREATE AGGREGATE - 定義一個新的聚集函數
SYNOPSIS
CREATE AGGREGATE name ( BASETYPE = input_data_type, SFUNC = sfunc, STYPE = state_data_type [ , FINALFUNC = ffunc ] [ , INITCOND = initial_condition ] )
DESCRIPTION 描述
CREATE AGGREGATE 定義一個新的聚集函數。 一些用于基本類型的聚集函數如 min(integer) 和 avg(double precision) 等已經包含在基礎軟件包里了。 如果你需要定義一個新類型或需要一個還沒有提供的聚集函數,這時便可用 CREATE AGGREGATE 來提供我們所需要的特性。
如果給出了一個模式的名字(比如,CREATE AGGREGATE myschema.myagg ...),那么該聚集函數是在指定模式中創建的。 否則它是在當前模式中創建的。
一個聚集函數是用它的名字和輸入數據類型來標識的。 同一模式中如果兩個聚集處理的輸入數據不同,它們可以有相同的名字。 一個聚集函數的輸入數據類型必須和所有同一模式中的普通函數的名字和輸入類型不同。
一個聚集函數是用一個或兩個普通函數做成的: 一個狀態轉換函數 sfunc, 和一個可選的終計算函數 ffunc. 它們是這樣使用的:
sfunc( internal-state, next-data-item ) ---> next-internal-state ffunc( internal-state ) ---> aggregate-value
PostgreSQL 創建一個類型為 stype的臨時變量。 它保存這個聚集的當前內部狀態。 對于每個輸入數據條目, 都調用狀態轉換函數計算內部狀態值的新數值。 在處理完所有數據后,調用一次最終處理函數以計算聚集的返回值。 如果沒有最終處理函數,那么將最后的狀態值當做返回值。
一個聚集函數還可能提供一個初始條件,也就是說,所用的該內部狀態值的初始值。 這個值是作為一個類型為 text 的字段存儲在數據庫里的, 不過它們必須是狀態值數據類型的合法的外部表現形式的常量。 如果沒有提供狀態,那么狀態值初始化為 NULL。
如果該狀態轉換函數被定義為 "strict", 那么就不能用 NULL 輸入調用它。這個時候,帶有這樣的轉換函數的聚集執行起來的現象如下所述。 NULL 輸入的值被忽略(不調用此函數并且保留前一個狀態值)。如果初始狀態值是 NULL,那么由第一個非 NULL 值替換該狀態值, 而狀態轉換函數從第二個非 NULL 的輸入值開始調用。這樣做讓我們比較容易實現象 max 這樣的聚集。 請注意這種行為只是當 state_type 與 input_data_type 相同的時候才表現出來。 如果這些類型不同,你必須提供一個非 NULL 的初始條件或者使用一個非strice的狀態轉換函數。
如果狀態轉換函數不是 strict(嚴格)的, 那么它將無條件地為每個輸入值調用, 并且必須自行處理 NULL 輸入和 NULL 轉換值, 這樣就允許聚集的作者對聚集中的空值有完全的控制。
如果終轉換函數定義為"strict",則如果最終狀態值是 NULL 時就不會調用它; 而是自動輸出一個NULL的結果。(當然,這才是 strict 函數的正常特征。) 不管是那種情況,終處理函數可以選擇返回 NULL。比如, avg 的終處理函數在零輸入記錄時就會返回 NULL。
PARAMETERS 參數
- name
- 要創建的聚集函數名(可以有模式修飾的)。
- input_data_type
- 本聚集函數要處理的基本數據類型。 對于不檢查輸入類型的聚集來說,這個參數可以聲明為"ANY"。 (比如 count(*))。
- sfunc
- 用于處理源數據列里的每一個輸入數據的狀態轉換函數名稱。 它通常是一個雙參數的函數,第一個參數的類型是 state_data_type 而第二個參數的類型是 input_data_type. 另外,對于一個不檢查輸入數據的聚集,該函數只接受一個類型為 state_data_type 的參數。 不管是哪種情況,此函數必須返回一個類型為 state_data_type的值。 這個函數接受當前狀態值和當前輸入數據條目,而返回下個狀態值。
- state_data_type
- 聚集的狀態值的數據類型。
- ffunc
- 在轉換完所有輸入域/字段后調用的最終處理函數。它計算聚集的結果。 此函數必須接受一個類型為 state_data_type 的參數。 聚集的輸出數據類型被定義為此函數的返回類型。 如果沒有聲明 ffunc 則使用聚集結果的狀態值作為聚集的結果,而輸出類型為 state_data_type。
- initial_condition
- 狀態值的初始設置(值)。它必須是一個數據類型 state_data_type 可以接受的文本常量值。 如果沒有聲明,狀態值初始為 NULL。
CREATE AGGREGATE 的參數可以以任何順序書寫,而不只是上面顯示的順序。
EXAMPLES 例子
參閱 ``User-defined Aggregates''
COMPATIBILITY 兼容性
CREATE AGGREGATE 是 PostgreSQL 語言的擴展。 在 SQL 標準里沒有 CREATE AGGREGATE。
SEE ALSO 參見
ALTER AGGREGATE [alter_aggregate(7)], DROP AGGREGATE [drop_aggregate(l)]
#p#
NAME
CREATE AGGREGATE - define a new aggregate function
SYNOPSIS
CREATE AGGREGATE name ( BASETYPE = input_data_type, SFUNC = sfunc, STYPE = state_data_type [ , FINALFUNC = ffunc ] [ , INITCOND = initial_condition ] )
DESCRIPTION
CREATE AGGREGATE defines a new aggregate function. Some aggregate functions for base types such as min(integer) and avg(double precision) are already provided in the standard distribution. If one defines new types or needs an aggregate function not already provided, then CREATE AGGREGATE can be used to provide the desired features.
If a schema name is given (for example, CREATE AGGREGATE myschema.myagg ...) then the aggregate function is created in the specified schema. Otherwise it is created in the current schema.
An aggregate function is identified by its name and input data type. Two aggregates in the same schema can have the same name if they operate on different input types. The name and input data type of an aggregate must also be distinct from the name and input data type(s) of every ordinary function in the same schema.
An aggregate function is made from one or two ordinary functions: a state transition function sfunc, and an optional final calculation function ffunc. These are used as follows:
sfunc( internal-state, next-data-item ) ---> next-internal-state ffunc( internal-state ) ---> aggregate-value
PostgreSQL creates a temporary variable of data type stype to hold the current internal state of the aggregate. At each input data item, the state transition function is invoked to calculate a new internal state value. After all the data has been processed, the final function is invoked once to calculate the aggregate's return value. If there is no final function then the ending state value is returned as-is.
An aggregate function may provide an initial condition, that is, an initial value for the internal state value. This is specified and stored in the database as a column of type text, but it must be a valid external representation of a constant of the state value data type. If it is not supplied then the state value starts out null.
If the state transition function is declared ``strict'', then it cannot be called with null inputs. With such a transition function, aggregate execution behaves as follows. Null input values are ignored (the function is not called and the previous state value is retained). If the initial state value is null, then the first nonnull input value replaces the state value, and the transition function is invoked beginning with the second nonnull input value. This is handy for implementing aggregates like max. Note that this behavior is only available when state_data_type is the same as input_data_type. When these types are different, you must supply a nonnull initial condition or use a nonstrict transition function.
If the state transition function is not strict, then it will be called unconditionally at each input value, and must deal with null inputs and null transition values for itself. This allows the aggregate author to have full control over the aggregate's handling of null values.
If the final function is declared ``strict'', then it will not be called when the ending state value is null; instead a null result will be returned automatically. (Of course this is just the normal behavior of strict functions.) In any case the final function has the option of returning a null value. For example, the final function for avg returns null when it sees there were zero input rows.
PARAMETERS
- name
- The name (optionally schema-qualified) of the aggregate function to create.
- input_data_type
- The input data type on which this aggregate function operates. This can be specified as "ANY" for an aggregate that does not examine its input values (an example is count(*)).
- sfunc
- The name of the state transition function to be called for each input data value. This is normally a function of two arguments, the first being of type state_data_type and the second of type input_data_type. Alternatively, for an aggregate that does not examine its input values, the function takes just one argument of type state_data_type. In either case the function must return a value of type state_data_type. This function takes the current state value and the current input data item, and returns the next state value.
- state_data_type
- The data type for the aggregate's state value.
- ffunc
- The name of the final function called to compute the aggregate's result after all input data has been traversed. The function must take a single argument of type state_data_type. The return data type of the aggregate is defined as the return type of this function. If ffunc is not specified, then the ending state value is used as the aggregate's result, and the return type is state_data_type.
- initial_condition
- The initial setting for the state value. This must be a string constant in the form accepted for the data type state_data_type. If not specified, the state value starts out null.
The parameters of CREATE AGGREGATE can be written in any order, not just the order illustrated above.
EXAMPLES
See the section called ``User-defined Aggregates'' in the documentation.
COMPATIBILITY
CREATE AGGREGATE is a PostgreSQL language extension. The SQL standard does not provide for user-defined aggregate function.
SEE ALSO
ALTER AGGREGATE [alter_aggregate(7)], DROP AGGREGATE [drop_aggregate(l)]