Skip to content

Commit

Permalink
Fix global ad hoc filters.
Browse files Browse the repository at this point in the history
  • Loading branch information
cwurm committed Jan 8, 2024
1 parent 9a5fc90 commit 53a5ca8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/data/CHDatasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
10 changes: 10 additions & 0 deletions src/data/adHocFilter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\\' '}`
);
});
});
7 changes: 6 additions & 1 deletion src/data/adHocFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 53a5ca8

Please sign in to comment.