Skip to content

fix(dashboard): remap native filter scopes when duplicating charts on save as - #45038

Merged
rusackas merged 1 commit into
apache:masterfrom
FrancescoCastaldi:fix/issue-44983-native-filters-scope-remapping
Oct 20, 2026
Merged

rusackas merged 1 commit into
apache:masterfrom
FrancescoCastaldi:fix/issue-44983-native-filters-scope-remapping

Conversation

@FrancescoCastaldi

Copy link
Copy Markdown
Contributor

SUMMARY

Fixes #44983.

When saving a dashboard as a copy with duplicated charts (duplicate_slices=True), chartsInScope and scope.excluded within native filters metadata continued to reference the old chart slice IDs, breaking filter scope bindings on the new dashboard.

This change:

  1. Implements _remap_filter_scopes and _remap_filter_scope in DashboardDAO (superset/daos/dashboard.py).
  2. Remaps chart IDs in chartsInScope and scope.excluded across native_filter_configuration, global_chart_configuration, and chart_configuration using old_to_new_slice_ids.
  3. Handles dividers and non-dictionary elements gracefully without raising errors.

TESTING INSTRUCTIONS

Run the dashboard DAO unit tests:
pytest tests/unit_tests/dao/dashboard_test.py

All tests pass, confirming correct slice ID remapping for single and multi-filter dashboards.

ADDITIONAL INFORMATION

@netlify

netlify Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit e7f230b
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6ad00ec7ffb1970b392a097a
😎 Deploy Preview https://deploy-preview-45038--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Comment thread superset/daos/dashboard.py Outdated
Comment on lines +568 to +577
scope = container.get("scope")
if isinstance(scope, dict) and isinstance(scope.get("excluded"), list):
remapped_excluded: list[int] = []
for cid in scope["excluded"]:
try:
int_id = int(cid)
remapped_excluded.append(old_to_new_slice_ids.get(int_id, int_id))
except (ValueError, TypeError):
remapped_excluded.append(cid)
scope["excluded"] = remapped_excluded

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: scope.selectedLayers encodes chart IDs in its keys, but this only remaps excluded. After duplication, layer-scoped filters still target the original chart IDs.

Assessment: 🟠 Major · 🔁 Occurrence: Sometimes · 🏷️ Incomplete implementation

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/daos/dashboard.py
**Line:** 568:577
**Comment:**
	*Incomplete Implementation: `scope.selectedLayers` encodes chart IDs in its keys, but this only remaps `excluded`. After duplication, layer-scoped filters still target the original chart IDs.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed—a filter scoped to a single layer keeps selectedLayers entries like chart-<old_id>-layer-0, so on the copied dashboard the layer scope is looked up under the original chart ID and the cloned chart gets no layer-specific scope, so the filter can hit every layer of the copy. Should the chart-ID part of each selectedLayers entry be remapped (keeping the layer index) alongside excluded and chartsInScope?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If scope.selectedLayers uses keys such as chart-<old_id>-layer-0, those keys need the same chart-ID remapping; otherwise the copied filter’s layer selection remains associated with the original chart. Remap only the chart-ID portion and preserve the layer suffix, leaving unrecognized or unmapped entries unchanged. Add a test asserting that chart-<old_id>-layer-0 becomes chart-<new_id>-layer-0. This complements the existing excluded and chartsInScope remapping.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of d7e799a.

Added _remap_selected_layers, which extracts chart IDs from selectedLayers keys, maps them to the duplicated chart IDs, and preserves each key’s suffix.

If that's not right, unresolve this thread and CodeAnt will leave it open.

Comment thread superset/daos/dashboard.py Outdated
Comment on lines +612 to +614
if isinstance(metadata.get("native_filter_configuration"), list):
for native_filter in metadata["native_filter_configuration"]:
cls._remap_filter_scope(native_filter, old_to_new_slice_ids)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: chart_customization_config also stores scope references, but this remaps only native filters. Customization exclusions and cached targets can therefore still point to original charts after duplication.

Assessment: 🟠 Major · 🔁 Occurrence: Sometimes · 🏷️ Incomplete implementation

Use CodeAnt Skill Fix in Cursor Fix in VSCode Claude

Prompt for AI Agent 🤖
This is a comment left during a code review.

**Path:** superset/daos/dashboard.py
**Line:** 612:614
**Comment:**
	*Incomplete Implementation: `chart_customization_config` also stores scope references, but this remaps only native filters. Customization exclusions and cached targets can therefore still point to original charts after duplication.

Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise.
Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix
👍 | 👎

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed—chart_customization_config uses the same scope.excluded/chartsInScope shape and is skipped by _remap_filter_scopes. A Dynamic Group By control that excludes chart B still lists B's original ID after Save As, so scope recomputation includes the copied B and the control changes it unexpectedly. Should the customization entries go through _remap_filter_scope as well?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Since chart_customization_config entries use the same scope.excluded and chartsInScope fields, they should also be passed through _remap_filter_scope during duplication. Otherwise, those references remain pointed at the original charts, and scope recomputation can behave differently on the copied dashboard. Handle the configuration’s entries defensively, as with native filters, and add a test for a customization that excludes a duplicated chart. The omission is a valid gap in the current remapping.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved as of d7e799a.

