利用大型语言模型实现数据库原生函数的自动化代码合成
Automating Database-Native Function Code Synthesis with LLMs
April 2, 2026
作者: Wei Zhou, Xuanhe Zhou, Qikang He, Guoliang Li, Bingsheng He, Quanqing Xu, Fan Wu
cs.AI
摘要
数据库系统内核中集成了日益丰富的原生函数,以支持新应用场景和业务迁移需求。这种增长催生了对数据库原生函数自动合成的迫切需求。尽管基于大语言模型的代码生成技术(如Claude Code)展现出潜力,但其通用性难以满足数据库特定开发需求。由于函数合成本身具有复杂性和易错性——单个函数的实现可能涉及多个功能单元注册、内部引用关联及逻辑正确实现,现有方法常出现逻辑幻觉或关键上下文遗漏。为此,我们提出DBCooker,基于大语言模型的数据库原生函数自动合成系统。该系统包含三大组件:首先,函数特征提取模块聚合多源声明,识别需专项编码的功能单元并追踪跨单元依赖关系;其次,我们设计了三项核心操作应对合成挑战:(1)基于伪代码的编码计划生成器,通过识别可复用函数等关键要素构建结构化实现框架;(2)融合概率先验与组件感知的混合填空模型,将核心逻辑与可复用例程集成;(3)包含语法检查、标准符合性验证及大语言模型引导语义核查的三级渐进验证机制。最后,自适应编排策略通过相似函数的编排历史动态调度这些操作,并与现有工具链协同。实验表明,DBCooker在SQLite、PostgreSQL和DuckDB上平均合成准确率提升34.55%,并能成功合成最新版SQLite(v3.50)中缺失的新函数。
English
Database systems incorporate an ever-growing number of functions in their kernels (a.k.a., database native functions) for scenarios like new application support and business migration. This growth causes an urgent demand for automatic database native function synthesis. While recent advances in LLM-based code generation (e.g., Claude Code) show promise, they are too generic for database-specific development. They often hallucinate or overlook critical context because database function synthesis is inherently complex and error-prone, where synthesizing a single function may involve registering multiple function units, linking internal references, and implementing logic correctly. To this end, we propose DBCooker, an LLM-based system for automatically synthesizing database native functions. It consists of three components. First, the function characterization module aggregates multi-source declarations, identifies function units that require specialized coding, and traces cross-unit dependencies. Second, we design operations to address the main synthesis challenges: (1) a pseudo-code-based coding plan generator that constructs structured implementation skeletons by identifying key elements such as reusable referenced functions; (2) a hybrid fill-in-the-blank model guided by probabilistic priors and component awareness to integrate core logic with reusable routines; and (3) three-level progressive validation, including syntax checking, standards compliance, and LLM-guided semantic verification. Finally, an adaptive orchestration strategy unifies these operations with existing tools and dynamically sequences them via the orchestration history of similar functions. Results show that DBCooker outperforms other methods on SQLite, PostgreSQL, and DuckDB (34.55% higher accuracy on average), and can synthesize new functions absent in the latest SQLite (v3.50).