Modern Apps git

← Modern-Apps.git

musicbrainz: partial dates must not beat precise ones

Commit
2b378524d02a8b0e4a77bd89c5e83ac6daf54fac
Author
vayun-mathur <[email protected]>
When
2026-08-20 23:45 UTC (3 months ago)
Parents
f824592

Diff

scripts/musicbrainz/mb_ingest/src/fixture.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/fixture.rs b/scripts/musicbrainz/mb_ingest/src/fixture.rsindex 71ac83f1ef..715c7d07a5 100644--- a/scripts/musicbrainz/mb_ingest/src/fixture.rs+++ b/scripts/musicbrainz/mb_ingest/src/fixture.rs@@ -176,12 +176,16 @@ pub fn write_fixture(dir: &Path) -> io::Result<()> {         ],     )?;     // release_country: release, country(area id), date y/m/d+    // Release 1 carries a precise date; release 2 (same release group) carries a+    // BARE YEAR in the same year. The group's derived first-release-date must be+    // the precise one -- comparing the packed values raw picks the bare year, which+    // is exactly the bug that lost 1973-03-24 for The Dark Side of the Moon.     table(dir, "release_country", &[&["1", "1", "1973", "3", "1"], &["3", "2", "1997", "9", "22"]])?;     // release_unknown_country: release, date y/m/d     table(         dir,         "release_unknown_country",-        &[&["2", "2011", N, N], &["4", "2001", "11", "5"]],+        &[&["2", "1973", N, N], &["4", "2001", "11", "5"]],     )?;      // medium: id, release, position, format, name, edits_pending, last_updated,
scripts/musicbrainz/mb_ingest/src/pack.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/pack.rs b/scripts/musicbrainz/mb_ingest/src/pack.rsindex 02286b3e45..4ebefd3878 100644--- a/scripts/musicbrainz/mb_ingest/src/pack.rs+++ b/scripts/musicbrainz/mb_ingest/src/pack.rs@@ -313,6 +313,28 @@ pub fn pack_date(year: Option<i64>, month: Option<i64>, day: Option<i64>) -> u32     (y << 9) | (m << 5) | d } +/// Ordering key for "which of these dates is earliest", with **unknown month and+/// day sorting LAST within their year**, and unknown dates sorting last overall.+///+/// The packed representation cannot be compared directly for this. `1973` packs+/// with month and day zero, so a raw `<` makes it earlier than `1973-03-24`, and+/// picking the minimum then loses the precise date. That is not a theoretical+/// concern: MusicBrainz gives *The Dark Side of the Moon* a first-release-date of+/// 1973-03-24, and deriving it with a raw comparison yielded a bare `1973` because+/// some other release of the group carries a year-only date. Verified against+/// musicbrainz.org/ws/2 for release group f5093c06-23e3-404f-aeaa-40f72885ee3a.+pub fn date_rank(packed: u32) -> u32 {+    if packed == 0 {+        return u32::MAX;+    }+    let y = packed >> 9;+    let m = (packed >> 5) & 0xf;+    let d = packed & 0x1f;+    let m = if m == 0 { 13 } else { m };+    let d = if d == 0 { 32 } else { d };+    (y << 11) | (m << 6) | d+}+ // --- MBIDs ---  /// Parse an MBID.@@ -1208,6 +1230,27 @@ mod tests {         assert_eq!(read(f.offset(huge_sym)), huge, "a string spanning blocks must survive");     } +    #[test]+    fn date_rank_sorts_partial_dates_last_within_their_year() {+        let y = pack_date(Some(1973), None, None);+        let ym = pack_date(Some(1973), Some(3), None);+        let ymd = pack_date(Some(1973), Some(3), Some(24));+        let earlier = pack_date(Some(1973), Some(1), Some(1));+        let unknown = 0u32;+        // The precise date must win the minimum against a bare year in the same+        // year -- the raw packed comparison gets this backwards and cost us the+        // real first-release-date of The Dark Side of the Moon.+        assert!(date_rank(ymd) < date_rank(y), "1973-03-24 must beat 1973");+        assert!(date_rank(ym) < date_rank(y), "1973-03 must beat 1973");+        assert!(date_rank(ymd) < date_rank(ym), "1973-03-24 must beat 1973-03");+        assert!(date_rank(earlier) < date_rank(ymd), "January still beats March");+        assert!(date_rank(y) < date_rank(unknown), "a known year beats no date");+        // Across years the ordinary ordering still holds.+        assert!(date_rank(pack_date(Some(1972), None, None)) < date_rank(ymd));+        // And the raw packed comparison really is wrong, so the test is meaningful.+        assert!(y < ymd, "raw packed order is the trap this function exists to avoid");+    }+     #[test]     fn dates_pack() {         assert_eq!(pack_date(None, None, None), 0);
scripts/musicbrainz/mb_ingest/tests/round_trip.rs
diff --git a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs b/scripts/musicbrainz/mb_ingest/tests/round_trip.rsindex 6e8693d191..24583da064 100644--- a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs+++ b/scripts/musicbrainz/mb_ingest/tests/round_trip.rs@@ -126,6 +126,11 @@ fn release_groups_and_discography() {     assert_eq!(&*g.primary_type, "Album");     assert_eq!(&*g.secondary_type, "");     assert_eq!(&*g.credit, "Pink Floyd");+    // Derived from the group's releases, because release_group_meta is not in the+    // core dump. One of this group's releases carries a BARE YEAR in the same year+    // (1973), and the precise date must still win -- comparing packed dates raw+    // picks the bare year, which is how the real Dark Side of the Moon lost its+    // 1973-03-24 and came out as "1973".     assert_eq!(g.first_release_date.to_ws2(), "1973-03-01");      let rg = pack.release_group_by_mbid(&mb(fixture::RG_ECHOES)).expect("echoes");@@ -191,7 +196,7 @@ fn releases_and_editions() {     let r2 = pack.release_by_mbid(&mb(fixture::REL_DARK_SIDE_BOOT)).expect("bootleg");     let boot = pack.release(r2).expect("release row");     assert_eq!(&*boot.status, "Bootleg");-    assert_eq!(boot.date.to_ws2(), "2011");+    assert_eq!(boot.date.to_ws2(), "1973");     assert_eq!(&*boot.country, "");     assert_eq!(&*boot.disambiguation, "vinyl bootleg");