The metadata traversal now applies _remap_filter_scope to entries in both native_filter_configuration and chart_customization_config, remapping their exclusions, selected layers, and chart-scope references.

If that's not right, unresolve this thread and CodeAnt will leave it open.

@codeant-ai-for-open-source

codeant-ai-for-open-source Bot commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

CodeAnt PR Risk: Low Risk

  • The PR appears safe to merge; duplicated chart IDs are remapped across native filters, chart customizations, cross-filters, and selected layers.
  • The added tests cover divider preservation, legacy chart IDs, overlapping ID mappings, and the affected metadata shapes.

Assessed commit: e7f230bbe956

@codecov

codecov Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.25000% with 15 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.36%. Comparing base (367b6f5) to head (e7f230b).

Files with missing lines Patch % Lines
superset/daos/dashboard.py 81.25% 9 Missing and 6 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #45038       +/-   ##
===========================================
+ Coverage   56.16%   83.36%   +27.20%     
===========================================
  Files        3040     3040               
  Lines      198655   198734       +79     
  Branches    46360    46383       +23     
===========================================
+ Hits       111565   165681    +54116     
+ Misses      86024    29823    -56201     
- Partials     1066     3230     +2164     
Flag Coverage Δ
hive 34.96% <16.25%> (-0.02%) ⬇️
mysql 52.97% <20.00%> (?)
postgres 52.97% <20.00%> (?)
presto 36.73% <16.25%> (-0.02%) ⬇️
python 86.92% <81.25%> (+50.13%) ⬆️
sqlite 52.73% <20.00%> (?)
unit 80.92% <81.25%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@FrancescoCastaldi

Copy link
Copy Markdown
Contributor Author

Hi @sadpandajoe,

Addressed both scope remapping gaps in commits d7e799a54f and 2813150228:

  1. scope.selectedLayers remapping:
    Updated _remap_filter_scope in superset/daos/dashboard.py to inspect scope.selectedLayers. Handled both standard frontend list format (string[]) and dictionary configurations. Keys matching chart-<old_id>-layer-<idx> have their chart ID segment remapped using old_to_new_slice_ids while preserving the layer suffix. Unmapped or unrecognized keys remain untouched.

  2. chart_customization_config remapping:
    In _remap_filter_scopes, metadata["chart_customization_config"] entries are now passed through _remap_filter_scope alongside native filter configurations and global cross-filter scopes, ensuring chart exclusions and target references point to the duplicated chart copies. Added divider detection for CHART_CUSTOMIZATION_DIVIDER.

  3. Single-pass remapping:
    Consolidated scope remapping strictly through set_dash_metadata to prevent double remapping on overlapping chart IDs.

Added regression unit tests in tests/unit_tests/dao/dashboard_test.py covering selectedLayers list and dict remapping, helper edge cases, and chart_customization_config scope remapping during dashboard duplication. All 19 unit tests and Astral Ruff pass cleanly.

]


def test_copy_dashboard_does_not_double_remap_overlapping_ids(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is named and commented as covering overlapping IDs, but the clones get fresh primary keys while both originals still exist, so no new ID ever equals an old mapping key. A regression that remapped twice would still pass these assertions. Could you drive a mapping like {1: 2, 2: 3} through set_dash_metadata (with chartsInScope=[1, 2], excluded=[1], selectedLayers=["chart-1-layer-0"]) and assert [2, 3], [2] and "chart-2-layer-0"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in commit 0786da887f:
test_set_dash_metadata_does_not_double_remap_overlapping_ids drives {1: 2, 2: 3} through set_dash_metadata with chartsInScope=[1, 2], excluded=[1], and selectedLayers=["chart-1-layer-0"]. It asserts that the saved metadata produces [2, 3], [2], and ["chart-2-layer-0"], verifying that single-pass atomic remapping is preserved and no chained remapping (1 -> 2 -> 3) occurs.

@FrancescoCastaldi

Copy link
Copy Markdown
Contributor Author

Hi @sadpandajoe @rusackas,

Pushed commit 0786da887f adding test_set_dash_metadata_does_not_double_remap_overlapping_ids in tests/unit_tests/dao/dashboard_test.py.

The test feeds overlapping mapping {1: 2, 2: 3} through DashboardDAO.set_dash_metadata with chartsInScope=[1, 2], excluded=[1], and selectedLayers=["chart-1-layer-0"]. It asserts [2, 3], [2], and ["chart-2-layer-0"], verifying that single-pass remapping holds and preventing chained remapping (1 -> 2 -> 3).

All 20 unit tests in tests/unit_tests/dao/dashboard_test.py pass cleanly. Ready for final review and merge.

@coderabbitai review
@bito-code-review

for key in ("native_filter_configuration", "chart_customization_config"):
if isinstance(metadata.get(key), list):
for item in metadata[key]:
cls._remap_filter_scope(item, old_to_new_slice_ids)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy chart_customization_config entries carry their target in a top-level chartId (no type key), and migrateChartCustomization rebuilds chartsInScope from it on load. Nothing here remaps that field, so after Save As with duplicated charts a legacy {chartId: 10, customization: {...}} keeps pointing at the original chart 10 even though the copy only contains the new chart. The control then won't show for the copied chart. Should _remap_filter_scope also map chartId for these items?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch @sadpandajoe! Updated DashboardDAO._remap_filter_scope in commit d07e3bbbed so legacy chart_customization_config entries with a top-level chartId (with a not isinstance(..., bool) guard) are remapped through old_to_new_slice_ids alongside scope and chartsInScope. Also added legacy chartId coverage in test_issue_44983_consolidated_evidence_gate and test_set_dash_metadata_does_not_double_remap_overlapping_ids.

f"chart-{copied_c1_id}-layer-0"
]

