This section describes the different functions that you need to define when you create an aggregate UDF. Section 29.4.2, “Adding a New User-Defined Function”, describes the order in which MySQL calls these functions.
This function is called when MySQL finds the first row in a new group. It should reset any internal summary variables and then use the given
UDF_ARGSargument as the first value in your internal summary value for the group. Declare
void xxx_reset(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
xxx_reset()is not needed or used in MySQL 8.0, in which the UDF interface uses
xxx_clear()instead. However, you can define both
xxx_clear()if you want to have your UDF work with older versions of the server. (If you do include both functions, the
xxx_reset()function in many cases can be implemented internally by calling
xxx_clear()to reset all variables, and then calling
xxx_add()to add the
UDF_ARGSargument as the first value in the group.)
This function is called when MySQL needs to reset the summary results. It is called at the beginning for each new group but can also be called to reset the values for a query where there were no matching rows. Declare
void xxx_clear(UDF_INIT *initid, char *is_null, char *error);
is_nullis set to point to
If something went wrong, you can store a value in the variable to which the
errorpoints to a single-byte variable, not to a string buffer.
xxx_clear()is required by MySQL 8.0.
This function is called for all rows that belong to the same group. You should use it to add the value in the
UDF_ARGSargument to your internal summary variable.
void xxx_add(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
xxx() function for an aggregate UDF
should be declared the same way as for a nonaggregate UDF. See
Section 184.108.40.206, “UDF Calling Sequences for Simple Functions”.
For an aggregate UDF, MySQL calls the
function after all rows in the group have been processed. You
should normally never access its
argument here but instead return a value based on your
internal summary variables.
Return value handling in
xxx() should be
done the same way as for a nonaggregate UDF. See
Section 220.127.116.11, “UDF Return Values and Error Handling”.
xxx_add() functions handle their
UDF_ARGS argument the same way as functions
for nonaggregate UDFs. See Section 18.104.22.168, “UDF Argument Processing”.
The pointer arguments to
error are the same for all calls to
xxx(). You can use this to remember
that you got an error or whether the
function should return
NULL. You should not
store a string into
error points to a single-byte variable, not
to a string buffer.
*is_null is reset for each group (before
*error is never reset.
are set when
xxx() returns, MySQL returns
NULL as the result for the group function.