24.2. Adding New Functions to MySQL

MySQL 5.0

24.2. Adding New Functions to MySQL

There are two ways to add new functions to MySQL:

  • You can add functions through the user-defined function (UDF) interface. User-defined functions are compiled as object files and then added to and removed from the server dynamically using the and statements. See Section 24.2.2, “ Syntax”.

  • You can add functions as native (built-in) MySQL functions. Native functions are compiled into the mysqld server and become available on a permanent basis.

Each method has advantages and disadvantages:

  • If you write user-defined functions, you must install object files in addition to the server itself. If you compile your function into the server, you don't need to do that.

  • Native functions require you to modify a source distribution. UDFs do not. You can add UDFs to a binary MySQL distribution. No access to MySQL source is necessary.

  • If you upgrade your MySQL distribution, you can continue to use your previously installed UDFs, unless you upgrade to a newer version for which the UDF interface changes. For native functions, you must repeat your modifications each time you upgrade.

Whichever method you use to add new functions, they can be invoked in SQL statements just like native functions such as or .

Another way to add functions is by creating stored functions. These are written using SQL statements rather than by compiling object code. The syntax for writing stored functions is described in Chapter 17, Stored Procedures and Functions.

The following sections describe features of the UDF interface, provide instructions for writing UDFs, discuss security precautions that MySQL takes to prevent UDF misuse, and describe how to add native mySQL functions.

For example source code that illustrates how to write UDFs, take a look at the file that is provided in MySQL source distributions.

24.2.1. Features of the User-Defined Function Interface

The MySQL interface for user-defined functions provides the following features and capabilities:

  • Functions can return string, integer, or real values.

  • You can define simple functions that operate on a single row at a time, or aggregate functions that operate on groups of rows.

  • Information is provided to functions that enables them to check the number and types of the arguments passed to them.

  • You can tell MySQL to coerce arguments to a given type before passing them to a function.

  • You can indicate that a function returns or that an error occurred.

24.2.2. CREATE FUNCTION Syntax

CREATE [AGGREGATE] FUNCTION  RETURNS {STRING|INTEGER|REAL|DECIMAL}
    SONAME 

A user-defined function (UDF) is a way to extend MySQL with a new function that works like a native (built-in) MySQL function such as or .

is the name that should be used in SQL statements to invoke the function. The clause indicates the type of the function's return value. As of MySQL 5.0.3, is a legal value after , but currently functions return string values and should be written like functions.

is the basename of the shared object file that contains the code that implements the function. The file must be located in a directory that is searched by your system's dynamic linker.

To create a function, you must have the and privilege for the database. This is necessary because adds a row to the system table that records the function's name, type, and shared library name. If you do not have this table, you should run the mysql_upgrade command to create it. See Section 5.6.2, “mysql_upgrade — Check Tables for MySQL Upgrade”.

An active function is one that has been loaded with and not removed with . All active functions are reloaded each time the server starts, unless you start mysqld with the option. In this case, UDF initialization is skipped and UDFs are unavailable.

For instructions on writing user-defined functions, see Section 24.2.4, “Adding a New User-Defined Function”. For the UDF mechanism to work, functions must be written in C or C++, your operating system must support dynamic loading and you must have compiled mysqld dynamically (not statically).

An function works exactly like a native MySQL aggregate (summary) function such as or . For to work, your table must contain a column. If your table does not have this column, you should run the mysql_upgrade program to create it (see Section 5.6.2, “mysql_upgrade — Check Tables for MySQL Upgrade”).

24.2.3. DROP FUNCTION Syntax

DROP FUNCTION 

This statement drops the user-defined function (UDF) named .

To drop a function, you must have the privilege for the database. This is because removes a row from the system table that records the function's name, type, and shared library name.

24.2.4. Adding a New User-Defined Function

For the UDF mechanism to work, functions must be written in C or C++ and your operating system must support dynamic loading. The MySQL source distribution includes a file that defines 5 new functions. Consult this file to see how UDF calling conventions work.

