tests/sims/joins.test.ts
The automated tests for the /simulators/sql-query-execution simulator. They run the model on its own, outside the browser, and compare it with a reference that doesn't depend on the model being right. They run on every change to the simulator's code.
- The textbook column matches the formulas, and the simulated I/O equals them when their assumptions hold.
- The book's worked example: 1,000 pages of Reserves joined with 500 pages of Sailors.
// /simulators/sql-query-execution: page I/O of the join algorithms, against the cost formulas in
// Ramakrishnan and Gehrke, Database Management Systems, chapter 14.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { run, type Input } from '../../src/lib/sims/joins.ts';
import { checker, lcg } from './check.ts';
/** External merge sort passes with B buffers: one pass to make ⌈P/B⌉ runs, then (B−1)-way merges. */
function passes(P: number, B: number) {
let runs = Math.ceil(P / B), p = 1;
while (runs > 1) { runs = Math.ceil(runs / (B - 1)); p++; }
return p;
}
function formulas(i: Input) {
const blk = i.outFrame ? i.B - 2 : i.B - 1;
return {
simple: i.M + i.m * i.N,
page: i.M + i.M * i.N,
block: i.M + Math.ceil(i.M / blk) * i.N,
sortmerge: 2 * i.M * passes(i.M, i.B) + 2 * i.N * passes(i.N, i.B) + i.M + i.N,
hash: 3 * (i.M + i.N)
};
}
test('the textbook column matches the formulas, and the simulated I/O equals them when their assumptions hold', () => {
const { ok, done } = checker();
const r = lcg(3);
let compared = 0;
for (let it = 0; it < 400; it++) {
const M = 1 + Math.floor(r() * 120), N = 1 + Math.floor(r() * 120), pR = 1 + Math.floor(r() * 20), pS = 1 + Math.floor(r() * 20), B = 3 + Math.floor(r() * 40);
for (const refined of [false, true]) for (const outFrame of [true, false]) {
const inp: Input = { M, m: M * pR, N, n: N * pS, B, C: 2, outFrame, refined };
const f = formulas(inp);
const res = Object.fromEntries(run(inp).map((x) => [x.algo, x]));
const s = JSON.stringify(inp);
ok(res.simple.textbook === f.simple && res.page.textbook === f.page && res.block.textbook === f.block, `${s}: nested loop formulas`);
ok(res.hash.textbook === f.hash, `${s}: hash formula ${res.hash.textbook} vs ${f.hash}`);
if (!refined) ok(res.sortmerge.textbook === f.sortmerge, `${s}: sort-merge formula ${res.sortmerge.textbook} vs ${f.sortmerge}`);
// the formulas assume the inner table does not fit in memory (nested loops) and enough buffers (refined sort-merge)
if (N > B - 2) { ok(res.simple.total === f.simple, `${s}: simple ${res.simple.total} vs ${f.simple}`); ok(res.page.total === f.page, `${s}: page ${res.page.total} vs ${f.page}`); compared++; }
if (N > 1) ok(res.block.total === f.block, `${s}: block ${res.block.total} vs ${f.block}`);
if (!refined) ok(res.sortmerge.total === f.sortmerge, `${s}: sort-merge ${res.sortmerge.total} vs ${f.sortmerge}`);
if (refined && Math.ceil(M / B) + Math.ceil(N / B) <= B - 1) ok(res.sortmerge.total === 3 * (M + N), `${s}: refined sort-merge ${res.sortmerge.total} vs ${3 * (M + N)}`);
// Grace hash join writes partially filled last pages, at most one per partition per table
if (!res.hash.note) ok(res.hash.total >= f.hash && res.hash.total <= f.hash + 4 * (B - 1), `${s}: hash ${res.hash.total} vs ${f.hash}`);
for (const x of Object.values(res)) if (x.ok) ok(x.rows === inp.m, `${s}: ${x.algo} produced ${x.rows} rows, expected ${inp.m}`);
}
}
ok(compared > 500, `only ${compared} cases met the nested-loop assumption`);
done();
});
test("the book's worked example: 1,000 pages of Reserves joined with 500 pages of Sailors", () => {
// R&G 14.4: Reserves M = 1000 pages (100 tuples each), Sailors N = 500 pages (80 tuples each)
const res = Object.fromEntries(run({ M: 1000, m: 100000, N: 500, n: 40000, B: 102, C: 2.2, outFrame: true, refined: false }).map((x) => [x.algo, x]));
assert.equal(res.simple.textbook, 1000 + 100000 * 500);
assert.equal(res.page.textbook, 1000 + 1000 * 500);
assert.equal(res.block.textbook, 1000 + 10 * 500); // blocks of 100 pages
assert.equal(res.hash.textbook, 4500);
});
Shared helpers: tests/sims/check.ts. The model under test is imported from src/lib/sims/.