From 6fb18ecf335e8fa1a88d0b8879100d8bf5141f9a Mon Sep 17 00:00:00 2001 From: Andreas Christou Date: Wed, 19 Feb 2025 17:23:52 +0000 Subject: [PATCH] [release-11.4.3] InfluxDB: Improve handling of template variables contained in regular expressions (InfluxQL) (#100987) InfluxDB: Improve handling of template variables contained in regular expressions (InfluxQL) (#100762) * Improve handling of template vars in regex * Review * Minor update (cherry picked from commit 8b3047ba1e00779efedaae0eb6345b44cd8b90d5) --- .../datasource/influxdb/datasource.test.ts | 12 ++++++++++ .../plugins/datasource/influxdb/datasource.ts | 23 +++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/datasource.test.ts b/public/app/plugins/datasource/influxdb/datasource.test.ts index b2cdd4522e3..284717ac701 100644 --- a/public/app/plugins/datasource/influxdb/datasource.test.ts +++ b/public/app/plugins/datasource/influxdb/datasource.test.ts @@ -363,6 +363,18 @@ describe('interpolateQueryExpr', () => { expect(result).toBe(expectation); }); + it('should **not** return the escaped value if the value **is not** wrapped in regex and the query is more complex (e.g. text is contained between two / but not a regex', () => { + const value = 'testmatch'; + const variableMock = queryBuilder().withId('tempVar').withName('tempVar').withMulti(false).build(); + const result = ds.interpolateQueryExpr( + value, + variableMock, + `select value where ("tag"::tag =~ /value/) AND where other = $tempVar $timeFilter GROUP BY time($__interval) tz('Europe/London')` + ); + const expectation = `testmatch`; + expect(result).toBe(expectation); + }); + it('should return floating point number as it is', () => { const variableMock = queryBuilder() .withId('tempVar') diff --git a/public/app/plugins/datasource/influxdb/datasource.ts b/public/app/plugins/datasource/influxdb/datasource.ts index 1292ac4b625..a0be6d90bf3 100644 --- a/public/app/plugins/datasource/influxdb/datasource.ts +++ b/public/app/plugins/datasource/influxdb/datasource.ts @@ -351,17 +351,30 @@ export default class InfluxDatasource extends DataSourceWithBackend escapeRegex(v)).join('|')})`; + return typeof value === 'string' ? escapeRegex(value) : `(${value.map((v) => escapeRegex(v)).join('|')})`; } return value;