From aebf6e7fc17eb30167a343249bc279e91d8c680a Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Thu, 4 May 2023 14:29:05 +0100 Subject: [PATCH] [v9.5.x] InfluxDB: Fix adding FROM statement when the measurement is an empty string (#67835) InfluxDB: Fix adding FROM statement when the measurement is an empty string (#67827) * If the measurement empty don't add FROM statement * Add comment line (cherry picked from commit 764f87b4859a119942e889f051125fbf8f7f15c5) Co-authored-by: ismail simsek --- .../app/plugins/datasource/influxdb/query_builder.ts | 11 +++++++++-- .../datasource/influxdb/specs/query_builder.test.ts | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/public/app/plugins/datasource/influxdb/query_builder.ts b/public/app/plugins/datasource/influxdb/query_builder.ts index e62d4c818cf..60d190602da 100644 --- a/public/app/plugins/datasource/influxdb/query_builder.ts +++ b/public/app/plugins/datasource/influxdb/query_builder.ts @@ -64,7 +64,8 @@ export class InfluxQueryBuilder { measurement = this.target.measurement; policy = this.target.policy; - if (!measurement.match('^/.*/')) { + // If there is a measurement and it is not empty string + if (!measurement.match(/^\/.*\/|^$/)) { measurement = '"' + measurement + '"'; if (policy && policy !== 'default') { @@ -73,6 +74,10 @@ export class InfluxQueryBuilder { } } + if (measurement === '') { + return 'SHOW FIELD KEYS'; + } + return 'SHOW FIELD KEYS FROM ' + measurement; } else if (type === 'RETENTION POLICIES') { query = 'SHOW RETENTION POLICIES on "' + this.database + '"'; @@ -89,7 +94,9 @@ export class InfluxQueryBuilder { measurement = policy + '.' + measurement; } - query += ' FROM ' + measurement; + if (measurement !== '') { + query += ' FROM ' + measurement; + } } if (withKey) { diff --git a/public/app/plugins/datasource/influxdb/specs/query_builder.test.ts b/public/app/plugins/datasource/influxdb/specs/query_builder.test.ts index 07be475e028..17569325243 100644 --- a/public/app/plugins/datasource/influxdb/specs/query_builder.test.ts +++ b/public/app/plugins/datasource/influxdb/specs/query_builder.test.ts @@ -213,5 +213,13 @@ describe('InfluxQueryBuilder', () => { const query = builder.buildExploreQuery('TAG_KEYS'); expect(query).toBe(`SHOW TAG KEYS WHERE "app" == ''`); }); + + it('should not add FROM statement if the measurement empty', () => { + const builder = new InfluxQueryBuilder({ measurement: '', tags: [] }); + let query = builder.buildExploreQuery('TAG_KEYS'); + expect(query).toBe('SHOW TAG KEYS'); + query = builder.buildExploreQuery('FIELDS'); + expect(query).toBe('SHOW FIELD KEYS'); + }); }); });