Skip to content

Put missing table sort values last in query and grouped views - #13485

Merged
tiensonqin merged 5 commits into
masterfrom
logseq/query-grouped-empty-sort-last-7e3b
Oct 12, 2026
Merged

tiensonqin merged 5 commits into
masterfrom
logseq/query-grouped-empty-sort-last-7e3b

Conversation

@tiensonqin

@tiensonqin tiensonqin commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

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-data sorted tag/class-object tables through sort-eids-by-sorting → compare-sort-values (nil last both directions), but query results and grouped-table rows went through sort-entities → by-one-sorting/common-util/by-sorting, which call compare — putting nil first when ascending.

sort-entities now uses a local by-sorting comparator built on compare-sort-values for both the major and minor sort keys, so every row-level sort in get-view-data shares the same nil-last semantics.

Tests

  • get-view-data-query-result-missing-custom-sort-value-stays-last-test
  • get-view-data-grouped-missing-custom-sort-value-stays-last-test

Pre-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? true and :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

@devin-ai-integration
devin-ai-integration Bot force-pushed the logseq/query-grouped-empty-sort-last-7e3b branch from a1262d1 to 7105dfc Compare October 2, 2026 14:48
@devin-ai-integration
devin-ai-integration Bot marked this pull request as ready for review October 2, 2026 14:58

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

(reduce
(fn [order {:keys [get-value asc?]}]
(if (zero? order)
(compare-sort-values (get-value a) (get-value b) asc?)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@megayu
megayu force-pushed the logseq/query-grouped-empty-sort-last-7e3b branch 2 times, most recently from 4029cd5 to a1e604a Compare October 12, 2026 02:46
@megayu
megayu requested a balanced review from Copilot October 12, 2026 02:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Comment thread deps/db/src/logseq/db/common/view.cljs Resolved
@megayu
megayu force-pushed the logseq/query-grouped-empty-sort-last-7e3b branch from 5e8815b to b97a38d Compare October 12, 2026 04:05
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Fixed in 088013b — find-title-id removed, all call sites now use the existing title-id helper.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

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 by-sorting comparator is covered by the multi-key and many-cardinality tests.

cursoragent and others added 5 commits October 12, 2026 21:18
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>
@tiensonqin
tiensonqin force-pushed the logseq/query-grouped-empty-sort-last-7e3b branch from 9ac5121 to 5ff627a Compare October 12, 2026 13:18
@tiensonqin
tiensonqin merged commit 5a68deb into master Oct 12, 2026
@tiensonqin
tiensonqin deleted the logseq/query-grouped-empty-sort-last-7e3b branch October 12, 2026 13:18
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.

Query and grouped tables sort empty values first ascending; tag tables sort them last

4 participants