[Paper Review] First-Class Functions for First-Order Database Engines
This paper introduces query defunctionalization, a non-invasive transformation that enables first-order database engines to natively execute queries involving first-class functions—treated as values, passed as arguments, and returned from higher-order functions. By encoding function closures as data structures and rewriting function applications into table-like dispatch mechanisms, the approach allows efficient and faithful execution of XQuery 3.0 and PL/SQL-style functional queries on standard relational engines, with experimental results showing strong performance and correctness.
We describe Query Defunctionalization which enables off-the-shelf first-order database engines to process queries over first-class functions. Support for first-class functions is characterized by the ability to treat functions like regular data items that can be constructed at query runtime, passed to or returned from other (higher-order) functions, assigned to variables, and stored in persistent data structures. Query defunctionalization is a non-invasive approach that transforms such function-centric queries into the data-centric operations implemented by common query processors. Experiments with XQuery and PL/SQL database systems demonstrate that first-order database engines can faithfully and efficiently support the expressive "functions as data" paradigm.
Motivation & Objective
- To enable first-order database engines—designed for atomic values and data structures—to natively support first-class functions, which are first-class values in modern query languages like XQuery 3.0 and PL/SQL.
- To address the lack of native runtime support for functional values in traditional database kernels, which typically represent only atomic values, sequences, and XML nodes.
- To provide a non-invasive transformation technique that preserves the semantics of functional queries while mapping them to standard relational algebra operations.
- To demonstrate that existing first-order engines can efficiently and correctly execute expressive functional query patterns, including higher-order functions and function literals, without requiring runtime language extensions.
Proposed method
- Query defunctionalization transforms functional constructs—function literals, named function references, and dynamic calls—into data-centric representations using closure constructors with unique labels.
- Function literals are encoded as labeled closures that capture free variables in their environment, represented as tuples of values bound at closure creation time.
- Function applications are replaced with a dispatch mechanism using a case-of-branch table that maps closure labels to their corresponding bodies and environments.
- The transformation is syntax-directed and whole-query, ensuring semantic consistency by analyzing the full query context, including function declarations and their free variables.
- The approach leverages defunctionalization techniques from programming language theory, adapted specifically for XQuery 3.0 and PL/SQL, to map higher-order constructs to first-order relational operations.
- The transformation is implemented as a source-to-source compiler pass, preserving query semantics while enabling execution on standard database engines.
Experimental results
Research questions
- RQ1Can first-order database engines natively support first-class functions without runtime language extensions or kernel modifications?
- RQ2How can functional query patterns such as higher-order functions, function literals, and closures be faithfully transformed into equivalent first-order relational operations?
- RQ3What performance overhead, if any, is introduced by defunctionalization when executing complex functional queries on standard database engines?
- RQ4To what extent can defunctionalization preserve the expressiveness and clarity of functional programming idioms like map, fold, and associative maps in XQuery and PL/SQL?
- RQ5Can the transformation be applied uniformly across diverse query patterns, including nested closures and higher-order abstractions like fold-right?
Key findings
- Query defunctionalization successfully transforms XQuery 3.0 and PL/SQL queries with first-class functions into equivalent first-order relational operations, enabling execution on standard database engines.
- The transformation preserves semantic correctness, as demonstrated by the faithful execution of complex functional idioms such as fold-right and map implementations using function literals.
- Experiments with XQuery and PL/SQL systems show that defunctionalized queries achieve performance comparable to native functional implementations, with minimal overhead.
- The approach enables direct implementation of maps as functions from keys to values, which is more concise and declarative than the equivalent XML-based data structure in Figure 3.
- The closure representation via labeled dispatch tables allows efficient function application and supports nested closures, including those with free variables captured from outer scopes.
- The method is non-invasive and reusable across different database systems, as it operates as a source-to-source transformation without requiring changes to the underlying query engine.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.