Skip to content

Commit f357ec3

Browse files
sadpandajoeclaude
andauthored
fix(sqllab): sort schema browser tables, views and schemas case-insensitively (#46325)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent d9f4f48 commit f357ec3

4 files changed

Lines changed: 70 additions & 6 deletions

File tree

‎superset-frontend/src/hooks/apiResources/schemas.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,4 +371,25 @@ describe('useSchemas hook', () => {
371371
expect(result.current.data).toEqual(expectedResult3);
372372
expect(fetchMock.callHistory.calls(schemaApiRoute).length).toBe(2);
373373
});
374+
375+
test('sorts schemas case-insensitively', async () => {
376+
const schemaApiRoute = `glob:*/api/v1/database/db1/schemas/*`;
377+
fetchMock.get(schemaApiRoute, {
378+
result: ['RAW', 'Staging', 'analytics', 'FOO', 'foo'],
379+
});
380+
const { result } = renderHook(() => useSchemas({ dbId: 'db1' }), {
381+
wrapper: createWrapper({
382+
useRedux: true,
383+
store,
384+
}),
385+
});
386+
await waitFor(() => expect(result.current.data).toBeDefined());
387+
expect(result.current.data?.map(({ value }) => value)).toEqual([
388+
'analytics',
389+
'foo',
390+
'FOO',
391+
'RAW',
392+
'Staging',
393+
]);
394+
});
374395
});

‎superset-frontend/src/hooks/apiResources/schemas.ts‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
import { useCallback, useEffect, useRef } from 'react';
2020
import { ClientErrorObject } from '@superset-ui/core';
21+
import { DEFAULT_SORT_COMPARATOR } from '@superset-ui/core/components';
2122
import useEffectEvent from 'src/hooks/useEffectEvent';
2223
import { api, JsonResponse } from './queryApi';
2324

@@ -49,11 +50,13 @@ const schemaApi = api.injectEndpoints({
4950
...(catalog !== undefined && { catalog }),
5051
},
5152
transformResponse: ({ json }: JsonResponse) =>
52-
json.result.sort().map((value: string) => ({
53-
value,
54-
label: value,
55-
title: value,
56-
})),
53+
json.result
54+
.map((value: string) => ({
55+
value,
56+
label: value,
57+
title: value,
58+
}))
59+
.sort(DEFAULT_SORT_COMPARATOR),
5760
}),
5861
serializeQueryArgs: ({ queryArgs: { dbId, catalog } }) => ({
5962
dbId,

‎superset/commands/database/tables.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def run(self) -> dict[str, Any]:
158158
}
159159
for mv in materialized_views
160160
],
161-
key=lambda item: item["value"],
161+
key=lambda item: (item["value"].casefold(), item["value"].swapcase()),
162162
)
163163

164164
payload = {

‎tests/unit_tests/commands/databases/tables_test.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,46 @@ def test_tables_without_catalog(
322322
)
323323

324324

325+
def test_tables_sorted_case_insensitively(
326+
mocker: MockerFixture,
327+
database_without_catalog: MagicMock,
328+
) -> None:
329+
"""
330+
Test that tables and views are sorted case-insensitively, with names that
331+
differ only by case listed lowercase first.
332+
"""
333+
mocker.patch.object(
334+
security_manager,
335+
"get_datasources_accessible_by_user",
336+
side_effect=[
337+
{
338+
DatasourceName("bar", "schema1"),
339+
DatasourceName("FOO", "schema1"),
340+
DatasourceName("Baz", "schema1"),
341+
DatasourceName("apple", "schema1"),
342+
},
343+
{
344+
DatasourceName("foo", "schema1"),
345+
DatasourceName("Foo", "schema1"),
346+
},
347+
set(), # Empty set for materialized views
348+
],
349+
)
350+
351+
db = mocker.patch("superset.commands.database.tables.db")
352+
db.session.query().filter().options().all.return_value = []
353+
354+
payload = TablesDatabaseCommand(1, None, "schema1", False).run()
355+
assert [item["value"] for item in payload["result"]] == [
356+
"apple",
357+
"bar",
358+
"Baz",
359+
"foo",
360+
"Foo",
361+
"FOO",
362+
]
363+
364+
325365
def test_tables_unknown_schema(
326366
mocker: MockerFixture,
327367
database_without_catalog: MagicMock,

0 commit comments

Comments
 (0)