import { DOMParser } from "@xmldom/xmldom"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { flattenExplainPlanNodes, parseExplainResult, sqlServerExplainResult } from "@/lib/diagram/explainPlan"; import { extractActualRows } from "@/lib/diagram/planCanvas"; import type { QueryResult } from "@/types/database"; // SET STATISTICS XML output: the SHOWPLAN_XML schema plus . // Gather Streams and its subtree run on 3 worker threads; the nested loop inner // seek runs 100 times in total and returns 10 rows per execution against a // 10-row estimate, so its cumulative 1000 rows are a 100x apparent overrun. const STATISTICS_XML = ` `; // A serial statement: one thread, one execution, no normalization to apply. const SERIAL_STATISTICS_XML = ` `; function result(columns: string[], rows: unknown[][]): QueryResult { return { columns, rows, affected_rows: 0, execution_time_ms: 1 }; } describe("SQL Server actual execution plan", () => { beforeEach(() => { vi.stubGlobal("DOMParser", DOMParser); }); afterEach(() => { vi.unstubAllGlobals(); }); // Parsed lazily: the DOMParser stub only exists once beforeEach has run. const parse = (xml: string) => flattenExplainPlanNodes(parseExplainResult("sqlserver", result(["Microsoft SQL Server 2005 XML Showplan"], [[xml]])).nodes); const node = (id: string) => parse(STATISTICS_XML).find((candidate) => candidate.id === id)!; const gatherStreams = () => node("0"); const innerSeek = () => node("3"); it("keeps the operator hierarchy the estimated plan already produced", () => { expect(parse(STATISTICS_XML).map((relOp) => relOp.nodeType)).toEqual(["Parallelism", "Nested Loops", "Index Scan", "Index Seek"]); expect(innerSeek()).toMatchObject({ relation: "dbo.orders", index: "ix_orders_customer", rows: "10", cost: "0.85" }); expect(innerSeek().details).toContain("Expression: [orders].[customer_id]=[customers].[id]"); }); it("sums measured rows across every worker thread", () => { expect(gatherStreams().details).toContain("Actual Rows: 1000"); expect(node("2").details).toContain("Actual Rows: 100"); expect(innerSeek().details).toContain("Actual Rows: 1000"); }); it("reports the slowest thread as elapsed time and the summed CPU work", () => { expect(gatherStreams().details).toContain("Actual Time: 121 ms"); expect(gatherStreams().details).toContain("Actual CPU: 114 ms"); expect(innerSeek().details).toContain("Actual Time: 97 ms"); expect(innerSeek().details).toContain("Actual CPU: 66 ms"); }); it("normalizes the repeated inner side of the nested loop to a per-execution figure", () => { expect(innerSeek().details).toContain("Actual Threads: 3"); expect(innerSeek().details).toContain("Actual Executions: 100"); expect(innerSeek().details).toContain("Actual Rows Per Execution: 10"); // The estimate is per execution, so this is what a mismatch check must read. expect(extractActualRows(innerSeek())).toBe(10); expect(Number(innerSeek().rows)).toBe(10); }); it("leaves parallel operators unnormalized, one execution per thread is still one execution", () => { for (const id of ["0", "1", "2"]) { expect(node(id).details.some((detail) => detail.startsWith("Actual Rows Per Execution"))).toBe(false); } expect(gatherStreams().details).toContain("Actual Threads: 3"); expect(gatherStreams().details).toContain("Actual Executions: 3"); expect(extractActualRows(gatherStreams())).toBe(1000); expect(extractActualRows(node("2"))).toBe(100); }); it("keeps a serial single-execution operator on the plain measured total", () => { const [root] = parseExplainResult("sqlserver", result(["Microsoft SQL Server 2005 XML Showplan"], [[SERIAL_STATISTICS_XML]])).nodes; expect(root.details).toContain("Actual Rows: 1200"); expect(root.details.some((detail) => detail.startsWith("Actual Threads"))).toBe(false); expect(root.details.some((detail) => detail.startsWith("Actual Executions"))).toBe(false); expect(root.details.some((detail) => detail.startsWith("Actual Rows Per Execution"))).toBe(false); expect(extractActualRows(root)).toBe(1200); }); it("adds no measured details to an estimated SHOWPLAN plan", () => { const estimated = STATISTICS_XML.replaceAll(/[\s\S]*?<\/RunTimeInformation>/g, ""); const parsed = parseExplainResult("sqlserver", result(["Microsoft SQL Server 2005 XML Showplan"], [[estimated]])); for (const node of flattenExplainPlanNodes(parsed.nodes)) { expect(node.details.some((detail) => detail.startsWith("Actual"))).toBe(false); expect(extractActualRows(node)).toBeUndefined(); } }); it("finds the plan when STATISTICS XML returns the query result set first", () => { const rowsResult = result(["id", "total"], [[1, "12.50"]]); const planResult = result(["Microsoft SQL Server 2005 XML Showplan"], [[STATISTICS_XML]]); expect(sqlServerExplainResult([rowsResult, planResult])).toEqual({ result: planResult }); }); });