Repository: prometheus/prometheus
File: promql/engine.go (Go)

Code before:
	}

	// Group series by their labelset hash.
	seriesByHash := make(map[uint64][]int)
	for i := range mat {
		hash := mat[i].Metric.Hash()
		seriesByHash[hash] = append(seriesByHash[hash], i)
	}

	// Check if any merging is needed.
	needsMerge := false
	for _, indices := range seriesByHash {
		if len(indices) > 1 {
			needsMerge = true
			break
		}
	}

	if !needsMerge {
		return mat
	}

	// Merge series with the same labelset.
	merged := make(Matrix, 0, len(seriesByHash))
	for _, indices := range seriesByHash {
		base := mat[indices[0]]

		if len(indices) == 1 {
			// No collision, add as-is.
			merged = append(merged, base)
			continue
		}

		// Multiple series with the same labelset - check for overlaps and merge.
		// Build a set of timestamps to detect overlaps.
		timestamps := make(map[int64]struct{}, len(base.Floats)+len(base.Histograms))
		for _, p := range base.Floats {
			timestamps[p.T] = struct{}{}
		}
		for _, p := range base.Histograms {
			timestamps[p.T] = struct{}{}
		}

		// Merge remaining series, checking for timestamp overlaps.
		for _, idx := range indices[1:] {
			series := mat[idx]

			// Check floats for overlaps.
			for _, p := range series.Floats {
				if _, exists := timestamps[p.T]; exists {
					ev.errorf("vector cannot contain metrics with the same labelset")

Review comment (suggestion):
Comparing this to my version in https://github.com/prometheus/prometheus/commit/3bf4b1712475c386a0573c10fdf4a39358f31f10#diff-c4ab017fbcdb5a144736e99798034dc43f3015c460d8a689d783e80e8d2ea2b0, which was mostly Claude to be fair (which is why I hadn't opened a PR yet), do we need this separate step for indexing the first series' (`base`) timestamp before all of the others? Wouldn't it be sufficient to just start directly with the loop below and including that first series in there? It would still make sense to keep the `if len(indices) == 1` special case of course.

Code after:
	}

	// Fast path: check if there are any duplicate labelsets without allocating.
	// This is the common case and we want to avoid allocations.
	if !mat.ContainsSameLabelset() {
		return mat
	}

	// Slow path: there are duplicates, so we need to merge series with non-overlapping timestamps.
	// Group series by their labelset hash.
	seriesByHash := make(map[uint64][]int)
	for i := range mat {
		hash := mat[i].Metric.Hash()
		seriesByHash[hash] = append(seriesByHash[hash], i)
	}

	// Merge series with the same labelset.
	merged := make(Matrix, 0, len(seriesByHash))
	for _, indices := range seriesByHash {
		if len(indices) == 1 {
			// No collision, add as-is.
			merged = append(merged, mat[indices[0]])
			continue
		}

		// Multiple series with the same labelset - merge all samples.
		base := mat[indices[0]]
		for _, idx := range indices[1:] {
			base.Floats = append(base.Floats, mat[idx].Floats...)
			base.Histograms = append(base.Histograms, mat[idx].Histograms...)
		}

		// Sort merged samples by timestamp.
		sort.Slice(base.Floats, func(i, j int) bool {
			return base.Floats[i].T < base.Floats[j].T
		})
		sort.Slice(base.Histograms, func(i, j int) bool {
			return base.Histograms[i].T < base.Histograms[j].T
		})

		// Check for duplicate timestamps in sorted samples.
		for i := 1; i < len(base.Floats); i++ {
			if base.Floats[i].T == base.Floats[i-1].T {
				ev.errorf("vector cannot contain metrics with the same labelset")
			}
		}
		for i := 1; i < len(base.Histograms); i++ {
			if base.Histograms[i].T == base.Histograms[i-1].T {
				ev.errorf("vector cannot contain metrics with the same labelset")
			}
		}