Go to the first, previous, next, last section, table of contents.


14 Adding new functions to MySQL

There are two ways to add new functions to MySQL:

14.1 Adding a new user-definable function

User-definable functions are added and removed using the CREATE FUNCTION and DROP FUNCTION statements. See section 7.29 CREATE FUNCTION/DROP FUNCTION syntax.

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 the file `udf_example.cc' that defines 5 new functions. Consult this file to see how UDF calling conventions work.

For each function xxx() that you define, you may also have a xxx_init() function and a xxx_deinit() function. The initialization function should allocate memory for the function and tell the main function about the maximum length of the result (for string-valued functions), the number of decimals (for double-valued functions) and whether or not the result may be a NULL value. The deinitialization function should deallocate any memory allocated by the initialization function.

If an instance of a function xxx() sets the "error" argument to 1, the function will no longer be called and mysqld will return NULL for all subsequent calls to this instance of the function.

All string arguments to functions are given as a string pointer plus a length, to allow handling of binary data. Remember that all functions must be thread-safe (not just the main function, but the initialization and deinitialization functions as well). This means that you are not allowed to allocate any global or static variables that change! If you need memory, you should allocate it in the xxx_init() function and free it in the xxx_deinit() function.

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 `udf_example.cc' that is included in the MySQL source distribution. This file contains the following functions:

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

shell> gcc -shared -o udf_example.so myfunc.cc

You can easily find out the correct compiler switches for your system by running this command in the `sql' directory of your MySQL source tree:

shell> make udf_example.o

You should run a compile command similar to the one that make displays, except that you should remove the -c option near the end of the line and add -o udf_example.so to the end of the line.

Once you compile a shared object containing UDFs, you must install it and tell MySQL about it. Compiling a shared object from `udf_example.cc' produces a file named something like `udf_example.so' (the exact name may vary from platform to platform). Copy this file to some directory searched by ld, such as `/usr/lib'.

After the library is installed, notify mysqld about the new functions with these commands:

mysql> CREATE FUNCTION metaphon RETURNS STRING SONAME "udf_example.so";
mysql> CREATE FUNCTION myfunc_double RETURNS REAL SONAME "udf_example.so";
mysql> CREATE FUNCTION myfunc_int RETURNS INTEGER SONAME "udf_example.so";
mysql> CREATE FUNCTION lookup RETURNS STRING SONAME "udf_example.so";
mysql> CREATE FUNCTION reverse_lookup RETURNS STRING SONAME "udf_example.so";

Functions should be created only once.

Functions can be deleted using DROP FUNCTION:

mysql> DROP FUNCTION metaphon;
mysql> DROP FUNCTION myfunc_double;
mysql> DROP FUNCTION myfunc_int;
mysql> DROP FUNCTION lookup;
mysql> DROP FUNCTION reverse_lookup;

The CREATE FUNCTION and DROP FUNCTION statements update the mysql.func table. All active functions are reloaded each time the server starts, unless --skip-grant-tables is specified on the command line. (An active function is one that has been loaded with CREATE and not removed with DROP.)

14.2 Adding a new native function

The procedure for adding a new native function is described below. Note that you cannot add native functions to a binary distribution since 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 (e.g., when a new version is released), you will need to repeat the procedure with the new version.

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

  1. Add one line to `sql_lex.cc' that defines the function name in the sql_functions[] array.
  2. Add two lines to `sql_yacc.yy'. 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 simple_expr parsing rule. For an example, check all occurrences of SOUNDEX in `sql_yacc.yy' to see how this is done.
  3. In `item_func.h', declare a class inheriting from Item_num_func or Item_str_func, depending on whether your function returns a number or a string.
  4. In `item_func.cc', add one of the following declarations, depending on whether you are defining a number function or a string function:
    double   Item_func_newname::val()
    longlong Item_func_newname::val_int()
    String  *Item_func_newname::Str(String *str)
    
  5. You should probably also define the following function:
    void Item_func_newname::fix_length_and_dec()
    
    This function should at least calculate max_length based on the given arguments. max_length is the maximum number of characters the function may return. This function should also set maybe_null = 0 if the main function can't return a NULL value.

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


Go to the first, previous, next, last section, table of contents.