mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-08 12:32:43 +00:00
The warm narrow query and the cold heavy query each referenced the canonical-source subquery twice — once in the window_stats CTE and once in the outer body — so the DISTINCT-ON pick over the live 90-day rows ran twice per rebuild. Extract canonicalMeasurementsCte (the SELECT body without the alias wrapper), bind it once as WITH cm AS (...), and select FROM cm m from both window_stats and the outer aggregate. Output is identical; the dominant self-join runs once. canonicalMeasurementsFrom now wraps the same body, byte-stable for the single-reference call sites (pinned by a new parity test).
60 lines
2.4 KiB
TypeScript
60 lines
2.4 KiB
TypeScript
/**
|
|
* Unit pins for the source-rank SQL builders.
|
|
*
|
|
* `canonicalMeasurementsCte` (v1.18.11 perf#3a) was extracted so a statement
|
|
* that references the canonical-source set more than once can bind it as a
|
|
* single `WITH cm AS (…)` CTE and fold away the duplicate DISTINCT-ON
|
|
* self-join. These tests pin:
|
|
* 1. the alias-wrapped `canonicalMeasurementsFrom` is exactly the CTE body
|
|
* wrapped in `( … ) m` — so the single-reference call site is unchanged;
|
|
* 2. the canonical body carries exactly ONE DISTINCT-ON pick — so a fold
|
|
* that references the CTE twice runs the expensive pick once;
|
|
* 3. the interval whitelist still rejects an unsafe splice.
|
|
*/
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
canonicalMeasurementsCte,
|
|
canonicalMeasurementsFrom,
|
|
} from "../source-rank-sql";
|
|
|
|
const RANK = "90";
|
|
|
|
describe("canonicalMeasurementsCte / canonicalMeasurementsFrom", () => {
|
|
it("wraps the CTE body verbatim in the ( … ) m alias", () => {
|
|
const cte = canonicalMeasurementsCte(RANK, "90 days");
|
|
const from = canonicalMeasurementsFrom(RANK, "90 days");
|
|
// The alias form is the CTE body plus the closing `) m` wrapper. Pinning
|
|
// equality means the single-reference call sites keep byte-stable SQL
|
|
// while the CTE body can be reused for the folded double-reference case.
|
|
expect(from).toBe(`(${cte}\n ) m`);
|
|
});
|
|
|
|
it("runs exactly one DISTINCT-ON canonical pick in the body", () => {
|
|
const cte = canonicalMeasurementsCte(RANK, "90 days");
|
|
const matches = cte.match(/DISTINCT ON/g) ?? [];
|
|
expect(matches).toHaveLength(1);
|
|
});
|
|
|
|
it("binds the user id as $1 and scopes both the inner and outer windows", () => {
|
|
const cte = canonicalMeasurementsCte(RANK, "90 days");
|
|
// user id is always a bound parameter, never spliced.
|
|
expect(cte).toContain('"user_id" = $1');
|
|
// The trailing-window bound appears on both the inner pick and the outer
|
|
// filter so the folded references stay scoped identically.
|
|
const intervalMatches = cte.match(/INTERVAL '90 days'/g) ?? [];
|
|
expect(intervalMatches).toHaveLength(2);
|
|
});
|
|
|
|
it("omits the window clause entirely when no interval is given", () => {
|
|
const cte = canonicalMeasurementsCte(RANK);
|
|
expect(cte).not.toContain("INTERVAL");
|
|
});
|
|
|
|
it("rejects an unsafe interval splice", () => {
|
|
expect(() => canonicalMeasurementsCte(RANK, "90 days; DROP TABLE")).toThrow(
|
|
/unsafe interval/,
|
|
);
|
|
});
|
|
});
|