apache/druid · error · IllegalArgumentException

Metric mismatch, index[%d] [%s] != [%s]

Error message

Metric mismatch, index[%d] [%s] != [%s]

What it means

Thrown during segment merging when the metrics actually present in the merged indices do not line up, position by position, with the requested (sorted) metrics for the output segment. Druid validates that each aggregator name in the requested aggregator spec matches the metric found at the same index in the merged segment; any ordering or naming divergence aborts the merge. This is an internal consistency check to prevent writing a corrupt segment with misaligned metric columns.

Source

Thrown at processing/src/main/java/org/apache/druid/segment/IndexMergerBase.java:429

       */
      if (metricIndex > -1) {
        sortedMetricAggs[metricIndex] = metricAgg;
      }
    }

    /*
      If there is nothing at sortedMetricAggs[i], then we did not have a metricAgg whose name matched the name
      of the ith element of mergedMetrics. I.e. There was a metric in the indices to merge that we did not ask for.
     */
    for (int i = 0; i < sortedMetricAggs.length; i++) {
      if (sortedMetricAggs[i] == null) {
        throw new IAE("Indices to merge contained metric[%s], but requested metrics did not", mergedMetrics.get(i));
      }
    }

    for (int i = 0; i < mergedMetrics.size(); i++) {
      if (!sortedMetricAggs[i].getName().equals(mergedMetrics.get(i))) {
        throw new IAE(
            "Metric mismatch, index[%d] [%s] != [%s]",
            i,
            sortedMetricAggs[i].getName(),
            mergedMetrics.get(i)
        );
      }
    }

    Function<List<TransformableRowIterator>, TimeAndDimsIterator> rowMergerFn;
    if (rollup) {
      rowMergerFn = rowIterators -> new RowCombiningTimeAndDimsIterator(rowIterators, sortedMetricAggs, mergedMetrics);
    } else {
      rowMergerFn = MergingRowIterator::new;
    }

    List<Metadata> metadataList = Lists.transform(indexes, IndexableAdapter::getMetadata);
    AggregatorFactory[] combiningMetricAggs = new AggregatorFactory[sortedMetricAggs.length];
    for (int i = 0; i < sortedMetricAggs.length; i++) {

View on GitHub (pinned to 9b90983fd2)

Solutions

  1. Align the AggregatorFactory[] passed to merge with the metric names and order present in the input segments
  2. Re-ingest or normalize the input segments so all share the same metric schema
  3. Regenerate segments with a consistent dataSchema instead of hand-merging mismatched segments
  4. Check intermediate merges (multiphaseMerge) so phase output metrics match the final requested metrics

Example fix

// before
aggs = new AggregatorFactory[]{new LongSumAggregatorFactory("sum", "val"), new CountAggregatorFactory("cnt")};
merger.merge(segments, false, ..., aggs);
// after
// derive aggregators in the same name/order the segments contain
aggs = new AggregatorFactory[]{new CountAggregatorFactory("cnt"), new LongSumAggregatorFactory("sum", "val")};
merger.merge(segments, false, ..., aggs);
Defensive patterns

Strategy: validation

Validate before calling

final Set<String> segMetrics = segments.stream().flatMap(s -> Arrays.stream(s.getMetrics())) ...; // compare names & order to requestedAggs before merge
if (!Arrays.stream(aggs).map(AggregatorFactory::getName).collect(Collectors.toList()).equals(expectedMetricNames)) { throw new IllegalArgumentException("aggregator names/order do not match segment metrics"); }

Type guard

boolean metricsMatch(Segment[] segs, AggregatorFactory[] aggs) { ... }

Prevention

When it happens

Trigger: Calling IndexMerger.merge (directly or via multiphaseMerge/phaseOutput) with an AggregatorFactory[] whose names/order differ from the metrics in the segments being merged, or whose rollup/metric set was altered between input segments.

Common situations: Changing aggregator definitions (renaming a metric or reordering aggregators) while re-ingesting or re-indexing old segments; supervisor spec updates between segments; schema drift between generation segments during compaction.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of apache/druid@9b90983fd2 (2026-09-07). Data as JSON: /api/errors/ee461fd2350c1d33. Report an issue: GitHub.