In this tutorial, we’ll walk you through the steps to add a flexible built-in function to TiDB that leverages LLM to process data. We’ll use the AI_PROCESS function as an example, which takes two parameters: a data string and a task_description string, returning a processed result.
Why Use a Cursor to Add Built-in Functions?
By using a tool like Cursor, developers can interact with the codebase of TiDB in an intuitive way, shortening development time. Cursor helps identify the necessary files to modify and can even suggest potential missing files. For this tutorial, we’ll rely on Cursor to streamline the process.
Step 1: Launch Cursor
Start by launching the Cursor tool. Cursor will allow you to browse through the TiDB source code directory seamlessly.
- Open the TiDB Source Code: Use the cursor to open any file within the project directory.
- MacOS Users: Hit “Command + L” to open the chatting window for further interactions with Cursor.
Step 2: Define the Function Signature
Next, define the function signature of the built-in function you want to add. In this case, it’s:
func AI_PROCESS(data string, task_description string) string
Step 3: Use Cursor to Locate the Files
You can ask Cursor to search through the codebase for the files where you need to make modifications. Start by inputting the following command in the Cursor chat window(I missed some files, but surprisingly cursor figured it out):
I want to add a built-in function to TiDB. The signature is: AI_PROCESS(data string task_description string). Locate all the files I need to change or add. Start with @parser.y, @builtin_other.go, @builtin.go, and @builtin_vectorized.go.
Cursor will identify all the required files, even if you miss a few. This is one of Cursor’s strengths—its ability to catch overlooked details.
Step 4: Implement the Stub Function
Once you have located the relevant files, the next step is to implement a stub function for AI_PROCESS. Again, Cursor can help you automatically fill in the necessary implementation.
Later, I asked the cursor to change the function a little bit to remove the json constraint.
Step 5: Testing the Function
Finally, type “make server” to build tidb-server, run it, and enjoy your new AI_PROCESS function in your local TiDB instance to ensure it works as expected.
Conclusion
Adding a built-in function to TiDB using Cursor can save you a significant amount of time by automating the file search process and providing helpful suggestions. In just 20 minutes, you can integrate an AI-powered function that enhances the flexibility of TiDB. Happy coding!