def test_set_dash_metadata_does_not_double_remap_overlapping_ids(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one blank line separates this def from the previous test, and the file ends with an extra blank line. The pre-commit job fails on ruff-format and fix end of files for this PR. Could you run pre-commit run --files superset/daos/dashboard.py tests/unit_tests/dao/dashboard_test.py and push the result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit d07e3bbbed. Restored the two-blank-line separation before test_set_dash_metadata_does_not_double_remap_overlapping_ids, normalized the trailing newline, and verified ruff check and ruff format pass cleanly on both files.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pre-commit (current) job is still red on this head: auto-walrus rewrites the re.match assignment and if in _remap_layer_key, and ruff-format reformats both files (the _remap_selected_layers list comprehension in superset/daos/dashboard.py and the end of the new overlap test here), so the check keeps failing. Could you run pre-commit run --files superset/daos/dashboard.py tests/unit_tests/dao/dashboard_test.py and push the result?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Applied auto-walrus syntax in _remap_layer_key and formatted both superset/daos/dashboard.py and tests/unit_tests/dao/dashboard_test.py with ruff-format in commit efe8edf.

@FrancescoCastaldi

Copy link
Copy Markdown
Contributor Author

Thanks for the follow-up review @sadpandajoe! I have pushed commit d07e3bbbed addressing both comments:

  1. Legacy chart_customization_config top-level chartId remapping: DashboardDAO._remap_filter_scope now remaps top-level chartId entries (guarded with not isinstance(..., bool)) alongside scope and chartsInScope, and unit tests now cover legacy chart_customization_config entries in both copy_dashboard and set_dash_metadata.
  2. Pre-commit / Ruff formatting: Restored the two-blank-line separation before test_set_dash_metadata_does_not_double_remap_overlapping_ids, normalized the trailing newline, and verified ruff check and ruff format pass cleanly on both touched files.

@github-actions github-actions Bot added the requires:rebase Requires rebasing on top of current master label Oct 13, 2026
… save as (apache#44983)

When saving a dashboard as a copy with duplicated charts (duplicate_slices=True), chartsInScope and scope.excluded within native filters metadata continued to reference the old chart slice IDs, breaking filter scope bindings on the new dashboard.

This change:

1. Implements _remap_filter_scopes, _remap_filter_scope, _remap_chart_configuration, _remap_selected_layers, and _remap_slice_id_list in DashboardDAO (superset/daos/dashboard.py).

2. Remaps chart IDs in chartsInScope, scope.excluded, selectedLayers, and legacy chartId across native_filter_configuration, global_chart_configuration, and chart_configuration using old_to_new_slice_ids.

3. Handles dividers and non-dictionary elements gracefully without raising errors.

4. Adds comprehensive unit test suites in tests/unit_tests/dao/dashboard_test.py.

Fixes apache#44983
@FrancescoCastaldi
FrancescoCastaldi force-pushed the fix/issue-44983-native-filters-scope-remapping branch from efe8edf to e7f230b Compare October 14, 2026 23:22
@FrancescoCastaldi

Copy link
Copy Markdown
Contributor Author

Rebase & Verification Update

The PR branch has been rebased cleanly onto latest master (upstream/master), reconciling with recent changes in superset/daos/dashboard.py and tests/unit_tests/dao/dashboard_test.py:

  • Mergeable Status: MERGEABLE (zero conflicts).
  • Evidence Gate:
    • pytest tests/unit_tests/dao/dashboard_test.py: 62 passed in 4.23s (100% PASS).
    • ruff check: All checks passed.
    • ruff format: Formatted cleanly to 88 columns.

The PR is up to date and ready for review.

@github-actions github-actions Bot removed the requires:rebase Requires rebasing on top of current master label Oct 15, 2026

@sadpandajoe sadpandajoe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rusackas
rusackas merged commit f0b43df into apache:master Oct 20, 2026
72 checks passed
villebro pushed a commit that referenced this pull request Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native filters break after "Save as" with duplicated charts (chartsInScope not remapped in copy_dashboard)

3 participants