Repository navigation
Closed
Description
Source: Audit doc /tmp/krl-quandle-tangle-audit-2026-06-01.md DB-6.
Claim
Users running KRL queries (find where ... = ... and ... < ...) cannot introspect why a query is slow. There is no plan inspection — just timing.
Why valuable
EXPLAIN is the standard answer in any query language. Without it, "this query is slow" debugging becomes guesswork; you can't tell whether the filter order was wrong, whether the index missed, or whether the result set is just genuinely large.
Aligns with the cost-based query rewriting work (DB-7), which is unblocked by having a plan to optimise.
Acceptance criteria
Phase A — minimum viable EXPLAIN
- Add
EXPLAIN find where ... and ... and ...as a KRL-level prefix. Parser change: accept the keyword. - Server returns a structured plan as JSON: an ordered list of filter operations, with each operation tagged:
[ {"op": "scan", "table": "knots"}, {"op": "filter", "column": "colouring_count_3", "value": 9, "selectivity_estimate": 0.02, "indexed": true}, {"op": "filter", "column": "crossing", "comparator": "<", "value": 8, "selectivity_estimate": 0.18, "indexed": false} ] - Selectivity estimates come from precomputed histograms on the index (or are a stub
0.5until DB-3 lands).
Phase B — cost-based reordering (DB-7)
- Apply most-selective filter first.
- Apply indexed filters before non-indexed.
- Verify with a benchmark corpus.
Phase C — user-visible diagnostics
-
EXPLAIN find where ...returns the plan as a readable string in addition to JSON. - Server logs the plan for every query above a configurable time threshold (default 50ms).
Effort
- Phase A: 1w (parser change + plan extraction + serialisation).
- Phase B: 1w (cost model + reordering).
- Phase C: 4h.
Cross-references
- Audit doc DB-6, DB-7 (selectivity-based rewriting).
server/serve.jlroute handlers — primary code location.server/krl/SqlFrontend.jl— SQL→KRL translation should also acceptEXPLAIN SELECT ....
Out of scope
ANALYZE(collect statistics). Separate issue.- Adaptive query execution. Far future.
Metadata
Metadata
Assignees
Labels
No labels
Activity
feat(db-3): Phase A — 3 new indexes (writhe, genus, generator_count) …
hyperpolymath commentedon Jul 28, 2026
Measured state after #81 (which landed EXPLAIN work), so this doesn't get closed on a false assumption:
What exists:
server/query_explain.jl+handle_explaininserve.jl— an HTTP endpoint that runsexplain_query_planover a SQL string via the DB connection, with a mutation guard (403 on non-SELECT/WITH). Covered byserver/test_query_explain.jl, which now runs in CI as its own named step (#72).What Phase A still asks for and does not exist:
EXPLAIN find where ...as a KRL-level prefix.grep -n EXPLAIN server/krl/Lexer.jl server/krl/Parser.jlreturns nothing — the keyword is not lexed or parsed, so EXPLAIN is reachable only over HTTP with SQL, not from KRL itself.selectivity_estimateandindexedfields.grep -rn selectivity server/krl/*.jl server/query_explain.jlreturns nothing; the Evaluator has"indexed"/not indexedstrings for path queries but no selectivity estimates, not even the stub0.5the criteria allow pending DB-3 (DB-3: secondary indexes on colouring_count_3 / colouring_count_5 / alexander_polynomial #33).So: the SQL-side plan surface is real and gated; the KRL-side surface and selectivity model are not started. Suggest keeping this open scoped to Phase A's two remaining bullets (parser keyword + structured plan with selectivity), with Phases B/C tracked separately.
feat(krl): DB-6 Phase A — explain keyword + structured query plan (#34)…
fix: retire the last @test_broken (main is red); land DB-6 docs + STA…
hyperpolymath commentedon Aug 3, 2026
Phase A verified complete on
main(b1c7141)Checked each acceptance criterion against the tree rather than against PR titles:
mainEXPLAINaccepted as a KRL prefixserver/krl/Lexer.jl:66registers the"explain"keyword;server/krl/Parser.jl:193handles it via_kw(ps, "explain")server/krl/ExplainPlan.jl—"op"×7,scan×2,filter×8selectivity_estimateper opserver/krl/ExplainPlan.jl×2indexedflag per opserver/krl/ExplainPlan.jl×4server/serve.jl→/api/explain→handle_explain(db, sdb, params)server/krl/test/explain_test.jl,server/test_query_explain.jlkrl-verification.yml:125— stepKRL EXPLAIN (DB-6)runsserver/test_query_explain.jldocs/db-6-explain-strategy.mdThe CI gate matters most here: this is not "code exists", it's "code exists and a green run proves it executed". The
KRL EXPLAIN (DB-6)step has been passing since the suite stopped hanging (#82).Closing as Phase A complete. One defect found during this audit —
/api/explainwas registered twice inroute(), the second unreachable — fixed in #93.If the later phases in the original audit doc (cost-based rewriting, DB-7) are still wanted, they are worth a separate issue rather than reopening this one.
chore: drop duplicate /api/explain route; correct ROADMAP HOMFLY bound (