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

Code before:
	for si := range inputMatrix {
		f, h, ok := ev.nextValues(enh.Ts, &inputMatrix[si])
		if !ok {
			continue
		}

		group := &groups[seriesToResult[si]]
		// Initialize this group if it's the first time we've seen it.
		if !group.seen {
			switch op {
			case parser.AVG, parser.SUM:
				*group = groupedAggregation{
					seen:                   true,
					floatValue:             f,
					floatMean:              f,
					incompatibleHistograms: false,
					groupCount:             1,
				}
				if h == nil {
					group.hasFloat = true
				} else {
					group.histogramValue = h.Copy()
					group.hasHistogram = true
				}
			case parser.STDVAR, parser.STDDEV:
				if h == nil {
					fv := float64(0)
					if math.IsNaN(f) || math.IsInf(f, 0) {
						fv = math.NaN()
					}
					*group = groupedAggregation{
						seen:                   true,
						floatValue:             fv,
						floatMean:              f,
						incompatibleHistograms: false,
						groupCount:             1,
					}
				}
			case parser.QUANTILE:
				*group = groupedAggregation{
					seen:                   true,
					floatValue:             f,
					floatMean:              f,
					incompatibleHistograms: false,
					groupCount:             1,
				}
				group.heap = make(vectorByValueHeap, 1)
				group.heap[0] = Sample{F: f}
			case parser.GROUP:
				*group = groupedAggregation{
					seen:                   true,

Review comment (suggestion):
Instead of initialising the groupedAggregation for each case, can we just set the `group.seen=false` when histogram is encountered for the first time in the group? If YES, then it will reduce a lot of code repeatations.

Code after:
		return lhs, nil, lhs != rhs, nil
	case parser.GTR:
		return lhs, nil, lhs > rhs, nil
	case parser.LSS:
		return lhs, nil, lhs < rhs, nil
	case parser.GTE:
		return lhs, nil, lhs >= rhs, nil
	case parser.LTE:
		return lhs, nil, lhs <= rhs, nil
	case parser.ATAN2:
		return math.Atan2(lhs, rhs), nil, true, nil
	}
	panic(fmt.Errorf("operator %q not allowed for operations between Vectors", op))
}

type groupedAggregation struct {
	floatValue     float64
	histogramValue *histogram.FloatHistogram
	floatMean      float64
	floatKahanC    float64 // "Compensating value" for Kahan summation.
	groupCount     float64
	heap           vectorByValueHeap

	// All bools together for better packing within the struct.
	seen                   bool // Was this output groups seen in the input at this timestamp.
	hasFloat               bool // Has at least 1 float64 sample aggregated.
	hasHistogram           bool // Has at least 1 histogram sample aggregated.
	incompatibleHistograms bool // If true, group has seen mixed exponential and custom buckets, or incompatible custom buckets.
	groupAggrComplete      bool // Used by LIMITK to short-cut series loop when we've reached K elem on every group.
	incrementalMean        bool // True after reverting to incremental calculation of the mean value.
}

// aggregation evaluates sum, avg, count, stdvar, stddev or quantile at one timestep on inputMatrix.
// These functions produce one output series for each group specified in the expression, with just the labels from `by(...)`.
// outputMatrix should be already populated with grouping labels; groups is one-to-one with outputMatrix.
// seriesToResult maps inputMatrix indexes to outputMatrix indexes.
func (ev *evaluator) aggregation(e *parser.AggregateExpr, q float64, inputMatrix, outputMatrix Matrix, seriesToResult []int, groups []groupedAggregation, enh *EvalNodeHelper) annotations.Annotations {
	op := e.Op
	var annos annotations.Annotations
	for i := range groups {
		groups[i].seen = false
	}

	for si := range inputMatrix {
		f, h, ok := ev.nextValues(enh.Ts, &inputMatrix[si])
		if !ok {
			continue
		}

		group := &groups[seriesToResult[si]]
		// Initialize this group if it's the first time we've seen it.