Repository navigation
feat: implement SORT dynamic array function (HF-69) - #1707
Conversation
✅ Deploy Preview for hyperformula-dev-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Task linked: HF-28 Modern dynamic array functions |
5648f53 to
05389bf
Compare
Performance comparison of head (f081be5) vs base (6e87a37) |
Add SORT(array, [sort_index], [sort_order], [by_col]) as a dynamic array
function, mirroring the SEQUENCE/FILTER machinery. Ordering reuses
ArithmeticHelper (mixed types, empties, collation) and is stable, so ties
keep input order. sort_order is validated strictly to {1,-1}; sort_index is
bounds-checked; errors in the input range propagate. Output shape equals the
input shape.
Includes i18n for all 17 language packs, docs (built-in-functions,
known-limitations), a changelog entry, and the shared ADR.
Source: https://app.clickup.com/t/86c89q1tt
ADR: docs/adr/2026-07-13-sort-unique-array-functions.md
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
05389bf to
2fe6a40
Compare
…HF-69) A whole-column reference to an empty sheet resolves to a zero-row range. The row/column sort paths then handed an empty 2-D array to SimpleRangeValue.onlyValues, which reads data[0].length and threw an uncaught TypeError. Guard the empty case up front and return #N/A (Empty range not allowed), matching UNIQUE and FILTER. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Without enableArrayArithmeticForArguments, an array expression passed as the first argument (e.g. =SORT(A1:A3*2)) is coerced as a scalar and only the first cell is computed. Matches FILTER/VSTACK/HSTACK so computed arrays can be sorted without an ARRAYFORMULA wrapper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 010aa8a. Configure here.
Excel's SORT places blank cells at the end of the result in both ascending and descending order, rather than coercing them to 0 and ordering by value. Force empties last (direction-independent) in the comparator before delegating to ArithmeticHelper. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
HF converts overflow/NaN to #NUM! during argument evaluation, so a non-finite sort_index never reaches the function body — the guard was dead code. Out-of-range indices are still handled by the <1 / >dimension checks. Removes the last uncovered lines in SortPlugin. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
There was a problem hiding this comment.
Do you think we should keep ADRs in repo? If so, I'd rather not have them in docs dir.
There was a problem hiding this comment.
Personally, I think we can keep some files describing the general architectural decisions, but ADR about a specific function implementation won't be useful in implementing future features so it should be removed. If there are any non-obvious decisions or implementation details, they should be noted in the code comments.
There was a problem hiding this comment.
Ok, let's keep it but move it to the hyperformula-tests repo please (dev_docs directory)
There was a problem hiding this comment.
Done ✅ Moved it to hyperformula-tests under dev_docs/2026-07-13-sort-unique-array-functions.md and dropped it from the engine repo here. Applied to both PRs — paired tests PRs: SORT handsontable/hyperformula-tests#24, UNIQUE handsontable/hyperformula-tests#25 (paired with #1708).
Per review on #1707: keep the ADR but relocate it out of the engine repo into hyperformula-tests/dev_docs, since a per-function implementation ADR belongs with the test suite rather than the shipped docs dir. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Per review on #1707: keep the ADR but relocate it out of the engine repo into hyperformula-tests/dev_docs, since a per-function implementation ADR belongs with the test suite rather than the shipped docs dir. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Describe the sort_order contract on its own terms instead of naming Excel in a published surface (JSDoc feeds the API docs). Internal implementation comments that reference Excel as the behavioural oracle are kept. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1707 +/- ##
==========================================
Coverage ? 97.18%
==========================================
Files ? 177
Lines ? 15546
Branches ? 3444
==========================================
Hits ? 15109
Misses ? 437
Partials ? 0
🚀 New features to boost your workflow:
|
## What & why Implements **UNIQUE** (`HF-68`, child of HF-28 "Modern dynamic array functions", sibling of the shipped SEQUENCE and of VSTACK/HSTACK). Adds `UNIQUE(array, [by_col], [exactly_once])` as a dynamic array function. Tests: handsontable/hyperformula-tests#25 (paired). Sibling PR: #1707 (SORT / HF-69). ADR: `docs/adr/2026-07-13-sort-unique-array-functions.md`. ## Behavior - Returns the **distinct rows** (or columns when `by_col` is `TRUE`) of the input, preserving first-occurrence order. - `by_col`: `FALSE` (default) compares rows; `TRUE` compares columns. - `exactly_once`: `TRUE` returns only rows/columns occurring exactly once; `FALSE` (default) returns all distinct. - Equality reuses `ArithmeticHelper.eq` → **case-insensitive by default** (honors `caseSensitive`), matching Excel's UNIQUE. - Result size is data-dependent; mirrors FILTER (predict input size as upper bound, return the smaller actual result). ## Design Mirrors the FILTER machinery for dynamic-size results: `sizeOfResultArrayMethod` + `vectorizationForbidden: true`, runtime via `runFunction`, parse-time size method returning a **fresh** `ArraySize` (drops the input's `isRef` flag). Deduplication is O(n²) in the number of vectors because locale-aware equality is not trivially hashable — noted in code; acceptable for v1. ## Notes — divergences from Excel (surfaced here + inline + in tests) - **Empty result** (only via `exactly_once` when nothing occurs exactly once) → `#N/A`. Excel returns `#CALC!`, which HyperFormula has no type for; mirrors FILTER's empty-result mapping (ADR `dec_8`). - Comparison honors HF's collation config rather than a byte-for-byte Excel oracle (no live Excel in this environment; ADR `con_1`). - In-range errors propagate (first error found; ADR `dec_7`). ## Definition of Done - [x] Production code (`UniquePlugin.ts`, registered via `plugin/index.ts`) - [x] i18n — all 17 language packs (authoritative MS Functions Translator names; enUS inherits enGB) - [x] Tests (paired tests PR) — across the standard array-function groups, dual-env safe - [x] Docs — `built-in-functions.md`, `known-limitations.md` - [x] JSDoc on all methods - [x] CHANGELOG entry - [x] ADR with audit-verified citations Source: https://app.clickup.com/t/86c89q1tq <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Additive array function behind existing dynamic-array machinery; no changes to auth, persistence, or core evaluation paths beyond new plugin registration. > > **Overview** > Adds the Excel-style **`UNIQUE(array, [ByCol], [ExactlyOnce])`** dynamic array function so formulas can return distinct rows or columns with first-occurrence order preserved. > > **`UniquePlugin`** implements deduplication via `ArithmeticHelper.eq` (honors `caseSensitive` / `accentSensitive`), optional column-wise mode and “exactly once” filtering, propagates the first in-range error, and returns **`#N/A`** when `ExactlyOnce` would yield an empty result (aligned with FILTER). Spill sizing follows FILTER: parse-time upper bound from input dimensions, `vectorizationForbidden: true`, and a fresh `ArraySize` so `isRef` is not carried through. > > Also registers the plugin, adds **`UNIQUE`** to all language packs, documents the function and known limitations, and records the change in the changelog. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit a4097a4. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: Kuba Sekowski <jakub.sekowski@handsontable.com>

