Skip to content

parser: add the rename command - #4

Merged
be-thomas merged 2 commits into
mainfrom
feat/rename-command
Oct 26, 2026
Merged

be-thomas merged 2 commits into
mainfrom
feat/rename-command

Conversation

@be-thomas

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

Copy link
Copy Markdown
Owner

What changes

Adds the classic SPL rename command: rename <field> AS <field>[, ...].

  • IR: ast.RenameStage holds all pairs of one command, because Splunk
    applies them as one command and the ES|QL RENAME that receives them
    carries them together. Each ast.Rename carries the positions of both field
    tokens.
  • Grammar: CmdRename with renamePair and renameField. A quoted name
    keeps its decoded text; a wildcard may lead or trail the name (*foo,
    properties.*, EU*). The leading-wildcard alternative starts on
    STAR_TIGHT, so SLL prediction never has to pick between alternatives that
    share a first token.
  • Dispatch: rename moves from the known-but-unimplemented table to the
    implemented one, and the token is accepted by the public Lex adapter and
    by the tight-token classifier.
  • ES|QL: one RENAME a AS b, c AS d. ES|QL defines a multi-pair RENAME
    as the equivalent sequence of single renames, so the pipeline tracks which
    columns exist: a renamed field resolves to its new name downstream, and a
    reference to a name that was renamed away is refused. Refusals:
    • rename-wildcard at the wildcarded field — RENAME has no wildcard form;
    • rename-duplicate when one field is renamed twice;
    • rename-conflict when a pair's target is another pair's source, or when
      two pairs share one target (ES|QL would keep only the rightmost column);
    • rename-source-dropped at a later reference to a name that was renamed
      away.
      rename a as a is a no-op and emits nothing, and no-op pairs are ignored
      when checking the other pairs.
  • PromQL: promql-unsupported-stage, as with the other row-shaping stages.
  • Mapping: the source goes through the field mapping (it is an existing
    column); the target is emitted as written, because it is the name the query
    chooses, not an ECS field.
  • Identifiers: a field name that spells an ES|QL keyword is backtick
    quoted, so keyword field names survive emission in every command.

Docs updated: README (supported table and regenerated conformance report),
COMPATIBILITY, CONFORMANCE, CHANGELOG, ROADMAP, ARCHITECTURE.

Tests

  • pkg/parser/spl1/parser_test.go: TestRename (8 cases asserting exact AST
    and ast.Pos including offsets) and TestRenameErrors (7 cases asserting
    the syntax code and exact position).
  • pkg/compiler/compiler_test.go: 12 rename ES|QL goldens (single pair,
    unmapped target, downstream resolution, two-stage chain, name restored by a
    later rename, both no-op shapes, multiple pairs, quoted source, keyword
    quoting, no-op command) plus one golden for keyword quoting in table, and
    TestRenameRefusals (10 refusal cases, each asserting one diagnostic with
    code, line, column and empty output).
  • conformance/cases/rename/: 12 cases — exact ES|QL for simple, multiple,
    quoted, no-op, downstream (stats over the new name) and mapped target;
    refusals for wildcard source, bare wildcard target, duplicate, target
    overlap, target reuse and a dropped-source reference, each with its PromQL
    refusal.
  • Corpus baselines re-recorded with make corpus-update; the README report
    table was regenerated with make corpus-report and matches byte for byte.

Verification: make check, go test -race ./..., make generate-check (no
drift), go test ./conformance -count=1.

Effect on the corpus: parse-to-IR 84 → 95 of 2,812, ES|QL compile 65 → 72.
rename blocked 117 detection queries; the remainder still fail for reasons
outside this command (see below).

Review

An independent review posted five inline findings, all addressed in
515188c and each thread replied to and resolved:

  1. Column state (major). The emitter did not track the columns rename
    creates and removes, so a later reference to a renamed field resolved
    through the ECS mapping and a reference to a renamed-away field named a
    dropped column. Fixed by recording both directions and refusing the latter.
  2. Ordering rationale. The comments and changelog claimed ES|QL applies
    pairs "to the same input columns"; Elastic documents the equivalent of
    sequential renames. The wording now reflects that, and the refusal for
    interacting pairs is stated as the conservative policy it is (Splunk's
    behaviour for those pairs is unverified).
  3. Duplicate targets. rename a as b, c as b was accepted though ES|QL
    keeps only the rightmost and Splunk rejects it; now rename-conflict.
  4. No-op pairs. rename a as a, a as b and rename a as b, b as b were
    wrongly reported as duplicates/conflicts; no-op pairs no longer count.
  5. Reserved words. quoteIdent did not quote ES|QL keywords, so
    rename where as search emitted a bare keyword (pre-existing for KEEP
    too). Fixed for every command, with two goldens.

