This is an automated email from the ASF dual-hosted git repository.
Kriskras99 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/avro-rs.git
The following commit(s) were added to refs/heads/main by this push:
new 6c057a3 fix: Write Value::Float as 8 bytes under a Double schema
(#706)
6c057a3 is described below
commit 6c057a3c8c86a306150058255f9bb1032ee150b2
Author: Viet Hung Nguyen <[email protected]>
AuthorDate: Tue Oct 13 22:56:18 2026 +0700
fix: Write Value::Float as 8 bytes under a Double schema (#706)
* fix: Write Value::Float as 8 bytes under a Double schema
In AVRO-3678, added supports for Value::validate accepting a Float value as
Double
but writers wrote it as 4 bytes f32.
* Update avro/src/encode.rs
Co-authored-by: Kriskras99 <[email protected]>
---------
Co-authored-by: Kriskras99 <[email protected]>
---
avro/src/encode.rs | 22 +++++++++++++++++++---
1 file changed, 19 insertions(+), 3 deletions(-)
diff --git a/avro/src/encode.rs b/avro/src/encode.rs
index 357bdaa..3464ce6 100644
--- a/avro/src/encode.rs
+++ b/avro/src/encode.rs
@@ -107,9 +107,12 @@ pub(crate) fn encode_internal<W: Write, S: Borrow<Schema>>(
| Value::LocalTimestampMicros(i)
| Value::LocalTimestampNanos(i)
| Value::TimeMicros(i) => encode_long(*i, writer),
- Value::Float(x) => writer
- .write(&x.to_le_bytes())
- .map_err(|e| Details::WriteBytes(e).into()),
+ Value::Float(x) => match schema {
+ // Automatically resolve float to double to match `.validate()`
implementation
+ Schema::Double => writer.write(&f64::from(*x).to_le_bytes()),
+ _ => writer.write(&x.to_le_bytes()),
+ }
+ .map_err(|e| Details::WriteBytes(e).into()),
Value::Double(x) => writer
.write(&x.to_le_bytes())
.map_err(|e| Details::WriteBytes(e).into()),
@@ -989,4 +992,17 @@ pub(crate) mod tests {
Ok(())
}
+
+ #[test]
+ fn avro_rs_706_encode_float_with_schema_double() -> TestResult {
+ let schema = Schema::Double;
+ let value = Value::Float(0.1);
+
+ value.validate(&schema)?;
+
+ let encoded = encode_to_vec(&value, &schema)?;
+ assert_eq!(encoded, &[0x0, 0x0, 0x0, 0xA0, 0x99, 0x99, 0xB9, 0x3F]);
+
+ Ok(())
+ }
}