Skip to content

fix(dashboard): don't clone charts a copied layout leaves out - #45528

Merged
aminghadersohi merged 5 commits into
apache:masterfrom
mikebridge:sc-128643-copy-orphan-clones
Oct 27, 2026
Merged

aminghadersohi merged 5 commits into
apache:masterfrom
mikebridge:sc-128643-copy-orphan-clones

Conversation

@mikebridge

Copy link
Copy Markdown
Member

SUMMARY

What users saw. Copying a dashboard with “duplicate charts” could create unused charts when the copied layout left out charts from the source dashboard, including when opening an older version as a new dashboard. With history capture enabled, the copy could also fail.

Why, in plain terms. The copy created every source chart first, then removed charts that the copied layout did not use. Those charts had already been saved.

What this changes. Only charts present in the copied layout are duplicated. Existing handling of archived or deleted charts and legacy chart IDs is preserved.

The clone loop uses the reconciled layout's normalized chart IDs before creating any chart rows. Regression tests assert total chart counts and orphan counts for subset and empty layouts.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

Not applicable: this corrects backend copy behavior without a UI change.

TESTING INSTRUCTIONS

  1. Create a dashboard with two charts.
  2. Copy it with chart duplication enabled and a layout containing only one source chart, then repeat with an empty layout.
  3. Confirm the copies contain one and zero charts respectively, no unused chart copies are created, and the source keeps both charts.

Validated locally: 311 DAO/dashboard unit tests, all 11 dashboard-copy API tests using isolated SQLite, and changed-file pre-commit checks including MyPy. The new duplicate-copy regressions fail before the fix.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.82%. Comparing base (2518658) to head (f0854f8).
⚠️ Report is 152 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##           master   #45528       +/-   ##
===========================================
+ Coverage   56.31%   83.82%   +27.50%     
===========================================
  Files        3054     3053        -1     
  Lines      200906   201698      +792     
  Branches    46913    47082      +169     
===========================================
+ Hits       113140   169072    +55932     
+ Misses      86681    29368    -57313     
- Partials     1085     3258     +2173     
Flag Coverage Δ
hive 34.77% <33.33%> (-0.14%) ⬇️
mysql 52.80% <33.33%> (?)
postgres 52.81% <33.33%> (?)
presto 36.50% <33.33%> (-0.16%) ⬇️
python 87.46% <100.00%> (+50.76%) ⬆️
sqlite 52.57% <33.33%> (?)
unit 81.98% <100.00%> (?)

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.

@pull-request-size pull-request-size Bot added size/L and removed size/M labels Oct 20, 2026
@mikebridge
mikebridge marked this pull request as ready for review October 20, 2026 19:35
@codeant-ai-for-open-source

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

Copy link
Copy Markdown
Contributor

CodeAnt  CodeAnt PR Risk

🟢  Low Risk

  • The PR appears safe to merge; no actionable issues were identified in the review.

Assessed commit: f0854f85b487

@fitzee fitzee left a comment

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.

LGTM, no blocking issues. This applies the fix suggested on #45472: clones are limited to charts whose IDs appear in the reconciled layout. The clone loop still iterates only original_dash.slices, so a layout chart that isn't a member of the source is never cloned. It still fails the remap with DashboardLayoutInvalidError, and zero charts are cloned, so the access gap noted on #45472 isn't widened. The duplicate_slices=False path is unchanged.

Verified:

  • Reverting only the continue condition makes test_copy_without_charts_dropped_from_layout[*-True] fail with IntegrityError: UNIQUE constraint failed: dashboard_slices_version.... That is the history-capture failure from the description. With the fix, both changed unit files pass (28/28).
  • A layout that references the same chart twice (int and legacy string ID) produces one clone, and both slots point to it.
  • A markdown-only layout produces zero clones. A malformed CHART node is still rejected with a 422.
  • Missing or null positions with duplicate_slices still ends in the same 400 as before. It now fails before any clone is flushed.
  • The removed lines are mock-based tests. They had to go: with positions: {} the fixed loop clones nothing, so clone.viewers would never be set. The persisted-fixture replacements exercise the real loop.
  • CI on 0099a02 is green (postgres, mysql, sqlite, unit, pre-commit). The skipped jobs are frontend/docs only.

Two non-blocking metadata follow-ups are inline.

# Duplicating slices as well, mapping old ids to new ones
for slc in original_dash.slices:
if slc.id in archived_ids:
if slc.id in archived_ids or slc.id not in layout_chart_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.

Non-blocking. A side effect of skipping omitted charts: old_to_new_slice_ids no longer has entries for them, so _remap_filter_scopes leaves their IDs as the source's IDs. Before this change they were rewritten to orphan-clone IDs. Example: source has A=1 and B=2, the layout keeps only A, and A is cloned to 4. The copy's json_metadata ends up with:

  • chart_configuration keys "2" and "4". The "2" entry belongs to the source's live chart B.
  • chart_configuration["4"].crossFilters.chartsInScope == [2].
  • A native filter's chartsInScope == [4, 2] and scope.excluded == [2].

The references were dangling before this change too. Now they point at a live chart on another dashboard. If B is later added to the copy, it picks up the stale cross-filter config. The follow-up suggested on #45472 (prune metadata to the layout's charts) is now the natural complement: after the remap, drop any ID in these structures that isn't in old_to_new_slice_ids.values().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, Matt. Confirmed: the fallback keeps omitted IDs in the source namespace on this branch. The stacked #45530 prunes chart configuration and filter scopes to retained chart IDs in 46b2623, so I'll keep that cleanup there rather than expand this clone-count fix.

