`create_window(name, flags, init, step, inverse, final, ncells, ...cells, ...args)`
natnat-mc opened this issue · 0 comments
natnat-mc commented
Creates a windowing / aggregate function.
The function works with internal cells (ncells
of them, with their names defined in ...cells
).
- The
init
function initializes every cells (it is compiled toSELECT init
internally), and its return values are saved in the cells (ncells
are expected, having more or less will fail). If this function is omitted, then all cells default toNULL
instead. - The
step
function takes the current cells and arguments (accessible by their names defined in...cells
and...args
in thec
anda
CTEs respectively), and returns the new values for the cells (it is compiled toWITH a(...names) AS (VALUES(...)), c(...cells) AS (VALUES(...)) SELECT step FROM a, c
internally). This function is required. - The
inverse
function works the same as thestep
function, except it is expected to undo a call tostep
. If this function is omitted, then the generated function will not be usable as a windowing function but only as an aggregate function. - The
final
function takes the cells (accessible by their names defined in...cells
) and returns the final value for the aggregate (it is compiled toWITH c(...cells) AS (VALUES(...)) SELECT (final) FROM c
internally). If this function is omitted, then the generated function will take its first cell as value.
Example:
-- creates a window function that computes an average
SELECT create_window(
'average', -- name
'di', -- flags
'0, 0', -- init
'n+1, s+v', -- step
'n-1, s-v', -- inverse
's/n', -- final
2, 'n', 's', -- cells
'v' -- args
);