Skip to content

Pivot: preserve expanded rows when adding a field, sorting, or refreshing - #9785

Open
mahdi13 wants to merge 3 commits into
rilldata:mainfrom
inkitt:pivot-value-based-expansion
Open

Pivot: preserve expanded rows when adding a field, sorting, or refreshing#9785
mahdi13 wants to merge 3 commits into
rilldata:mainfrom
inkitt:pivot-value-based-expansion

Conversation

@mahdi13

@mahdi13 mahdi13 commented Aug 4, 2026

Copy link
Copy Markdown

The pivot table tracked expanded rows by TanStack's positional row ids (dot paths like 0.1.2 that index into subRows). Any config change re-partitions or re-orders the row tree, so those ids stop pointing at the same rows and the expanded state was discarded. Adding a measure or dimension, reordering row dimensions, or sorting all collapsed the tree back to the top level.

This switches expansion to value-based hierarchical keys: the dimension values from the root to a node, so a node keeps the same key across add, sort, and data refreshes.

  • PivotTable sets a value-based getRowId (parent id plus this row's value), with a reserved id for the prepended grand-totals row.
  • getValuesForExpandedKey and addExpandedDataToPivot resolve a key by matching each row's own value (stored under rowDimensions[0] at every depth) instead of positional indices, which also removes the totals-row offset.
  • queryExpandedRowMeasureValues derives nesting depth from the key's segment count.
  • getFiltersForCell, getValuesForFlatTable, getRawRowValues, the show-more row limits, and the ancestor-highlight walk all use the value keys.
  • The defensive expanded = {} resets on add/sort/columns/rows are dropped. A reorder just leaves keys that match nothing (inert), and the legacy positional pivotExpanded proto field is no longer read.
  • Existing unit tests updated for value-based resolution, plus a Playwright test that expands a nested row and asserts it stays open after adding a measure and sorting.

Reordering the row dimensions changes a node's value path, so those specific nodes are not restored (they were not before either). Adding a field, sorting, and refreshing now keep the tree expanded.

Closes #9781

Checklist:

  • Covered by tests
  • Ran it and it works as intended
  • Reviewed the diff before requesting a review
  • Checked for unhandled edge cases
  • Linked the issues it closes
  • Checked if the docs need to be updated. If so, create a separate Linear DOCS issue
  • Intend to cherry-pick into the release branch
  • I'm proud of this work!

mahdi13 added 2 commits August 4, 2026 15:28
Foundation for keying pivot row expansion by dimension-value paths instead
of positional TanStack row ids, so expansion survives sorting, adding a
field, and data refreshes (rilldata#9781). Keys are the dimension
values from root to a node, NUL-joined and hierarchical (depth is the
segment count, parent is strip-last-segment). Pure helpers with unit tests.
Wire the value-based expand keys through the pivot so expansion survives
sorting, adding a field, and data refreshes (rilldata#9781).

- PivotTable sets a hierarchical value-based getRowId, with a reserved id
  for the grand-totals row.
- getValuesForExpandedKey and addExpandedDataToPivot resolve a key by
  matching each row's value (stored under rowDimensions[0]) instead of
  positional indices.
- queryExpandedRowMeasureValues derives depth from the key's segment count.
- getFiltersForCell, getValuesForFlatTable, getRawRowValues, the show-more
  keys, and the ancestor-highlight walk use the value keys.
- Drop the expanded={} resets on add/sort/columns/rows and stop reading the
  legacy positional pivotExpanded proto field.
- Tests updated for value-based resolution.

Fixes rilldata#9781
@mahdi13
mahdi13 force-pushed the pivot-value-based-expansion branch from 63818ef to 0262ac9 Compare August 4, 2026 13:29
Expand a nested row, then add a measure and sort, and assert the row stays
expanded (rilldata#9781).
@mahdi13
mahdi13 force-pushed the pivot-value-based-expansion branch from 0262ac9 to c35cd9a Compare August 4, 2026 15:09
@mahdi13
mahdi13 marked this pull request as ready for review August 4, 2026 15:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pivot: expanded rows collapse on any config change (adding a measure/dimension, reordering, or sorting)

1 participant