Skip to content

fix(query): sort FieldIndex via jsonb -> (#158)#159

Merged
jensens merged 1 commit into
mainfrom
fix/field-sort-numeric-jsonb
Apr 21, 2026
Merged

fix(query): sort FieldIndex via jsonb -> (#158)#159
jensens merged 1 commit into
mainfrom
fix/field-sort-numeric-jsonb

Conversation

@jensens
Copy link
Copy Markdown
Member

@jensens jensens commented Apr 21, 2026

Closes #158.

Summary

  • _process_sort now emits idx->'{key}' (jsonb operator) instead of idx->>'{key}' (text operator) for the FieldIndex fallback.
  • PG text-cast comparison is lexicographic, so "10" < "2". JSONB comparison is type-aware — numbers sort numerically, strings lexically.
  • Affects any FieldIndex with numeric source data (counters, priorities, prices, weights). Standard Plone columns (sortable_title, portal_type, …) are strings and sort the same as before.

Scope

Only the FieldIndex/fallback branch of _process_sort. DATE, BOOLEAN, GOPIP already cast explicitly and keep their current expressions. Dedicated path column unchanged.

Low-prio sort gaps uncovered during the audit (allowedRolesAndUsers ExtraIdxColumn TEXT[] no-op, effectiveRange, SearchableText) are collected in #157 — not in this PR.

Test plan

  • New unit test: test_sort_field_uses_jsonb_operator asserts the operator switch
  • New integration test: numeric FieldIndex with values [10, 2, 20, 3, 1] sorts 1, 2, 3, 10, 20 (would have been 1, 10, 2, 20, 3 before)
  • Existing sort unit tests updated to new expression
  • Full suite: 1453 passed, 23 skipped

🤖 Generated with Claude Code

#158)

Text operator `idx->>'{key}'` casts to TEXT before ORDER BY, so a
FieldIndex over a numeric attribute ranked "10" before "2". The jsonb
operator `idx->'{key}'` preserves type — numbers sort numerically,
strings lexically — so homogeneous FieldIndexes sort correctly
regardless of value type.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@jensens jensens merged commit d2da632 into main Apr 21, 2026
5 checks passed
@jensens jensens deleted the fix/field-sort-numeric-jsonb branch April 21, 2026 09:21
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.

Numeric FieldIndex sorts lexically ("10" < "2")

1 participant