Skip to content

Fix IMSUM, IMPRODUCT and TEXTJOIN throwing RangeError for large ranges (HF-593) - #1901

Merged
sequba merged 8 commits into
developfrom
fix/hf-593-range-spread
Nov 19, 2026
Merged

sequba merged 8 commits into
developfrom
fix/hf-593-range-spread

Conversation

@marcin-kordas-hoc

@marcin-kordas-hoc marcin-kordas-hoc commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Context

IMSUM, IMPRODUCT and TEXTJOIN threw an uncaught RangeError: Maximum call stack size exceeded for a range of about 125,000 cells or more (for example =IMSUM(A1:D40000)). The error escaped the recalculation, so buildFromArray or the CRUD call that triggered it failed. The cell did not get a value or an error value.

Cause: ArithmeticHelper.coerceComplexExactRanges, used by IMSUM and IMPRODUCT, and TEXTJOIN appended a range's values with push(...values). That passes one call argument per value, and the call stack runs out at roughly 125,000 values on Node 24. The values are now appended in a loop. Results for ranges that worked before do not change.

The rule is also added to hyperformula/dev-docs/INTERPRETER.md: never spread a range's values into a call.

How did you test your changes?

  • Paired branch fix/hf-593-range-spread in hyperformula-tests adds a 160,000-cell case to the specs for IMSUM, IMPRODUCT and TEXTJOIN. Each case asserts the value Excel returns for the same input, and the IMPRODUCT and TEXTJOIN cases put a distinct value in the last cell, so a range that is read only in part fails.
  • Each spread was reverted on its own, without the other. Reverting the ArithmeticHelper loop fails the IMSUM and IMPRODUCT cases with RangeError (2 failed, 47 passed). Reverting the TEXTJOIN loop fails the TEXTJOIN case (1 failed, 48 passed).
  • tsc --noEmit, npm run lint and the full Jest suite with the private suite attached: tsc exit 0, lint 0 errors, Jest 529 suites passed (7,277 tests passed, 3 skipped) on engine 860aabef2 with tests f1a6edb1e.
  • A base-versus-head run against develop covered 116 formulas: the three functions at sizes from 1,000 to 1,000,000 values, both as cell ranges and as SEQUENCE arrays, plus small inputs.
    • 50 results unchanged.
    • 54 that threw now return a number, and 12 that threw now return an error value.
    • No result went from a number to an error, and RAND() inside an argument is still evaluated once.
    • With the two heads swapped, the same run reports 54 regressions.

AI assistance

Claude Code (Claude Opus 5.5) wrote the code change, the tests, the changelog entry, the dev-docs line and this description. It also ran the Excel measurements. A maintainer reviews it like any other change.

Compatibility with Excel and Google Sheets

Excel results were measured in Excel Online. The 160,000 values come from a spilled SEQUENCE (1, 2, 3, 4 in every row, or 1 everywhere; the last cell typed in where it differs). Google Sheets was not checked.

Formula Excel Google Sheets HyperFormula (develop → this branch)
=IMSUM(A1:D40000), 1, 2, 3, 4 in every row 400000 Not checked RangeError → 400000
=IMPRODUCT(A1:D40000), 1 in every cell 1 Not checked RangeError → 1
=IMPRODUCT(A1:D40000), 1 in every cell but i in D40000 i Not checked RangeError → i
=TEXTJOIN("-",TRUE,B1:E40000,"x","y"), B1:E40000 empty x-y Not checked RangeError → x-y
=TEXTJOIN("-",TRUE,B1:E40000,"x","y"), B1:E40000 empty but z in E40000 z-x-y Not checked RangeError → z-x-y
=IMSUM(SEQUENCE(40000,4,1,0)) 160000 Not checked RangeError → 160000
=TEXTJOIN("",TRUE,A1:D40000), 1, 2, 3, 4 in every row (160,000 characters) #CALC! Not checked RangeError → #VALUE!
=IMPRODUCT(A1:D40000), 1, 2, 3, 4 in every row (overflows) #VALUE! Not checked RangeError → #NUM!
=IMPRODUCT("1e200","1e200") #VALUE! Not checked #NUM! → #NUM!
=LEN(TEXTJOIN("",TRUE,SEQUENCE(32767,1,1,0))) 32767 Not checked 32767 → 32767

HyperFormula spells the boolean as TRUE(). The last three rows show error types that differed from Excel before this change and still differ. They are listed under follow-ups.

Public API changes

None.

