From 1c2cca25672a02ddc7873e5c9a3d0a4de6c506bb Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 23 Oct 2026 11:23:54 +0530 Subject: [PATCH 1/2] docs(storage): state where encode_value puts NaN (#688) encode_value writes every NaN as 0x7FF8_0000_0000_0000 without the sign transform, so NaN sorts among the negative subnormals and is byte-equal to f64::from_bits(0x8007_FFFF_FFFF_FFFF). The bytes are v7 on-disk AVET keys, so the encoding stays; the doc comment now says so, the old test comment claiming NaN sorts last is gone, and a test pins the position. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0169pNQsLcCEn48te9X9WuZW --- CLAUDE.md | 2 +- docs/TEST_COVERAGE.md | 2 +- src/storage/index.rs | 37 ++++++++++++++++++++++++++++++++++--- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index 98040d9cf7..93308b8756 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -179,7 +179,7 @@ Auto-migrates v1/v2/v3/v4/v5/v6 → v7 on open/checkpoint. ## Test Coverage -**1325 tests** (1316 passing, 9 ignored; unit + integration + doc). +**1326 tests** (1317 passing, 9 ignored; unit + integration + doc). See `docs/TEST_COVERAGE.md` for the full per-file breakdown. **Testing conventions** — see the Testing Conventions section below before writing any tests. diff --git a/docs/TEST_COVERAGE.md b/docs/TEST_COVERAGE.md index e54b262fc8..bb77cc7002 100644 --- a/docs/TEST_COVERAGE.md +++ b/docs/TEST_COVERAGE.md @@ -4,7 +4,7 @@ **Verified**: 2026-10-23 with `cargo test` -**Result**: 1,316 passing tests and 9 ignored tests (1,325 total), including the golden-file corpus (#391) +**Result**: 1,317 passing tests and 9 ignored tests (1,326 total), including the golden-file corpus (#391) For v2.0.5, `tests/edge_cases_test.rs` checks that a file of 1 to 4,095 bytes is `STG-045`, unchanged and with no WAL, when it is a database cut below one page or foreign bytes, and opens as a new database that takes a write and reads it back after reopen when it is a prefix of the initial header or all zeros (#506). Rule-body tests in `tests/recursive_rules_test.rs` check that a `:db/valid-at` pattern in a rule definition, in its `not` and `not-join` bodies and in its `or` branches binds the query's valid time, on the semi-naive, stratified and magic-sets paths (#617). diff --git a/src/storage/index.rs b/src/storage/index.rs index 5d0b692847..0e97a89f1a 100644 --- a/src/storage/index.rs +++ b/src/storage/index.rs @@ -47,6 +47,16 @@ pub struct FactRef { /// /// Within each type, big-endian layout ensures byte-wise comparison matches /// the natural order of the type. +/// +/// Except for NaN (#688). Every NaN encodes to `0x03` followed by +/// `0x7FF8_0000_0000_0000`, without the sign transform the other floats get. +/// Those are also the bytes of the negative subnormal +/// `f64::from_bits(0x8007_FFFF_FFFF_FFFF)` (about `-1.1125e-308`), so NaN sorts +/// among the negative subnormals, before `-0.0` and every positive float, and +/// is byte-equal to that one value. `Value::cmp` instead puts NaN after `+∞`. +/// The encoding is the `value_bytes` of on-disk v7 AVET keys, so it stays as it +/// is: don't build a range scan by value on these bytes that assumes NaN sorts +/// last, or an equality that must tell NaN from that subnormal. pub fn encode_value(v: &Value) -> Vec { match v { Value::Null => vec![0x00], @@ -65,7 +75,8 @@ pub fn encode_value(v: &Value) -> Vec { let mut bytes = Vec::with_capacity(9); bytes.push(0x03); let bits = if f.is_nan() { - // Canonicalize all NaN to a single bit pattern (quiet NaN, positive) + // Canonicalize all NaN to a single bit pattern. It is not + // sign-transformed, so NaN sorts among the negative subnormals. 0x7FF8_0000_0000_0000u64 } else { let raw = f.to_bits(); @@ -560,11 +571,31 @@ mod tests { let nan2 = encode_value(&Value::Float(f64::NAN)); // All NaN values produce the same bytes assert_eq!(nan1, nan2); - // NaN sorts above all positive finite values (it uses quiet NaN bit pattern) - // Just verify it doesn't panic and produces a fixed-length result assert_eq!(nan1.len(), 9); } + /// The NaN position `encode_value`'s doc comment states (#688). These are + /// v7 on-disk bytes, so this pins them as they are rather than as they + /// should sort. + #[test] + fn test_encode_value_nan_sorts_among_negative_subnormals() { + let nan = encode_value(&Value::Float(f64::NAN)); + assert_eq!(nan, [0x03, 0x7F, 0xF8, 0, 0, 0, 0, 0, 0]); + let twin = encode_value(&Value::Float(f64::from_bits(0x8007_FFFF_FFFF_FFFF))); + assert_eq!(nan, twin, "NaN is byte-equal to one negative subnormal"); + let below = encode_value(&Value::Float(f64::from_bits(0x8008_0000_0000_0000))); + let above = encode_value(&Value::Float(f64::from_bits(0x8007_FFFF_FFFF_FFFE))); + assert!( + below < nan && nan < above, + "NaN sits among negative subnormals" + ); + assert!(nan < encode_value(&Value::Float(-0.0)), "NaN < -0.0"); + assert!( + nan < encode_value(&Value::Float(f64::INFINITY)), + "NaN < +inf" + ); + } + /// #445 widened `slot_index` from `u16` to `u32`. postcard must still write /// and read exactly the bytes a `u16` slot produced, so v7 index pages are /// unchanged on disk. From 1f07cd46831c1c491ff3a6b6c5c9ab7039c278b1 Mon Sep 17 00:00:00 2001 From: Aditya Mukhopadhyay Date: Fri, 23 Oct 2026 11:58:36 +0530 Subject: [PATCH 2/2] test(storage): split the NaN position assert so each branch runs (#688) Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0169pNQsLcCEn48te9X9WuZW --- src/storage/index.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/storage/index.rs b/src/storage/index.rs index 0e97a89f1a..2e0687e03f 100644 --- a/src/storage/index.rs +++ b/src/storage/index.rs @@ -585,10 +585,8 @@ mod tests { assert_eq!(nan, twin, "NaN is byte-equal to one negative subnormal"); let below = encode_value(&Value::Float(f64::from_bits(0x8008_0000_0000_0000))); let above = encode_value(&Value::Float(f64::from_bits(0x8007_FFFF_FFFF_FFFE))); - assert!( - below < nan && nan < above, - "NaN sits among negative subnormals" - ); + assert!(below < nan, "NaN sorts after a smaller negative subnormal"); + assert!(nan < above, "NaN sorts before a larger negative subnormal"); assert!(nan < encode_value(&Value::Float(-0.0)), "NaN < -0.0"); assert!( nan < encode_value(&Value::Float(f64::INFINITY)),