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

Code before:
	)

	for i := 2; i < len(vectorVals); i++ {
		q := vectorVals[i][0].F

		if err := validateQuantile(q, args[i]); err != nil {
			annos.Add(err)
		}

		qs = append(qs, q)
	}

	annos.Merge(enh.resetHistograms(inVec, args[1]))

	for _, q := range qs {
		var qStr string

		getOrCreateLblsWithQuantile := func(lbls labels.Labels) labels.Labels {
			if enh.signatureToLabelsWithQuantile == nil {
				enh.signatureToLabelsWithQuantile = make(map[string]map[float64]labels.Labels)
			}

			enh.lblBuf = lbls.Bytes(enh.lblBuf)
			cachedLbls, ok := enh.signatureToLabelsWithQuantile[string(enh.lblBuf)]
			if !ok {
				enh.signatureToLabelsWithQuantile[string(enh.lblBuf)] = make(map[float64]labels.Labels, len(qs))
			}

			cachedLblsWithQuantile, ok := cachedLbls[q]
			if !ok {
				if qStr == "" {
					qStr = fmt.Sprintf("%g", q)
				}
				cachedLblsWithQuantile = labels.NewBuilder(lbls).
					Set(quantileLabel, qStr).
					Labels()

				enh.signatureToLabelsWithQuantile[string(enh.lblBuf)] = cachedLbls
			}

			return cachedLblsWithQuantile
		}

		// Deal with the native histograms.
		for _, sample := range enh.nativeHistogramSamples {
			if sample.H == nil {
				// Native histogram conflicts with classic histogram at the same timestamp, ignore.
				continue
			}
			if !enh.enableDelayedNameRemoval {
				sample.Metric = sample.Metric.DropReserved(schema.IsMetadataLabel)

Review comment (performance):
Should this be the following?

```suggestion
				cachedLbls = make(map[float64]labels.Labels, len(qs))
				enh.signatureToLabelsWithQuantile[string(enh.lblBuf)] = cachedLbls
```

Code after:
			// Native histogram conflicts with classic histogram at the same timestamp, ignore.
			continue
		}
		if !enh.enableDelayedNameRemoval {
			sample.Metric = sample.Metric.DropReserved(schema.IsMetadataLabel)
		}
		hf, hfAnnos := HistogramFraction(lower, upper, sample.H, getMetricName(sample.Metric), args[0].PositionRange())
		annos.Merge(hfAnnos)
		enh.Out = append(enh.Out, Sample{
			Metric:   sample.Metric,
			F:        hf,
			DropName: true,
		})
	}

	// Deal with classic histograms that have already been filtered for conflicting native histograms.
	for _, mb := range enh.signatureToMetricWithBuckets {
		if len(mb.buckets) == 0 {
			continue
		}
		if !enh.enableDelayedNameRemoval {
			mb.metric = mb.metric.DropReserved(schema.IsMetadataLabel)
		}

		enh.Out = append(enh.Out, Sample{
			Metric:   mb.metric,
			F:        BucketFraction(lower, upper, mb.buckets),
			DropName: true,
		})
	}

	return enh.Out, annos
}

// === histogram_quantile(k parser.ValueTypeScalar, Vector parser.ValueTypeVector) (Vector, Annotations) ===
func funcHistogramQuantile(vectorVals []Vector, _ Matrix, args parser.Expressions, enh *EvalNodeHelper) (Vector, annotations.Annotations) {
	if len(vectorVals) < 2 || len(vectorVals[0]) == 0 {
		return enh.Out, nil
	}
	q := vectorVals[0][0].F
	inVec := vectorVals[1]
	var annos annotations.Annotations

	if err := validateQuantile(q, args[0]); err != nil {
		annos.Add(err)
	}
	annos.Merge(enh.resetHistograms(inVec, args[1]))

	// Deal with the native histograms.
	for _, sample := range enh.nativeHistogramSamples {
		if sample.H == nil {