grafana / sqlds

A package that assists writing SQL-driven datasources
Apache License 2.0
17 stars 12 forks source link

Macro matching is too lax #82

Closed fionera closed 1 year ago

fionera commented 1 year ago

While debugging the Unit-Tests of https://github.com/grafana/clickhouse-datasource I found out that sqlds macro match is too lax. This is basically an extension of https://github.com/grafana/sqlds/issues/53. The biggest issue is that it does not fail always, because it is dependent on the order of the map, which is not stable

Input:

select * from foo where ( date >= $__fromTime and date <= $__toTime ) limit 100

Output:

select * from foo where ( date >= toDateTime(intDiv(-6795364578871,1000)) ) limit 100

Expected Output:

select * from foo where ( date >= toDateTime(intDiv(-6795364578871,1000)) and date <= toDateTime(intDiv(-6795364578871,1000)) )

Reason:

It fails when it is evaluating toTime (the second macro inside the parentheses) and then fromTime. The match will contain the already evaluated macro and call strings.Replace onto it, which will replace it and create the wrong query. image

When evaluating fromTime first, the match is correct: image

Additional Info:

Working:

image

Broken:

image