mirror of
https://github.com/navidrome/navidrome.git
synced 2026-10-21 00:37:09 +02:00
* feat: add optional natural sort order for names and titles Album, artist, song and playlist lists sort with a plain text comparison, so names containing numbers come out as "Foo 1, Foo 10, Foo 2" instead of "Foo 1, Foo 2, Foo 10" (issue #4554). Adds an EnableNaturalSorting option, default off, that switches those sorts to a NATSORT collation registered on every connection and backed by natural.CompareFold. natural.Compare gained an ASCII case-folding variant because it replaces 'collate nocase': sort_* columns hold raw tag values, so without folding they would order uppercase before lowercase. Applying the collation only inside mapSortOrder would have missed the default configuration entirely, since that mapper runs only when PreferSortTags is on. setSortMappings now also rewrites the order_* columns when natural sorting is enabled on its own. Sorts over plain text columns that are not order_* columns (playlist.name, album.name, media_file.title, playlist_tracks title) are wrapped explicitly, and qualified with their table because 'user' is joined and also has a 'name' column. The option defaults to off because the collation cannot use the existing indexes: measured on a synthetic 110k album library, the first page of an album-by-name listing goes from 0.03ms to 14ms. Indexing the expression was rejected outright - an index declared with a custom collation makes the whole database unreadable to any tool that does not register it, including the sqlite3 CLI, which fails even on 'select count(*)' and 'pragma integrity_check'. * refactor: fold the two sort-order mappers into one mapSortOrder and mapNaturalOrder shared the same regex and loop, differing only in the expression they substituted, and setSortMappings picked between them with a two-case switch. mapSortOrder now selects the column shape itself and defers to collatedSort for the collation, so the 'collate' clause is emitted in one place and the caller only has to decide whether any mapping is needed at all. The mapper tests were three near-identical cases that each hard-coded one flag combination; they are now a DescribeTable covering all four combinations of PreferSortTags and EnableNaturalSorting, which the previous set did not. The album sorting specs collapse the same way. Behavior is unchanged. * fix: leave plain sort columns alone when natural sorting is off collatedSort wrapped its column unconditionally, so the tiebreakers added for plain text columns picked up 'collate nocase' even with EnableNaturalSorting off. media_file.title, the playlist_tracks alias of it, and user.user_name are all declared without a collation, so a default install would have silently switched those tiebreaks from binary to case-insensitive ordering. Only playlist.name was already NOCASE and genuinely unaffected. The helper is now naturalSort and returns the column untouched unless the option is on, so the default path keeps the collation each column was declared with. sortCollation had a single remaining caller and folded into mapSortOrder. Tests: the CompareFold table body was a verbatim copy of the Compare one, so both now go through one expectOrder helper, and the album sorting specs inline two single-use closures. * fix(natural): defer the leading-zero tie-break to keep ordering transitive Compare applied the padding difference between numerically equal digit runs only when one side ended at the digit boundary, and ignored it mid-string. That made the relation intransitive: CompareFold("1","1a") < 0 and CompareFold("1a","01a") == 0, yet CompareFold("1","01a") > 0. SQLite requires a collating function to be transitive and leaves ORDER BY undefined otherwise, so registering this as NATSORT was not safe. Reproduced with the real driver on three artist names that occur in practice - "3", "3 doors down" and "03 greedo" - where paging one row at a time returned "03 greedo" twice and dropped "3" entirely. The padding difference is now carried as a tie-break that is applied only when the strings are otherwise equal, which restores transitivity while keeping the documented intent (a01 < a1, a0 < a00). Three existing entries changed: each asserted that two distinct strings compare equal, which was the same defect seen from the other side. Found by the Codex review on #6015.
194 lines
6.5 KiB
Go
194 lines
6.5 KiB
Go
package natural_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/navidrome/navidrome/utils/natural"
|
|
. "github.com/onsi/ginkgo/v2"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
func TestNatural(t *testing.T) {
|
|
RegisterFailHandler(Fail)
|
|
RunSpecs(t, "Natural Suite")
|
|
}
|
|
|
|
// expectOrder asserts the sign of cmp(a, b) matches expected.
|
|
func expectOrder(cmp func(string, string) int, a, b string, expected int) {
|
|
result := cmp(a, b)
|
|
switch {
|
|
case expected < 0:
|
|
ExpectWithOffset(1, result).To(BeNumerically("<", 0), "expected %q < %q", a, b)
|
|
case expected > 0:
|
|
ExpectWithOffset(1, result).To(BeNumerically(">", 0), "expected %q > %q", a, b)
|
|
default:
|
|
ExpectWithOffset(1, result).To(Equal(0), "expected %q == %q", a, b)
|
|
}
|
|
}
|
|
|
|
var _ = Describe("Compare", func() {
|
|
DescribeTable("returns correct ordering",
|
|
func(a, b string, expected int) {
|
|
expectOrder(natural.Compare, a, b, expected)
|
|
},
|
|
// Basic string ordering
|
|
Entry("a < b", "a", "b", -1),
|
|
Entry("b > a", "b", "a", 1),
|
|
Entry("a < aa (prefix)", "a", "aa", -1),
|
|
Entry("aa > a", "aa", "a", 1),
|
|
|
|
// Equal strings
|
|
Entry("equal strings return 0", "abc", "abc", 0),
|
|
Entry("both empty", "", "", 0),
|
|
Entry("a01 == a01", "a01", "a01", 0),
|
|
Entry("a1 == a1", "a1", "a1", 0),
|
|
|
|
// Empty string edge cases
|
|
Entry("empty < non-empty", "", "a", -1),
|
|
Entry("non-empty > empty", "a", "", 1),
|
|
|
|
// Numeric comparison
|
|
Entry("2 < 10 numerically", "2", "10", -1),
|
|
Entry("10 > 2 numerically", "10", "2", 1),
|
|
Entry("equal numbers", "42", "42", 0),
|
|
Entry("9 < 10", "9", "10", -1),
|
|
Entry("99 < 100", "99", "100", -1),
|
|
|
|
// Simple numeric segments (from original library)
|
|
Entry("a0 < a1", "a0", "a1", -1),
|
|
Entry("a0 < a00", "a0", "a00", -1),
|
|
Entry("a00 < a01", "a00", "a01", -1),
|
|
Entry("a01 < a1", "a01", "a1", -1),
|
|
Entry("a01 < a2", "a01", "a2", -1),
|
|
Entry("a01x < a2x", "a01x", "a2x", -1),
|
|
Entry("a01 > a00", "a01", "a00", 1),
|
|
Entry("a2 > a01", "a2", "a01", 1),
|
|
Entry("a2x > a01x", "a2x", "a01x", 1),
|
|
|
|
// Multiple numeric groups (from original library)
|
|
Entry("a0b00 < a00b1", "a0b00", "a00b1", -1),
|
|
Entry("a0b00 < a00b01", "a0b00", "a00b01", -1),
|
|
Entry("a00b0 < a0b00", "a00b0", "a0b00", -1),
|
|
Entry("a00b00 < a0b01", "a00b00", "a0b01", -1),
|
|
Entry("a00b00 < a0b1", "a00b00", "a0b1", -1),
|
|
Entry("a00b00 > a0b0", "a00b00", "a0b0", 1),
|
|
Entry("a00b01 > a0b00", "a00b01", "a0b00", 1),
|
|
// Distinct strings must not compare equal: the padding difference in the first
|
|
// run decides once everything else matches.
|
|
Entry("a00b00 > a0b00", "a00b00", "a0b00", 1),
|
|
|
|
// Leading zeros at end of string — lexical tie-break
|
|
Entry("file01 < file1", "file01", "file1", -1),
|
|
|
|
// Prefix comparison
|
|
Entry("abc < abcd", "abc", "abcd", -1),
|
|
Entry("abcd > abc", "abcd", "abc", 1),
|
|
|
|
// Navidrome use cases: cover art sorting
|
|
Entry("cover < cover.1", "cover", "cover.1", -1),
|
|
Entry("cover.1 < cover.2", "cover.1", "cover.2", -1),
|
|
Entry("cover.2 < cover.10", "cover.2", "cover.10", -1),
|
|
|
|
// Navidrome use cases: disc sorting
|
|
Entry("disc1 < disc2", "disc1", "disc2", -1),
|
|
Entry("disc2 < disc10", "disc2", "disc10", -1),
|
|
Entry("disc1 < disc10", "disc1", "disc10", -1),
|
|
|
|
// Multiple numeric segments
|
|
Entry("a1b2 < a1b10", "a1b2", "a1b10", -1),
|
|
Entry("a2b1 > a1b2", "a2b1", "a1b2", 1),
|
|
|
|
// Numbers at the start
|
|
Entry("2abc < 10abc", "2abc", "10abc", -1),
|
|
|
|
// Numbers larger than uint64 max (from original library)
|
|
Entry("large: fewer digits < more digits",
|
|
"a99999999999999999999", "a100000000000000000000", -1),
|
|
Entry("large: digit-by-digit comparison",
|
|
"a123456789012345678901234567890", "a123456789012345678901234567891", -1),
|
|
Entry("large: more digits > fewer digits",
|
|
"a999999999999999999999", "a1000000000000000000000", -1),
|
|
Entry("large: 20 digits < 100 digits by length",
|
|
"a20000000000000000000", "a100000000000000000000", -1),
|
|
Entry("large: 100 digits > 20 digits",
|
|
"a100000000000000000000", "a20000000000000000000", 1),
|
|
Entry("large: reverse of above",
|
|
"a1000000000000000000000", "a999999999999999999999", 1),
|
|
Entry("large: equal",
|
|
"a100000000000000000000", "a100000000000000000000", 0),
|
|
Entry("large: leading zeros with trailing data",
|
|
"a00000000000000000000001x", "a1x", -1),
|
|
Entry("large: leading zeros with trailing data (2)",
|
|
"a099999999999999999999x", "a99999999999999999999x", -1),
|
|
)
|
|
})
|
|
|
|
var _ = Describe("CompareFold", func() {
|
|
DescribeTable("orders case-insensitively",
|
|
func(a, b string, expected int) {
|
|
expectOrder(natural.CompareFold, a, b, expected)
|
|
},
|
|
Entry("numbers compare numerically", "foo 2", "foo 10", -1),
|
|
Entry("numbers compare numerically, reversed", "foo 10", "foo 2", 1),
|
|
Entry("case is ignored", "apple 2", "Banana 10", -1),
|
|
Entry("case is ignored, reversed", "Banana 10", "apple 2", 1),
|
|
Entry("same word, different case, is equal", "ABC", "abc", 0),
|
|
Entry("case ignored while comparing numbers", "Vol 2", "vol 10", -1),
|
|
Entry("uppercase digits boundary", "Track9", "track10", -1),
|
|
Entry("empty vs empty", "", "", 0),
|
|
Entry("empty sorts first", "", "a", -1),
|
|
Entry("non-ASCII is left untouched", "café 2", "café 10", -1),
|
|
)
|
|
|
|
// SQLite requires a collating function to be transitive; if it is not, the behavior of
|
|
// ORDER BY is undefined and paginated queries can drop or duplicate rows.
|
|
It("is transitive, as a SQLite collation requires", func() {
|
|
var corpus []string
|
|
var build func(prefix string, depth int)
|
|
build = func(prefix string, depth int) {
|
|
if prefix != "" {
|
|
corpus = append(corpus, prefix)
|
|
}
|
|
if depth == 0 {
|
|
return
|
|
}
|
|
for _, c := range []string{"0", "1", "a"} {
|
|
build(prefix+c, depth-1)
|
|
}
|
|
}
|
|
build("", 3)
|
|
|
|
sign := func(n int) int {
|
|
switch {
|
|
case n < 0:
|
|
return -1
|
|
case n > 0:
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
for _, a := range corpus {
|
|
for _, b := range corpus {
|
|
ab := sign(natural.CompareFold(a, b))
|
|
for _, c := range corpus {
|
|
bc := sign(natural.CompareFold(b, c))
|
|
ac := sign(natural.CompareFold(a, c))
|
|
if ab == 0 && bc == 0 {
|
|
Expect(ac).To(Equal(0), "%q==%q and %q==%q but %q vs %q is %d", a, b, b, c, a, c, ac)
|
|
}
|
|
if ab < 0 && bc < 0 {
|
|
Expect(ac).To(BeNumerically("<", 0), "%q<%q<%q but %q vs %q is %d", a, b, c, a, c, ac)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
|
|
It("matches Compare when both sides are already lowercase", func() {
|
|
pairs := [][2]string{{"foo 2", "foo 10"}, {"a01", "a1"}, {"a", "aa"}, {"vol 3", "vol 3"}}
|
|
for _, p := range pairs {
|
|
Expect(natural.CompareFold(p[0], p[1])).To(Equal(natural.Compare(p[0], p[1])),
|
|
"CompareFold(%q,%q) should match Compare", p[0], p[1])
|
|
}
|
|
})
|
|
})
|