A UDF contains code that becomes part of the running server, so when you write a UDF, you are bound by any and all constraints that otherwise apply to writing server code. For example, you may have problems if you attempt to use functions from the library. Note that these constraints may change in future versions of the server, so it is possible that server upgrades will require revisions to UDFs that were originally written for older servers. For information about these constraints, see Section 2.9.2, “Typical configure Options”, and Section 2.9.4, “Dealing with Problems Compiling MySQL”.

To be able to use UDFs, you need to link mysqld dynamically. Don't configure MySQL using . If you want to use a UDF that needs to access symbols from mysqld (for example, the function in that uses ), you must link the program with (see ). If you plan to use UDFs, the rule of thumb is to configure MySQL with unless you have a very good reason not to.

If you must use a precompiled distribution of MySQL, use MySQL-Max, which contains a dynamically linked server that supports dynamic loading.

For each function that you want to use in SQL statements, you should define corresponding C (or C++) functions. In the following discussion, the name “xxx” is used for an example function name. To distinguish between SQL and C/C++ usage, (uppercase) indicates an SQL function call, and (lowercase) indicates a C/C++ function call.

The C/C++ functions that you write to implement the interface for are:

  • (required)

    The main function. This is where the function result is computed. The correspondence between the SQL function data type and the return type of your C/C++ function is shown here:

    SQL Type C/C++ Type

    It is also possible to declare a function, but currently the value is returned as a string, so you should write the UDF as though it were a function.

  • (optional)

    The initialization function for . It can be used for the following purposes:

    • To check the number of arguments to .

    • To check that the arguments are of a required type or, alternatively, to tell MySQL to coerce arguments to the types you want when the main function is called.

    • To allocate any memory required by the main function.

    • To specify the maximum length of the result.

    • To specify (for functions) the maximum number of decimal places in the result.

    • To specify whether the result can be .

  • (optional)

    The deinitialization function for . It should deallocate any memory allocated by the initialization function.

When an SQL statement invokes , MySQL calls the initialization function to let it perform any required setup, such as argument checking or memory allocation. If returns an error, MySQL aborts the SQL statement with an error message and does not call the main or deinitialization functions. Otherwise, MySQL calls the main function once for each row. After all rows have been processed, MySQL calls the deinitialization function so that it can perform any required cleanup.

For aggregate functions that work like , you must also provide the following functions:

  • (required in 5.0)

    Reset the current aggregate value but do not insert the argument as the initial aggregate value for a new group.

  • (required)

    Add the argument to the current aggregate value.

MySQL handles aggregate UDFs as follows:

  1. Call to let the aggregate function allocate any memory it needs for storing results.

  2. Sort the table according to the expression.

  3. Call for the first row in each new group.

  4. Call for each new row that belongs in the same group.

  5. Call to get the result for the aggregate when the group changes or after the last row has been processed.

  6. Repeat 3-5 until all rows has been processed

  7. Call to let the UDF free any memory it has allocated.

All functions must be thread-safe. This includes not just the main function, but the initialization and deinitialization functions as well, and also the additional functions required by aggregate functions. A consequence of this requirement is that you are not allowed to allocate any global or static variables that change! If you need memory, you should allocate it in and free it in .

24.2.4.1. UDF Calling Sequences for Simple Functions

This section describes the different functions that you need to define when you create a simple UDF. Section 24.2.4, “Adding a New User-Defined Function”, describes the order in which MySQL calls these functions.

The main function should be declared as shown in this section. Note that the return type and parameters differ, depending on whether you declare the SQL function to return , , or in the statement:

For functions:

char *xxx(UDF_INIT *initid, UDF_ARGS *args,
          char *result, unsigned long *length,
          char *is_null, char *error);

For functions:

long long xxx(UDF_INIT *initid, UDF_ARGS *args,
              char *is_null, char *error);

For functions:

double xxx(UDF_INIT *initid, UDF_ARGS *args,
              char *is_null, char *error);

The initialization and deinitialization functions are declared like this:

my_bool xxx_init(UDF_INIT *initid, UDF_ARGS *args, char *message);

void xxx_deinit(UDF_INIT *initid);

