Algorithm Search⌘K

Techniqueadvanced

Data-centric query compilation

Also called produce-consume code generation, push-based compiled execution.

Compile each pipeline of a plan into one tight loop that pushes tuples through its operators, keeping values in registers.

Data-centric query compilation turns a query plan into machine code with one loop per pipeline. The loop reads a tuple from the source of the pipeline and pushes it through all operators of the pipeline, with the values in CPU registers, until it reaches a pipeline breaker such as the build side of a hash join, where the tuple is stored. The code is generated by two calls on each operator, produce and consume, which walk the plan once and leave no operator boundaries in the generated code. Neumann introduced the method for the HyPer system in 2011, and compared with the iterator model it moves the data, not the control, to the center.

In plain words

In the iterator model, each operator asks the one below it for the next row, again and again, through a function call. The row goes up the plan one hop at a time, and at each hop it is copied out of the fast registers of the CPU. Data-centric compilation writes a new program for each query. In that program, there is one loop for each stretch of the plan where rows can flow without stopping. The loop takes one row and does everything to it: the test of the filter, the lookup in the hash table, the update of the sum. The row stays in registers the whole time. Only where a row must wait, for example in a hash table that is being built, is it written to memory.

When it applies

  • The data is in memory, so the CPU work per tuple decides the speed.
  • Queries run long enough, or often enough, that the time to generate and compile code is small against the time to run it.
  • The plan is a tree of operators with clear pipelines: scans, filters, projections, hash joins and aggregations.
  • The engine can call a compiler while the query runs, as in just-in-time compilation. HyPer used LLVM for the generated code and called pre-compiled C++ for complex parts.

The template

Every operator implements PRODUCE, which asks it to make all its tuples, and CONSUME(a,s)(a, s), which tells it that its child ss has a tuple with the attributes aa in registers. These calls run at compile time, and each one emits code. The holes are the code each operator emits.

  1. PRODUCE(scan of R)
  2. 1emit "for each tuple t of R do"
  3. 2CONSUME(parent, attributes of t, this scan)
  4. PRODUCE(σ_p)
  5. 3PRODUCE(child)
  6. CONSUME(σ_p, a, s)
  7. 4emit "if p(a) then"
  8. 5CONSUME(parent, a, this)
  9. PRODUCE(hash join, build B, probe P)
  10. 6PRODUCE(B); PRODUCE(P) ▷ the build pipeline is emitted first
  11. CONSUME(hash join, a, s)
  12. 7if s = B then emit "insert a into the hash table under its key" ▷ the pipeline breaker
  13. 8else emit "for each match m of a in the hash table do"
  14. 9CONSUME(parent, a ∪ m, this)
  15. PRODUCE(γ, child c)
  16. 10PRODUCE(c); emit "for each group g of the table do"; CONSUME(parent, g, this)
  17. CONSUME(γ, a, s)
  18. 11emit "find or insert the group of a, and update its aggregates" ▷ the pipeline breaker

A call of PRODUCE on the root generates the whole program. The code of an operator ends up inside the loop of its pipeline. The test of a filter becomes an if inside the loop of the scan, and the probe of a join becomes a nested loop over the matches. At a pipeline breaker, CONSUME stores the tuple and the loop ends, and the breaker starts a new loop in its PRODUCE.

An example of generated code. For γR.g; SUM(S.v)(σR.x<10(R)⋈R.k=S.kS)\gamma_{R.g;\,\text{SUM}(S.v)}(\sigma_{R.x < 10}(R) \bowtie_{R.k = S.k} S) with SS as the build side, the calls give two pipelines plus the output loop:

for each tuple s of S do
    insert s into table H under s.k
for each tuple r of R do
    if r.x < 10 then
        for each match s of r.k in H do
            find or insert the group r.g in table G; add s.v to its sum
for each group g of G do
    output g

Each value of rr and ss is loaded once from memory and stays in a register for all operators in its loop. There is no call per tuple, and no operator boundary.

Why it is fast. In the iterator model, a tuple that passes dd operators costs dd calls of next through pointers, which the compiler cannot inline, and each call writes and reads the tuple in memory. In the generated code, the same tuple costs the work of the operators and nothing else. For a cache-resident pipeline, this is close to the program that a programmer would write by hand for that query.

