Skip to content

fix: criteria, COUNTBLANK and a table answer over a range covering most of the sheet answer at once (#676) - #684

Merged
isamu merged 4 commits into
mainfrom
fix/huge-range-per-cell-reads
Dec 6, 2026
Merged

isamu merged 4 commits into
mainfrom
fix/huge-range-per-cell-reads

Conversation

@isamu

@isamu isamu commented Dec 6, 2026 •

Copy link
Copy Markdown
Member

Summary

Part of #676. #677 bounded the reads that skip blank cells; the reads that keep one entry per cell still listed a range's whole area. This change bounds three of them by the cells the sheet holds, as Excel bounds them by the used range:

  • COUNTBLANK over a range reaching past the sheet counts the area less its filled cells, arithmetically.
  • The COUNTIF family (COUNTIF, SUMIF, AVERAGEIF, COUNTIFS, SUMIFS, AVERAGEIFS, MAXIFS, MINIFS) tests the cells the sheet holds; when every criteria matches a blank, the blank cells past the sheet are counted arithmetically.
  • A table the cell's whole formula answers (=B2:D1048576, =1+B2:XFD1048576, =ABS(C2:D1048576)) is checked from its shape before it is built. Past the sheet's edge, or (for a table larger than MAX_TABLE_CELLS) over a filled cell, it answers #SPILL! without being built, with the same reported error as before.

How:

  • getSparseRangeValues (rangeReading.ts): a range reaching past the sheet is read as its cells inside the sheet, each read as getRangeValuesKeepingBlanks reads it, at its place in reading order, plus the area's size. A range inside the sheet returns null and keeps the dense read. The per-cell read was lifted into cellReading, shared by both reads.
  • sparseCells.ts (pure): blankCount, countMeetingAll, placesMeetingAll, placesMatchingWithValues. A handler takes the sparse path only when one of its ranges is sparse; when all are dense the code it ran before runs unchanged.
  • spillRefusal.ts (pure) and hugeTableAnswer.ts: EvaluatorContext.refusesTable is set only on the context of a cell's top-level evaluation (calculator.ts evaluateAnswer) and asked by the sole-range branch of evaluateFormula and by evaluateOverTables. When it refuses, the first element and the ranges' cells inside the sheet are read first, so loops through them are still found (=1+A1:D1048576 in B1 is still #REF!).

Items to Confirm / Review

  • Exactness of the sparse read: a place past both the written and the calculated grid holds no formula and no spill, and its read notes nothing; the cells inside the grid are read in the same order as the dense read. The differential below found no difference.
  • A refused table evaluates only its first element. A position-dependent reference (INDIRECT("A"&ROW(B1:B9))) reached only by another element is no longer read. For a table that would have spilled past the edge this changes nothing unless such a read closes a loop.
  • Past-the-edge refusal applies to tables of any size (e.g. =ABS(Sheet2!A1:XFD1) in B1). The taken-cell check before building is only for tables larger than MAX_TABLE_CELLS, because listing the taken cells costs a pass over the sheet; a smaller table is still checked when placed.
  • New optional members: getSparseRangeValues on FunctionContext / EvaluatorContext, refusesTable on EvaluatorContext.
  • Left open (A range written cell to cell over most of the sheet (SUM(B2:XFD1048576)) never finishes and runs out of memory #676 stays open): getRangeGrid still lists a huge area (D-functions, TRANSPOSE, SORT, …): its callers need every position, and a D-function's computed criteria is evaluated per record, so blank records past the sheet are not alike. A huge table nested in a function (=SUM(1+B2:D1048576)), or a huge table that fits and spills (as Excel spills it), is still built element by element.

Verification

  • yarn format && yarn lint && yarn build && yarn typecheck && yarn test && yarn test:engine && yarn test:fixtures && yarn quality, chained with set -e, passed before the push.
  • tests/engine/test_sparseCells.ts and test_spillRefusal.ts: the rules both ways (blank-matching criteria, mismatched sizes, empty inputs, a whole-sheet count under a time bound), plus generated ranges compared with counting over the dense list, and generated tables compared with listing every cell spillTargets covers.
  • tests/engine/test_hugeRangeReads.ts: COUNTBLANK, COUNTIF, SUMIF, AVERAGEIF, COUNTIFS, SUMIFS, AVERAGEIFS, MAXIFS over areas to XFD1048576, mismatched sizes, a loop through the formula's cell, and refused tables (past the edge, over a filled cell with its reported error, through a loop), each with a 10 s timeout. The engine at main runs out of heap on this file.
  • Old engine (main) vs this branch, the same formula alone in B1 of a small workbook, 1M-row ranges the old engine can finish: 20 cases, same answers. Timings (old → new): =1+A1:D1048576 118 s → 9 ms (#REF!), =1+C2:D1048576 16 s → 7 ms, =-Sheet2!A1:B1048576 18 s → 10 ms, =COUNTIF(A1:C1048576,"") 1.8 s → 10 ms, other criteria and COUNTBLANK about 0.5 s → 10 ms. Over XFD-wide areas =COUNTIF(B2:XFD1048576,""), =COUNTBLANK(C2:XFD1048576) and =1+B2:XFD1048576 ran out of memory on main and answer in under 10 ms. (Measured at dcc80db vs this branch on a loaded machine; ratios, not absolutes.)
  • Differential, whole calculateWorkbook results compared (old tree copied from main before the change):
    Pinned: old = dcc80db (main), new = 4f3261c. Each seed's workbook is generated, calculated by both trees, and data, errors, circularLoops and spills are compared whole.
    • formula generator (one generated formula over a seeded sheet): 1000 compared, 0 differ.
    • mixed generator (token soup, strict syntax, range chains, huge ranges): 262 compared, 0 differ (a further 348 compared, 0 differ, on the head before the reach fix).
    • area generator (normal-sized areas larger than the data, which take the sparse paths): 123 compared, 0 differ. On the head before the reach fix this generator found 5 workbooks whose errors list came in another order (values equal): a refused table did not record its reach. That is fixed in 4f3261c and pinned by a regression test in test_hugeRangeReads.ts.
    • Seeds that time out are skipped on both trees. They are workbooks spilling a legitimately huge table that fits (e.g. =A3:B4:C1048576+1), which is out of scope. Under load a few time out on one side only, and those are not compared.

User Prompt

AI時代のスプレッドシートとしてExcelやGoogle Sheetsを超えるよう、Codexと並列で改善を続けて

work in GUIChatPluginSpreadsheet

🤖 Generated with Claude Code

isamu and others added 4 commits December 6, 2026 20:38
…676)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…st of the sheet answer at once (#676)

A range reaching past the sheet is read sparse: the cells inside the sheet at their
places, every other place blank. The COUNTIF family and COUNTBLANK count those blank
places arithmetically. A table the cell's whole formula answers is checked from its
shape before it is built, and answers #SPILL! unbuilt when it cannot spill.

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

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The next pass reads each table's reach to decide which tables a read calculates
early; without it the cells were calculated, and their errors reported, in another
order than before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@isamu

isamu commented Dec 6, 2026 •

Copy link
Copy Markdown
Member Author

Review ledger (local codex exec)

Round Head Verdict Notes
1 2ca37fd LGTM —
own differential 2ca37fd finding A table refused before it was built did not record its reach, so the next pass calculated another table early and the errors list came in another order (values unchanged). Fixed in the next commit: placeResult takes the refusal and sets the reach first; regression test added. Count restarts.
2 4f3261c LGTM Old/new whole results compared over sparse aggregates, cross-sheet reads, loops, spill refusals and the settled order workbook: no difference.
3 4f3261c LGTM SUMIF/AVERAGEIF with value ranges of another shape, criteria as a cell reference and an array, COUNTIFS/SUMIFS/AVERAGEIFS/MAXIFS over 200k-row ranges: old and new equal. Two consecutive LGTMs on an unchanged head.

Merged without re-merging main (main had moved to c271efc): no file in common between git diff --name-only 4f3261c...origin/main and this PR's files, git merge-tree clean, every PR check green. After merging (edd1043): yarn typecheck and yarn test:engine pass on main.

@isamu
isamu merged commit edd1043 into main Dec 6, 2026
17 checks passed
@isamu
isamu deleted the fix/huge-range-per-cell-reads branch December 6, 2026 11:56
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.

1 participant