From 8214676363cdaddb11cb7898ae6a13cbf7e879e1 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Tue, 15 Dec 2015 11:29:20 -0700 Subject: [PATCH] fix panic when merging empty series --- CHANGELOG.md | 1 + services/httpd/handler.go | 18 ++++++++++-------- services/httpd/handler_test.go | 19 +++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f577cdc8c..4d308b77fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ With this release InfluxDB is moving to Go 1.5. ### Bugfixes - [#5042](https://github.com/influxdb/influxdb/issues/5042): Count with fill(none) will drop 0 valued intervals. +- [#4735](https://github.com/influxdb/influxdb/issues/4735): Fix panic when merging empty results. - [#5016](https://github.com/influxdb/influxdb/pull/5016): Don't panic if Meta data directory not writable. Thanks @oiooj - [#5059](https://github.com/influxdb/influxdb/pull/5059): Fix unmarshal of database error by client code. Thanks @farshidtz - [#4940](https://github.com/influxdb/influxdb/pull/4940): Fix distributed aggregate query query error. Thanks @li-ang diff --git a/services/httpd/handler.go b/services/httpd/handler.go index 53b7ef2b5d..a1ab8ecb00 100644 --- a/services/httpd/handler.go +++ b/services/httpd/handler.go @@ -328,17 +328,19 @@ func (h *Handler) serveQuery(w http.ResponseWriter, r *http.Request, user *meta. resp.Results = append(resp.Results, r) } else if resp.Results[l-1].StatementID == r.StatementID { cr := resp.Results[l-1] - lastSeries := cr.Series[len(cr.Series)-1] rowsMerged := 0 + if len(cr.Series) > 0 { + lastSeries := cr.Series[len(cr.Series)-1] - for _, row := range r.Series { - if !lastSeries.SameSeries(row) { - // Next row is for a different series than last. - break + for _, row := range r.Series { + if !lastSeries.SameSeries(row) { + // Next row is for a different series than last. + break + } + // Values are for the same series, so append them. + lastSeries.Values = append(lastSeries.Values, row.Values...) + rowsMerged++ } - // Values are for the same series, so append them. - lastSeries.Values = append(lastSeries.Values, row.Values...) - rowsMerged++ } // Append remaining rows as new rows. diff --git a/services/httpd/handler_test.go b/services/httpd/handler_test.go index 9702821b64..48576dfcca 100644 --- a/services/httpd/handler_test.go +++ b/services/httpd/handler_test.go @@ -195,6 +195,25 @@ func TestHandler_Query_MergeResults(t *testing.T) { } } +// Ensure the handler merges results from the same statement. +func TestHandler_Query_MergeEmptyResults(t *testing.T) { + h := NewHandler(false) + h.QueryExecutor.ExecuteQueryFn = func(q *influxql.Query, db string, chunkSize int, closing chan struct{}) (<-chan *influxql.Result, error) { + return NewResultChan( + &influxql.Result{StatementID: 1, Series: models.Rows{}}, + &influxql.Result{StatementID: 1, Series: models.Rows([]*models.Row{{Name: "series1"}})}, + ), nil + } + + w := httptest.NewRecorder() + h.ServeHTTP(w, MustNewJSONRequest("GET", "/query?db=foo&q=SELECT+*+FROM+bar", nil)) + if w.Code != http.StatusOK { + t.Fatalf("unexpected status: %d", w.Code) + } else if w.Body.String() != `{"results":[{"series":[{"name":"series1"}]}]}` { + t.Fatalf("unexpected body: %s", w.Body.String()) + } +} + // Ensure the handler can parse chunked and chunk size query parameters. func TestHandler_Query_Chunked(t *testing.T) { h := NewHandler(false)