From d56c5285e213152c1a4b325da2e9fea6fca5881c Mon Sep 17 00:00:00 2001 From: Ricky Putra Date: Tue, 16 Feb 2021 00:15:57 +0800 Subject: [PATCH] Postgres: Fix timeGroup macro converts long intervals to invalid numbers when TimescaleDB is enabled (#31179) Fixes #27253 --- pkg/tsdb/postgres/macros.go | 2 +- pkg/tsdb/postgres/macros_test.go | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pkg/tsdb/postgres/macros.go b/pkg/tsdb/postgres/macros.go index a256bde330e..d11f3e19cd4 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('%vs',%s)", interval.Seconds(), args[0]), nil + return fmt.Sprintf("time_bucket('%.0fs',%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 b11a3efccb4..106d71effa0 100644 --- a/pkg/tsdb/postgres/macros_test.go +++ b/pkg/tsdb/postgres/macros_test.go @@ -104,6 +104,13 @@ func TestMacroEngine(t *testing.T) { So(sql, ShouldEqual, "GROUP BY time_bucket('300s',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)") + }) + Convey("interpolate __unixEpochFilter function", func() { sql, err := engine.Interpolate(query, timeRange, "select $__unixEpochFilter(time)") So(err, ShouldBeNil)