Expand description
Puts a canonical merge where a label rewrite made one necessary.
A fused input hands over the pieces of a metric one scan partition holds, and a label rewrite in
the tree can give two of those pieces one label set. Every consumer that answers a whole label
set - a fold, a ranking, a vector match, the statement’s own output - then reads two series where
Prometheus reads one, or answers the sum of a group twice. NativeExpr::Merge is the node that
collects the pieces of one label set into one series, and this module decides where one is
needed.
§What decides it
Only a rewrite can alias, and only one that writes a label its input already carries or drops one
its output would still be told apart by: duplicates_canonical_labels is that fact, derived
from the tree and the tags each source reports. A tree with no such rewrite gains no merge at
all, so a per-series query keeps every scan partition and pays nothing for this pass.
Where a merge is added is a property of the consumer, and of what the operations between the consumer and the rewrite do to one sample:
| consumer | merge below it? | why |
|---|---|---|
| the root | yes | the answer names one series per label set, so two rows sharing one set are the error Prometheus reports |
Aggregate | yes | a fold sums the values it is handed at a point, so two pieces of one series are summed as if the query said so twice |
Vector | yes | ranking, presence checks and folds must read canonical series rather than duplicate fragments |
Binary/Set over two vectors | yes, on both sides | each side is matched by label, and the kernel refuses two values of one field at a point only after the pair was matched |
Info | yes, on both sides | it looks the info series up by identifying labels, so the pieces of one label set have to be one series first |
Window over an evaluated vector | yes | a window folds a range of one series, so it reads the series whole |
Map, ParameterizedMap | yes, below it | point-wise, but not sample-preserving: the histogram functions drop float samples, math ignores native histograms, and a parameterized clamp can empty a point - and the label set a point’s occupancy is judged under is the rewritten one, so a merge above such a map would be handed the occupancy that is already gone |
Timestamp, Instant, a scalar broadcast | no | each rewrites the value of a sample it read and keeps that sample’s position, so no occupancy is lost and the pieces are still told apart where the rewrite left them |
A merge is never added below a raw scan, a scalar or aggregate state: it binds samples evaluated on a grid, and no consumer asks for one over those.
A node can be a source of duplicates without rewriting a label its input carries: a range
function that computes its result and timestamp in both of its forms drop the metadata labels -
the metric name, __type__ and __unit__ - and hand out a set those no longer tell apart. What
they produce is collected by their consumer - the root included - exactly as a rewrite of an
existing label is, unless
duplicates_canonical_labels proves from the
labels alone that nothing they drop was telling two of their series apart: a source bound to one
metric names every series it hands over, and a source whose tags carry neither __type__ nor
__unit__ holds no series those two tell apart either. rate(some_metric[5m]) over such a scan
is then a window that streams its scan, and the same query over a selector that discovered
several metrics keeps the collection it needs. The same rule is what a math or histogram map, a
parameterized map and an arithmetic or bool scalar broadcast are judged by.
The labels are the whole of that proof, and the scan’s key columns are not part of it. A key
column that holds NULL in one stored series and '' in another decodes to an absent and to an
empty label, and the drop removes the empty one with the name, leaving both under one label set -
a pair that no label can state and that is deliberately outside the proof, because every tag
column of a metric-engine table is nullable and collecting every name-dropping kernel over one
for it costs every such statement a canonical merge. The fact is still counted
(leaves_a_drop_to_the_storage_keys),
so the statements the pair can hide stay observable. Its other reader is the physical per-series
scan, which asks it of its own binding to decide whether its partitions hand over whole series
(keys_survive_metadata_drop) - a
partition-identity question at execution, never a reason to insert a Merge.
A drop above a merge is not asked about the empty value it removes, which is what keeps a chain of
drops to one collection: NativeExpr::Merge hands over one series per canonical label set - the
comparison that treats an empty label value as no label - so no drop above it can fold two of its
series by dropping an empty one. The metadata labels a drop removes are another matter: a merge
keeps two series that differ in __name__, __type__ or __unit__, so the labels half of the
proof is still asked above it. With that, a chain of drops - abs(abs(rate(x[5m]))) - pays for
the one merge at the bottom of the chain rather than one per drop.
Canonicalizing a map’s input is what makes the histogram case work: in
histogram_count(label_replace(x, "host", "same", "host", ".*")) two scan partitions each
rewrite one series to {host="same"} and histogram_count drops both - they are float samples -
before anything could report that the two now share a label set at one point. The merge is
inserted between the rewrite and histogram_count, so the canonical router judges the occupancy
the rewrite created and reports the collision instead of an empty answer.
Running the pass over its own output changes nothing: the node that gained a merge then answers
one series per label set, so duplicates_canonical_labels is false for it and a second run
finds nothing to add. That also makes the pass safe to apply at every segment boundary rather
than only at the statement root.
What this pass does not decide is placement: whether the merge can be answered inside one execution is the lowering’s question. A merge that spans partitions uses a producer and the existing canonical router; downstream consumers read ordinary evaluated frames.
Functions§
- canonical 🔒
- Wraps
nodein aNativeExpr::Mergewhen its pieces can share a label set. - merge_
label_ rewrites - Rewrites
treeso that every consumer of a whole canonical series is handed one. - rewrite 🔒
- Rewrites
nodebottom-up: its children first, then the merge its own consumer contract needs of them.