PostgreSQL: __unixEpochGroup to support arithmetic expression as argument (#46764)

* __unixEpochGroup to support arithmetic argument

Following call generates wrong expression : 
 
$__unixEpochGroupAlias(height+42,$__interval)

=> floor(height+42/60)*60 AS "time"

instead of

=> floor((height+42)/60)*60 AS "time"

* Update test of __unixEpochGroup related to issue #46764
This commit is contained in:
Julien NOEL 2022-03-26 18:44:14 +01:00 committed by GitHub
parent 7bc821baf2
commit f4b6c4019c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 4 deletions

View File

@ -146,7 +146,7 @@ func (m *postgresMacroEngine) evaluateMacro(timeRange backend.TimeRange, query *
return "", err
}
}
return fmt.Sprintf("floor(%s/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
return fmt.Sprintf("floor((%s)/%v)*%v", args[0], interval.Seconds(), interval.Seconds()), nil
case "__unixEpochGroupAlias":
tg, err := m.evaluateMacro(timeRange, query, "__unixEpochGroup", args)
if err == nil {

View File

@ -137,11 +137,11 @@ func TestMacroEngine(t *testing.T) {
})
t.Run("interpolate __unixEpochGroup function", func(t *testing.T) {
sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column,'5m')")
sql, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroup(time_column+time_adjustment,'5m')")
require.NoError(t, err)
sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column,'5m')")
sql2, err := engine.Interpolate(query, timeRange, "SELECT $__unixEpochGroupAlias(time_column+time_adjustment,'5m')")
require.NoError(t, err)
require.Equal(t, "SELECT floor(time_column/300)*300", sql)
require.Equal(t, "SELECT floor((time_column+time_adjustment)/300)*300", sql)
require.Equal(t, sql2, sql+" AS \"time\"")
})
})