Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nan/inf/-inf as float/double default #4

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions lang/rust/avro/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,10 @@ impl Value {
Value::Long(n) => Ok(Value::Float(n as f32)),
Value::Float(x) => Ok(Value::Float(x)),
Value::Double(x) => Ok(Value::Float(x as f32)),
Value::String(x) => match Self::parse_special_float(&x) {
Some(f) => Ok(Value::Float(f)),
None => Err(Error::GetFloat(ValueKind::String)),
},
other => Err(Error::GetFloat(other.into())),
}
}
Expand All @@ -870,10 +874,25 @@ impl Value {
Value::Long(n) => Ok(Value::Double(n as f64)),
Value::Float(x) => Ok(Value::Double(f64::from(x))),
Value::Double(x) => Ok(Value::Double(x)),
Value::String(x) => match Self::parse_special_float(&x) {
Some(f) => Ok(Value::Double(f.into())),
None => Err(Error::GetDouble(ValueKind::String)),
},
other => Err(Error::GetDouble(other.into())),
}
}

/// IEEE 754 NaN and infinities are not valid JSON numbers.
/// So they are represented in JSON as strings.
fn parse_special_float(s: &str) -> Option<f32> {
match s.trim().to_ascii_lowercase().as_str() {
"nan" | "+nan" | "-nan" => Some(f32::NAN),
"inf" | "+inf" | "infinity" | "+infinity" => Some(f32::INFINITY),
"-inf" | "-infinity" => Some(f32::NEG_INFINITY),
_ => None,
}
}

/// <https://github.com/apache/avro/blob/release-1.11.3/lang/java/avro/src/main/java/org/apache/avro/io/parsing/ResolvingGrammarGenerator.java#L315>
fn encode_iso_8859_1(s: &str) -> Result<Vec<u8>, Error> {
s.chars()
Expand Down
29 changes: 24 additions & 5 deletions lang/rust/avro/tests/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ lazy_static! {
(r#""long""#, "5", Value::Long(5)),
(r#""float""#, "1.1", Value::Float(1.1)),
(r#""double""#, "1.1", Value::Double(1.1)),
(r#""float""#, r#"" +inf ""#, Value::Float(f32::INFINITY)),
(r#""double""#, r#""-Infinity""#, Value::Double(f64::NEG_INFINITY)),
(r#""double""#, r#""-NAN""#, Value::Double(f64::NAN)),
(r#"{"type": "fixed", "name": "F", "size": 2}"#, r#""a""#, Value::Fixed(1, vec![97])), // ASCII 'a' => one byte
(r#"{"type": "fixed", "name": "F", "size": 2}"#, r#""\u00FF""#, Value::Fixed(1, vec![255])), // The value is between U+0080 and U+07FF => ISO-8859-1
(r#"{"type": "enum", "name": "F", "symbols": ["FOO", "BAR"]}"#, r#""FOO""#, Value::Enum(0, "FOO".to_string())),
Expand Down Expand Up @@ -264,11 +267,27 @@ fn test_default_value() -> TestResult {
&mut Cursor::new(encoded),
Some(&reader_schema),
)?;
assert_eq!(
datum_read, datum_to_read,
"{} -> {}",
*field_type, *default_json
);
match default_datum {
Value::Double(f) if f.is_nan() => {
let Value::Record(fields) = datum_read else {
panic!()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you write a meaningful error in this panic?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is guaranteed to be record by the testing setup itself. I have updated it to unreachable

};
let Value::Double(f) = fields[0].1 else {
panic!("double expected")
};
assert!(
f.is_nan(),
"{field_type} -> {default_json} is parsed as {f} rather than NaN"
);
}
_ => {
assert_eq!(
datum_read, datum_to_read,
"{} -> {}",
*field_type, *default_json
);
}
}
}

Ok(())
Expand Down
Loading