From b71700ec86e75acd1033ec08ae141e7d7f9b46f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Mon, 1 Dec 2025 11:42:50 -0300 Subject: [PATCH 1/3] Add sorting --- .../src/SqlLab/components/QueryHistory/index.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/superset-frontend/src/SqlLab/components/QueryHistory/index.tsx b/superset-frontend/src/SqlLab/components/QueryHistory/index.tsx index 9e1c46c95599..ddc457c0af8f 100644 --- a/superset-frontend/src/SqlLab/components/QueryHistory/index.tsx +++ b/superset-frontend/src/SqlLab/components/QueryHistory/index.tsx @@ -91,7 +91,11 @@ const QueryHistory = ({ editorId, ) .concat(data.result) - .reverse() + .sort((a, b) => { + const aTime = a.startDttm || 0; + const bTime = b.startDttm || 0; + return aTime - bTime; + }) : getEditorQueries(queries, editorId), [queries, data, editorId], ); From 56a6936f3c6135a85993ee95ed68bbd33db7ddfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Mon, 1 Dec 2025 18:30:38 -0300 Subject: [PATCH 2/3] Add tests --- .../QueryHistory/QueryHistory.test.tsx | 109 ++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx b/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx index a63f0cc7d995..20d5fb7da81f 100644 --- a/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx +++ b/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx @@ -133,3 +133,112 @@ test('fetches the query history by the tabViewId', async () => { expect(queryResultText).toBeInTheDocument(); isFeatureEnabledMock.mockClear(); }); + +test('displays multiple queries with newest query first', async () => { + const isFeatureEnabledMock = mockedIsFeatureEnabled.mockImplementation( + featureFlag => featureFlag === FeatureFlag.SqllabBackendPersistence, + ); + + const multipleQueriesApiResult = { + count: 2, + ids: [694, 693], + result: [ + { + changed_on: '2024-03-12T20:10:02.497775', + client_id: 'd2ZDzRYzn', + database: { + database_name: 'examples', + id: 1, + }, + end_time: '1710274202496.047852', + error_message: null, + executed_sql: 'SELECT COUNT(*) from "FCC 2018 Survey"\nLIMIT 1001', + id: 694, + limit: 1000, + limiting_factor: 'DROPDOWN', + progress: 100, + results_key: null, + rows: 1, + schema: 'main', + select_as_cta: false, + sql: 'SELECT COUNT(*) from "FCC 2018 Survey"', + sql_editor_id: '22', + start_time: '1710274202445.992920', + status: QueryState.Success, + tab_name: 'Untitled Query 2', + tmp_table_name: null, + tracking_url: null, + user: { + first_name: 'admin', + id: 1, + last_name: 'user', + }, + }, + { + changed_on: '2024-03-12T20:01:02.497775', + client_id: 'b0ZDzRYzn', + database: { + database_name: 'examples', + id: 1, + }, + end_time: '1710273662496.047852', + error_message: null, + executed_sql: 'SELECT * from "FCC 2018 Survey"\nLIMIT 1001', + id: 693, + limit: 1000, + limiting_factor: 'DROPDOWN', + progress: 100, + results_key: null, + rows: 443, + schema: 'main', + select_as_cta: false, + sql: 'SELECT * from "FCC 2018 Survey"', + sql_editor_id: '22', + start_time: '1710273662445.992920', + status: QueryState.Success, + tab_name: 'Untitled Query 1', + tmp_table_name: null, + tracking_url: null, + user: { + first_name: 'admin', + id: 1, + last_name: 'user', + }, + }, + ], + }; + + const editorQueryApiRoute = `glob:*/api/v1/query/?q=*`; + fetchMock.get(editorQueryApiRoute, multipleQueriesApiResult); + const { container } = render(setup(), { useRedux: true, initialState }); + + await waitFor(() => + expect(fetchMock.calls(editorQueryApiRoute).length).toBe(1), + ); + + expect(screen.getByTestId('listview-table')).toBeVisible(); + expect(screen.getByRole('table')).toBeVisible(); + + const tableRows = container.querySelectorAll( + 'table > tbody > tr:not(.ant-table-measure-row)', + ); + expect(tableRows).toHaveLength(2); + + // Check that both queries are present + const olderQueryRow = screen.getByText('443'); + const newerQueryElements = screen.getAllByText('1'); + expect(olderQueryRow).toBeInTheDocument(); + expect(newerQueryElements.length).toBeGreaterThan(0); + + // Verify ordering: newer query (1 row) should appear before older query (443 rows) + // Find the actual row elements to check their order + const firstDataRow = tableRows[0]; + const secondDataRow = tableRows[1]; + + // The newer query should be in the first row (has 1 result row) + expect(firstDataRow).toHaveTextContent('1'); + // The older query should be in the second row (has 443 result rows) + expect(secondDataRow).toHaveTextContent('443'); + + isFeatureEnabledMock.mockClear(); +}); From e1058b5acab04dc98a0773030dc0fe81d3470ae1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20L=C3=B3pez?= Date: Wed, 3 Dec 2025 11:19:49 -0300 Subject: [PATCH 3/3] Fix linting issues --- .../src/SqlLab/components/QueryHistory/QueryHistory.test.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx b/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx index 20d5fb7da81f..52edf19aaf74 100644 --- a/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx +++ b/superset-frontend/src/SqlLab/components/QueryHistory/QueryHistory.test.tsx @@ -211,7 +211,7 @@ test('displays multiple queries with newest query first', async () => { const editorQueryApiRoute = `glob:*/api/v1/query/?q=*`; fetchMock.get(editorQueryApiRoute, multipleQueriesApiResult); const { container } = render(setup(), { useRedux: true, initialState }); - + await waitFor(() => expect(fetchMock.calls(editorQueryApiRoute).length).toBe(1), );