Out of scope and follow-ups

  • When the TEXTJOIN result is longer than 32,767 characters, HyperFormula returns #VALUE! and Excel returns #CALC!. Add the #CALC! error type (HF-134) #1889 changes it.
  • On overflow, IMPRODUCT returns #NUM! and Excel returns #VALUE!, for any range size. Follow-up task: HF-600.
  • manyToNumbers in ArithmeticHelper has the same spread. It makes SKEW and the other functions that use it throw for 160,000 cells. Add KURT, MODE.SNGL, PERCENTRANK.EXC and TRIMMEAN functions (HF-427, HF-423, HF-422, HF-426) #1800 replaces it with a loop.
  • Array literals spread their rows in Interpreter: HF-577.
  • Two more spreads throw the same RangeError outside functions. Inserting 200,000 rows at once with addRows throws in DenseStrategy, and a 200,000-cell circular reference throws in TopSort. Follow-up task: HF-601.

Types of changes

  • Breaking change (existing functionality no longer works as before) – agreed with the maintainers
  • New feature or improvement (a non-breaking change that adds functionality)
  • Bug fix (a non-breaking change that fixes an issue)
  • Additional language file, or a change to an existing language file (translations)
  • Change to the documentation

Related issues

  1. HF-593

Checklist

🤖 Generated with Claude Code

marcin-kordas-hoc and others added 3 commits November 16, 2026 09:28
coerceComplexExactRanges and TEXTJOIN appended a range's values with
push(...values), which passes one call argument per value. A range of
about 125,000 cells exceeded the call stack and the RangeError escaped
the recalculation. The values are now appended in a loop.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r fix

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Deploying with  Cloudflare Workers  Cloudflare Workers

The latest updates on your project. Learn more about integrating Git with Workers.

Status Name Latest Commit Preview URL Updated (UTC)
✅ Deployment successful!
View logs
hyperformula-docs 7d85532 Commit Preview URL

Branch Preview URL
Nov 19 2026, 10:51 AM

@handsontable-pr-review handsontable-pr-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

Medium risk: Small, semantically equivalent refactor (spread push → loop) on the interpreter's argument-coercion hot path for IMSUM/IMPRODUCT/TEXTJOIN; behaviour for previously working inputs is unchanged, so the blast radius is limited to the newly un-crashed large-range cases.

Full review: 1 potential issue(s).

hyperformula/src/interpreter/ArithmeticHelper.ts:511

manyToNumbers still spreads range values, fix incomplete vs new rule

Medium severity

The PR fixes the push(...values) stack overflow in coerceComplexExactRanges and TEXTJOIN, and adds a dev-docs rule (INTERPRETER.md L23: "Never spread a range's values into a call"), but manyToNumbers — the shared path behind coerceNumbersExactRanges and coerceNumbersCoerceRangesDropNulls — still does expandedVals.push(...arr). Any built-in function coercing a large range through those helpers (e.g. SUMSQ-style exact-range coercion) still throws the same uncaught RangeError: Maximum call stack size exceeded out of the recalculation for ranges of roughly 125,000+ values, so the class of bug the PR claims to fix and codifies as a repository rule remains live in the same file it edited.

Suggestion: Apply the same loop append in manyToNumbers (replace expandedVals.push(...arr) with a for loop), or explicitly scope the new dev-docs rule and note the remaining call sites as follow-ups.

Want a deeper review? Comment /review improve on its own line: a full review of the latest commit with an extra fix-oriented pass (about 2x the cost).

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Performance comparison of head (7d85532) vs base (91f4681)

                                     testName |    base |    head |  change
---------------------------------------------------------------------------
                                      Sheet A |  320.16 |  354.15 | +10.62%
                                      Sheet B |   98.87 |  117.55 | +18.89%
                                      Sheet T |   88.09 |   102.6 | +16.47%
                                Column ranges |  446.24 |  487.31 |  +9.20%
                                Sorted lookup | 15270.3 | 15482.7 |  +1.39%
Sheet A:  change value, add/remove row/column |   10.82 |   12.09 | +11.74%
 Sheet B: change value, add/remove row/column |   82.86 |  102.66 | +23.90%
                   Column ranges - add column |   121.8 |  122.72 |  +0.76%
                Column ranges - without batch |   391.1 |  377.59 |  -3.45%
                        Column ranges - batch |   89.69 |    95.7 |  +6.70%

@handsontable-pr-review handsontable-pr-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Note

