Skip to content

fix(table-core): prevent median aggregation overflow for large finite values - #6655

Open
EnesKartal wants to merge 1 commit into
TanStack:mainfrom
EnesKartal:fix-median-overflow
Open

EnesKartal wants to merge 1 commit into
TanStack:mainfrom
EnesKartal:fix-median-overflow

Conversation

@EnesKartal

@EnesKartal EnesKartal commented Nov 7, 2026 •

Copy link
Copy Markdown

🎯 Changes

Fixes #6654.

When calculating the median of an even-length array, aggregationFn_median sums the two middle values before dividing by two ((values[mid - 1]! + values[mid]!) / 2).

When both middle values are large finite numbers (e.g. [Number.MAX_VALUE, Number.MAX_VALUE] or [1e308, 1.5e308]), the intermediate addition overflows to Infinity even though the true average is finite and representable.

This fix:

  • Evaluates const sum = a + b.
  • If sum is finite, or if either operand is non-finite (Infinity, -Infinity, or NaN), it returns sum / 2. This ensures that tiny/subnormal values like [Number.MIN_VALUE, Number.MIN_VALUE] are preserved without prematurely underflowing to 0.
  • If the intermediate sum overflows to ±Infinity while both operands are finite, it calculates a / 2 + b / 2, returning the accurate finite median.

Added unit test coverage for large finite overflow, subnormal preservation, and non-finite controls.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with pnpm test and pnpm test:e2e, or these tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed median calculations for even-sized datasets containing very large finite values, preventing incorrect Infinity results.
    • Improved handling of subnormal values and positive or negative infinities.

@changeset-bot

changeset-bot Bot commented Nov 7, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: dba0a85

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@tanstack/table-core Patch
@tanstack/alpine-table Patch
@tanstack/angular-table-devtools Patch
@tanstack/angular-table Patch
@tanstack/ember-table Patch
@tanstack/lit-table Patch
@tanstack/octane-table Patch
@tanstack/preact-table Patch
@tanstack/react-table Patch
@tanstack/solid-table Patch
@tanstack/svelte-table Patch
@tanstack/vue-table Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The even-length median calculation now avoids intermediate overflow for large finite middle values. Tests cover large, subnormal, and infinite values. A patch changeset records the correction.

Changes: Median aggregation correction

Layer / File(s) Summary
Even-length median calculation and validation
packages/table-core/src/features/row-aggregation/aggregationFns.ts, packages/table-core/tests/unit/fns/aggregationFns.test.ts, .changeset/fix-median-overflow.md
The calculation avoids overflow when adding large finite middle values and retains the sum-then-divide path when that sum is finite or an operand is non-finite. Tests cover large values, subnormal values, and infinities. The changeset describes the fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: kevinvandy

Merge Risk: Minimal · up to dba0a

The median overflow fix appears mergeable after normal checks; no actionable issue is identified.

Pre-merge checks: Passed 5

Check name Status Explanation
Title check Passed The title names the core change: a fix that prevents median aggregation overflow for large finite values in table-core. It matches the aggregationFn_median change and the added tests. It is specific a…
Description check Passed The description follows the repository template. It has the Changes section with motivation and a clear explanation of the overflow bug and fix. It links issue #6654. The checklist items are marked, i…
Linked Issues check Passed [#6654] The issue requires that even-length medians of large finite middle values not overflow, that tiny/subnormal results be preserved, and that zero, NaN, and infinity behavior stay unchanged. At h…
Out of Scope Changes check Passed The whole-PR diff (merge base c14e022 to head dba0a85) touches three paths. (1) packages/table-core/src/features/row-aggregation/aggregationFns.ts changes only the even-length median branch, which i…
Docstring Coverage Passed No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat…
Full details: Title check

Explanation

The title names the core change: a fix that prevents median aggregation overflow for large finite values in table-core. It matches the aggregationFn_median change and the added tests. It is specific and concise, and it follows the conventional commit style.

Full details: Description check

Explanation

The description follows the repository template. It has the Changes section with motivation and a clear explanation of the overflow bug and fix. It links issue #6654. The checklist items are marked, including the changeset item under Release Impact. The description covers the required information, so the check passes. Minor note: the Changes section does not describe the test additions in detail, but it does mention them.

Full details: Linked Issues check

Explanation

[#6654] The issue requires that even-length medians of large finite middle values not overflow, that tiny/subnormal results be preserved, and that zero, NaN, and infinity behavior stay unchanged. At head, aggregationFn_median (packages/table-core/src/features/row-aggregation/aggregationFns.ts) computes sum = a + b. It returns sum / 2 when sum is finite or when either operand is non-finite. Otherwise, when both operands are finite and sum overflows, it returns a / 2 + b / 2. Checked cases: [MAX, MAX] gives MAX; [-MAX, -MAX] gives -MAX; [1e308, 1.5e308] gives 1.25e308; [MIN, MIN] gives MIN (no underflow to 0); [Infinity, Infinity] gives Infinity; [-Infinity, Infinity] gives NaN. Every case that did not overflow takes the same sum / 2 path as before, so existing non-overflow behavior is unchanged. The test added in packages/table-core/tests/unit/fns/aggregationFns.test.ts ('does not overflow median for large finite values and preserves subnormals') covers these overflow, subnormal, and non-finite cases. Limitation: I did not run the test suite. The exact-equality assertion for 1e308/1.5e308 is not verified by execution.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff (merge base c14e022 to head dba0a85) touches three paths. (1) packages/table-core/src/features/row-aggregation/aggregationFns.ts changes only the even-length median branch, which is the overflow fix in #6654. (2) packages/table-core/tests/unit/fns/aggregationFns.test.ts adds median overflow, subnormal, and non-finite cases for that fix. Other existing assertions are unchanged. (3) .changeset/fix-median-overflow.md adds a patch changeset for the published package, as the PR description states. No change falls outside the scope of #6654.

Full details: Docstring Coverage

Explanation

No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files. (1 skipped: 1 unsupported.)

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between c14e022 and dba0a85.

Files selected for processing: 3

  • .changeset/fix-median-overflow.md
  • packages/table-core/src/features/row-aggregation/aggregationFns.ts
  • packages/table-core/tests/unit/fns/aggregationFns.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

This branch has not been deployed

No deployments
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.

Median aggregation overflows for large finite middle values

1 participant