Add a function
Last updated
Was this helpful?
Last updated
Was this helpful?
To add a builtin function to TiDB the best practice is to look at MySQL first and try to implement the function in such a way that it is commpatible. Avoid adding functions that are already deprecated in MySQL or that might soon be deprecrated.
Here we will implement a HELLO()
function that has one argument that is a string. For this you need
The first step is to define the name of the function in parser/ast/functions.go
:
This links ast.Hello
with "hello". Note that the lookup for the function is done with the lowercase name, so always use the lowercase name, otherwise it won't find the function.
The next step is to modify expression/builtin.go
Now we need to define helloFunctionClass
. We will do this in expression/builtin_string.go
. The functions are organised in multiple files, pick the one that fits best.
The getFunction()
method can return different functions depending on the type and number of arguments. This example always returns the same function that has one string argument and returns a string.
Here evalString()
gets called for every row. If the function returns an integer you have to use evalInt
and there are also functions for Decimal, Real, Time and JSON.
The final result:
To show the function with multiple rows:
Now you need to again and try the newly added function.
For have a look at expression/builtin_string_test.go
.