Skip to content

LARGE, SMALL: read the rank before the data, so the rank's error wins - #4341

Open
Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/large-small-rank-error-first
Open

Hamza0101 wants to merge 1 commit into
storytold:mainfrom
Hamza0101:fix/large-small-rank-error-first

Conversation

@Hamza0101

@Hamza0101 Hamza0101 commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4278

What was wrong

large and small in crates/functions/src/stat/descriptive.rs read the data (arg_nums(a, 0)?) before the rank (num(a, 1)?). When both held an error, the data's error came back first. Native Excel returns the rank's error, as the issue shows in both orders.

What changed

LARGE and SMALL read the rank first, then the data. The ranking itself doesn't change. AGGREGATE 14/15 has its own argument handling (#4277) and isn't touched.

QA report

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

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

Setup:

  • A1:A4 = 10, 20, =NA(), 40
  • C1:C4 = 10, 20, =1/0, 40 (the issue's swap)
  • D1:D4 = 10, 20, 30, 40
  • E1 = =1/0, E2 = =NA(), E3 = 2, E4 = ="x"
Cell Formula main this PR Excel (issue)
G1 =LARGE(A1:A4,E1) #N/A #DIV/0! #DIV/0!
G2 =SMALL(A1:A4,E1) #N/A #DIV/0! #DIV/0!
G3 =LARGE(C1:C4,E2) (swapped) #DIV/0! #N/A #N/A
G4 =SMALL(C1:C4,E2) (swapped) #DIV/0! #N/A #N/A
G5 =LARGE(A1:A4,E3) (only the data error) #N/A #N/A #N/A
G6 =SMALL(D1:D4,E1) (only the rank error) #DIV/0! #DIV/0! #DIV/0!
G7 =LARGE(D1:D4,E3) (no error) 30 30
G8 =SMALL(A1:A4,E4) (text rank, data error) #N/A #VALUE! not measured
G9 =AGGREGATE(14,6,A1:A4,E3) (untouched) 20 20

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

Test

stat::tests::large_and_small_return_the_rank_error_before_a_data_error checks both functions in both orders, with only a data error, with only a rank error, and with no error.

Negative control

With descriptive.rs from origin/main and the new test kept (on top of 69a69ab0, then restored with git checkout), the test fails: left Error(NA), right Error(Div0).

Gate (macOS arm64)

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

Not covered

Summary by CodeRabbit

  • Bug Fixes
    • LARGE and SMALL now report an invalid rank before an error in the data argument when both inputs are invalid. When only one input is invalid, its error is reported as before.

Excel returns the rank's error when both the data and the rank hold one. Fixes storytold#4278.
@coderabbitai

coderabbitai Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

LARGE and SMALL now evaluate the rank argument before the data argument. Tests verify which error is returned when one or both arguments contain errors.

Changes: LARGE and SMALL error precedence

Layer / File(s) Summary
Argument evaluation and validation
crates/functions/src/stat/descriptive.rs, crates/functions/src/stat/tests.rs
LARGE and SMALL evaluate and convert the rank before reading the data. Tests cover error precedence when either or both arguments contain errors, plus successful LARGE evaluation with rank 1.

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

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: aiongg

Merge Risk: Minimal · up to 69a69

LARGE and SMALL now return the rank's error when both arguments contain errors, matching the behavior the linked issue requests. Ranking behavior is otherwise unchanged and the added test covers the error cases. 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 states the main change: LARGE and SMALL now read the rank argument before the data argument, so a rank error takes precedence. The raw summary and PR objectives both describe this exact beha…
Linked Issues check Passed Issue #4278 requires LARGE and SMALL to return the rank's error when the data and rank hold different errors. The diff at head (crates/functions/src/stat/descriptive.rs) reads the rank with num(a, 1)?…
Out of Scope Changes check Passed The code diff touches only the large and small functions in descriptive.rs, plus a comment that cites #4278. The test named in the PR description is in stat/tests.rs and checks the same functions. No …
Full details: Title check

Explanation

The title states the main change: LARGE and SMALL now read the rank argument before the data argument, so a rank error takes precedence. The raw summary and PR objectives both describe this exact behavior change, and the title names the functions involved. The title is short, specific, and accurate.

Full details: Linked Issues check

Explanation

Issue #4278 requires LARGE and SMALL to return the rank's error when the data and rank hold different errors. The diff at head (crates/functions/src/stat/descriptive.rs) reads the rank with num(a, 1)? into k before reading the data with arg_nums(a, 0)?, in both large and small. The data error therefore no longer takes precedence. When only one argument holds an error, that error is still returned, because the same error propagation path runs. The PR description reports a unit test, large_and_small_return_the_rank_error_before_a_data_error, that covers both orders, a data-only error, a rank-only error, and the no-error case, and reports that it fails on origin/main. I did not open the test file. The end-to-end table in the PR description matches the issue's expected results for the issue's cases. Numeric-rank behavior (#4275) and AGGREGATE (#4277) are not changed, which matches the issue's scope.

Full details: Out of Scope Changes check

Explanation

The code diff touches only the large and small functions in descriptive.rs, plus a comment that cites #4278. The test named in the PR description is in stat/tests.rs and checks the same functions. No unrelated files or functions are changed in the diff I read. One behavior change is outside the issue's two error-value examples: a text rank with a data error (G8, =SMALL(A1:A4,E4)) now returns #VALUE! instead of #N/A. This follows from the same rank-first evaluation order and is not a separate change. The PR description states this, so it is disclosed, not hidden. AGGREGATE is untouched.

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 19f07f3 and 69a69ab.

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; 4 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.

LARGE and SMALL choose the data error when the rank also contains a different error

1 participant