Medium risk: Touches core interpreter coercion paths (ArithmeticHelper, TextPlugin) used by IMSUM/IMPRODUCT/TEXTJOIN/CONCAT recalculation, but the changes are narrow loop replacements covered by base-vs-head runs; this incremental push only edits copyright headers.

Incremental review: 1 potential issue(s).

hyperformula/src/interpreter/plugin/TextPlugin.ts:195

CONCATENATE still spreads range values, throws RangeError on large ranges

Medium severity

CONCATENATE is declared with expandRanges: true, so a range argument is expanded into one callback argument per cell. The callback then does ''.concat(...args), spreading every value into a single call — exactly the pattern this PR removes from coerceComplexExactRanges and textjoin and forbids in the new dev-docs rule. For a range of roughly 125,000 cells or more (e.g. =CONCATENATE(A1:D40000)), this throws the same uncaught RangeError: Maximum call stack size exceeded that escapes recalculation and fails the triggering CRUD call, so the fix is incomplete for the CONCAT family.

Suggestion: Build the result with a loop, e.g. iterate args and append each string to an accumulator (or reuse the loop-based approach already implemented in concat/textjoin), instead of ''.concat(...args).

Want a deeper review? Comment /review improve on its own line: a full review of the latest commit with an extra fix-oriented pass (about 2x the cost).

@Tobiadefami Tobiadefami left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The 160,000-cell specs throw RangeError on develop and pass here, also through SEQUENCE and with an error inside the range. Results for small ranges are unchanged.

@marcin-kordas-hoc
marcin-kordas-hoc enabled auto-merge (squash) November 19, 2026 10:13
@codecov

codecov Bot commented Nov 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.51%. Comparing base (293c595) to head (3c6a9c9).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1901   +/-   ##
========================================
  Coverage    97.51%   97.51%           
========================================
  Files          210      210           
  Lines        17133    17135    +2     
  Branches      3924     3890   -34     
========================================
+ Hits         16707    16709    +2     
  Misses         418      418           
  Partials         8        8           
Files with missing lines Coverage Δ
hyperformula/src/interpreter/ArithmeticHelper.ts 98.68% <100.00%> (+<0.01%) ⬆️
hyperformula/src/interpreter/plugin/TextPlugin.ts 99.54% <100.00%> (+<0.01%) ⬆️
🚀 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.

@sequba
sequba disabled auto-merge November 19, 2026 11:51
@sequba
sequba merged commit 1c7093a into develop Nov 19, 2026
32 of 34 checks passed
@sequba
sequba deleted the fix/hf-593-range-spread branch November 19, 2026 11:51
sequba added a commit that referenced this pull request Nov 19, 2026
### Context
The PR removes the `gpl-v3` license key from the engine, the tests and
the docs.

- **Why:** HyperFormula 4.0 is proprietary only, but `gpl-v3` still
unlocked everything.
- **What changed:**
- `hyperformula/src/`: `gpl-v3` reads as invalid. On `handsontable.com`
and its one-label subdomains, the license check is skipped, so the live
docs examples run without a key.
- Console messages for a missing, invalid or expired key use
Handsontable's text, with the expiry day in two digits. A key without a
HyperFormula entry prints the invalid-key warning.
- Tests, `check-file.js`, the docs build and the formula editor demo use
an internal test key. Its text says it is not licensed for production.
One constant per consumer; the private suite's
`unit/license/test-license-key.spec.js` fails if they differ, or if the
key appears anywhere else, in this repository or the private suite.
- Docs previews get the test key in the live example scripts only, and
only on an explicit opt-in (`DOCS_TEST_LICENSE_KEY=true`, set by `npm
run preview:cf`), never for `master`. Without the opt-in, a build fails
if the key is anywhere in its output, and `npm run deploy:cf` refuses a
built site that holds it.
- "Open in StackBlitz" projects show an "add your license key" notice
when the engine reports a missing or invalid key.
- About 190 code examples (JSDoc, 12 docs examples, guides, READMEs)
show the commented-out key placeholder.
- `license-key.md` and `licensing.md` are rewritten,
`migration-from-3.x-to-4.0.md` leads with the license change and links
#1888, and "open source" wording is gone from current pages.
- `dev-docs/SENSITIVE-INFORMATION.md`: two exceptions to the rule, the
internal test license key and links to Claude Code sessions.
- **Result:** no current code, test or page uses or offers `gpl-v3`.

Deviations from Handsontable:

