From 004bb7689da17806f0263a9503a2599c424a46dc Mon Sep 17 00:00:00 2001
From: George Robinson <george.robinson@grafana.com>
Date: Wed, 12 Oct 2022 17:34:28 +0100
Subject: [PATCH] Alerting: Fix mathexp.NoData for ConditionsCmd (#56816)

---
 pkg/expr/classic/classic.go      |  5 +++++
 pkg/expr/classic/classic_test.go | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/pkg/expr/classic/classic.go b/pkg/expr/classic/classic.go
index f0969c33ad7..4bcc06a21a9 100644
--- a/pkg/expr/classic/classic.go
+++ b/pkg/expr/classic/classic.go
@@ -82,6 +82,11 @@ func (cmd *ConditionsCmd) Execute(_ context.Context, vars mathexp.Vars) (mathexp
 			var reducedNum mathexp.Number
 			var name string
 			switch v := val.(type) {
+			case mathexp.NoData:
+				// To keep this code as simple as possible we translate mathexp.NoData into a
+				// mathexp.Number with a nil value so number.GetFloat64Value() returns nil
+				reducedNum = mathexp.NewNumber("no data", nil)
+				reducedNum.SetValue(nil)
 			case mathexp.Series:
 				reducedNum = c.Reducer.Reduce(v)
 				name = v.GetName()
diff --git a/pkg/expr/classic/classic_test.go b/pkg/expr/classic/classic_test.go
index c5a895ae35e..ad679d1cb62 100644
--- a/pkg/expr/classic/classic_test.go
+++ b/pkg/expr/classic/classic_test.go
@@ -326,6 +326,29 @@ func TestConditionsCmdExecute(t *testing.T) {
 		},
 		{
 			name: "single query with no data",
+			vars: mathexp.Vars{
+				"A": mathexp.Results{
+					Values: []mathexp.Value{mathexp.NoData{}.New()},
+				},
+			},
+			conditionsCmd: &ConditionsCmd{
+				Conditions: []condition{
+					{
+						InputRefID: "A",
+						Reducer:    reducer("avg"),
+						Operator:   "and",
+						Evaluator:  &thresholdEvaluator{"gt", 1},
+					},
+				},
+			},
+			resultNumber: func() mathexp.Number {
+				v := valBasedNumber(nil)
+				v.SetMeta([]EvalMatch{{Metric: "NoData"}})
+				return v
+			},
+		},
+		{
+			name: "single query with no values",
 			vars: mathexp.Vars{
 				"A": mathexp.Results{
 					Values: []mathexp.Value{},