What & why
Implements SORT (
HF-69, child of HF-28 "Modern dynamic array functions", sibling of the shipped SEQUENCE and of VSTACK/HSTACK). AddsSORT(array, [sort_index], [sort_order], [by_col])as a dynamic array function.Tests: handsontable/hyperformula-tests#24 (paired).
Sibling PR: #1708 (UNIQUE / HF-68).
ADR:
docs/adr/2026-07-13-sort-unique-array-functions.md.Behavior
sort_index(default 1): 1-based index into the sort dimension.sort_order:1ascending (default) or-1descending.by_col:FALSE(default) reorders rows;TRUEreorders columns.ArithmeticHelper(mixed types: numbers < text < logical; empties; locale collation viacaseSensitive/accentSensitive) and is stable — ties keep input order.Design
Mirrors the SEQUENCE/FILTER machinery:
sizeOfResultArrayMethod+vectorizationForbidden: true, runtime viarunFunctionreturningSimpleRangeValue/CellError, parse-time size method returning a freshArraySize(the input'sisRefflag is dropped — a ref-flagged size is treated as scalar and would collapse the spill).Notes — divergences from Excel (surfaced here + inline + in tests)
sort_orderis strictly{1, -1}; any other value →#VALUE!. Excel documents only{1,-1}; the reported "sort_order=0does not error" quirk is undocumented and could not be re-verified against live Excel in this environment, so the strict documented contract was chosen (see ADRdec_2,con_1). Flagged for live-Excel/Kuba confirmation.sort_index(e.g.{1,2}) is not supported in v1 (documented inknown-limitations.md; ADRdec_6).dec_7).Error-type map
sort_order ∉ {1,-1}→#VALUE!(BadMode) ·sort_index < 1→#VALUE!(LessThanOne) ·sort_index >dimension →#VALUE!(ValueLarge) · in-range error → propagate · wrong arity →#N/A.Definition of Done
SortPlugin.ts, registered viaplugin/index.ts)built-in-functions.md,known-limitations.mdSource: https://app.clickup.com/t/86c89q1tt
Note
Low Risk
Self-contained new array function following existing SEQUENCE/FILTER patterns; no changes to auth, persistence, or core recalculation beyond registering one plugin.
Overview
Adds the SORT dynamic array function:
SORT(Array, [SortIndex], [SortOrder], [ByCol])returns the input range reordered by row (default) or column, same dimensions as the source.Implementation lives in new
SortPlugin.ts, wired like other array functions (sizeOfResultArrayMethod,vectorizationForbidden, spill size copied from input without propagatingisRef). Sort keys useArithmeticHelper(with empty cells forced last); invalidsort_order(not1or-1), badsort_index, in-range errors, and empty ranges get the documented#VALUE!/#N/A/ error propagation behavior.Docs and changelog are updated; known-limitations documents single-key only, strict sort order, and HF comparison rules. SORT is added to all 17 language packs.
Reviewed by Cursor Bugbot for commit f081be5. Bugbot is set up for automated code reviews on this repo. Configure here.