Skip to content
Source
Sign inSign up
TanStack/table

getGroupedRowModel().flatRows puts every group row after all its descendants, not just tree data below terminal depth #6551

Send to my agent
ClosedMILLERMARRU(GitHub profile, opens in a new tab) opened this issue 0 comments

getSortedRowModel().flatRows was just fixed by #6529 to put each parent ahead of its own sub-rows instead of post-order, and #6529's own description notes createCoreRowModel/createPaginatedRowModel already push parent-before-children, and that getFilteredRowModel().flatRows was deliberately left post-order for now (I filed #6536 for that one, still open).

getGroupedRowModel().flatRows has the same class of bug, and it's not scoped to "tree data below the terminal grouping depth" the way perf-new.md's N1 write-up frames it ("(a) tree-data rows below terminal depth move from preorder to postorder in grouped flatRows... the sorted model already emits postorder"). That justification is stale now that #6529 fixed sorted's ordering, and reading the current createGroupedRowModel.ts on main, the issue is broader than tree-data: every group-level row, at every depth, is pushed to flatRows by its parent's loop, which only runs after that parent's own recursive call has fully returned:

const subRows = groupUpRecursively(groupedRows, depth + 1, id)   // descend first
...
const row = constructRow(table, id, ...)                          // build this group's row
...
subRows.forEach((subRow) => {                                     // push the CHILDREN
  groupedFlatRows.push(subRow)
  groupedRowsById[subRow.id] = subRow
})
return row                                                         // return row WITHOUT pushing it

and the group row itself is only pushed by whichever loop called groupUpRecursively for it — for a top-level group, that's the very last statement in the whole function (groupedRows.forEach((subRow) => { groupedFlatRows.push(subRow); ... })), which can't run until recursion has finished for every top-level group.

Concrete repro, single-level grouping (grouping: ['a']), no tree data at all, so N1's terminal-depth explanation doesn't apply:

// rows: { id: 'r1', a: 1 }, { id: 'r2', a: 1 }, { id: 'r3', a: 2 }
table.getCoreRowModel().flatRows.map(r => r.id)
// ['r1', 'r2', 'r3']

table.getGroupedRowModel().rows.map(r => r.id)
// ['a:1', 'a:2']  -- correctly parent-first as a tree

table.getGroupedRowModel().flatRows.map(r => r.id)
// actual:   ['r1', 'r2', 'r3', 'a:1', 'a:2']
// expected: ['a:1', 'r1', 'r2', 'a:2', 'r3']  (matches `rows`, matches core/paginated/now-sorted convention)

Every leaf row from every group lands ahead of every group row, not just "descendants below the terminal depth" — the two getGroupedRowModel tests added for N1 (single-level flat 12→7, two-level flat 13→9, etc., in tests/implementation/features/column-grouping/createGroupedRowModel.test.ts) check the flatRows count dropped after de-duplication but don't assert this ordering, so it wasn't caught there.

This also means getExpandedRowModel().flatRows and the paginated model inherit the same ordering, same as the N1 write-up already notes for the duplicate-push case ("expandRows forwards grouped flatRows by reference... the duplication propagates down the pipeline") — here it'd be "the ordering propagates down the pipeline" instead.

Given #6529 already established parent-before-children as the intended contract for flatRows and specifically invalidated the "sorted already does this" reasoning perf-new.md used to accept the grouped-model gap, it seems worth re-opening/widening N1's accepted-delta scope rather than leaving it as accepted. Happy to help verify a fix mirroring #6529's approach (take the row's flatRows slot before descending, overwrite it when the row's subRows get reassigned) if useful.

  1. KevinVandy closed this

Sign in to join this conversation.