Skip to content

parser: add the tstats command - #5

Open
be-thomas wants to merge 9 commits into
mainfrom
feat/tstats
Open

be-thomas wants to merge 9 commits into
mainfrom
feat/tstats

Conversation

@be-thomas

@be-thomas be-thomas commented Oct 26, 2026 •

Copy link
Copy Markdown
Owner

What changes

Adds tstats, Splunk's data-model aggregation, across the parser, the IR, the
mapping, and the ES|QL emitter. PromQL refuses the stage.

tstats gets its own IR node because it is not a stats stage over a
different source: it names a data model, accepts acceleration options, and
writes fields as Dataset.attribute. Keeping those three parts separate lets
each emitter resolve them on its own terms.

Piece Behaviour
Source --index, else index= in WHERE, else datamodels[<name>], else default_index
Options summariesonly, allow_old_summaries, prestats ignored with a tstats-option info; fillnull_value=null accepted; anything else refused
Fields Processes.dest → dest, then through fields; one tstats-field-prefix warning
Groups _time → @timestamp; with span= → BUCKET(@timestamp, N unit)
Position First stage only; tstats-position otherwise
Aggregations values, earliest, and latest added to ES|QL

Files

  • pkg/parser/spl1/grammar/SPL1.g4: tstatsBody, tstatsAgg*, option,
    FROM/WHERE/BY rules, and the TSTATS/FROM keyword tokens.
  • pkg/ast/ast.go: TstatsStage and TstatsOption.
  • pkg/parser/spl1/build.go: lowering, including the datamodel= check.
  • pkg/mapping/mapping.go: datamodels key and Config.Datamodel.
  • pkg/emitter/esql/esql.go: tstatsIndex, emitTstats, tstatsOptions,
    tstatsGroups, prefix stripping, and the three new aggregations.
  • Conformance: cases/tstats/ (13 cases) and fixtures/mappings/tstats.json.

Two grammar notes that matter for review:

  • FROM, WHERE, and BY are excluded from tstatsAggWord. SLL prediction
    cannot use a semantic predicate to choose between "another aggregation" and
    "the next clause", so the choice is made from the token type alone.
  • datamodel after FROM is parsed as an identifier and validated in the
    builder. That way a misspelling is refused at the word itself, with the
    expected spelling in the message, instead of as a generic syntax error.

Tests run

  • make check — format, vet, tests, build: pass.
  • go test -race ./...: pass.
  • make generate-check: generated parsers are current.
  • go test ./conformance -count=1: pass.
  • Corpus re-recorded with make corpus-update; TestCorpus passes.

Measured over the corpus, 679 of 2,812 queries now parse (24.1%), 669 compile
end to end to ES|QL (23.8%), and 6 to PromQL (0.2%). 584 queries (567
detections and 17 documented examples) moved from error:unsupported-command
to accepted. Another 428 detections moved from unsupported-command to
error:syntax, because the tstats stage now parses and then refuses the rest
of the pipeline at its real cause, often an IN (...) value list.

Open questions

  1. Prefix stripping is lossy. Processes.dest and a bare dest are not
    guaranteed to be the same column, which is why it warns rather than being
    silent. An alternative is a datamodel_fields mapping key that names the
    ECS column per Dataset.attribute, at the cost of a larger mapping file.
    I kept the simpler form first; say if you want the explicit one.
  2. fillnull_value is refused unless it is null. ES|QL has no fill step
    in this translation. Emitting COALESCE in a later EVAL would change the
    column shape, so refusing seemed closer to the "exact, warned, or refused"
    rule than an approximation.
  3. Unknown options are errors, not warnings. Ignoring chunk_size would
    be harmless for results, but an unrecognised option may also be a typo for
    one that does change results, so it fails loudly. Tell me if you prefer a
    warning.
  4. A span without _time warns and is dropped. Splunk applies span= to
    _time; if _time is not grouped there is nothing to bucket, so the
    warning names the reason rather than inventing a bucket column.

tstats aggregates fields of a Splunk data model. It is not a stats stage
with a different source: it names a data model, accepts acceleration
options, and writes fields as Dataset.attribute, so it gets its own IR
node. Keeping the data model, the options, and the span separate lets an
emitter resolve each one on its own terms.

The grammar parses the options, then the aggregations, then FROM, WHERE,
and BY in that order. FROM, WHERE, and BY are excluded from the
aggregation word so `tstats count by host` is unambiguously one
aggregation and a BY clause; the choice is made from the token type
alone, because SLL prediction cannot consult a semantic predicate to
make it. `datamodel` after FROM is parsed as an identifier and validated
by the builder, so a misspelling is refused at that word with the
expected spelling in the message.

earliest and latest are lifted out of WHERE with the same helper a
search stage uses, because they are global search modifiers there too.
A later span= wins over an earlier one, matching bin.

tstatsAggWord admits every keyword as an aggregation name except the
three clause starts, so `tstats count by by` still groups by a field
named by.

