Commit 1601f6d1 by Sven Klemm Committed by Torkel Ödegaard

[mysql] fix $__timeGroup rounding (#12469)

* fix $__timeGroup rounding for mysql

* revert accidentally commented out line
parent f017c04a
...@@ -103,7 +103,7 @@ func (m *MySqlMacroEngine) evaluateMacro(name string, args []string) (string, er ...@@ -103,7 +103,7 @@ func (m *MySqlMacroEngine) evaluateMacro(name string, args []string) (string, er
m.Query.Model.Set("fillValue", floatVal) m.Query.Model.Set("fillValue", floatVal)
} }
} }
return fmt.Sprintf("cast(cast(UNIX_TIMESTAMP(%s)/(%.0f) as signed)*%.0f as signed)", args[0], interval.Seconds(), interval.Seconds()), nil return fmt.Sprintf("UNIX_TIMESTAMP(%s) DIV %.0f * %.0f", args[0], interval.Seconds(), interval.Seconds()), nil
case "__unixEpochFilter": case "__unixEpochFilter":
if len(args) == 0 { if len(args) == 0 {
return "", fmt.Errorf("missing time column argument for macro %v", name) return "", fmt.Errorf("missing time column argument for macro %v", name)
......
...@@ -39,7 +39,7 @@ func TestMacroEngine(t *testing.T) { ...@@ -39,7 +39,7 @@ func TestMacroEngine(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')") sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY cast(cast(UNIX_TIMESTAMP(time_column)/(300) as signed)*300 as signed)") So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
}) })
Convey("interpolate __timeGroup function with spaces around arguments", func() { Convey("interpolate __timeGroup function with spaces around arguments", func() {
...@@ -47,7 +47,7 @@ func TestMacroEngine(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestMacroEngine(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')") sql, err := engine.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')")
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(sql, ShouldEqual, "GROUP BY cast(cast(UNIX_TIMESTAMP(time_column)/(300) as signed)*300 as signed)") So(sql, ShouldEqual, "GROUP BY UNIX_TIMESTAMP(time_column) DIV 300 * 300")
}) })
Convey("interpolate __timeFilter function", func() { Convey("interpolate __timeFilter function", func() {
......
...@@ -209,11 +209,12 @@ func TestMySQL(t *testing.T) { ...@@ -209,11 +209,12 @@ func TestMySQL(t *testing.T) {
So(queryResult.Error, ShouldBeNil) So(queryResult.Error, ShouldBeNil)
points := queryResult.Series[0].Points points := queryResult.Series[0].Points
So(len(points), ShouldEqual, 6) // without fill this should result in 4 buckets
So(len(points), ShouldEqual, 4)
dt := fromStart dt := fromStart
for i := 0; i < 3; i++ { for i := 0; i < 2; i++ {
aValue := points[i][0].Float64 aValue := points[i][0].Float64
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0) aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
So(aValue, ShouldEqual, 15) So(aValue, ShouldEqual, 15)
...@@ -221,9 +222,9 @@ func TestMySQL(t *testing.T) { ...@@ -221,9 +222,9 @@ func TestMySQL(t *testing.T) {
dt = dt.Add(5 * time.Minute) dt = dt.Add(5 * time.Minute)
} }
// adjust for 5 minute gap // adjust for 10 minute gap between first and second set of points
dt = dt.Add(5 * time.Minute) dt = dt.Add(10 * time.Minute)
for i := 3; i < 6; i++ { for i := 2; i < 4; i++ {
aValue := points[i][0].Float64 aValue := points[i][0].Float64
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0) aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
So(aValue, ShouldEqual, 20) So(aValue, ShouldEqual, 20)
...@@ -259,7 +260,7 @@ func TestMySQL(t *testing.T) { ...@@ -259,7 +260,7 @@ func TestMySQL(t *testing.T) {
dt := fromStart dt := fromStart
for i := 0; i < 3; i++ { for i := 0; i < 2; i++ {
aValue := points[i][0].Float64 aValue := points[i][0].Float64
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0) aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
So(aValue, ShouldEqual, 15) So(aValue, ShouldEqual, 15)
...@@ -267,17 +268,23 @@ func TestMySQL(t *testing.T) { ...@@ -267,17 +268,23 @@ func TestMySQL(t *testing.T) {
dt = dt.Add(5 * time.Minute) dt = dt.Add(5 * time.Minute)
} }
// check for NULL values inserted by fill
So(points[2][0].Valid, ShouldBeFalse)
So(points[3][0].Valid, ShouldBeFalse) So(points[3][0].Valid, ShouldBeFalse)
// adjust for 5 minute gap // adjust for 10 minute gap between first and second set of points
dt = dt.Add(5 * time.Minute) dt = dt.Add(10 * time.Minute)
for i := 4; i < 7; i++ { for i := 4; i < 6; i++ {
aValue := points[i][0].Float64 aValue := points[i][0].Float64
aTime := time.Unix(int64(points[i][1].Float64)/1000, 0) aTime := time.Unix(int64(points[i][1].Float64)/1000, 0)
So(aValue, ShouldEqual, 20) So(aValue, ShouldEqual, 20)
So(aTime, ShouldEqual, dt) So(aTime, ShouldEqual, dt)
dt = dt.Add(5 * time.Minute) dt = dt.Add(5 * time.Minute)
} }
// check for NULL values inserted by fill
So(points[6][0].Valid, ShouldBeFalse)
}) })
Convey("When doing a metric query using timeGroup with float fill enabled", func() { Convey("When doing a metric query using timeGroup with float fill enabled", func() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment