Skip to content

Commit 5a10b2a

Browse files
mistercrunchclaude
andcommitted
refactor(dashboard): address review feedback on dependent-filter fix
- Extract `resolveTransitiveParentIds` and `FilterConfigMap` into a new `dashboard/components/nativeFilters/dependencyGraph.ts` module so any non-hook caller can reuse the traversal and future graph helpers have a clear home alongside `hasCircularDependency`. - Type the `useSelector` call in `useTransitiveParentIds` with `RootState`. - Narrow `FilterConfigMap` to an inline `{ cascadeParentIds?: string[] }` so dividers and chart-customization entries (which may omit the field) are also assignable without using `Pick<Filter, ...>`. - Drop `any` from `state.test.ts` in favor of `DataMaskStateWithId` and `FilterConfigMap`. - Add a test asserting topological order: the closest ancestor's `time_range` override wins over further ancestors, which is the behavior `mergeExtraFormData` relies on. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 6bc6be5 commit 5a10b2a

3 files changed

Lines changed: 116 additions & 57 deletions

File tree

‎superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/state.test.ts‎

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,16 @@ import { Provider } from 'react-redux';
2121
import configureStore from 'redux-mock-store';
2222
import { createElement } from 'react';
2323
import type { ReactNode } from 'react';
24+
import type { DataMaskStateWithId } from '@superset-ui/core';
2425
import {
26+
FilterConfigMap,
2527
resolveTransitiveParentIds,
26-
useFilterDependencies,
27-
useTransitiveParentIds,
28-
} from './state';
28+
} from '../../dependencyGraph';
29+
import { useFilterDependencies, useTransitiveParentIds } from './state';
2930

3031
const mockStore = configureStore([]);
3132

32-
const buildWrapper = (filters: Record<string, any>) => {
33+
const buildWrapper = (filters: FilterConfigMap) => {
3334
const store = mockStore({ nativeFilters: { filters } });
3435
return ({ children }: { children: ReactNode }) =>
3536
createElement(Provider, { store }, children);
@@ -113,7 +114,7 @@ test('useFilterDependencies merges extraFormData across the full chain', () => {
113114
C: { cascadeParentIds: ['B'] },
114115
D: { cascadeParentIds: ['C'] },
115116
});
116-
const dataMaskSelected = {
117+
const dataMaskSelected: DataMaskStateWithId = {
117118
A: {
118119
id: 'A',
119120
extraFormData: { filters: [{ col: 'country', op: 'IN', val: ['US'] }] },
@@ -128,7 +129,7 @@ test('useFilterDependencies merges extraFormData across the full chain', () => {
128129
},
129130
};
130131
const { result } = renderHook(
131-
() => useFilterDependencies('D', dataMaskSelected as any),
132+
() => useFilterDependencies('D', dataMaskSelected),
132133
{ wrapper },
133134
);
134135
const cols = (result.current.filters ?? []).map(f => f.col);
@@ -137,6 +138,33 @@ test('useFilterDependencies merges extraFormData across the full chain', () => {
137138
expect(result.current.filters ?? []).toHaveLength(3);
138139
});
139140

141+
test('useFilterDependencies lets the closest ancestor override scalar fields', () => {
142+
// `mergeExtraFormData` appends filter arrays but overrides scalar fields
143+
// like `time_range`. Topological order must put the closest parent last so
144+
// its scalar overrides win over further ancestors.
145+
const wrapper = buildWrapper({
146+
A: { cascadeParentIds: [] },
147+
B: { cascadeParentIds: ['A'] },
148+
C: { cascadeParentIds: ['B'] },
149+
});
150+
const dataMaskSelected: DataMaskStateWithId = {
151+
A: {
152+
id: 'A',
153+
extraFormData: { time_range: 'Last year' },
154+
},
155+
B: {
156+
id: 'B',
157+
extraFormData: { time_range: 'Last month' },
158+
},
159+
};
160+
const { result } = renderHook(
161+
() => useFilterDependencies('C', dataMaskSelected),
162+
{ wrapper },
163+
);
164+
// B is C's closest ancestor with a time_range, so it should win over A.
165+
expect(result.current.time_range).toBe('Last month');
166+
});
167+
140168
test('useFilterDependencies returns empty object when parent state is missing', () => {
141169
const wrapper = buildWrapper({
142170
A: { cascadeParentIds: [] },

‎superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/state.ts‎

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,57 +18,13 @@
1818
*/
1919
import { useMemo } from 'react';
2020
import { shallowEqual, useSelector } from 'react-redux';
21-
import {
22-
DataMaskStateWithId,
23-
ensureIsArray,
24-
ExtraFormData,
25-
Filter,
26-
} from '@superset-ui/core';
21+
import { DataMaskStateWithId, ExtraFormData } from '@superset-ui/core';
22+
import { RootState } from 'src/dashboard/types';
2723
import { mergeExtraFormData } from '../../utils';
28-
29-
type FilterConfigMap = Record<string, Pick<Filter, 'cascadeParentIds'>>;
30-
31-
/**
32-
* Resolve the full set of transitive ancestor filter ids for the given filter,
33-
* in topological order (furthest ancestor first, closest parent last).
34-
*
35-
* Dependent filters only list their *direct* parents in `cascadeParentIds`, but
36-
* the child filter's options and applied extra-form-data must reflect the full
37-
* chain of upstream filters. A chain A -> B -> C -> D where each level lists
38-
* only its direct parent used to compute C's options from B alone and D's
39-
* options from C alone; this meant the child filter's generated
40-
* `extra_form_data` did not include A's clause even though A is an effective
41-
* ancestor. That mismatch caused only a subset of filter values to land on
42-
* charts until the user pressed Apply a second time.
43-
*
44-
* Cycles are silently skipped; the filter-config modal enforces acyclicity at
45-
* save time, but we still defend against malformed config so that a bad entry
46-
* can never produce an infinite loop here.
47-
*/
48-
export function resolveTransitiveParentIds(
49-
id: string,
50-
filterConfig: FilterConfigMap,
51-
): string[] {
52-
const ordered: string[] = [];
53-
const visited = new Set<string>([id]);
54-
55-
const visit = (currentId: string) => {
56-
const parents = ensureIsArray(
57-
filterConfig[currentId]?.cascadeParentIds,
58-
) as string[];
59-
parents.forEach(parentId => {
60-
if (visited.has(parentId)) return;
61-
visited.add(parentId);
62-
// Depth-first so the furthest ancestors are appended before the
63-
// closer parents, giving a stable topological order.
64-
visit(parentId);
65-
ordered.push(parentId);
66-
});
67-
};
68-
69-
visit(id);
70-
return ordered;
71-
}
24+
import {
25+
FilterConfigMap,
26+
resolveTransitiveParentIds,
27+
} from '../../dependencyGraph';
7228

7329
/**
7430
* Resolve the transitive ancestor ids for a given filter from the live
@@ -77,7 +33,7 @@ export function resolveTransitiveParentIds(
7733
* always agree on which parents count.
7834
*/
7935
export function useTransitiveParentIds(id: string): string[] {
80-
const filterConfig = useSelector<any, FilterConfigMap | undefined>(
36+
const filterConfig = useSelector<RootState, FilterConfigMap | undefined>(
8137
state => state.nativeFilters?.filters,
8238
shallowEqual,
8339
);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
import { ensureIsArray } from '@superset-ui/core';
20+
21+
/**
22+
* Shared graph primitives for the native-filter dependency graph.
23+
*
24+
* A dependent filter stores only its *direct* parents in `cascadeParentIds`,
25+
* but most consumers (options queries, `extra_form_data` emitted to charts,
26+
* readiness checks) need the full transitive ancestor set. Keeping the graph
27+
* primitives here lets the hook layer and any non-React callers (e.g. form
28+
* validation, batch checks) share one traversal implementation.
29+
*/
30+
31+
/**
32+
* Minimal shape we need from the native-filter config map. Kept intentionally
33+
* narrow so dividers / chart customizations (which may omit `cascadeParentIds`)
34+
* are also assignable.
35+
*/
36+
export type FilterConfigMap = Record<string, { cascadeParentIds?: string[] }>;
37+
38+
/**
39+
* Resolve the full set of transitive ancestor filter ids for the given filter,
40+
* in topological order (furthest ancestor first, closest parent last).
41+
*
42+
* The ordering is significant: `mergeExtraFormData` appends filter arrays but
43+
* *overrides* scalar fields like `time_range`, so iterating ancestors from
44+
* furthest to closest makes the closest parent's scalar overrides win. A chain
45+
* A -> B -> C -> D where each level lists only its direct parent therefore
46+
* produces `[A, B, C]` for D, which matches the intended precedence.
47+
*
48+
* Cycles are silently skipped. The filter-config modal enforces acyclicity at
49+
* save time (see `hasCircularDependency`), but defensive traversal here keeps
50+
* a malformed saved config from spinning forever.
51+
*/
52+
export function resolveTransitiveParentIds(
53+
id: string,
54+
filterConfig: FilterConfigMap,
55+
): string[] {
56+
const ordered: string[] = [];
57+
const visited = new Set<string>([id]);
58+
59+
const visit = (currentId: string) => {
60+
const parents = ensureIsArray(
61+
filterConfig[currentId]?.cascadeParentIds,
62+
) as string[];
63+
parents.forEach(parentId => {
64+
if (visited.has(parentId)) return;
65+
visited.add(parentId);
66+
// Depth-first so the furthest ancestors are appended before the
67+
// closer parents, giving a stable topological order.
68+
visit(parentId);
69+
ordered.push(parentId);
70+
});
71+
};
72+
73+
visit(id);
74+
return ordered;
75+
}

0 commit comments

Comments
 (0)