Fix MSSQL queries failing because of bad interpolation (#63167)

fix failing mssql queries
This commit is contained in:
Victor Marin 2023-02-09 10:09:52 +02:00 committed by GitHub
parent 804bd08f11
commit 62b078e4e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -49,9 +49,13 @@ func (m *msSQLMacroEngine) Interpolate(query *backend.DataQuery, timeRange backe
// TODO: Return any error // TODO: Return any error
rExp, _ := regexp.Compile(sExpr) rExp, _ := regexp.Compile(sExpr)
var macroError error var macroError error
formattedSql, fctCallArgsMap := replaceFunctionCallArgsWithPlaceholders(sql) var fctCallArgsMap map[string]string
sql = m.ReplaceAllStringSubmatchFunc(rExp, formattedSql, func(groups []string) string { if rExp.FindAllSubmatchIndex([]byte(sql), -1) != nil {
sql, fctCallArgsMap = replaceFunctionCallArgsWithPlaceholders(sql)
}
sql = m.ReplaceAllStringSubmatchFunc(rExp, sql, func(groups []string) string {
args := strings.Split(groups[2], ",") args := strings.Split(groups[2], ",")
for i, arg := range args { for i, arg := range args {
args[i] = strings.Trim(arg, " ") args[i] = strings.Trim(arg, " ")

View File

@ -349,6 +349,20 @@ func TestMacroEngine(t *testing.T) {
require.NotNil(t, err) require.NotNil(t, err)
} }
}) })
t.Run("should return unmodified sql if there are no macros present", func(t *testing.T) {
sqls := []string{
"select * from table",
"select count(val) from table",
"select col1, col2,col3, col4 from table where col1 = 'val1' and col2 = 'val2' order by col1 asc",
}
for _, sql := range sqls {
actual, err := engine.Interpolate(query, timeRange, sql)
require.Nil(t, err)
require.Equal(t, sql, actual)
}
})
}) })
} }