Errors while creating a UDF function in MySQL
I am trying to create a UDF function for MySQL (a C function performed by MySQL) and I am facing left and right issues. I'm just going to give you the code. I have a file levenshtein.c
that includes the following declarations and corresponding implementations:
my_bool sql_levenshtein_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void sql_levenshtein_deinit(UDF_INIT *initid);
longlong sql_levenshtein(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
I will compile the file (against std = c99 and for arch = i386 if needed). I copy the generated .so file to my mysql plugin folder. I go to mysql and execute the command
mysql> create function sql_levenshtein returns int soname 'levenshtein.so';
Instead of this command successfully creating this function, I am getting this error
ERROR 1127 (HY000): Can't find symbol 'sql_levenshtein' in library
In trying to solve the problem, I haven't come up with a lot of useful information. After googling, I used the command nm
to look at the .so file. And I got this output:
00001600 t ___inline_memset_chk
00001870 t ___inline_strcpy_chk
U ___memset_chk
U ___stack_chk_fail
U ___stack_chk_guard
U ___strcpy_chk
00001220 T _cell_value
U _free
00001180 T _is_levenshtein_within_normthresh
00001100 T _is_levenshtein_within_thresh
00001460 T _levenshtein
00000cf0 T _levenshtein_k
U _malloc
00001400 t _minimum
00001660 T _normlevenshtein
000018f0 T _sql_levenshtein
000018c0 T _sql_levenshtein_deinit
00001710 T _sql_levenshtein_init
00001df0 T _sql_levenshtein_within_normthreshold
00001dd0 T _sql_levenshtein_within_normthreshold_deinit
00001d00 T _sql_levenshtein_within_normthreshold_init
00001c00 T _sql_normlevenshtein
00001be0 T _sql_normlevenshtein_deinit
00001b20 T _sql_normlevenshtein_init
U dyld_stub_binder
Note _sql_levenshtein
, so the function seems to exist. Not sure what else would be helpful. Thank you for your guidance!
Kurt
========= Update in response to comment: Well, for what it's worth, I also did
mysql> create function _sql_levenshtein returns int soname 'levenshtein.so';
ERROR 1127 (HY000): Can't find symbol '_sql_levenshtein' in library
Therefore, this does not seem to be the root cause of the problem. Maybe I missed something.
source to share
I don't know if you found your answer, but when you create your function in MySQL, you must use the name of the function you defined. So, if you have a UDF called "my_func_udf" and you have something called My_Func () in your code, in your case, you called it:
longlong sql_levenshtein(UDF_INIT *initid, UDF_ARGS *args, char *is_null, char *error);
This is where all the magic happens, you have to use the name of this function in your create statement i.e. CREATE FUNCTION My_Func RETURNS (STRING, INTEGER, REAL) SONAME "YOUR FILE NAME".
example from your code above:
CREATE FUNCTION sql_levenshtein RETURNS INTEGER SONAME "levenshtein.so";
You defined something and then call something else, it is your fault.
source to share