natnat-mc/sqlite-fn

`create_window(name, flags, init, step, inverse, final, ncells, ...cells, ...args)`

natnat-mc opened this issue · 0 comments

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 to SELECT 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 to NULL instead.
  • The step function takes the current cells and arguments (accessible by their names defined in ...cells and ...args in the c and a CTEs respectively), and returns the new values for the cells (it is compiled to WITH 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 the step function, except it is expected to undo a call to step. 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 to WITH 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
);