diff --git a/CHANGELOG.md b/CHANGELOG.md index d0a7ebe..2a79f85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,9 @@ All notable changes to this project are documented here. The format follows - Conformance cases under `cases/spl2/` and an `options.spl_version` metadata key; unknown versions fail the case instead of silently running as classic SPL. +- `make corpus-report` measures the corpus end to end: how many queries parse + to the IR, and how many of those compile to ES\|QL and to PromQL. The table + is published in the README's "Conformance report" section. - Fuzz targets `FuzzParseSPL1` and `FuzzParseSPL2`, and a lexer-parity test that tokenizes one corpus with both generated lexers. - A query corpus under `conformance/corpus/` with 2,812 queries in three diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 3488c3f..226a255 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -155,6 +155,11 @@ one of two kinds: stage in the collections of its dialect. A command with no corpus query is a command nothing tests. +`make corpus-report` measures what the corpus achieves rather than what it +asserts: how many queries parse to the IR, and how many of those emit to ES|QL +and to PromQL. The current table is in the README's "Conformance report" +section. + The dialect, the kind, and the source versions live in each collection's `metadata.json`, and each query lists the versions that contain it. Nothing is inferred from the query text: `from` is the only textual hint, and classic SPL diff --git a/Makefile b/Makefile index 5971cc8..82c68c6 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ LDFLAGS := -s -w \ -X github.com/be-thomas/openspl/pkg/version.Commit=$(COMMIT) \ -X github.com/be-thomas/openspl/pkg/version.Date=$(DATE) -.PHONY: all build test race vet fmt fmt-check check hooks generate generate-check conformance conformance-update corpus-update e2e clean help +.PHONY: all build test race vet fmt fmt-check check hooks generate generate-check conformance conformance-update corpus-update corpus-report e2e clean help all: check @@ -68,6 +68,11 @@ conformance-update: corpus-update: go test ./conformance -count=1 -run TestCorpus -update-corpus +# The compile report printed here is pasted into README.md; see +# conformance/report_test.go. +corpus-report: + go test ./conformance -count=1 -run TestCorpusReport -v -corpus-report + e2e: ./conformance/engines/run.sh @@ -90,5 +95,6 @@ help: 'conformance run the offline conformance suite' \ 'conformance-update regenerate goldens and expectations' \ 'corpus-update re-record the query corpus outcomes' \ + 'corpus-report print the corpus compile report for README.md' \ 'e2e run the Docker engine harness' \ 'clean remove build output' diff --git a/README.md b/README.md index 8a887ff..c79357e 100644 --- a/README.md +++ b/README.md @@ -200,6 +200,38 @@ refused with a positioned diagnostic. Full matrix → [docs/COMPATIBILITY.md](docs/COMPATIBILITY.md) +## Conformance report + +Measured over the checked-in query corpus: 2,812 queries in total, made up of +549 documented SPL1 examples, 2,171 detection searches from Splunk +`security_content`, and 92 documented SPL2 examples. The IR is the parsed +`*ast.Pipeline`; ES\|QL (the SQL-style back-end) and PromQL are the two +back-ends, so there is no separate SQL target. `SPL→x` is the share of every +query in the row; `IR→x` is the share of the queries that parsed. All figures +use the compiled-in default mapping with no per-query index, metric, or span +override. + +| Collection | Queries | → IR | SPL→IR | → ES\|QL | SPL→ES\|QL | IR→ES\|QL | → PromQL | SPL→PromQL | IR→PromQL | +|---|---|---|---|---|---|---|---|---|---| +| `spl1` | 549 | 69 | 12.6% | 59 | 10.7% | 85.5% | 6 | 1.1% | 8.7% | +| `spl1-detections` | 2,171 | 15 | 0.7% | 6 | 0.3% | 40.0% | 0 | 0.0% | 0.0% | +| `spl2` | 92 | 0 | 0.0% | 0 | 0.0% | — | 0 | 0.0% | — | +| **Total** | **2,812** | **84** | **3.0%** | **65** | **2.3%** | **77.4%** | **6** | **0.2%** | **7.1%** | + +84 of 2,812 queries (3.0%) parse to the IR, 65 (2.3%) compile end-to-end to +ES\|QL, and 6 (0.2%) to PromQL. The low SPL1 detection figure is parser +coverage, not emitter coverage — `tstats` alone accounts for 1,022 of the +refusals. Where a query does parse, ES\|QL emits for 77.4% and PromQL for 7.1%. + +The 78 PromQL refusals are deliberate. The largest single cause is +`promql-no-aggregation` (21): the pipeline does not end in an aggregation, so +it produces rows rather than a time series. Another 33 use a stage with no +PromQL form — `eval` 17, `table` 9, `rex` 5, `fields` 2 — and 16 combine or +name an aggregation PromQL cannot express. The remaining 8 are refused over +row filters, free-text terms, or ordering. + +Regenerate with `make corpus-report`; it prints this table from the corpus. + --- ## Architecture diff --git a/conformance/report_test.go b/conformance/report_test.go new file mode 100644 index 0000000..065b1e3 --- /dev/null +++ b/conformance/report_test.go @@ -0,0 +1,151 @@ +// Corpus compile report. +// +// The corpus tests assert outcomes; this report measures them. It answers +// three questions for every collection: how many queries parse to the IR, how +// many of those the IR translates to each back-end, and how many translate +// end-to-end. Run it with +// +// make corpus-report +// +// and paste the printed table into README.md's "Conformance report" section. +package conformance + +import ( + "flag" + "fmt" + "path/filepath" + "strings" + "testing" + + "github.com/be-thomas/openspl/pkg/ast" + "github.com/be-thomas/openspl/pkg/compiler" +) + +var corpusReport = flag.Bool("corpus-report", false, + "print the corpus compile report") + +// reportTally counts one row of the report. +type reportTally struct { + queries int + parsed int + esql int + promql int +} + +func (r *reportTally) add(other reportTally) { + r.queries += other.queries + r.parsed += other.parsed + r.esql += other.esql + r.promql += other.promql +} + +type reportRow struct { + name string + total bool + tally reportTally +} + +// label is the first cell: a code-formatted collection name, or the bold +// total row. +func (r reportRow) label() string { + if r.total { + return "**Total**" + } + return "`" + r.name + "`" +} + +// cell formats one count or percentage, bold on the total row. The printed +// table is meant to be pasted into README.md unchanged. +func (r reportRow) cell(s string) string { + if r.total { + return "**" + s + "**" + } + return s +} + +func TestCorpusReport(t *testing.T) { + if !*corpusReport { + t.Skip("set -corpus-report to print the compile report") + } + var rows []reportRow + var total reportTally + for _, dir := range corpusDirs(t) { + meta, queries := loadCorpus(t, dir) + got := measure(t, meta, queries) + rows = append(rows, reportRow{name: filepath.Base(dir), tally: got}) + total.add(got) + } + rows = append(rows, reportRow{name: "Total", total: true, tally: total}) + + fmt.Println("| Collection | Queries | → IR | SPL→IR | → ES\\|QL | SPL→ES\\|QL | IR→ES\\|QL | → PromQL | SPL→PromQL | IR→PromQL |") + fmt.Println("|---|---|---|---|---|---|---|---|---|---|") + for _, row := range rows { + t := row.tally + fmt.Printf("| %s | %s | %s | %s | %s | %s | %s | %s | %s | %s |\n", + row.label(), + row.cell(count(t.queries)), row.cell(count(t.parsed)), + row.cell(percent(t.parsed, t.queries)), + row.cell(count(t.esql)), row.cell(percent(t.esql, t.queries)), + row.cell(percent(t.esql, t.parsed)), + row.cell(count(t.promql)), row.cell(percent(t.promql, t.queries)), + row.cell(percent(t.promql, t.parsed))) + } +} + +// measure counts, for one collection, the queries that parse and the queries +// that also emit a non-empty query for each back-end. +func measure(t *testing.T, meta corpusMeta, queries []corpusQuery) reportTally { + t.Helper() + version := compiler.SPLVersion1 + if meta.Dialect == "spl2" { + version = compiler.SPLVersion2 + } + var tally reportTally + for _, q := range queries { + tally.queries++ + p, err := compiler.ParseVersion(q.Query, version) + if err != nil { + continue + } + tally.parsed++ + if emits(p, compiler.ESQL) { + tally.esql++ + } + if emits(p, compiler.PromQL) { + tally.promql++ + } + } + return tally +} + +func emits(p *ast.Pipeline, target compiler.Target) bool { + res, err := compiler.Transpile(p, target, compiler.Options{}) + if err != nil || res == nil { + return false + } + return strings.TrimSpace(res.Output) != "" +} + +// count formats a count with thousands separators, so the printed table can be +// pasted into the README unchanged. +func count(n int) string { + s := fmt.Sprintf("%d", n) + if len(s) <= 3 { + return s + } + var b strings.Builder + for i, r := range s { + if i > 0 && (len(s)-i)%3 == 0 { + b.WriteByte(',') + } + b.WriteRune(r) + } + return b.String() +} + +func percent(n, of int) string { + if of == 0 { + return "—" + } + return fmt.Sprintf("%.1f%%", 100*float64(n)/float64(of)) +}