| Area | Handsontable | HyperFormula |
|---|---|---|
| Blocked state | Lock screen | `#LIC!` and
`LicenseCapabilityMissingError` |
| Placeholder URL | `handsontable.com/pricing/` |
`handsontable.com/pricing/?tab=hyperformula` |
| Licensing page | Lists license types | Proprietary, pricing link,
sales email |
| Preview key | Vite transform on example modules, at build time |
Rewrite of inline example scripts in the built site, by the preview
deploy command. Same opt-in, `DOCS_TEST_LICENSE_KEY=true` |
| Domain check | Hex-encoded regex | Plain regex, same pattern |
| `#LIC!` and error messages | None (lock screen) | `#LIC!` and
`LicenseCapabilityMissingError` texts give `sales@handsontable.com` |

Messages follow Handsontable exactly, so they differ from the
HyperFormula key spec: L1–L3 are longer, and M7 and M8 print as
warnings. They also differ from HOT-15706, which asked for docs links:
like Handsontable's, they have no links, and give
`support@handsontable.com` or `sales@handsontable.com`, or say to
purchase a license. The trial and subscription messages name those
licenses, as Handsontable's do.

### How did you test your changes?
Paired branch in `hyperformula-tests`: `feat/hot-15706-remove-gpl`.

- Lint: 0 errors. Karma (Chrome): 8354 passed. UI e2e and bundle check:
passed.
- Jest, after merging `develop` (with #1786 and #1901): 547 suites,
9,090 passed, 3 skipped, 0 failed. CI is green on the later `develop`
merges.
- Docs: a build without the opt-in holds no key.
`inject-preview-license-key.mjs` refuses without the opt-in and for
`master`, and with it writes the key into the 12 example scripts only.
Running it again on an injected build changes nothing. `deploy:cf`
refuses a site after `preview:cf` has written the key into it.
- Preview pages against the 3.5.0 engine in Chromium: every example
runs, with no license message. The StackBlitz notice shows for the
basic-usage example on 3.5.0 with no key.

### AI assistance
Claude Code wrote the code, tests and docs.

### Compatibility with Excel and Google Sheets
Not applicable.

### Public API changes
- The `licenseKey` value `gpl-v3` is invalid.
- License console messages have new text.
- The `#LIC!` and `LicenseCapabilityMissingError` messages end with
`sales@handsontable.com`: for a missing, invalid or expired key, and for
a function or feature the key does not include.

### Out of scope and follow-ups
- The 0.6, 1.x and 2.x migration guides keep `gpl-v3`: they describe
those versions.
- The vendored key parser still mentions `gpl-v3` and GPL. The upstream
fix is in handsontable/license-key#16; it reaches this repository with
the next re-vendor.
- `release-notes.md` is generated from the changelog at release time.
`### Changed` comes first under `[Unreleased]`, so the generated 4.0.0
section leads with the license change.
- The docs deploy must wait until 4.0.0 is on the CDN. Until then,
production pages run 3.5.0, which has no domain skip.
- `npm run docs:dev` can't give the live examples a key;
`docs/README.md` gives the workaround.
- `licenseKeyValidityState` stays internal; making it public is a
separate API change.

### Types of changes
- [x] Breaking change (existing functionality no longer works as before)
– agreed with the maintainers
- [ ] New feature or improvement (a non-breaking change that adds
functionality)
- [ ] Bug fix (a non-breaking change that fixes an issue)
- [ ] Additional language file, or a change to an existing language file
(translations)
- [x] Change to the documentation

### Related issues
1. HOT-15706
2. #1888

### Checklist
- [x] My pull request meets the [Definition of
Done](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DEFINITION-OF-DONE.md).
External contributors: see also [Contributing to
HyperFormula](https://handsontable.com/docs/hyperformula/guide/contributing.html).
- [ ] I have signed the [Contributor License
Agreement](https://goo.gl/forms/yuutGuN0RjsikVpM2).
- [ ] I read my own diff end to end before requesting a review.
- [x] I added a
[CHANGELOG.md](https://github.com/handsontable/hyperformula/blob/develop/CHANGELOG.md)
entry, written by [the changelog
rules](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DOC-STANDARDS.md#the-changelog)
– or none is needed (docs, tests or tooling only).
- [x] I updated the documentation and JSDoc to match the change.
- [x] Breaking change only: I added a section to the migration guide.
- [x] I updated the `dev-docs/` pages this change affects – or none
needed it.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

https://claude.ai/code/session_01KD7f9XHrxQxDeC5G3HMDM1

---------

Co-authored-by: Claude <noreply@anthropic.com>
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.

3 participants