Styxis
Company
Knowledge

Polars group extrema after reverse: compare membership as well as the maximum

Separate reordering from selecting fewer elements before deriving an independent expected extreme.

Reordering preserves an extreme when membership is unchanged

Changing order without changing a group's elements must preserve its extreme. Selecting only the highest-ranked elements changes the comparison set itself. Confusing these operations can approve an incorrect result or reject a legitimate subset result. [1, 2, 3]

Aggregation using stale sortedness information

Polars issue 30550 describes extreme-value paths trusting prior sortedness after reverse or sort_by reorders a group. It presents differing reverse-max and post-selection-min results. The source distinguishes execution paths and regression scope, so one grouped example does not establish failure of every aggregation. [1]

Record selected elements before aggregating

Compare group multisets and extremes before and after reversal. For a top-k subset, do not reuse the whole group's extreme as the expected result. Recomputing the reference through the same optimized path can reproduce the defect on both sides; use a small independent array or explicit value comparisons instead. [1, 3]

Reversing the same three elements

Reversing illustrative group [1,2,3] to [3,2,1] leaves expected maximum 3. A maximum of 3 in the reversed group preserves the extreme of the same members. A maximum of 1 treats another member as the extreme even though only order changed. First establishing preserved membership permits that expectation for a pure reorder. If elements were removed by a selection, derive the expected extreme from the selected set instead. [1, 3]

Supply independent results to Truthound

Truthound Core's ColumnPairValidator can compare an independently derived expected extreme with the observation for the same group. Prepare selection records and missing-value policy first. The validator does not repair sorted flags or the group executor, and a reference generated by the same defective path cannot substantiate a passing check. [1, 2, 3]

Application conditions

Preserve selected_members for each group_id as an independent array, linking expected_extreme and observed_extreme to that same selected scope.

Alternatives

Reconstruct group members as an independent array and calculate the extreme there.

Limitations

  • Calculating the reference with the same aggregation that trusts the stale sorted flag makes the comparison non-independent.

Evidence

Event checked
Subject checked
Modified