https://github.com/MariaDBJones/levenshtein-udf
How to make levenshtein function a UDF
https://github.com/MariaDBJones/levenshtein-udf
Last synced: about 1 year ago
JSON representation
How to make levenshtein function a UDF
- Host: GitHub
- URL: https://github.com/MariaDBJones/levenshtein-udf
- Owner: MariaDBJones
- Created: 2019-02-26T17:11:13.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-07-11T21:53:14.000Z (almost 2 years ago)
- Last Synced: 2025-02-15T02:42:59.003Z (over 1 year ago)
- Language: C
- Size: 19.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# levenshtein-udf
How to create a levenshtein function as a UDF in MariaDB / MySQL
## Sources
The sources are currently located in the root directory on the machine: [levenshteinUDF.c](https://github.com/SylvainA77/levenshtein-udf/blob/master/levenshteinUDF.c). These are calculation functions that I downloaded and converted into MySQL-compatible libraries. The specs for the overlay can be found here: https://dev.mysql.com/doc/refman/5.6/en/adding-udf.html
Recommendation: Save the sources in an SVN repository. [levenshteinSP.sql](https://github.com/SylvainA77/levenshtein-udf/blob/master/levenshteinSP.sql) is only presented so that one can benchmark by himself the performance of compiled UDF.
## Libraries
The aforementioned sources are compiled into libraries using gcc : **gcc -o levenshteinUDF.so -shared levenshteinUDF.c `mysql_config --cflags` -fPIC** . It is crucial not to forget the -shared flag during compilation; otherwise, creating the function in MySQL will not go smoothly.
Recommendation: Place the libraries (the levenshteinUDF.so file in our case) in the /lib directory of the machine and then create a symbolic link of the same name from _/usr/local/mysql/lib/plugin_. This way, a program that needs to call these functions won't have to go through the database.
## Functions
Finally, you need to create the functions in MySQL to use them in queries. Use the following command: _CREATE FUNCTION RETURNS SONAME '';_
Example: In the levenshtein.so library, we have three functions: levenshtein, levenshtein_k, and levenshtein_ratio. The first two return an integer, while the last one, which we are interested in, returns a real number. To create the function in MySQL, use the following command: **CREATE FUNCTION levenshtein_ratio RETURNS REAL SONAME 'levenshteinUDF.so';**
We can now use it like any other function in a query.
Tip: The list of UDFs can be found using this query: _SELECT * FROM mysql.func_.
Mentioned in [](https://github.com/Vettabase/awesome-mariadb)