Classic examples

  • HyPer on TPC-CH. Neumann (2011) ran the analytic queries of TPC-CH on a machine with two Intel X5570 quad-core CPUs and 64 GB of memory (a measurement). For Query 1, HyPer with LLVM took 35 ms, HyPer with generated C++ took 142 ms, Vectorwise 98 ms, MonetDB 72 ms, and a commercial system 4221 ms. For Query 5, the times were 1105 ms, 1416 ms, 1107 ms, 12028 ms and 15212 ms.
  • Compile time. On the same machine, the TPC-C transactions compiled with the C++ backend took 16.53 s to compile, and with LLVM 0.81 s, with about the same throughput (a measurement). This is why HyPer generates LLVM code and not C++ source.
  • Typer against Tectorwise. Kersten et al. (2018) built a compiled engine (Typer) and a vectorized engine (Tectorwise) in the same test system, on an Intel i9-7900X with 10 cores among other machines (a measurement). On TPC-H at scale factor 100 with one thread, Query 1 took 4426 ms on Typer and 7871 ms on Tectorwise, while Query 9 took 28086 ms and 20371 ms. Their summary: compilation needs fewer CPU instructions, which helps queries whose data is in cache, and vectorization hides the latency of cache misses better, which helps joins with large hash tables. Overall the two were close in analytic workloads.

Variations

  • Targets. The generated code can be C or C++ source, LLVM code, or code for a virtual machine that an interpreter runs at first and a compiler compiles later. Some systems start with an interpreter so that a short query does not wait for the compiler.
  • Parallel pipelines. Morsel-driven parallelism runs the loop of one pipeline in many threads, each on a small piece of the input, and was built in the same system.
  • Mixed models. A compiled loop can process a batch of tuples in each step, or stop at chosen points to prefetch hash table entries, which brings back some of the benefits of vectorization for joins.
  • Relation to program specialization. Generating code for a fixed plan is a form of partial evaluation of the interpreter with respect to the plan, an instance of the first Futamura projection. Pushing a tuple through fused operators is close to loop fusion.

Pitfalls

  • Compile time on short queries. If a query runs in a few milliseconds, the time to compile can be larger than the time to run it. Systems need a fast path, such as an interpreter or cached code.
  • Large generated functions. A plan with many operators gives one very large loop, which is slow to compile and can be hard for register allocation. Complex operators are better called as pre-compiled functions.
  • Debugging and profiling. Generated code has no source lines that map to operators, so a profile says little about which operator is slow, unless the system adds that mapping.
  • Cache misses in joins. One tuple at a time gives the CPU only one hash lookup to work on. When the table is much larger than the cache, a vectorized loop with independent lookups can be faster, as in Query 9 above.
  • Measuring. All times above are measurements on given hardware, versions and benchmarks. They show the trends found by the authors, not bounds.

Exercises

  1. Write PRODUCE and CONSUME for a projection and for a LIMIT kk, and show the code they emit inside the loop of a scan.
  2. For the example plan, list the pipelines and the pipeline breakers. Which of them would change if RR were the build side?
  3. A pipeline of dd operators processes NN tuples. Count the calls of next in the iterator model, and the function calls in the generated code, for N=106N = 10^6 and d=5d = 5.
  4. Explain why Query 1 of TPC-H, an aggregation over one table with a few groups, gains more from compilation than Query 9, a query with several large joins.

References

  1. Thomas Neumann, Efficiently compiling efficient query plans for modern hardware, Proceedings of the VLDB Endowment 4(9), 539 to 550 (2011), the definition of pipeline breakers, the produce and consume interface, Section 4 (code generation with LLVM and C++) and Section 6 (evaluation)
  2. Timo Kersten, Viktor Leis, Alfons Kemper, Thomas Neumann, Andrew Pavlo and Peter Boncz, Everything you always wanted to know about compiled and vectorized queries but were afraid to ask, Proceedings of the VLDB Endowment 11(13), 2209 to 2222 (2018), the comparison of the Typer and Tectorwise engines, and the summary of the strengths of each model
  3. Peter Boncz, Marcin Zukowski and Niels Nes, MonetDB/X100, hyper-pipelining query execution, Proceedings of CIDR 2005, 225 to 237, Section 3 (the hand-written program for TPC-H Query 1 as a lower bound for an engine)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • five exercises