diff --git a/pkg/tsdb/influxdb/influxql/response_parser.go b/pkg/tsdb/influxdb/influxql/response_parser.go index bd09676cb8f..c8824163823 100644 --- a/pkg/tsdb/influxdb/influxql/response_parser.go +++ b/pkg/tsdb/influxdb/influxql/response_parser.go @@ -83,6 +83,10 @@ func transformRows(rows []models.Row, query models.Query) data.Frames { } } + if len(rows) == 0 { + return make([]*data.Frame, 0) + } + // Preallocate for the worst-case scenario frames := make([]*data.Frame, 0, len(rows)*len(rows[0].Columns)) diff --git a/pkg/tsdb/influxdb/influxql/response_parser_test.go b/pkg/tsdb/influxdb/influxql/response_parser_test.go index b84eeb1860f..0b68b619d61 100644 --- a/pkg/tsdb/influxdb/influxql/response_parser_test.go +++ b/pkg/tsdb/influxdb/influxql/response_parser_test.go @@ -407,6 +407,7 @@ func TestInfluxdbResponseParser(t *testing.T) { ) testFrame.Meta = &data.FrameMeta{PreferredVisualization: graphVisType, ExecutedQueryString: "Test raw query"} result := ResponseParse(prepare(response), 200, generateQuery(query)) + t.Run("should parse aliases", func(t *testing.T) { if diff := cmp.Diff(testFrame, result.Frames[0], data.FrameTestCompareOptions()...); diff != "" { t.Errorf("Result mismatch (-want +got):\n%s", diff) @@ -575,6 +576,7 @@ func TestInfluxdbResponseParser(t *testing.T) { t.Errorf("Result mismatch (-want +got):\n%s", diff) } }) + t.Run("shouldn't parse aliases", func(t *testing.T) { query = models.Query{Alias: "alias words with no brackets"} result = ResponseParse(prepare(response), 200, generateQuery(query)) @@ -681,6 +683,23 @@ func TestInfluxdbResponseParser(t *testing.T) { _, err := parseTimestamp("hello") require.Error(t, err) }) + + t.Run("InfluxDB returns empty DataResponse when there is empty response", func(t *testing.T) { + response := ` + { + "results": [ + { + "statement_id": 0 + } + ] + } + ` + + query := models.Query{} + result := ResponseParse(prepare(response), 200, generateQuery(query)) + assert.NotNil(t, result.Frames) + assert.Equal(t, 0, len(result.Frames)) + }) } func TestResponseParser_Parse_RetentionPolicy(t *testing.T) {