From b0364d305ddfd2af8f3cc2f9b462e54d4269ab71 Mon Sep 17 00:00:00 2001
From: SpookyJumpyBeans <116844292+SpookyJumpyBeans@users.noreply.github.com>
Date: Sat, 3 Oct 2026 23:39:01 -0400
Subject: [PATCH 1/2] Show that Run did something when the answer has not
changed
The page runs the first preset on load, so clicking Run again redrew
the same rows and a near-identical plan, and looked like a dead button.
Each run now shows a Running state (after yielding a frame, since Python
blocks the main thread), numbers itself in the status line, and briefly
highlights the plan and result panels.
---
site/app.js | 69 +++++++++++++++++++++++++++++++++++++-------------
site/style.css | 11 ++++++++
2 files changed, 62 insertions(+), 18 deletions(-)
diff --git a/site/app.js b/site/app.js
index 6ccb460..599f300 100644
--- a/site/app.js
+++ b/site/app.js
@@ -69,26 +69,59 @@ function renderPresets(presets) {
el.sql.value = presets[0].sql;
}
-function runQuery() {
- if (!demo) return;
- const started = performance.now();
- const result = JSON.parse(demo.run(el.sql.value));
- const elapsed = performance.now() - started;
-
- if (result.error) {
- setStatus("Query failed", true);
- const message = document.createElement("p");
- message.className = "error-text";
- message.textContent = result.error;
- el.results.replaceChildren(message);
- el.plan.textContent = "";
- return;
+let runCount = 0;
+let running = false;
+
+// Python runs on the main thread, so the page cannot repaint until a query
+// finishes. Wait for a frame first, or "Running…" would never be seen.
+const nextFrame = () => new Promise((resolve) => requestAnimationFrame(() => setTimeout(resolve)));
+
+async function runQuery() {
+ if (!demo || running) return;
+ running = true;
+ el.run.disabled = true;
+ el.run.textContent = "Running…";
+ setStatus("Running…");
+ await nextFrame();
+
+ try {
+ const started = performance.now();
+ const result = JSON.parse(demo.run(el.sql.value));
+ const elapsed = performance.now() - started;
+ runCount += 1;
+
+ if (result.error) {
+ setStatus(`Run ${runCount}: query failed`, true);
+ const message = document.createElement("p");
+ message.className = "error-text";
+ message.textContent = result.error;
+ el.results.replaceChildren(message);
+ el.plan.textContent = "";
+ } else {
+ const n = result.rows.length;
+ setStatus(
+ `Run ${runCount}: ${n.toLocaleString()} row${n === 1 ? "" : "s"} in ` +
+ `${elapsed.toFixed(0)}ms, orders v${result.version}`,
+ );
+ el.plan.textContent = result.text;
+ renderRows(result.columns, result.rows);
+ }
+ flash(el.plan, el.results);
+ } finally {
+ running = false;
+ el.run.disabled = false;
+ el.run.textContent = "Run";
}
+}
- const n = result.rows.length;
- setStatus(`${n.toLocaleString()} row${n === 1 ? "" : "s"} in ${elapsed.toFixed(0)}ms, orders v${result.version}`);
- el.plan.textContent = result.text;
- renderRows(result.columns, result.rows);
+// Re-running a query often produces the same rows and nearly the same plan,
+// so mark the panels as refreshed or the click looks like it did nothing.
+function flash(...panels) {
+ for (const panel of panels) {
+ panel.classList.remove("refreshed");
+ void panel.offsetWidth; // restart the animation
+ panel.classList.add("refreshed");
+ }
}
function renderRows(columns, rows) {
diff --git a/site/style.css b/site/style.css
index d0c6ea3..07e7e88 100644
--- a/site/style.css
+++ b/site/style.css
@@ -175,3 +175,14 @@ th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
background: var(--code-bg);
color: var(--muted);
}
+
+.refreshed { animation: refreshed 0.6s ease-out; }
+
+@keyframes refreshed {
+ from { box-shadow: 0 0 0 2px var(--accent); }
+ to { box-shadow: 0 0 0 2px transparent; }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .refreshed { animation: none; }
+}
From b630ede09a367e08977e1bac6486fd9d3544dc14 Mon Sep 17 00:00:00 2001
From: SpookyJumpyBeans <116844292+SpookyJumpyBeans@users.noreply.github.com>
Date: Sat, 3 Oct 2026 23:45:42 -0400
Subject: [PATCH 2/2] Draw the plan as a tree with estimated vs actual row bars
Each operator gets a pair of bars, estimated rows over actual rows, on a
log scale shared by the whole plan, so a gap of the same size means the
same factor anywhere in the tree. Nodes off by the profiler's
MISESTIMATE_FACTOR or more are flagged with an icon and label. demo.run()
now returns that factor, so the page and the text output flag the same
nodes.
Hovering or focusing a row shows the exact counts and both timings. The
text rendering stays one click away. Below 640px each operator stacks
above full-width bars rather than hiding them behind a sideways scroll.
The plan now spans the full width under the query and result.
---
README.md | 4 +-
site/app.js | 39 +++++++++-
site/build.py | 2 +-
site/demo.py | 8 +-
site/index.html | 19 ++++-
site/plan.js | 158 ++++++++++++++++++++++++++++++++++++++++
site/style.css | 155 ++++++++++++++++++++++++++++++++++++++-
tests/test_site_demo.py | 2 +-
8 files changed, 372 insertions(+), 15 deletions(-)
create mode 100644 site/plan.js
diff --git a/README.md b/README.md
index ec80133..2a0c5d3 100644
--- a/README.md
+++ b/README.md
@@ -212,7 +212,9 @@ pytest
`site/` is a static page that runs tinyquery and tinydelta in the browser with
[Pyodide](https://pyodide.org). It builds a small shop -- a tinydelta `orders`
table with six versions, plus customers and line items -- and runs every query
-under `EXPLAIN ANALYZE`.
+under `EXPLAIN ANALYZE`. The plan is drawn as a tree, with estimated and actual
+rows as paired bars on one log scale, and any operator off by 10x or more is
+flagged, the same as in the text output.
```bash
pip install -e ".[delta]"
diff --git a/site/app.js b/site/app.js
index 599f300..3224c73 100644
--- a/site/app.js
+++ b/site/app.js
@@ -2,6 +2,7 @@
// builds the demo tables, and runs queries through demo.run().
import { loadPyodide } from "https://cdn.jsdelivr.net/pyodide/v314.0.7/full/pyodide.mjs";
+import { renderPlanTree } from "./plan.js";
const MAX_ROWS_SHOWN = 100;
@@ -10,7 +11,11 @@ const el = {
run: document.getElementById("run"),
status: document.getElementById("status"),
presets: document.getElementById("presets"),
- plan: document.getElementById("plan"),
+ planView: document.querySelector(".plan-view"),
+ planTree: document.getElementById("plan-tree"),
+ planText: document.getElementById("plan-text"),
+ tooltip: document.getElementById("plan-tooltip"),
+ viewButtons: document.querySelectorAll("[data-view]"),
results: document.getElementById("results"),
history: document.getElementById("orders-history"),
};
@@ -96,17 +101,20 @@ async function runQuery() {
message.className = "error-text";
message.textContent = result.error;
el.results.replaceChildren(message);
- el.plan.textContent = "";
+ el.planTree.replaceChildren();
+ el.planText.textContent = "";
} else {
const n = result.rows.length;
setStatus(
`Run ${runCount}: ${n.toLocaleString()} row${n === 1 ? "" : "s"} in ` +
`${elapsed.toFixed(0)}ms, orders v${result.version}`,
);
- el.plan.textContent = result.text;
+ el.tooltip.hidden = true;
+ renderPlanTree(el.planTree, el.tooltip, result.plan, result.misestimate_factor);
+ el.planText.textContent = result.text;
renderRows(result.columns, result.rows);
}
- flash(el.plan, el.results);
+ flash(el.planView, el.results);
} finally {
running = false;
el.run.disabled = false;
@@ -158,6 +166,29 @@ function formatValue(value) {
return String(value);
}
+function showView(view) {
+ el.planTree.hidden = view !== "tree";
+ el.planText.hidden = view !== "text";
+ el.tooltip.hidden = true;
+ for (const button of el.viewButtons) {
+ button.setAttribute("aria-pressed", String(button.dataset.view === view));
+ }
+ try {
+ localStorage.setItem("plan-view", view);
+ } catch {
+ // Storage can be unavailable (private mode, blocked site data); the default is fine.
+ }
+}
+
+for (const button of el.viewButtons) {
+ button.addEventListener("click", () => showView(button.dataset.view));
+}
+try {
+ if (localStorage.getItem("plan-view") === "text") showView("text");
+} catch {
+ // As above: fall back to the tree.
+}
+
el.run.addEventListener("click", runQuery);
el.sql.addEventListener("keydown", (event) => {
if (event.key === "Enter" && (event.ctrlKey || event.metaKey)) {
diff --git a/site/build.py b/site/build.py
index 207ffe3..2bbb93f 100644
--- a/site/build.py
+++ b/site/build.py
@@ -20,7 +20,7 @@
SITE = Path(__file__).resolve().parent
ROOT = SITE.parent
-PAGE_FILES = ("index.html", "style.css", "app.js")
+PAGE_FILES = ("index.html", "style.css", "app.js", "plan.js")
def build(out: Path) -> Path:
diff --git a/site/demo.py b/site/demo.py
index bb34942..cd3c37c 100644
--- a/site/demo.py
+++ b/site/demo.py
@@ -28,7 +28,7 @@
from tinydelta import DeltaTable, Schema
from tinyquery.catalog import Catalog
from tinyquery.engine import explain_analyze
-from tinyquery.profiler import format_analyze
+from tinyquery.profiler import MISESTIMATE_FACTOR, format_analyze
from tinyquery.schema import Column, Row
from tinyquery.schema import Schema as RowSchema
@@ -165,8 +165,8 @@ def run(
) -> str:
"""Run `sql` under EXPLAIN ANALYZE against `orders` at `version`.
- Returns JSON: columns, rows, the plan tree, the text rendering of it, and
- the version actually read. Any failure comes back as {"error": ...} rather
+ Returns JSON: columns, rows, the plan tree, the text rendering of it, the
+ version actually read, and the factor at which a misestimate is flagged. Any failure comes back as {"error": ...} rather
than an exception, so the page has one shape to handle.
"""
try:
@@ -184,6 +184,8 @@ def run(
"rows": [list(row) for row in result.rows],
"plan": result.plan.to_dict(),
"text": format_analyze(result.plan),
+ # So the page flags exactly the nodes the text output flags.
+ "misestimate_factor": MISESTIMATE_FACTOR,
}
)
diff --git a/site/index.html b/site/index.html
index f7bfb7d..3ff054e 100644
--- a/site/index.html
+++ b/site/index.html
@@ -38,8 +38,23 @@
Query
-
Plan
-
Run a query to see its plan.
+
+
Plan
+
+ Estimated rows
+ Actual rows
+ log scale
+
+
+
+
+
+
+
+
Run a query to see its plan.
+
+
+
diff --git a/site/plan.js b/site/plan.js
new file mode 100644
index 0000000..b273473
--- /dev/null
+++ b/site/plan.js
@@ -0,0 +1,158 @@
+// Draws an EXPLAIN ANALYZE plan as a tree. Each operator gets a pair of bars,
+// estimated rows over actual rows, on one log scale shared by the whole plan,
+// so a 25x misestimate is as long a gap at the top of the tree as at the leaves.
+
+const NUMBER = new Intl.NumberFormat("en-US", { maximumFractionDigits: 0 });
+
+export function renderPlanTree(container, tooltip, plan, flagFactor) {
+ const rows = flatten(plan);
+ const largest = Math.max(10, ...rows.flatMap(({ node }) => [node.estimated_rows, node.actual_rows]));
+ const top = 10 ** Math.ceil(Math.log10(largest));
+ // log(v + 1) so that 0 rows has a zero-length bar and 1 row a visible stub.
+ const x = (v) => (v <= 0 ? 0 : (100 * Math.log10(v + 1)) / Math.log10(top + 1));
+ const ticks = [];
+ for (let t = 1; t <= top; t *= 10) ticks.push(t);
+
+ const table = document.createElement("table");
+ table.className = "plan-table";
+ table.innerHTML = `
+
+