diff --git a/public/app/features/query/state/runRequest.test.ts b/public/app/features/query/state/runRequest.test.ts index 6eb5095e8dc..bf789c25f62 100644 --- a/public/app/features/query/state/runRequest.test.ts +++ b/public/app/features/query/state/runRequest.test.ts @@ -12,6 +12,7 @@ import { PanelData, } from '@grafana/data'; import { setEchoSrv } from '@grafana/runtime'; +import { ExpressionDatasourceRef } from '@grafana/runtime/src/utils/DataSourceWithBackend'; import { DataQuery } from '@grafana/schema'; import { deepFreeze } from '../../../../test/core/redux/reducerTester'; @@ -35,6 +36,12 @@ jest.mock('app/features/dashboard/services/DashboardSrv', () => ({ }, })); +jest.mock('app/features/expressions/ExpressionDatasource', () => ({ + dataSource: { + query: jest.fn(), + }, +})); + class ScenarioCtx { ds!: DataSourceApi; request!: DataQueryRequest; @@ -532,6 +539,28 @@ describe('callQueryMethod', () => { ); }); + it('Should not call filterQuery when targets include expression query', async () => { + setup({ + targets: [ + { + refId: 'A', + q: 'SUM(foo)', + }, + { + refId: 'B', + q: 'SUM(foo2)', + }, + { + datasource: ExpressionDatasourceRef, + refId: 'C', + q: 'SUM(foo3)', + }, + ], + filterQuery: (query: DataQuery) => query.refId !== 'A', + }); + expect(filterQuerySpy).not.toHaveBeenCalled(); + }); + it('Should get ds default query when query is empty', async () => { setup({ targets: [ diff --git a/public/app/features/query/state/runRequest.ts b/public/app/features/query/state/runRequest.ts index b7f9d8589b4..7a4e6e2fdf7 100644 --- a/public/app/features/query/state/runRequest.ts +++ b/public/app/features/query/state/runRequest.ts @@ -201,15 +201,6 @@ export function callQueryMethod( : t ); - // do not filter queries in case a custom query function is provided (for example in variable queries) - if (!queryFunction) { - request.targets = request.targets.filter((t) => datasource.filterQuery?.(t) ?? true); - } - - if (request.targets.length === 0) { - return of({ data: [] }); - } - // If its a public datasource, just return the result. Expressions will be handled on the backend. if (config.publicDashboardAccessToken) { return from(datasource.query(request)); @@ -221,6 +212,15 @@ export function callQueryMethod( } } + // do not filter queries in case a custom query function is provided (for example in variable queries) + if (!queryFunction) { + request.targets = request.targets.filter((t) => datasource.filterQuery?.(t) ?? true); + } + + if (request.targets.length === 0) { + return of({ data: [] }); + } + // Otherwise it is a standard datasource request const returnVal = queryFunction ? queryFunction(request) : datasource.query(request); return from(returnVal);