From 2b0cc8c9b46703a132eeb63843994818adc19bec Mon Sep 17 00:00:00 2001 From: Marcus Efraimsson Date: Wed, 21 Apr 2021 16:55:59 +0200 Subject: [PATCH] Postgres: Fix time group macro when TimescaleDB is enabled and interval is less than a second (#33153) (#33219) Fixing a special case with time group macro when using TimescaleDB and interval is lower than a second. Fixes #33124 (cherry picked from commit dd0ba96d7c75c6c5f3624d3349b0a3c8c2e5103a) --- pkg/tsdb/postgres/macros.go | 2 +- pkg/tsdb/postgres/macros_test.go | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/pkg/tsdb/postgres/macros.go b/pkg/tsdb/postgres/macros.go index d11f3e19cd4..05dc547fb09 100644 --- a/pkg/tsdb/postgres/macros.go +++ b/pkg/tsdb/postgres/macros.go @@ -107,7 +107,7 @@ func (m *postgresMacroEngine) evaluateMacro(name string, args []string) (string, } if m.timescaledb { - return fmt.Sprintf("time_bucket('%.0fs',%s)", interval.Seconds(), args[0]), nil + return fmt.Sprintf("time_bucket('%.1fs',%s)", interval.Seconds(), args[0]), nil } return fmt.Sprintf( diff --git a/pkg/tsdb/postgres/macros_test.go b/pkg/tsdb/postgres/macros_test.go index 106d71effa0..d285adcce55 100644 --- a/pkg/tsdb/postgres/macros_test.go +++ b/pkg/tsdb/postgres/macros_test.go @@ -94,21 +94,28 @@ func TestMacroEngine(t *testing.T) { sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column,'5m')") So(err, ShouldBeNil) - So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)") + So(sql, ShouldEqual, "GROUP BY time_bucket('300.0s',time_column)") }) Convey("interpolate __timeGroup function with spaces between args and TimescaleDB enabled", func() { sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '5m')") So(err, ShouldBeNil) - So(sql, ShouldEqual, "GROUP BY time_bucket('300s',time_column)") + So(sql, ShouldEqual, "GROUP BY time_bucket('300.0s',time_column)") }) Convey("interpolate __timeGroup function with large time range as an argument and TimescaleDB enabled", func() { sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '12d')") So(err, ShouldBeNil) - So(sql, ShouldEqual, "GROUP BY time_bucket('1036800s',time_column)") + So(sql, ShouldEqual, "GROUP BY time_bucket('1036800.0s',time_column)") + }) + + Convey("interpolate __timeGroup function with small time range as an argument and TimescaleDB enabled", func() { + sql, err := engineTS.Interpolate(query, timeRange, "GROUP BY $__timeGroup(time_column , '200ms')") + So(err, ShouldBeNil) + + So(sql, ShouldEqual, "GROUP BY time_bucket('0.2s',time_column)") }) Convey("interpolate __unixEpochFilter function", func() {