Skip to content

Add support for creating SQLite aggregate functions #204

Closed
@mqudsi

Description

Currently sql.js supports creating custom SQL functions via the exposed create_function function. This allows you to use a locally-defined function from within SQLite queries, such as SELECT custom_func(col) FROM ....

SQLite also supports user-defined aggregate functions. This is supported via the same native C SQLite function that create_function uses, namely sqlite3_create_function; however, it requires the definition of two additional function pointers to be passed in to the function which are called at certain points during the aggregate calculation process.

This is all document on the SQLite page for sqlite3_create_function:

The sixth, seventh and eighth parameters, xFunc, xStep and xFinal, are pointers to C-language functions that implement the SQL function or aggregate. A scalar SQL function requires an implementation of the xFunc callback only; NULL pointers must be passed as the xStep and xFinal parameters. An aggregate SQL function requires an implementation of xStep and xFinal and NULL pointer must be passed for xFunc. To delete an existing SQL function or aggregate, pass NULL pointers for all three function callbacks.

Currently we are only defining the first of these (xFunc) for scalar function support. xStep and xFinal would also have to be defined. When a custom aggregate function is used in SQLite, xFunc is first called then xStep is called for each value being aggregated, and finally xFinal is called to finalize the results and return a result.

The definitions for xFunc and xStep are identical, while xFinal does not take a parameter besides the SQLite3 context (since it is a finalizer):

void (*xFunc)(sqlite3_context*,int,sqlite3_value**),
void (*xStep)(sqlite3_context*,int,sqlite3_value**),
void (*xFinal)(sqlite3_context*)

The create_function method in sql.js can only support custom scalar functions, a different definition would be needed to support creating aggregate functions.

In Python, scalar function support is implemented in the same way as sql.js with a single create_function method. Aggregate function support was implemented via a create_aggregate function that takes a class with methods step and finalize which are called accordingly; for sql.js I would propose a method defined along the lines of:

create_aggregate(name, func, step, final)

Where func, step, and final are function callbacks. It should be straightforward to understand and use for anyone that is remotely familiar with SQLite custom functions in C or any other language.

Thanks for considering!

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions