Skip to content

SUBTOTAL, AGGREGATE: COUNT and COUNTA don't fail on an error in the cells - #4348

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/subtotal-count-errors
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/subtotal-count-errors

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4271

What was wrong

aggregate_impl in crates/functions/src/stat/descriptive.rs is the reducer behind SUBTOTAL and AGGREGATE codes 1–13. It returned the first error it met in the cells before looking at which function was asked for. So SUBTOTAL/AGGREGATE 2 (COUNT) and 3 (COUNTA), and SUBTOTAL 102/103, returned #N/A for a range holding one. Native Excel's COUNT forms pass over the error and its COUNTA forms count it as a nonempty cell, just as COUNT and COUNTA do, as the issue shows.

What changed

For codes 2 and 3 (and so 102 and 103), an error in the cells no longer stops the reduction:

  • COUNT, which counts only numbers, passes over it;
  • COUNTA counts it, since it's nonempty.

Every other code returns the error as before. Option 6 (ignore errors) works as before.

QA report

Main vs branch, end to end (gridcraft-cli run, real cells)

The cells are built with cell.set, printed, saved to .xlsx, and reopened with gridcraft-cli cat. main is a gridcraft-cli built from origin/main (19f07f3a); the branch is this PR (c742b7d6). The Excel column is the issue's native Excel 16.0 result; I didn't run Excel myself.

Setup: A1:A2 = 10, =NA(); B1:B2 = 10, =1/0; C1:C4 = 10, x, =NA(), blank.

Cell Formula main this PR Excel (issue)
E1 =SUBTOTAL(2,A1:A2) #N/A 1 1
E2 =SUBTOTAL(3,A1:A2) #N/A 2 2
E3 =AGGREGATE(2,0,A1:A2) #N/A 1 1
E4 =AGGREGATE(3,0,A1:A2) #N/A 2 2
E5 =COUNT(A1:A2) 1 1 1
E6 =COUNTA(A1:A2) 2 2 2
E7 =SUBTOTAL(102,B1:B2) (#DIV/0!) #DIV/0! 1 1 (issue: "with =1/0 and with codes 102 and 103")
E8 =SUBTOTAL(103,B1:B2) #DIV/0! 2 2
E9 =SUBTOTAL(2,B1:B2) #DIV/0! 1 1
E10 =SUBTOTAL(9,A1:A2) (SUM) #N/A #N/A #N/A (issue: "propagate the error when summing")
E11 =AGGREGATE(9,0,A1:A2) #N/A #N/A #N/A
E12 =AGGREGATE(2,6,A1:A2) (errors ignored) 1 1 1 (issue: "agree when option 6 excludes errors")
E13 =AGGREGATE(3,6,A1:A2) 1 1
E14 =SUBTOTAL(2,C1:C4) (number, text, error, blank) #N/A 1
E15 =SUBTOTAL(3,C1:C4) #N/A 3
E16 =SUBTOTAL(1,A1:A2) (AVERAGE) #N/A #N/A

Both builds reopened their .xlsx with the same 16 values. gridcraft-cli check on the branch's file: 19 formula cells, 19 matched, round trip 0 of 1 files differ.

Through gridcraft-cli eval with a literal array, the same reducer gives:

Formula main this PR
=AGGREGATE(2,4,{10,#N/A}) #N/A 1
=AGGREGATE(3,4,{10,#N/A}) #N/A 2
=SUBTOTAL(9,{10,#N/A}) #N/A #N/A

Test

stat::tests::subtotal_counts_pass_over_or_count_errors covers:

  • codes 2, 3, 102 and 103 with #N/A and with #DIV/0!;
  • codes 1, 4, 5, 9, 12, 109 and AGGREGATE 14, which still return the error;
  • a mix of a number, text, a blank, an error and a logical: COUNT 1, COUNTA 4.

Negative control

With the error returned for every code again (on top of c742b7d6, then restored with git checkout), the test fails at its first assertion (SUBTOTAL 2 over 10 and #N/A).

Gate (macOS arm64)

Step Result
cargo fmt --all -- --check clean
cargo clippy -p gridcraft-functions -p gridcraft-calc -p gridcraft-engine --all-targets -- -D warnings clean
cargo test -p gridcraft-functions -p gridcraft-calc -p gridcraft-engine 184 + 130 (+3 ignored) + 987 + 120 + 7 passed, 0 failed

Not covered

  • Literal arrays: SUBTOTAL(2,{10,#N/A}) now gives 1 too, because it reaches the same reducer. The issue measured references. Excel's SUBTOTAL itself only takes references.
  • Excel: the Excel column is the issue's results; I didn't run Excel myself.
  • cargo xtask ci: I didn't run the full cargo xtask ci (wasm, layers, the other crates).

Summary by CodeRabbit

  • Bug Fixes
    • SUBTOTAL and AGGREGATE COUNT functions now skip error values, while COUNTA functions count them as nonempty.
    • Other aggregate functions continue to report errors.

…ells

The shared reducer returned the first error it met before choosing the function, so SUBTOTAL and AGGREGATE 2, 3, 102 and 103 gave #N/A for a range holding one. Excel's COUNT passes over the error and COUNTA counts it, as COUNT and COUNTA themselves do; the other functions still return it. Fixes storytold#4271.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

SUBTOTAL and AGGREGATE COUNT operations now skip referenced errors. COUNTA operations count referenced errors as nonempty cells. Other aggregate operations continue to propagate errors.

Changes: Aggregate count error handling

Layer / File(s) Summary
Count error handling and validation
crates/functions/src/stat/descriptive.rs, crates/functions/src/stat/tests.rs
aggregate_impl skips errors for COUNT codes 2 and 102, and counts errors for COUNTA codes 3 and 103. Tests verify these cases and confirm other aggregate operations propagate errors.

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

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: aiongg, harrison-booth

Merge Risk: Minimal · up to c742b

SUBTOTAL and AGGREGATE count functions now ignore referenced errors, and COUNTA variants count them, which matches the linked issue. Other aggregate functions still return errors. No merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the change: SUBTOTAL and AGGREGATE COUNT and COUNTA functions stop failing on error values in cells. The raw summary says aggregate_impl now skips errors for codes 2 and 3 (and 102 and…
Linked Issues check Passed Directly linked issue #4271 has coding requirements only. It asks that SUBTOTAL and AGGREGATE codes 2 and 3 (and SUBTOTAL 102 and 103) pass over referenced errors for COUNT and count them as nonempty …
Out of Scope Changes check Passed The whole-PR diff touches one function branch in descriptive.rs (+4/-1 lines) and adds one test in crates/functions/src/stat/tests.rs. Both changes implement #4271. The code comment cites #4271 and st…
Full details: Title check

Explanation

The title names the change: SUBTOTAL and AGGREGATE COUNT and COUNTA functions stop failing on error values in cells. The raw summary says aggregate_impl now skips errors for codes 2 and 3 (and 102 and 103) and counts them for COUNTA. The objectives describe the same behavior change, so the title matches the main change. It is specific and concise, and it does not cover the test additions, which is acceptable.

Full details: Linked Issues check

Explanation

Directly linked issue #4271 has coding requirements only. It asks that SUBTOTAL and AGGREGATE codes 2 and 3 (and SUBTOTAL 102 and 103) pass over referenced errors for COUNT and count them as nonempty for COUNTA. It also requires that other codes still propagate errors and that AGGREGATE option 6 still excludes errors. The diff to crates/functions/src/stat/descriptive.rs (merge base 19f07f3 to head c742b7d) adds let counting = matches!(code, 2 | 3); and changes the error arm to Value::Error(e) if !counting => return Err(*e). Because code maps 101–111 to 1–11, codes 102 and 103 are covered. Code 2 counts only Value::Number values. Code 3 counts non-empty values, and an error is non-empty, so it is counted. Other codes still return the error, and the option 6 path is not changed by the diff. The PR adds stat::tests::subtotal_counts_pass_over_or_count_errors, which the author reports passes, and the negative control reportedly fails. I did not run the tests. The PR reports the Excel values from the issue, and I did not verify them independently. The requirements are met by the code change.

Full details: Out of Scope Changes check

Explanation

The whole-PR diff touches one function branch in descriptive.rs (+4/-1 lines) and adds one test in crates/functions/src/stat/tests.rs. Both changes implement #4271. The code comment cites #4271 and states the intended behavior. The test covers the changed codes and the codes that must still propagate errors. No unrelated files or behavior changes appear in the summary or the inspected hunk. Literal-array inputs such as SUBTOTAL(2,{10,#N/A}) also change, because they reach the same reducer. The PR description discloses this. This effect follows from the shared reducer and does not add unrelated scope.

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 ef99f1e and c742b7d.

Files selected for processing: 2

  • crates/functions/src/stat/descriptive.rs
  • crates/functions/src/stat/tests.rs

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

emrahoksak pushed a commit to emrahoksak/girdcraft- that referenced this pull request Nov 18, 2026

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.

SUBTOTAL and AGGREGATE propagate errors when counting referenced cells

1 participant