From 8ca8293d111611c8046463238098dbce1709c8f6 Mon Sep 17 00:00:00 2001 From: Daniel Morsing Date: Mon, 31 Aug 2015 11:51:12 -0700 Subject: [PATCH] only look at the first values for first() Since we set up the aggregate queries so that iterator is ordered, we only need to look at the first value. This cuts about 10 seconds off a large single series query running first. --- influxql/functions.go | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/influxql/functions.go b/influxql/functions.go index 7e59620544..f2505ac643 100644 --- a/influxql/functions.go +++ b/influxql/functions.go @@ -935,28 +935,20 @@ type firstLastMapOutput struct { } // MapFirst collects the values to pass to the reducer +// This function assumes time ordered input func MapFirst(itr Iterator) interface{} { - out := &firstLastMapOutput{} - pointsYielded := false - - for k, v := itr.Next(); k != -1; k, v = itr.Next() { - // Initialize first - if !pointsYielded { - out.Time = k - out.Val = v - pointsYielded = true - } - if k < out.Time { - out.Time = k - out.Val = v - } else if k == out.Time && greaterThan(v, out.Val) { - out.Val = v - } + k, v := itr.Next() + if k == -1 { + return nil } - if pointsYielded { - return out - } - return nil + nextk, nextv := itr.Next() + for nextk == k { + if greaterThan(nextv, v) { + v = nextv + } + nextk, nextv = itr.Next() + } + return &firstLastMapOutput{k, v} } // ReduceFirst computes the first of value.