Skip to content

DB-6: EXPLAIN for KRL queries — surface plan + selectivity #34

Closed

Description

@hyperpolymath

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.5 until 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.jl route handlers — primary code location.
  • server/krl/SqlFrontend.jl — SQL→KRL translation should also accept EXPLAIN SELECT ....

Out of scope

  • ANALYZE (collect statistics). Separate issue.
  • Adaptive query execution. Far future.

Activity

  1. hyperpolymath commented on Jul 28, 2026

    @hyperpolymath
    OwnerAuthor

    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_explain in serve.jl — an HTTP endpoint that runs explain_query_plan over a SQL string via the DB connection, with a mutation guard (403 on non-SELECT/WITH). Covered by server/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.jl returns nothing — the keyword is not lexed or parsed, so EXPLAIN is reachable only over HTTP with SQL, not from KRL itself.
    • The structured per-operation plan with selectivity_estimate and indexed fields. grep -rn selectivity server/krl/*.jl server/query_explain.jl returns nothing; the Evaluator has "indexed"/not indexed strings for path queries but no selectivity estimates, not even the stub 0.5 the 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.

  2. hyperpolymath commented on Aug 3, 2026

    @hyperpolymath
    OwnerAuthor

    Phase A verified complete on main (b1c7141)

    Checked each acceptance criterion against the tree rather than against PR titles:

    Criterion Evidence on main
    EXPLAIN accepted as a KRL prefix server/krl/Lexer.jl:66 registers the "explain" keyword; server/krl/Parser.jl:193 handles it via _kw(ps, "explain")
    Structured JSON plan, ordered ops server/krl/ExplainPlan.jl — "op" ×7, scan ×2, filter ×8
    selectivity_estimate per op server/krl/ExplainPlan.jl ×2
    indexed flag per op server/krl/ExplainPlan.jl ×4
    Server endpoint server/serve.jl → /api/explain → handle_explain(db, sdb, params)
    Tests server/krl/test/explain_test.jl, server/test_query_explain.jl
    Gated in CI krl-verification.yml:125 — step KRL EXPLAIN (DB-6) runs server/test_query_explain.jl
    Design doc docs/db-6-explain-strategy.md

    The 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/explain was registered twice in route(), 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions