Skip to content

fix(table): sort an attribute table column in one total order - #3536

Merged
jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:fix/3483-table-sort-order
Dec 1, 2026
Merged

jgregoriorincon merged 3 commits into
opengeos:mainfrom
jgregoriorincon:fix/3483-table-sort-order

Conversation

@jgregoriorincon

@jgregoriorincon jgregoriorincon commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Sorting an attribute table column used Value's partial_cmp, which isn't a total order: a non-finite float is compared as its shown text, so in a decimal column -inf came out between the negative and the positive numbers (-4.5, -0.5, -inf, 3.5, …), and a column mixing kinds could be ordered differently depending on how its rows came.

The table now sorts with value::cmp_values, the order a layout's table already uses (#3479). What changes besides the infinities:

  • in a column mixing numbers and text, text that reads as a number sorts with the text, after the numbers;
  • in a column mixing dates and text, text that reads as a date sorts with the text, after the dates;
  • texts differing only in case have a fixed order (A before a) instead of keeping row order.

cmp_values also now ties two equal dates without building their texts, so a column of repeated dates sorts as fast as before. The layout table and the Statistics group keys use it too; their order is unchanged.

The sort is in ui-egui's attribute table, for layers and standalone tables, on the desktop and in the browser. The command line, Python and share.exportTable don't use it, and the touch interface has no sort of its own. No command or project format change.

Testing

New unit tests on the table's row order (row_order_* in crates/ui-egui/src/table.rs): -inf both ways, inf, NaN and null, every permutation of a column mixing null, yes/no, whole, decimal, date and text values (ascending in each, descending once), and sorting with a filter or a selection. Three of the four tests fail without the change; the inf/NaN one pins an order main already gives.

Locally, fmt, clippy, doctests, the layer check and the schema check pass. The full test run had 11884 passing and 8 failing: 7 in rustgis-geolibre and line_strokes_editor_edits_one_class, which also fail here on main. After a last comment and test edit, the row_order tests and clippy on rustgis-ui-egui pass again.

Headless ui_shot captures of a GeoPackage whose decimal column holds -inf, before and after.

UI evidence

Ascending:

asc.png

Descending:

desc.png

Related issue

Closes #3483

Summary by CodeRabbit

  • Bug Fixes
    • Improved table sorting for mixed value types, numeric edge cases, and filtered or selected rows.
    • Corrected comparisons for dates and date-times representing the same instant.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change updates temporal comparison in cmp_values and uses that comparator for attribute-table row sorting. Tests cover equal date and date-time values, midnight ordering, non-finite floats, mixed value kinds, input permutations, and sorting after filtering or selection.

Changes: Value and Table Sorting

Layer / File(s) Summary
Temporal value comparison
crates/data/src/value.rs
cmp_values returns equal when date and date-time values have equal temporal keys and stored values. Tests cover identical values and date-before-date-time ordering at midnight.
Attribute-table row sorting
crates/ui-egui/src/table.rs
RowOrder::displayed uses cmp_values instead of Value::partial_cmp. Tests cover non-finite floats, mixed value kinds, input permutations, and sorting after filtering or selection.

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

Change: Bug fix · Severity of issue fixed: Low

Possibly related PRs

  • opengeos/RustGIS#3497: This PR also changes table sorting and includes a test that compares text ordering with cmp_values.

Suggested reviewers: mavrick-f, giswqs

Merge Risk: Minimal · up to 78b55

Attribute-table columns now sort in one consistent order, including infinities and mixed value kinds. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the change: attribute table column sorting moves to one total order. It matches the main change in crates/ui-egui/src/table.rs, which switches RowOrder::displayed to cmp_values, and th…
Description check Passed The description follows the template. It has a Summary that explains the change and why it is needed, and a Testing section that names the new row_order_* tests, local checks, and the failing tests th…
Linked Issues check Passed [#3483] requires one row order for an attribute-table column, using rustgis_data::value::cmp_values, instead of Value::partial_cmp. The reviewed diff of crates/ui-egui/src/table.rs (base to head…
Out of Scope Changes check Passed The table.rs change is limited to the attribute-table sort comparator and its unit tests. Both connect directly to [#3483]. The crates/data/src/value.rs change, as described in the raw summary, adds…
Full details: Title check

Explanation

The title names the change: attribute table column sorting moves to one total order. It matches the main change in crates/ui-egui/src/table.rs, which switches RowOrder::displayed to cmp_values, and the PR objectives describe the same change. The title follows the conventional commit format and is specific enough for a teammate scanning history.

Full details: Description check

Explanation

The description follows the template. It has a Summary that explains the change and why it is needed, and a Testing section that names the new row_order_* tests, local checks, and the failing tests that also fail on main. The UI evidence section has before and after screenshots for both ascending and descending sorts, which the template requires for visible UI changes. The Related issue section links issue #3483. The template's 'No visible UI change' line is not used, which is correct because this PR changes visible sorting.

Full details: Linked Issues check

Explanation

[#3483] requires one row order for an attribute-table column, using rustgis_data::value::cmp_values, instead of Value::partial_cmp. The reviewed diff of crates/ui-egui/src/table.rs (base to head) replaces the partial_cmp(...).unwrap_or(Equal) call in RowOrder::displayed with cmp_values(ds.value(*a, c), ds.value(*b, c)), and reverses the result for descending order. This matches the requested comparator. New tests in the same file cover negative and positive infinity, NaN, null, every permutation of a mixed column (null, yes/no, whole, decimal, date, text), descending order, and sorting with a filter or selection. The row_order_* tests call RowOrder::displayed, so they exercise the changed path. Limit: the diff of crates/data/src/value.rs could not be read, so the internal total-order behavior of cmp_values (including the new equal-date tie in the PR summary) is taken from the PR description and the issue, not from a direct read. The table requirement is met on the evidence available. No test run results were verified by this assessment.

Full details: Out of Scope Changes check

Explanation

The table.rs change is limited to the attribute-table sort comparator and its unit tests. Both connect directly to [#3483]. The crates/data/src/value.rs change, as described in the raw summary, adds an equal-date fast path inside cmp_values (ties equal dates without building text). The PR description states this supports the sort's performance after the switch to cmp_values, and that the layout table and Statistics group-key order are unchanged. This is a supporting change to the function the sort now calls, so it is connected to the issue. Limit: the value.rs diff could not be read, so the claim that other callers keep their order is not independently verified here. No unrelated change was found in the evidence available.

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

A rabbit checks the rows in place,
And sorts each value with care and grace.
Dates meet at midnight, side by side,
Mixed kinds now find an ordered ride.
I nibble greens and hop away!

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d1af9655-c0f1-4681-9dd6-86d186e7206f
Commits

Reviewing files that changed from the base of the PR and between 0a03240 and 78b55c8.

Files selected for processing: 2

  • crates/data/src/value.rs
  • crates/ui-egui/src/table.rs

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

Comment thread crates/ui-egui/src/table.rs Resolved
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

I reviewed the diff (crates/ui-egui/src/table.rs), the cmp_values / cmp_number / cmp_int_float helpers it calls in crates/data/src/value.rs, and the repository rules in pr-context/AGENTS.md. Prior review threads: none, so no earlier concerns were skipped.

Bugs

  • None found. I checked cmp_values for totality: ranks separate Null, numbers, dates and text; numbers compare exactly, with NaN normalised and above all values; text compares by lowercase then by bytes; date ties fall to text. No panics, and the as i64 cast in cmp_int_float is bounded by the range check. High confidence.
  • The new tests look right. The Heap's-algorithm permutation loop matches the standard iterative form, and the expected orders in the mixed-kind, infinity/NaN and filter/selection tests check out against cmp_values and Value::to_string.

Security

  • None found.

Performance

  • Date ties allocate on every comparison (table.rs:91, the cost sits in value.rs:433, outside the diff). Medium confidence on impact, high on the mechanism. Inline comment posted. The old partial_cmp path didn't allocate for date pairs, so a column of repeated dates now formats two strings per comparison on the UI thread.

Quality

  • The test row_order_of_mixed_kinds_does_not_depend_on_how_the_rows_came runs 8! = 40,320 full dataset builds and sorts. It is fine, but it is slow in debug builds. A comment on the cost, or a smaller set, would help.
  • row_order swallows None with unwrap_or_default(), so row_order(&[], …) passes whether or not the table returns None. Minor.
  • The #[test] helper relies on Debug output for comparisons. Acceptable here, but a comment would say why.

AGENTS.md

  • No breaks found. No new literals need translation, nothing is saved, and nothing is lossy. The PR's surface note is accurate: the sort lives only in ui-egui's RowOrder, and the layout table already uses cmp_values, so there is no second sort site to fix. Pre-existing and out of scope: the sort runs on the UI thread, as before.

Change size

Compared with the whole PR against main (merge base 0a03240 to PR head 1e0e782).

Measure Value
Files changed 1 (added 0, modified 1, removed 0, renamed 0)
Size before 132.2 KB
Size after 135.5 KB
Total size change +3.3 KB
Path Before After Change
crates/ui-egui/src/table.rs 132.2 KB 135.5 KB +3.3 KB

@jgregoriorincon

Copy link
Copy Markdown
Contributor Author

On the summary points, in 78b55c8:

  • row_order now unwraps, so a missing row order fails the test instead of reading as empty.
  • shown says why values are compared as debug text: NaN doesn't equal itself.
  • The permutation test stays as it is: it ran in 0.14 s in the full test run, and every row order is the property under test.
  • The sort running on the UI thread is unchanged by this pull request; a header click is not a command.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

Bugs: none found.

  • I checked cmp_values as a whole: rank, then value, then kind. It is a total order. Ranks split nulls, numbers, dates and text. cmp_number normalises NaN and −0 and compares whole numbers and decimals exactly, so -inf, inf and NaN now sort as the PR describes.
  • The new x == y shortcut in the date branch is sound. DateTime derives PartialEq over micros and offset, which are exactly what its display uses, so equal values always had equal text and the tie-break result is unchanged.
  • The table sort is stable and the comparator is total, so the cached RowOrder indices are deterministic.
  • Confidence: high.

Security: none found. No input handling, parsing or I/O changes.

Performance: no regression. The sort now compares text without parsing dates or allocating, which the old partial_cmp path did per comparison. The sort still runs on the UI thread, as it did before this PR, so this is not new. Confidence: high.

Quality:

  • Nit: the mixed-kind test in table.rs (row_order_of_mixed_kinds_does_not_depend_on_how_the_rows_came) runs all 8! = 40,320 permutations, each building a dataset and sorting it. That is slow for a unit test and hard to read. Checking a fixed set of orders (a reversed and a few rotated arrangements) would cover the same risk. Confidence: medium. Left out of inline comments since it is a grouped nit.
  • Note, not a defect: cmp_values now orders mixed columns differently from the Cell comparator in crates/engine/src/cmd/share/table.rs (export). The PR body says export is unchanged, so the table view and the export can disagree on mixed columns. Worth a follow-up. Confidence: high that it's true.

AGENTS.md: no breaks found.

  • No unwrap/expect in non-test code. The new tests use unwrap, as the module's other tests do.
  • No timer waits. The assert_eq! sides are not token-identical.
  • No silent data loss: sorting drops nothing.
  • No layer, command, schema, saved-format or wasm change, so the schema, command-surface and wasm rules don't apply.
  • The PR body states which surfaces it reaches.
  • Twins: none of the families named in the review brief (readers, overlay tools, add/open commands, wasm branches) changed. I also checked the other Value sorts. analysis/stats.rs and map/layout/elements.rs already use cmp_values. symbology.rs:1505 sorts f64s. query.rs min/max still uses Value's partial_cmp, but it is outside this diff and I did not raise it.

Earlier review threads: 1 concern left alone. It was the performance note on the date tie-break to_string. The author fixed it in 78b55c88, which is in this head, and the thread is resolved.

No inline comments were posted: nothing rose to the level of an inline finding.

Change size

Compared with main (merge base 0a03240 to PR head 78b55c8).

Measure Value
Files changed 2 (added 0, modified 2, removed 0, renamed 0)
Size before 180.9 KB
Size after 185.0 KB
Total size change +4.1 KB
Path Before After Change
crates/ui-egui/src/table.rs 132.2 KB 135.6 KB +3.4 KB
crates/data/src/value.rs 48.7 KB 49.4 KB +728 B

@jgregoriorincon

Copy link
Copy Markdown
Contributor Author

On the two summary notes (no code change):

  • Permutation test: kept, as answered above; it ran in 0.14 s in the full test run, and every row order is the property under test.
  • Export order: share.exportTable already ordered rows differently from the table before this pull request, by its own documented rule (nulls and non-finite numbers, which exports write as null, stay last in both directions). This pull request leaves the export as it is.

@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

📖 Docs preview

Item Value
Preview https://preview.rustgis.app/pr-3536/
Web app https://preview.rustgis.app/pr-3536/app/
Commit 78b55c8

GitHub Pages takes a minute or two to publish a new commit. Newer commits from the fork aren't published until a maintainer adds the preview label (once per commit).

@jgregoriorincon
jgregoriorincon marked this pull request as ready for review December 1, 2026 13:56
@github-actions

github-actions Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Code review

I found no bugs, security issues, or AGENTS.md breaks in the two changed files. I did not run the tests, since I have no shell here. I checked the code by reading it and the surrounding source.

Bugs

None found. I checked that cmp_values is a total order, which matters because Rust's sort_by can panic on an inconsistent comparator. Rank orders nulls, then numbers, dates and text. Dates compare by key, then by text only when the values differ. Text compares by lowercase, then exact. Numbers compare with total_cmp, with NaN canonicalised and -0 folded to 0, and kind breaks ties. The x == y shortcut is safe because equal values always produce equal text. Confidence: high.

Security

None found.

Performance

  • The earlier thread on repeated-date tie-breaks (crates/data/src/value.rs:433) is fixed in 78b55c8. The x == y shortcut avoids building texts for equal dates. Text-to-text comparisons also no longer allocate, because the old partial_cmp path called to_string().to_lowercase(). Confidence: high.
  • Date and date-time pairs at one instant with different text still allocate. This is rare. Confidence: high.
  • The sort still runs on the UI thread, as it did before this PR. The PR does not change that, so I am noting it for context only.

Quality

  • crates/data/src/value.rs:435: the tie-break is a dense one-line closure. A named helper or a match would read better. Nit, low priority.
  • The "repeated dates sort as fast" claim rests on the benchmark table in the thread, not on a test. A timing test would be flaky, so I would leave it as is. Nit.

AGENTS.md

None found. I checked the following:

  • Non-test code has no unwrap. The new test helpers use unwrap, as the existing tests in that module do.
  • No project format or command changes, so no schema or snapshot update is needed.
  • The new strings are none, so no tl! wrapping is needed.
  • The ui-egui change has no platform-specific code, so no wasm cfg is needed.

Twins checked

  • The layout table (crates/map/src/layout/elements.rs:566) and the Statistics group keys (crates/analysis/src/stats.rs:264) already use cmp_values, so they match.
  • crates/data/src/query.rs (lines 1662, 1677, 2416) still uses partial_cmp for filter comparisons. These are not sorts, and the PR does not touch them, so they are out of scope.
  • crates/engine/src/cmd/share/table.rs has its own Cell comparison, not Value. The PR says the export path doesn't sort this way, and the code agrees.

Prior reviews

One thread was already on the PR: the performance concern about the date tie-break. The author fixed it in 78b55c8, and the current code matches the fix. I left it alone and did not repeat it. No new inline comments were posted.

Change size

Compared: the whole PR against main (merge base 0a03240 to PR head 78b55c8).

Measure Value
Files changed 2 (0 added, 2 modified, 0 removed, 0 renamed)
Size before 180.9 KB
Size after 185.0 KB
Total size change +4.1 KB
Path Before After Change
crates/ui-egui/src/table.rs 132.2 KB 135.6 KB +3.4 KB
crates/data/src/value.rs 48.7 KB 49.4 KB +0.7 KB

@jgregoriorincon
jgregoriorincon added this pull request to the merge queue Dec 1, 2026
Merged via the queue into opengeos:main with commit 23a2d1d Dec 1, 2026
31 checks passed
@jgregoriorincon
jgregoriorincon deleted the fix/3483-table-sort-order branch December 1, 2026 14:40
@jgregoriorincon

Copy link
Copy Markdown
Contributor Author

On the two summary nits (no code change): the tie-break stays inline, as one expression with its reason in the comment above it; and no timing test is added, for the flakiness you name.

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.

[Bug]: Sorting an attribute table column with infinities or mixed values can order it wrongly

1 participant