Skip to content

Commit f58eb5d

Browse files
fix(ag-grid-table): skip totals query for CSV/Excel downloads (#46246)
Co-authored-by: Joe Li <joe@preset.io>
1 parent 530dfdc commit f58eb5d

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎superset-frontend/plugins/plugin-chart-ag-grid-table/src/buildQuery.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ export const buildQueryUncached: BuildQuery<TableChartFormData> = (
242242
['csv', 'xlsx'].includes(formData?.result_format || '') ||
243243
(formData?.result_format === 'json' &&
244244
formData?.result_type === 'results');
245+
const isFileExport = ['csv', 'xlsx'].includes(
246+
formData?.result_format || '',
247+
);
245248

246249
if (isDownloadQuery) {
247250
moreProps.row_limit = Number(formDataCopy.row_limit) || 0;
@@ -747,7 +750,10 @@ export const buildQueryUncached: BuildQuery<TableChartFormData> = (
747750
? getTotalsMetrics(metrics ?? [], totalsAggregate)
748751
: undefined;
749752

750-
if (showAggregateTotals || rawSummaryColumns.length > 0) {
753+
if (
754+
!isFileExport &&
755+
(showAggregateTotals || rawSummaryColumns.length > 0)
756+
) {
751757
// Start from the original, pre-filter extras (captured before any AG Grid
752758
// complexWhere/havingClause fragments were merged in above) rather than
753759
// trying to subtract those fragments back out of the now-combined

‎superset-frontend/plugins/plugin-chart-ag-grid-table/test/buildQuery.test.ts‎

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,45 @@ const basicFormData: TableChartFormData = {
3333
metrics: ['count'],
3434
};
3535

36+
test.each([
37+
['json', 2],
38+
['csv', 1],
39+
['xlsx', 1],
40+
])(
41+
'show_totals with result_format %s builds %i query(ies)',
42+
(resultFormat, expectedQueries) => {
43+
const { queries } = buildQueryUncached({
44+
...basicFormData,
45+
show_totals: true,
46+
result_format: resultFormat,
47+
} as TableChartFormData);
48+
49+
expect(queries).toHaveLength(expectedQueries);
50+
},
51+
);
52+
53+
test.each([
54+
['json', 2],
55+
['csv', 1],
56+
['xlsx', 1],
57+
])(
58+
'raw summary with result_format %s builds %i query(ies)',
59+
(resultFormat, expectedQueries) => {
60+
const { queries } = buildQueryUncached(
61+
{
62+
...basicFormData,
63+
query_mode: QueryMode.Raw,
64+
all_columns: ['state', 'population'],
65+
show_totals: true,
66+
result_format: resultFormat,
67+
} as TableChartFormData,
68+
{ ownState: { rawSummaryColumns: ['population'] } },
69+
);
70+
71+
expect(queries).toHaveLength(expectedQueries);
72+
},
73+
);
74+
3675
test('preserves saved semantic pagination offsets and ordering', () => {
3776
const saved: TableChartFormData = Object.freeze({
3877
...basicFormData,
@@ -890,7 +929,9 @@ describe('plugin-chart-ag-grid-table', () => {
890929
...basicFormData,
891930
show_totals: true,
892931
query_mode: QueryMode.Aggregate,
893-
result_format: 'csv',
932+
result_format: 'json',
933+
result_type: 'results',
934+
server_pagination: true,
894935
},
895936
{
896937
ownState: {
@@ -906,7 +947,6 @@ describe('plugin-chart-ag-grid-table', () => {
906947
);
907948

908949
const mainQuery = queries[0];
909-
// Downloads never get a rowcount query, so totals is queries[1].
910950
const totalsQuery = queries[1];
911951

912952
expect(mainQuery.extras?.having).toBe('count > 10');
@@ -1028,7 +1068,9 @@ describe('plugin-chart-ag-grid-table', () => {
10281068
...basicFormData,
10291069
show_totals: true,
10301070
query_mode: QueryMode.Aggregate,
1031-
result_format: 'csv',
1071+
result_format: 'json',
1072+
result_type: 'results',
1073+
server_pagination: true,
10321074
},
10331075
{
10341076
ownState: {
@@ -1040,7 +1082,6 @@ describe('plugin-chart-ag-grid-table', () => {
10401082
);
10411083

10421084
const mainQuery = queries[0];
1043-
// Downloads never get a rowcount query, so totals is queries[1].
10441085
const totalsQuery = queries[1];
10451086
const stateFilter = { col: 'state', op: '==', val: 'CA' };
10461087

@@ -2227,7 +2268,7 @@ test.each(['json', 'csv'])(
22272268
};
22282269
const original = structuredClone(form);
22292270
const { queries } = buildQueryUncached(form);
2230-
expect(queries.length).toBeGreaterThan(1);
2271+
expect(queries).toHaveLength(result_format === 'csv' ? 1 : 3);
22312272
queries.forEach(query => {
22322273
expect(query.extras).not.toHaveProperty('time_grain_sqla');
22332274
});

0 commit comments

Comments
 (0)