# the remap below skips that slot because it has no chartId.
reconcile_position_json(metadata.get("positions"), original_dash.id)
chart_id: int | None
layout_chart_ids: set[int] = {

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.

Non-blocking, pre-existing. timed_refresh_immune_slices and expanded_slices are never remapped on the duplicate path, even for charts that are cloned. In the A=1 to clone 4 case above, the copy keeps timed_refresh_immune_slices: [1, 2] and expanded_slices: {"1": true, "2": true}. As a result, every duplicated copy loses refresh immunity and expanded state for its charts. _remap_filter_scopes covers neither key. Out of scope here, but worth handling in the same follow-up: remap through old_to_new_slice_ids and prune to the layout. (default_filters has a similar problem: its old-ID keys are filtered against the new slice_ids, so they are all dropped.)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, confirmed these are pre-existing. The stacked #45530 remaps and prunes expanded charts and refresh exemptions in 46b2623. Legacy default_filters still needs a separate follow-up; I'm leaving that behavior out of this fix.

@fitzee fitzee left a comment

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.

No blocking issues; LGTM. The clone loop now skips charts the copied layout leaves out. It does this before any Slice row is written, the change is limited to the duplicate_slices=True path, and no blocking issues turned up.

Paths I checked that hold up:

  • Which charts count as "in the layout". superset/daos/dashboard.py:973 reads CHART nodes from the payload layout after the archived-slot replacement and reconcile_position_json. Hard-deleted and archived slots are already markdown at that point, so they can't add an ID. Tabs, rows and markdown carry no chartId. Native filters target datasets, not charts. No chart is rendered from outside positions, so a membership chart missing from the layout has nowhere to appear, and skipping its clone is correct.
  • Legacy dashboards with null position_json. The UI copy (dashboardState.ts copyPayload) always sends the client layout. For a legacy dashboard that layout is built from slices, so it includes every chart and these copies don't lose charts. An API call with duplicate_slices=True and no positions raises a KeyError at :973. Base already raised the same KeyError at the remap loop (:995), so this is not a regression. It only fails sooner, before any clones are written.
  • Charts from other dashboards. The loop still iterates only original_dash.slices. A layout chart that isn't a source member gets no clone, and the remap at :995 raises DashboardLayoutInvalidError. The copy can't clone a chart the source doesn't own. The duplicate_slices=False path is unchanged. It still links any chart the payload layout names without checking access. That gap is older than this PR and is what #45538 addresses.
  • Mutation test. I reverted only the :980 condition. test_copy_without_charts_dropped_from_layout[kept0-True] and [kept1-True] then fail with IntegrityError: UNIQUE constraint failed: dashboard_slices_version..., the history-capture failure described in the PR body. With the fix, all 28 tests in the two changed unit files pass. The test_creator_group_call_sites.py rewrite was needed: with positions: {} the old mocks would now clone nothing, and the new len(copied.slices) == 3 assertion proves the loop really runs.
  • Interaction with the related PRs. #45530 is stacked directly on this branch. It contains this hunk unchanged and handles the remaining chart-ID cleanup in metadata (see below). This PR merges cleanly with #45538, and their semantics agree: with a subset layout, allowed_chart_ids becomes the clone IDs; with an empty layout, it falls back to the source members, which is harmless. #45530 and #45538 do conflict with each other textually around dash.params = original_dash.params, so whichever merges second needs a rebase.

Already raised in the existing threads and deferred to #45530:

  • Out-of-layout chart IDs left in metadata. These are chart_configuration keys and the excluded/chartsInScope/chartId fields of filter scopes. They now stay as the source's chart IDs, where before they pointed at orphan clones. They are dangling either way and don't render. #45530 prunes them.

Nits (non-blocking):

  • superset/dashboards/schemas.py:488-492: the duplicate_slices description still reads "copy all charts on the dashboard". It now copies only the charts in the supplied layout. Updating it also means regenerating the OpenAPI spec.
  • tests/integration_tests/dashboards/api_tests.py:5447: the comment "Clones carry the source names, including ones the layout left out" no longer matches. Charts the layout leaves out aren't cloned anymore. The cleanup itself is still a reasonable safety net.

@EnxDev EnxDev left a comment

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.

Read through this one. Limiting the clone loop to the reconciled layout's chart IDs is the right fix, it runs before any Slice row is flushed, and the parametrised unit and API tests pin both the chart count and the orphan count for the subset and empty layouts. The mock-to-fixture rewrite in the creator-group tests is a real improvement too, since the old mocks would have passed with the loop cloning nothing.

One note inline on the docs page that still says live charts are always cloned.

# Duplicating slices as well, mapping old ids to new ones
for slc in original_dash.slices:
if slc.id in archived_ids:
if slc.id in archived_ids or slc.id not in layout_chart_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.

This changes what "Open as new" does for a version whose layout predates some of the current charts, and the docs still describe the old behaviour.

docs/docs/using-superset/version-history.mdx:321 says "Live charts are still cloned" under copying with charts. Worth a clause there saying only the charts in the copied layout are cloned, so a user opening an older version isn't surprised when newer charts don't come along.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks. Updated the version-history docs to say that only live charts in the copied layout are cloned, so opening an older version won't bring along charts added later.

@github-actions github-actions Bot added the doc Namespace | Anything related to documentation label Oct 26, 2026
@netlify

netlify Bot commented Oct 26, 2026

Copy link
Copy Markdown

✅ Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit f0854f8
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6adf92895350d36f0483da8c
😎 Deploy Preview https://deploy-preview-45528--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.

@aminghadersohi aminghadersohi left a comment

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.

LGTM. Reviewed the diff and the thread replies; the fixes and regression tests cover the reported cases.

@aminghadersohi
aminghadersohi merged commit 9a52708 into apache:master Oct 27, 2026
72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc Namespace | Anything related to documentation size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants