Skip to content

Instantly share code, notes, and snippets.

@jwilder
Created August 26, 2015 03:16
Show Gist options
  • Save jwilder/f6fb45c18bcd1976c4e3 to your computer and use it in GitHub Desktop.
Save jwilder/f6fb45c18bcd1976c4e3 to your computer and use it in GitHub Desktop.
Deterministic First/Last
diff --git a/influxql/functions.go b/influxql/functions.go
index 3d75c5a..918f241 100644
--- a/influxql/functions.go
+++ b/influxql/functions.go
@@ -949,6 +949,8 @@ func MapFirst(itr Iterator) interface{} {
if k < out.Time {
out.Time = k
out.Val = v
+ } else if k == out.Time && greaterThan(v, out.Val) {
+ out.Val = v
}
}
if pointsYielded {
@@ -976,6 +978,8 @@ func ReduceFirst(values []interface{}) interface{} {
if val.Time < out.Time {
out.Time = val.Time
out.Val = val.Val
+ } else if val.Time == out.Time && greaterThan(val.Val, out.Val) {
+ out.Val = val.Val
}
}
if pointsYielded {
@@ -999,6 +1003,8 @@ func MapLast(itr Iterator) interface{} {
if k > out.Time {
out.Time = k
out.Val = v
+ } else if k == out.Time && greaterThan(v, out.Val) {
+ out.Val = v
}
}
if pointsYielded {
@@ -1027,6 +1033,8 @@ func ReduceLast(values []interface{}) interface{} {
if val.Time > out.Time {
out.Time = val.Time
out.Val = val.Val
+ } else if val.Time == out.Time && greaterThan(val.Val, out.Val) {
+ out.Val = val.Val
}
}
if pointsYielded {
@@ -1078,6 +1086,20 @@ func ReducePercentile(percentile float64) ReduceFunc {
}
}
+func greaterThan(a, b interface{}) bool {
+ switch t := a.(type) {
+ case int64:
+ return t > b.(int64)
+ case float64:
+ return t > b.(float64)
+ case string:
+ return t > b.(string)
+ case bool:
+ return t == true
+ }
+ return false
+}
+
// IsNumeric returns whether a given aggregate can only be run on numeric fields.
func IsNumeric(c *Call) bool {
switch c.Name {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment