The functions described in Creating User-defined Functions are expanded on this page. They are declared as follows:
If x() returns an integer, it is declared as follows:
long long x(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
If x() returns a string (DECIMAL functions also return string values), it is declared as follows:
char *x(UDF_INIT *initid, UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *error);
If x() returns a real, it is declared as follows:
double x(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
my_bool x_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void x_deinit(UDF_INIT *initid);
initid is a parameter passed to all three functions that points to a UDF_INIT structure, used for communicating information between the functions. Its structure members are:
x_clear() is a required function for aggregate functions, and is declared as follows:
void x_clear(UDF_INIT *initid, char *is_null, char *error);
It is called when the summary results need to be reset, that is at the beginning of each new group. but also to reset the values when there were no matching rows.
is_null is set to point to CHAR(0) before calling x_clear().
In the case of an error, you can store the value to which the error argument points (a single-byte variable, not a string string buffer) in the variable.
x_reset() is declared as follows:
void x_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
It is called on finding the first row in a new group. Should reset the summary variables, and then use UDF_ARGS as the first value in the group's internal summary value. The function is not required if the UDF interface uses x_clear().
x_add() is declared as follows:
void x_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
It is called for all rows belonging to the same group, and should be used to add the value in UDF_ARGS to the internal summary variable.
x_remove() was added in MariaDB 10.4 and is declared as follows (same as x_add()):
void x_remove(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char *error );
It adds more efficient support of aggregate UDFs as window functions. x_remove() should "subtract" the row (reverse x_add()). In MariaDB 10.4 aggregate UDFs will work as WINDOW functions without x_remove() but it will not be so efficient.
If x_remove() supported (defined) detected automatically.
© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/user-defined-functions-calling-sequences/