Tests: stage, aggregation, option, clause, and time-bound lowering with
exact positions; a table of missing-argument positions; and the FROM
datamodel-name refusal.
tstats names a data model, not an index, and the two namespaces are
unrelated: `datamodel=Endpoint.Processes` says nothing about where those
events live in Elasticsearch. Add a `datamodels` key that supplies the
index pattern for a data-model name, alongside `indexes`.

The lookup returns (string, bool) rather than a bare string so an
unmapped data model is distinguishable from one mapped to the empty
string. Falling back to the default index is the emitter's decision to
make, and it needs to warn when it happens.

Tests cover the default mapping having no data models, a mapped value, an
empty-string entry not counting as a mapping, and case-sensitive names.
A tstats stage lowers to the same pieces as a search followed by an
aggregation, so the emitter builds a search from the stage's WHERE clause
and then a STATS line, and the existing search and aggregation code does
the work. Aggregations gain values, earliest, and latest, which tstats
detections use heavily.

The FROM value is resolved in the order that keeps the query's own intent
first: --index, then an index= filter inside WHERE, then the mapping's
datamodels, then the default index. An unmapped data model warns at its
name rather than silently searching the wrong index, and an empty
mapping value is not a mapping.

Data-model field references keep their Dataset.attribute spelling for the
whole pipeline, so a later table or stats stage resolves them exactly as
tstats did. The prefix is dropped to the attribute name and the field
dictionary is applied after that. Stripping is lossy in general, so the
first dropped prefix produces one tstats-field-prefix warning.

Options are handled by what they mean. summariesonly,
allow_old_summaries, and prestats only describe Splunk's summary index,
so they do not change the result and produce one info diagnostic;
fillnull_value=null is the natural state of COUNT and passes; any other
value is refused, because ES|QL has no equivalent fill. An unknown option
is refused at its name instead of being ignored.

A span applies to _time only, so it becomes BUCKET(@timestamp, N unit)
when _time is grouped and warns when it is not. tstats after stage 0 is
refused with tstats-position, since the data model determines FROM.
Thirteen cases for the new stage: the full form with options,
aggregations, and a data model that is mapped; a mapped data model with a
prefix on every field; a span bucketing _time; a span without _time;
an index filter in WHERE overriding the data model; no source at all;
an unmapped data model; time bounds lifted out of WHERE; a later stage
resolving a prefixed field; and the four refusals — non-null fill, an
unknown option, a tstats after another stage, and PromQL. Each carries
its own mapping fixture so the cases do not depend on the default one.

The corpus is re-recorded: 584 previously refused detections now parse,
and 669 compile end to end, because tstats queries are a large share of
the refusal baseline.
Move tstats out of the refused-command list and give it a row in the
support tables. The compatibility page records the source-resolution
order, the option policy, span handling, and the seven new diagnostic
codes. The mapping page gains the datamodels key and explains why
prefix stripping warns.

The README report table and its prose are regenerated from the corpus:
679 queries parse (24.1%), 669 (23.8%) compile to ES|QL, and 6 (0.2%) to
PromQL. The refusal breakdowns are measured rather than estimated, and
name what is left — IN value lists and commands such as fillnull, bucket,
and spath on the parser side, and tstats on the PromQL side.
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/parser/spl1/grammar/SPL1.g4 Resolved
Review of the tstats support found that prefix stripping ran on every dotted
name once a tstats stage had been emitted, because a single boolean governed
the whole pipeline. ECS names are dotted by design, so

    tstats count FROM datamodel=Web WHERE source.address="10.0.0.1"

emitted `WHERE address == ...`, and `host.name` in a later eval became
`name`. The mapping does not correct it: `host.name` is not a key in the
field dictionary, so an already-correct name was silently corrupted.

Stripping now depends on the data model rather than on having seen a
tstats stage. The emitter holds the names a data-model reference can be
prefixed with — the model name and its root dataset, longest first — and
drops only a prefix that matches one of them followed by a dot. Everything
else is a column name and resolves through the mapping as before.

The same review found the `tstats-field-prefix` warning was emitted at the
end of the tstats stage, so a prefix first used later
(`tstats … | table Web.src`) was dropped silently. The warning now comes
at the end of the pipeline walk, which also covers that case; diagnostics
are sorted before they are returned, so the position is unchanged for the
in-stage case.
tstatsOptionValue allowed an unsigned number, so `fillnull_value=-1` was a
syntax error at the minus instead of the value refusal the emitter has for
every non-null fill. Allow a sign before the number; the builder slices the
source, so the value reaches the emitter as "-1" and is refused there with
tstats-fillnull at the option name.
Three cases from the review of the tstats support:

- dotted-names: `source.address` in WHERE and a `Web.src` group. Only the
  data-model prefix is dropped, so the ECS column is unchanged and the
  warning points at Web.src.
- late-prefix: a prefixed field first referenced after the tstats stage is
  resolved the same way and still warns.
- fillnull-negative: `fillnull_value=-1` parses and is refused by value.
Record that only the data model name or its root dataset is dropped from
a tstats field, that other dotted names are columns, and that a prefix
first used in a later stage also warns.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant