Syntax
Added in v2.3.0: Support
IF NOT EXISTS.Parameters
In the function_body, the code should define several functions to implement the aggregate function.
Required functions:
create_state() -> state: Create a new state.accumulate(state, *args) -> state: Accumulate a new value into the state, returning the updated state.
finish(state) -> value: Get the result of the aggregate function. If not defined, the state is returned as the result.retract(state, *args) -> state: Retract a value from the state, returning the updated state. If not defined, the state can not be updated incrementally in materialized views and performance may be affected.
Examples
Python
The following command creates an aggregate function namedweighted_avg to calculate the weighted average.
Python UDAF
JavaScript
The following command creates an aggregate function namedweighted_avg to calculate the weighted average.
JavaScript UDAF
Using UDAFs
After creating aggregate functions, you can use them in SQL queries like any built-in aggregate functions.Use UDAF
See also
DROP AGGREGATE
Drop a user-defined aggregate function
CREATE FUNCTION
Create a user-defined scalar or table function