Repository navigation
Put missing table sort values last in query and grouped views - #13485
Conversation
a1262d1 to
7105dfc
Compare
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| (reduce | ||
| (fn [order {:keys [get-value asc?]}] | ||
| (if (zero? order) | ||
| (compare-sort-values (get-value a) (get-value b) asc?) |
There was a problem hiding this comment.
🟡 Empty multi-value properties sort first
For ascending query or grouped sorts, by-sorting places empty multi-value properties before populated ones. get-property-value-for-search turns missing numbers into 0 and missing text into "", so neither sorts as nil.
Learn more
A query or grouped table passes entities through sort-entities, which obtains sort keys with get-value-for-sort. For a cardinality-many property, get-property-value-for-search reduces an absent numeric collection to zero or joins an absent text collection into an empty string. The new comparator only treats nil as missing. It therefore leaves absent multi-value properties ahead of populated properties in ascending order.
Example: A many-valued number property has no values on row A and #{2} on row B. Ascending query sorting compares 0 with 2 and returns A before B, rather than B before A.
Recommended fix: Make get-property-value-for-search return nil for absent or empty multi-value collections, while retaining the existing aggregate for populated collections. Test ascending query and grouped sorts for both numeric and text multi-value properties.
Was this helpful? React with 👍 or 👎 to provide feedback.
There was a problem hiding this comment.
Checked this against both revisions: the behavior is unchanged by this PR — it's pre-existing get-property-value-for-search semantics, not a regression from the comparator swap.
For a :db.cardinality/many property a row with no value produces (reduce + ()) → 0 (numbers) or (string/join ", " ()) → "" (text), so it sorts at that position rather than as nil. That was already true on master under compare: 0/"" sorted at the same position before this change too. The PR only changed how nil (absent single-value) keys order — it neither introduced nor worsened the empty-many case.
Whether a missing multi-value should even reach 0/"" is a design question in get-property-value-for-search — it's shared with the search path, so changing it would alter more than sorting (and "sum of an empty set = 0" is arguably the intended semantic for multi-number sorts). I'd keep it out of scope here; if you want missing many-cardinality values treated as nil-last too, that deserves its own issue since it touches the search contract.
There was a problem hiding this comment.
Fixed in 5e8815b — get-property-value-for-search now returns nil for empty many-cardinality properties (numbers and strings), so they tie on nil and sort last in both directions via compare-sort-values, matching the tag-table path. Covered by get-view-data-missing-many-cardinality-sort-value-stays-last-test on both the class-objects and query-result paths.
4029cd5 to
a1e604a
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The newly modified large-table AVET path lacks targeted regression coverage.
2 open findings
What changed in this PR
Aligns missing-value sorting across query, grouped, and tag/class-object tables.
Changes:
- Adds nil-last comparators for ascending and descending sorts.
- Preserves missing values in AVET and cardinality-many paths.
- Adds regression coverage for row, group, and multi-key sorting.
| File | Description |
|---|---|
deps/db/src/logseq/db/common/view.cljs |
Unifies missing-value sort semantics. |
deps/db/test/logseq/db/common/view_test.cljs |
Adds sorting regression tests. |
🧠 Review effort: Balanced
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
5e8815b to
b97a38d
Compare
|
Fixed in 088013b — |
|
Decided against retaining this: a >10k-row fixture was run during development to verify the branch (all rows retained, missing rows last in both directions), but the fixture cost outweighs its regression value for a threshold edge that only differs in performance path, not semantics. The shared |
Query tables and grouped tables currently put empty sort values first when ascending. Add failing coverage that matches tag/class-object tables, where missing values stay last in both directions. Co-authored-by: Tienson Qin <[email protected]>
Query and grouped tables compared sort keys with compare, so nil came first when ascending. Use the same nil-last comparator as tag tables for entity sorts in both directions. Co-authored-by: Tienson Qin <[email protected]>
Empty many-cardinality properties now yield nil instead of 0/"" so they stay last like single-cardinality misses. The >10000-datoms fast path no longer drops rows lacking the sort attribute; it appends them last in both directions, matching sort-eids-from-avet. Group headers also use the nil-last comparator, so the MAX_SAFE_INTEGER special case for journal-day groups is removed. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
9ac5121 to
5ff627a
Compare


Fixes logseq/db-test#1320
Summary
Rows missing the sort property now stay last in both directions for query and grouped tables, matching tag/class-object tables.
get-view-datasorted tag/class-object tables throughsort-eids-by-sorting→compare-sort-values(nil last both directions), but query results and grouped-table rows went throughsort-entities→by-one-sorting/common-util/by-sorting, which callcompare— puttingnilfirst when ascending.sort-entitiesnow uses a localby-sortingcomparator built oncompare-sort-valuesfor both the major and minor sort keys, so every row-level sort inget-view-datashares the same nil-last semantics.Tests
get-view-data-query-result-missing-custom-sort-value-stays-last-testget-view-data-grouped-missing-custom-sort-value-stays-last-testPre-fix nbb result for query ascending was
["undated" "dated"]. After the fix both query and grouped tables return dated rows first, then undated, for:asc? trueand:asc? false.Link to Devin session: https://app.devin.ai/sessions/2ddf0efaa7ce4f29b8fc8679617e9330
Open in Devin Desktop: https://app.devin.ai/desktop/session/2ddf0efaa7ce4f29b8fc8679617e9330?variant=devin
Requested by: @megayu