diff --git a/superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts b/superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts index 986b74ae1331..2749071368b8 100644 --- a/superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts +++ b/superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts @@ -28,6 +28,7 @@ import { QueryFormOrderBy, QueryMode, QueryObject, + QueryObjectExtras, removeDuplicates, PostProcessingRule, BuildQuery, @@ -322,7 +323,9 @@ const buildQuery: BuildQuery = ( }; orderedColumns = reorderByColumnOrder(columns) as typeof columns; - orderedMetrics = reorderByColumnOrder(metrics || []) as typeof metrics; + orderedMetrics = metrics + ? (reorderByColumnOrder(metrics) as typeof metrics) + : metrics; } let queryObject = { @@ -473,7 +476,7 @@ const buildQuery: BuildQuery = ( ? `${queryObject.extras.having} AND ${havingClause}` : havingClause, }), - }; + } as QueryObjectExtras; } } } diff --git a/superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts b/superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts index 382bcc36474a..12647e99a5b0 100644 --- a/superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts +++ b/superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts @@ -475,4 +475,28 @@ describe('plugin-chart-ag-grid-table', () => { expect(query.columns).toEqual(['state', 'city']); }); }); + + describe('buildQuery - metrics handling in different query modes', () => { + test('should not include metrics in raw records mode', () => { + const query = buildQuery({ + viz_type: VizType.Table, + datasource: '11__table', + query_mode: QueryMode.Raw, + all_columns: ['state', 'city'], + }).queries[0]; + + expect(query.metrics).toBeUndefined(); + }); + + test('should set metrics to empty array in aggregate mode when no metrics specified', () => { + const query = buildQuery({ + viz_type: VizType.Table, + datasource: '11__table', + query_mode: QueryMode.Aggregate, + groupby: ['state'], + }).queries[0]; + + expect(query.metrics).toEqual([]); + }); + }); });