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