How to Add a Built-in Function to TiDB Using a Cursor in 20 Minutes
作者: ngaut 原文来源:https://tidb.net/blog/aebad6f1
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 CursorStart by launching the Cursor tool. Cursor will allow you to browse through the TiDB source code directory seamlessly.1. Open the TiDB Source Code: Use the cursor to open any file within the project directory.2. MacOS Users: Hit “Command + L” to open the chatting window for further interactions with Cursor.### Step 2: Define the Function SignatureNext, 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 FilesYou 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 FunctionOnce 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 FunctionFinally, 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.
ConclusionAdding 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!
版权声明: 本文为 InfoQ 作者【TiDB 社区干货传送门】的原创文章。
原文链接:【http://xie.infoq.cn/article/d5528ea94cf675993a6257421】。文章转载请联系作者。
评论