diff --git a/src/data/CHDatasource.ts b/src/data/CHDatasource.ts index 23b3e3e4..92b2d492 100644 --- a/src/data/CHDatasource.ts +++ b/src/data/CHDatasource.ts @@ -511,7 +511,6 @@ export class Datasource this.skipAdHocFilter = true; if (tagSource.source === undefined) { - this.adHocFilter.setTargetTable('default'); const rawSql = 'SELECT name, type, table FROM system.columns'; const results = await this.runQuery({ rawSql }); return { type: TagType.schema, frame: results }; diff --git a/src/data/adHocFilter.test.ts b/src/data/adHocFilter.test.ts index c2355814..0e73fde9 100644 --- a/src/data/adHocFilter.test.ts +++ b/src/data/adHocFilter.test.ts @@ -194,4 +194,14 @@ describe('AdHocManager', () => { expect(warn).toHaveBeenCalledTimes(1); expect(warn).toHaveBeenCalledWith('Invalid adhoc filter will be ignored:', value); }); + + it('apply ad hoc filter with no set table', () => { + const ahm = new AdHocFilter(); + const val = ahm.apply('SELECT stuff FROM foo', [ + { key: 'key', operator: '=', value: 'val' } + ] as AdHocVariableFilter[]); + expect(val).toEqual( + `SELECT stuff FROM foo settings additional_table_filters={'foo' : ' key = \\'val\\' '}` + ); + }); }); diff --git a/src/data/adHocFilter.ts b/src/data/adHocFilter.ts index f11cd33e..360502ec 100644 --- a/src/data/adHocFilter.ts +++ b/src/data/adHocFilter.ts @@ -24,7 +24,12 @@ export class AdHocFilter { if (filter.key?.includes('.')) { this._targetTable = filter.key.split('.')[0]; } - if (this._targetTable === '' || !sql.match(new RegExp(`.*\\b${this._targetTable}\\b.*`, 'gi'))) { + else if (this._targetTable === '') { + this._targetTable = getTable(sql); + } + + // sql can contain a query with double quotes around the database and table name, e.g. "default"."table", so we remove those + if (this._targetTable === '' || !sql.replace(/"/g, '').match(new RegExp(`.*\\b${this._targetTable}\\b.*`, 'gi'))) { return sql; }