paperbot · PL 论文追踪

全部论文 待读 收藏

P2 A SQL to C compiler in 500 lines of code

用 500 行代码实现的 SQL 到 C 编译器
JFP vol.292019匹配度 2/10 · 该论文属于编译原理与 DSL/部分求值方向,核心是 SQL 到 C 的生成式编程,与用户关注的 LLM/Agent 和静态分析、浮点程序分析、贝叶斯程序分析均无直接交集。仅可作为 PL 生成式编程与编译器实现的背景参考。引用 4 inbox
TIARK ROMPF, NADA AMIN
crossref: 2026-09-10

AI 速览

一句话:用 500 行 Scala 将 SQL 关系代数解释器通过 LMS 与 Futamura 第一投影编译为高性能 C 代码。

本文提出一个 SQL 查询处理器,其核心是用 Scala 写的关系代数解释器,整个实现约 500 行代码。作者利用开源轻量级模块化分阶段(LMS)框架,将解释器转变为查询编译器;其理论依据是部分求值中的 Futamura 第一投影:能把解释器特化到任意输入程序的机制等价于编译器。文中讨论了混合阶段数据结构(如静态模式与动态字段的数据记录)以及生成底层 C 代码的技术,包括专用数据结构和数据加载原语。结果表明,该处理器性能优于现有数据库系统,说明在追求极致性能的系统级编程中,高层函数式编程也能胜过 C。

问题
如何以极少代码实现高性能 SQL 查询处理,并证明高层函数式编程可用于性能敏感的系统级编程。
方法
用 Scala 实现关系代数解释器,借助 LMS 和 Futamura 第一投影将解释器分阶段特化为查询编译器,并生成含专用数据结构和数据加载原语的 C 代码。
评估
将实现的 SQL 查询处理器与现有数据库系统进行性能对比,报告其性能更优,且核心实现仅约 500 行 Scala。
原文摘要(Abstract)

Abstract We present the design and implementation of a SQL query processor that outperforms existing database systems and is written in just about 500 lines of Scala code – a convincing case study that high-level functional programming can handily beat C for systems-level programming where the last drop of performance matters. The key enabler is a shift in perspective toward generative programming. The core of the query engine is an interpreter for relational-algebra operations, written in Scala. Using the open-source lightweight modular staging framework, we turn this interpreter into a query compiler with very low effort. To do so, we capitalize on an old and widely known result from partial evaluation: the first Futamura projection, which states that a process that can specialize an interpreter to any given input program is equivalent to a compiler. In this context, we discuss lightweight modular staging programming patterns such as mixed-stage data structures (e.g., data records with static schema and dynamic field components) and techniques to generate low-level C code, including specialized data structures and data loading primitives.

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@article{paperbot733,
  title = {A SQL to C compiler in 500 lines of code},
  author = {TIARK ROMPF and NADA AMIN},
  journal = {Journal of Functional Programming},
  volume = {29},
  year = {2019},
  doi = {10.1017/s0956796819000054}
}