|
| 1 | +# SPDX-License-Identifier: MPL-2.0 |
| 2 | +# Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +# |
| 4 | +# DB-6 Phase A — structured query plan for `explain from … | …`. |
| 5 | +# |
| 6 | +# Produces the shape the DB-6 acceptance criteria specify: an ordered list of |
| 7 | +# operations, each tagged with its kind and, for filters, the column, the |
| 8 | +# comparator, the literal value, whether the column is indexed, and a |
| 9 | +# selectivity estimate. |
| 10 | +# |
| 11 | +# HONESTY NOTE ON SELECTIVITY |
| 12 | +# --------------------------- |
| 13 | +# There are no column histograms yet — that is DB-3 (#33). The criteria |
| 14 | +# explicitly permit a stub until DB-3 lands, so every estimate here is |
| 15 | +# `SELECTIVITY_STUB` and is reported with `"selectivity_source" => "stub"`. |
| 16 | +# Nothing downstream may treat these as measured: `plan_is_costed` returns |
| 17 | +# false while any estimate is a stub, so a future DB-7 cost-based reorder |
| 18 | +# cannot silently optimise against made-up numbers. |
| 19 | + |
| 20 | +# Included directly into the `KRL` module namespace (as Ast.jl / Parser.jl / |
| 21 | +# Evaluator.jl are) — not a submodule, so AST types resolve without imports. |
| 22 | + |
| 23 | +"""Placeholder selectivity used until DB-3 (#33) provides real histograms.""" |
| 24 | +const SELECTIVITY_STUB = 0.5 |
| 25 | + |
| 26 | +""" |
| 27 | +Columns carrying a secondary index today. Kept deliberately small and |
| 28 | +explicit: an over-claiming list would make `indexed` a fake signal, which is |
| 29 | +worse than reporting `false`. Extend as DB-3 lands real indexes. |
| 30 | +""" |
| 31 | +const INDEXED_COLUMNS = Set([ |
| 32 | + "colouring_count_3", |
| 33 | + "colouring_count_5", |
| 34 | + "presentation_hash", |
| 35 | + "quandle_key", |
| 36 | +]) |
| 37 | + |
| 38 | +_cmp_symbol(k::Symbol) = get(Dict( |
| 39 | + :eq => "=", :neq => "!=", :lt => "<", :lte => "<=", |
| 40 | + :gt => ">", :gte => ">=", :iso => "≅", :path => "~>", :in => "in", |
| 41 | +), k, string(k)) |
| 42 | + |
| 43 | +_source_name(s::KRLSource) = |
| 44 | + s isa KRLSourceKnots ? "knots" : |
| 45 | + s isa KRLSourceDiagrams ? "diagrams" : |
| 46 | + s isa KRLSourceInvariants ? "invariants" : |
| 47 | + s isa KRLSourceNamed ? s.name : |
| 48 | + s isa KRLSourceSubquery ? "<subquery>" : "<unknown>" |
| 49 | + |
| 50 | +# Pull (column, comparator, value) out of a comparison predicate when the |
| 51 | +# shape is the simple `column <cmp> literal` the plan can describe. Anything |
| 52 | +# else (nested boolean, function call, column-to-column) yields `nothing` and |
| 53 | +# is reported as an opaque predicate rather than being mis-described. |
| 54 | +_literal_value(e::KRLExpr) = |
| 55 | + e isa KRLInt ? e.n : |
| 56 | + e isa KRLFloat ? e.x : |
| 57 | + e isa KRLString ? e.s : |
| 58 | + e isa KRLBool ? e.b : |
| 59 | + e isa KRLKnotName ? e.name : nothing |
| 60 | + |
| 61 | +function _simple_comparison(e::KRLExpr) |
| 62 | + e isa KRLCompare || return nothing |
| 63 | + e.left isa KRLVar || return nothing |
| 64 | + val = _literal_value(e.right) |
| 65 | + val === nothing && return nothing |
| 66 | + (e.left.name, _cmp_symbol(e.op), val) |
| 67 | +end |
| 68 | + |
| 69 | +# Split a conjunction into its conjuncts so `filter a = 1 and b < 2` plans as |
| 70 | +# two filter operations, which is what makes a future reorder meaningful. |
| 71 | +function _conjuncts(e::KRLExpr) |
| 72 | + e isa KRLAnd && return vcat(_conjuncts(e.left), _conjuncts(e.right)) |
| 73 | + [e] |
| 74 | +end |
| 75 | + |
| 76 | +function _filter_ops(pred::KRLExpr) |
| 77 | + ops = Dict{String, Any}[] |
| 78 | + for c in _conjuncts(pred) |
| 79 | + simple = _simple_comparison(c) |
| 80 | + if simple === nothing |
| 81 | + push!(ops, Dict{String, Any}( |
| 82 | + "op" => "filter", |
| 83 | + "predicate" => "<expression>", |
| 84 | + "indexed" => false, |
| 85 | + "selectivity_estimate" => SELECTIVITY_STUB, |
| 86 | + "selectivity_source" => "stub", |
| 87 | + )) |
| 88 | + else |
| 89 | + col, cmp, val = simple |
| 90 | + push!(ops, Dict{String, Any}( |
| 91 | + "op" => "filter", |
| 92 | + "column" => col, |
| 93 | + "comparator" => cmp, |
| 94 | + "value" => val, |
| 95 | + "indexed" => col in INDEXED_COLUMNS, |
| 96 | + "selectivity_estimate" => SELECTIVITY_STUB, |
| 97 | + "selectivity_source" => "stub", |
| 98 | + )) |
| 99 | + end |
| 100 | + end |
| 101 | + ops |
| 102 | +end |
| 103 | + |
| 104 | +""" |
| 105 | + explain_plan(q::KRLQuery) -> Vector{Dict{String, Any}} |
| 106 | +
|
| 107 | +Ordered plan for `q`: a `scan` of the source followed by one entry per |
| 108 | +pipeline stage, with conjoined filters split into separate `filter` |
| 109 | +operations. |
| 110 | +
|
| 111 | +The order returned is **execution order as written** — no reordering is |
| 112 | +performed. Cost-based reordering is DB-7 and requires real selectivity |
| 113 | +(DB-3), which is why every estimate here is flagged `"stub"`. |
| 114 | +""" |
| 115 | +function explain_plan(q::KRLQuery)::Vector{Dict{String, Any}} |
| 116 | + plan = Dict{String, Any}[Dict{String, Any}( |
| 117 | + "op" => "scan", |
| 118 | + "table" => _source_name(q.source), |
| 119 | + )] |
| 120 | + for stage in q.stages |
| 121 | + if stage isa KRLFilterStage |
| 122 | + append!(plan, _filter_ops(stage.pred)) |
| 123 | + elseif stage isa KRLSortStage |
| 124 | + push!(plan, Dict{String, Any}( |
| 125 | + "op" => "sort", |
| 126 | + "keys" => length(stage.items), |
| 127 | + )) |
| 128 | + elseif stage isa KRLTakeStage |
| 129 | + push!(plan, Dict{String, Any}("op" => "take", "n" => stage.n)) |
| 130 | + elseif stage isa KRLSkipStage |
| 131 | + push!(plan, Dict{String, Any}("op" => "skip", "n" => stage.n)) |
| 132 | + else |
| 133 | + push!(plan, Dict{String, Any}("op" => _stage_op_name(stage))) |
| 134 | + end |
| 135 | + end |
| 136 | + plan |
| 137 | +end |
| 138 | + |
| 139 | +_stage_op_name(s::KRLPipeStage) = |
| 140 | + s isa KRLReturnStage ? "return" : |
| 141 | + s isa KRLGroupByStage ? "group_by" : |
| 142 | + lowercase(replace(string(nameof(typeof(s))), "KRL" => "", "Stage" => "")) |
| 143 | + |
| 144 | +""" |
| 145 | + plan_is_costed(plan) -> Bool |
| 146 | +
|
| 147 | +`true` only when every selectivity estimate in `plan` came from real |
| 148 | +statistics. While DB-3 (#33) is outstanding this is always `false` — the |
| 149 | +guard that stops DB-7 optimising against stub numbers. |
| 150 | +""" |
| 151 | +plan_is_costed(plan::Vector{Dict{String, Any}}) = |
| 152 | + !any(get(op, "selectivity_source", "stub") == "stub" for op in plan) |
0 commit comments