Open questions

  1. Field names that are not identifiers. 33 rename-blocked corpus queries
    name a {} JSON path (rename data{} as x) and 1 uses a hyphenated name
    (x-amz-copy-source). Both are general identifier-lexing gaps that affect
    every command, not just rename. They look like the next feature.
  2. Leading-wildcard targets. rename a as *foo parses on the source side
    but not after AS, because the tight-token pass classifies a star after an
    operand as STAR_OP. Splunk's documented wildcard rename is symmetric
    trailing (EU* AS EMEA*), which works. Should a leading-wildcard target be
    supported, or is the syntax error acceptable?
  3. rename wildcard expansion. The refusal is deliberate, but a future
    emitter could expand wildcards when it knows the schema. That needs a
    mapping-level field list, which does not exist yet.
  4. Splunk's semantics for interacting pairs. If Splunk applies
    rename a as b, b as c pair by pair like ES|QL, the refusal could be
    lifted and the two emitted as one command.

No push to main; merging waits for maintainer review.

Splunk rename takes one or more `from AS to` pairs and applies them in a
single pass. The whole command lowers to one RenameStage so the pairs stay
together, matching both Splunk and the ES|QL RENAME command that receives
them.

Field spellings follow Splunk: a name may be quoted, and a wildcard may
touch or lead the name (EU*, properties.*, *foo). Wildcards parse because
they are valid SPL; the ES|QL emitter refuses them at the offending field
with rename-wildcard, since RENAME has no wildcard form and expanding one
would need the field list.

ES|QL applies every pair of a RENAME to the same input columns, so a pair
whose target is another pair's source has no order-independent meaning.
That form, and renaming one field twice, are refused with rename-conflict
and rename-duplicate. `rename a as a` is a no-op and emits nothing.

The target name is emitted as written rather than through the field
mapping: it is the name the query chooses, not an ECS field. The source
is an existing column, so it is mapped.

PromQL refuses the stage with promql-unsupported-stage.

Tests: parser stage and position tables plus syntax-error positions,
compiler goldens and refusal positions, and ten conformance cases. The
corpus baselines are re-recorded and the README report table regenerated.
make check and go test -race ./... pass.
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/emitter/esql/esql.go Resolved
Comment thread pkg/emitter/esql/esql.go Resolved
Review of the rename support found that the ES|QL emitter did not track
the columns a rename creates or removes. A later stage that referenced a
renamed field resolved it through the field mapping, so
`rename dest as host | stats count by host` emitted `BY `host.name`` for
a column that now exists as `host`, and `rename host as dest | stats
count by host` emitted a column RENAME had just dropped.

The emitter now records the target as a column of its own name and the
source as no longer available. A reference to a name that was renamed
away is refused at the reference with rename-source-dropped instead of
emitting a column that does not exist, and a later rename that restores
the name clears it.

Two pairs that would interact are now refused consistently. Two pairs
cannot share a target: Splunk rejects it, and ES|QL keeps only the
rightmost column, so `rename a as b, c as b` silently lost one. No-op
pairs no longer count as renames, so `rename a as b, a as a` and
`rename a as b, b as b` emit the real pair instead of reporting a
duplicate. The conflict message no longer claims ES|QL's order is
unspecified: Elastic documents a multi-pair RENAME as the equivalent
sequence of single renames.

Identifiers that spell an ES|QL keyword are now backtick-quoted, so
`rename where as search` emits `RENAME `where` AS search` rather than a
bare keyword.

Tests cover the resolved name, a two-stage chain, restoring a renamed
name, both no-op shapes, both refusal positions, keyword quoting, and
three new conformance cases.
@be-thomas
be-thomas merged commit db686d0 into main Oct 26, 2026
5 checks passed
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