写点什么

How to Add a Built-in Function to TiDB Using a Cursor in 20 Minutes

  • 2024-09-13
    北京
  • 本文字数:1325 字

    阅读完需:约 4 分钟

作者: 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!

发布于: 刚刚阅读数: 4
用户头像

TiDB 社区官网:https://tidb.net/ 2021-12-15 加入

TiDB 社区干货传送门是由 TiDB 社区中布道师组委会自发组织的 TiDB 社区优质内容对外宣布的栏目,旨在加深 TiDBer 之间的交流和学习。一起构建有爱、互助、共创共建的 TiDB 社区 https://tidb.net/

评论

发布
暂无评论
How to Add a Built-in Function to TiDB Using a Cursor in 20 Minutes_TiDB 源码解读_TiDB 社区干货传送门_InfoQ写作社区