The parameter is passed to all three functions. It points to a structure that is used to communicate information between functions. The structure members follow. The initialization function should fill in any members that it wishes to change. (To use the default for a member, leave it unchanged.)

  • should set to if can return . The default value is if any of the arguments are declared .

  • The number of decimal digits to the right of the decimal point. The default value is the maximum number of decimal digits in the arguments passed to the main function. (For example, if the function is passed , , and , the default would be 3, because has 3 decimal digits.

  • The maximum length of the result. The default value differs depending on the result type of the function. For string functions, the default is the length of the longest argument. For integer functions, the default is 21 digits. For real functions, the default is 13 plus the number of decimal digits indicated by . (For numeric functions, the length includes any sign or decimal point characters.)

    If you want to return a blob value, you can set to 65KB or 16MB. This memory is not allocated, but the value is used to decide which data type to use if there is a need to temporarily store the data.

  • A pointer that the function can use for its own purposes. For example, functions can use to communicate allocated memory among themselves. should allocate the memory and assign it to this pointer:

    initid->ptr = allocated_memory;
    

    In and , refer to to use or deallocate the memory.

  • should set to if always returns the same value and to otherwise.

24.2.4.2. UDF Calling Sequences for Aggregate Functions

This section describes the different functions that you need to define when you create an aggregate UDF. Section 24.2.4, “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 argument as the first value in your internal summary value for the group. Declare as follows:

    char *xxx_reset(UDF_INIT *initid, UDF_ARGS *args,
                    char *is_null, char *error);
    

    is not needed or used in MySQL 5.0, in which the UDF interface uses instead. However, you can define both and if you want to have your UDF work with older versions of the server. (If you do include both functions, the function in many cases can be implemented internally by calling to reset all variables, and then calling to add the argument 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 as follows:

    char *xxx_clear(UDF_INIT *initid, char *is_null, char *error);
    

    is set to point to before calling .

    If something went wrong, you can store a value in the variable to which the argument points. points to a single-byte variable, not to a string buffer.

    is required by MySQL 5.0.

  • This function is called for all rows that belong to the same group, except for the first row. You should use it to add the value in the argument to your internal summary variable.

    char *xxx_add(UDF_INIT *initid, UDF_ARGS *args,
                  char *is_null, char *error);
    

The function for an aggregate UDF should be declared the same way as for a non-aggregate UDF. See Section 24.2.4.1, “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 should be done the same way as for a non-aggregate UDF. See Section 24.2.4.4, “UDF Return Values and Error Handling”.

The and functions handle their argument the same way as functions for non-aggregate UDFs. See Section 24.2.4.3, “UDF Argument Processing”.

The pointer arguments to and are the same for all calls to , , and . You can use this to remember that you got an error or whether the function should return . You should not store a string into ! points to a single-byte variable, not to a string buffer.

is reset for each group (before calling ). is never reset.

If or are set when returns, MySQL returns as the result for the group function.

24.2.4.3. UDF Argument Processing

The parameter points to a structure that has the members listed here:

  • The number of arguments. Check this value in the initialization function if you require your function to be called with a particular number of arguments. For example:

    if (args->arg_count != 2)
    {
        strcpy(message,"XXX() requires two arguments");
        return 1;
    }
    
  • A pointer to an array containing the types for each argument. The possible type values are , , and .

    To make sure that arguments are of a given type and return an error if they are not, check the array in the initialization function. For example:

    if (args->arg_type[0] != STRING_RESULT ||
        args->arg_type[1] != INT_RESULT)
    {
        strcpy(message,"XXX() requires a string and an integer");
        return 1;
    }
    

    As an alternative to requiring your function's arguments to be of particular types, you can use the initialization function to set the elements to the types you want. This causes MySQL to coerce arguments to those types for each call to . For example, to specify that the first two arguments should be coerced to string and integer, respectively, do this in :

    args->arg_type[0] = STRING_RESULT;
    args->arg_type[1] = INT_RESULT;
    
  • communicates information to the initialization function about the general nature of the arguments passed to your function. For a constant argument , points to the argument value. (See below for instructions on how to access the value properly.) For a non-constant argument, is . A constant argument is an expression that uses only constants, such as or or . A non-constant argument is an expression that refers to values that may change from row to row, such as column names or functions that are called with non-constant arguments.

    For each invocation of the main function, contains the actual arguments that are passed for the row currently being processed.

    Functions can refer to an argument as follows:

    • An argument of type is given as a string pointer plus a length, to allow handling of binary data or data of arbitrary length. The string contents are available as and the string length is . You should not assume that strings are null-terminated.

    • For an argument of type , you must cast to a value:

      long long int_val;
      int_val = *((long long*) args->args[i]);
      
    • For an argument of type , you must cast to a value:

      double    real_val;
      real_val = *((double*) args->args[i]);
      
  • For the initialization function, the array indicates the maximum string length for each argument. You should not change these. For each invocation of the main function, contains the actual lengths of any string arguments that are passed for the row currently being processed. For arguments of types or , still contains the maximum length of the argument (as for the initialization function).

24.2.4.4. UDF Return Values and Error Handling

The initialization function should return if no error occurred and otherwise. If an error occurs, should store a null-terminated error message in the parameter. The message is returned to the client. The message buffer is characters long, but you should try to keep the message to less than 80 characters so that it fits the width of a standard terminal screen.

The return value of the main function is the function value, for and functions. A string function should return a pointer to the result and set and to the contents and length of the return value. For example:

memcpy(result, "result string", 13);
*length = 13;

The buffer that is passed to the function is 255 bytes long. If your result fits in this, you don't have to worry about memory allocation for results.

If your string function needs to return a string longer than 255 bytes, you must allocate the space for it with in your function or your function and free it in your function. You can store the allocated memory in the slot in the structure for reuse by future calls. See Section 24.2.4.1, “UDF Calling Sequences for Simple Functions”.

To indicate a return value of in the main function, set to :

*is_null = 1;

To indicate an error return in the main function, set to :

*error = 1;

If sets to for any row, the function value is for the current row and for any subsequent rows processed by the statement in which was invoked. ( is not even called for subsequent rows.)

24.2.4.5. Compiling and Installing User-Defined Functions

Files implementing UDFs must be compiled and installed on the host where the server runs. This process is described below for the example UDF file that is included in the MySQL source distribution.

The immediately following instructions are for Unix. Instructions for Windows are given later in this section.

The file contains the following functions:

  • returns a metaphon string of the string argument. This is something like a soundex string, but it's more tuned for English.

  • returns the sum of the ASCII values of the characters in its arguments, divided by the sum of the length of its arguments.

  • returns the sum of the length of its arguments.

  • returns a sequence starting from the given number or 1 if no number has been given.

  • returns the IP number for a hostname.

  • returns the hostname for an IP number. The function may be called either with a single string argument of the form or with four numbers.

A dynamically loadable file should be compiled as a sharable object file, using a command something like this:

shell> 

If you are using gcc, you should be able to create with a simpler command:

shell> 

You can easily determine the correct compiler options for your system by running this command in the directory of your MySQL source tree:

shell> 

You should run a compile command similar to the one that make displays, except that you should remove the option near the end of the line and add to the end of the line. (On some systems, you may need to leave the on the command.)

After you compile a shared object containing UDFs, you must install it and tell MySQL about it. Compiling a shared object from produces a file named something like (the exact name may vary from platform to platform). Copy this file to some directory such as that searched by your system's dynamic (runtime) linker, or add the directory in which you placed the shared object to the linker configuration file (for example, ).

The dynamic linker name is system-specific (for example, ld-elf.so.1 on FreeBSD, ld.so on Linux, or dyld on Mac OS X). Consult your system documentation for information about the linker name and how to configure it.

On many systems, you can also set the or environment variable to point at the directory where you have the files for your UDF. The manual page tells you which variable you should use on your system. You should set this in mysql.server or mysqld_safe startup scripts and restart mysqld.

On some systems, the ldconfig program that configures the dynamic linker does not recognize a shared object unless its name begins with . In this case you should rename a file such as to .

On Windows, you can compile user-defined functions by using the following procedure:

  1. You need to obtain the BitKeeper source repository for MySQL 5.0. See Section 2.9.3, “Installing from the Development Source Tree”.

  2. In the source repository, look in the directory. There are files named , , and there.

  3. In the source repository, look in the directory. Copy the from this directory to the directory and rename the file to .

  4. Open the file with Visual Studio VC++ and use it to compile the UDFs as a normal project.

After the shared object file has been installed, notify mysqld about the new functions with these statements:

mysql> 
mysql> 
mysql> 
mysql> 
mysql> 
    ->        
mysql> 
    ->        

Functions can be deleted using :

mysql> 
mysql> 
mysql> 
mysql> 
mysql> 
mysql> 

The and statements update the system table in the database. The function's name, type and shared library name are saved in the table. You must have the and privileges for the database to create and drop functions.

You should not use to add a function that has previously been created. If you need to reinstall a function, you should remove it with and then reinstall it with . You would need to do this, for example, if you recompile a new version of your function, so that mysqld gets the new version. Otherwise, the server continues to use the old version.

An active function is one that has been loaded with and not removed with . All active functions are reloaded each time the server starts, unless you start mysqld with the option. In this case, UDF initialization is skipped and UDFs are unavailable.

24.2.4.6. User-Defined Function Security Precautions

MySQL takes the following measures to prevent misuse of user-defined functions.

You must have the privilege to be able to use and the privilege to be able to use . This is necessary because these statements add and delete rows from the table.

UDFs should have at least one symbol defined in addition to the symbol that corresponds to the main function. These auxiliary symbols correspond to the , , , , and functions. As of MySQL 5.0.3, mysqld supports an option that controls whether UDFs that have only an symbol can be loaded. By default, the option is off, to prevent attempts at loading functions from shared object files other than those containing legitimate UDFs. If you have older UDFs that contain only the symbol and that cannot be recompiled to include an auxiliary symbol, it may be necessary to specify the option. Otherwise, you should avoid enabling this capability.

UDF object files cannot be placed in arbitrary directories. They must be located in some system directory that the dynamic linker is configured to search. To enforce this restriction and prevent attempts at specifying pathnames outside of directories searched by the dynamic linker, MySQL checks the shared object file name specified in statements for pathname delimiter characters. As of MySQL 5.0.3, MySQL also checks for pathname delimiters in filenames stored in the table when it loads functions. This prevents attempts at specifying illegitimate pathnames through direct manipulation of the table. For information about UDFs and the runtime linker, see Section 24.2.4.5, “Compiling and Installing User-Defined Functions”.

24.2.5. Adding a New Native Function

The procedure for adding a new native function is described here. Note that you cannot add native functions to a binary distribution because the procedure involves modifying MySQL source code. You must compile MySQL yourself from a source distribution. Also note that if you migrate to another version of MySQL (for example, when a new version is released), you need to repeat the procedure with the new version.

To add a new native MySQL function, follow these steps:

  1. Add one line to that defines the function name in the array.

  2. If the function prototype is simple (just takes zero, one, two or three arguments), you should in specify ) (where is the number of arguments) as the second argument in the array and add a function that creates a function object in . Take a look at and for an example of this.

    If the function prototype is complicated (for example, if it takes a variable number of arguments), you should add two lines to . One indicates the preprocessor symbol that yacc should define (this should be added at the beginning of the file). Then define the function parameters and add an “item” with these parameters to the parsing rule. For an example, check all occurrences of in to see how this is done.

  3. In , declare a class inheriting from or , depending on whether your function returns a number or a string.

  4. In , add one of the following declarations, depending on whether you are defining a numeric or string function:

    double   Item_func_newname::val()
    longlong Item_func_newname::val_int()
    String  *Item_func_newname::Str(String *str)
    

    If you inherit your object from any of the standard items (like ), you probably only have to define one of these functions and let the parent object take care of the other functions. For example, the class defines a function that executes on the value returned by .

  5. You should probably also define the following object function:

    void Item_func_newname::fix_length_and_dec()
    

    This function should at least calculate based on the given arguments. is the maximum number of characters the function may return. This function should also set if the main function can't return a value. The function can check whether any of the function arguments can return by checking the arguments' variable. You can take a look at for a typical example of how to do this.

All functions must be thread-safe. In other words, don't use any global or static variables in the functions without protecting them with mutexes)

If you want to return , from , or you should set to 1 and return 0.

For object functions, there are some additional considerations to be aware of:

  • The argument provides a string buffer that may be used to hold the result. (For more information about the type, take a look at the file.)

  • The function should return the string that holds the result or if the result is .

  • All current string functions try to avoid allocating any memory unless absolutely necessary!