Uploaded image for project: 'Rust Driver'
  1. Rust Driver
  2. RUST-930

decimal128 does not always round trip through string

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: None
    • Component/s: BSON
    • None

      Given the decimal128 value from the [decq021] Normality bson corpus test case, round tripping a Decimal128 through to_string() and from_str is lossy. This appears to be due to the fact that to_string() produces an incorrect value. Curiously, the two values still compare as equal despite serializing to different values.

      let canonical_hex = "18000000136400F2AF967ED05C82DE3297FF6FDE3
      let canonical = hex::decode(canonical_hex.clone()).unwrap();  
      let doc = Document::from_reader(canonical.as_slice()).unwrap()
      let expected = doc.get_decimal128("d").unwrap().clone();      
                                                                                             
      let roundstring = Decimal128::from_str(expected.to_string().as_str());
      assert_eq!(roundstring, expected); // succeeds                
                                                                    
      let manual_expected = doc! { "d": Bson::Decimal128(expected) }
      assert_eq!(manual_expected, doc); // succeeds                 
                                                                    
      let mut manual_expected_bytes = Vec::new();                   
      manual_expected.to_writer(&mut manual_expected_bytes).unwrap()
      assert_eq!(hex::encode(manual_expected_bytes), canonical_hex); // succeeds
                                                                    
      let manual = doc! { "d": Bson::Decimal128(roundstring) };     
      assert_eq!(manual, doc); // also succeeds                     
                                                                    
      let mut manual_bytes = Vec::new();                            
      manual.to_writer(&mut manual_bytes).unwrap();                 
      assert_eq!(hex::encode(manual_bytes), canonical_hex); // fails
      assert_eq!(expected.to_string(), "-1234567890123456789012345678901234"); // also fails
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            patrick.freed@mongodb.com Patrick Freed
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: