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

fix(convert): Revert PR #1108 "return error when conversion exceeds f64 bounds" #1179

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions changelog.d/1179.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reverted `to_float` [change](https://github.com/vectordotdev/vrl/pull/1107) because the new logic is too restrictive
e.g. attempting to convert "0" returns an error.
5 changes: 0 additions & 5 deletions src/compiler/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ impl Conversion {
let parsed = s
.parse::<f64>()
.with_context(|_| FloatParseSnafu { s: s.clone() })?;
if !parsed.is_normal() {
return Err(Error::NanFloat {
s: format!("Invalid float \"{s}\": not a normal f64 number"),
});
}
let f = NotNan::new(parsed).map_err(|_| Error::NanFloat { s: s.to_string() })?;
f.into()
}
Expand Down
34 changes: 1 addition & 33 deletions src/compiler/conversion/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use bytes::Bytes;
use chrono::{DateTime, Utc};
use ordered_float::NotNan;

use crate::compiler::conversion::{parse_bool, Conversion, Error};
use crate::compiler::TimeZone;
use crate::compiler::conversion::parse_bool;

#[cfg(unix)] // see https://github.com/vectordotdev/vector/issues/1201
mod unix;
Expand Down Expand Up @@ -92,34 +91,3 @@ fn parse_bool_errors() {
assert!(parse_bool("yes or no").is_err());
assert!(parse_bool("123.4").is_err());
}

fn convert_float(input: impl ToString) -> Result<StubValue, Error> {
let input = input.to_string();
let converter = Conversion::parse("float", TimeZone::Local).expect("float conversion");
converter.convert::<StubValue>(input.into())
}

#[test]
fn convert_float_ok() {
let max_float = format!("17976931348623157{}", "0".repeat(292));
let min_float = format!("-{max_float}");

assert_eq!(convert_float(max_float), Ok(StubValue::Float(f64::MAX)));
assert_eq!(convert_float("1"), Ok(StubValue::Float(1.0)));
assert_eq!(convert_float("1.23"), Ok(StubValue::Float(1.23)));
assert_eq!(convert_float("-1"), Ok(StubValue::Float(-1.0)));
assert_eq!(convert_float("-1.23"), Ok(StubValue::Float(-1.23)));
assert_eq!(convert_float(min_float), Ok(StubValue::Float(f64::MIN)));
}

#[test]
fn convert_float_errors() {
let exceeds_max_float = format!("17976931348623159{}", "0".repeat(292)); // last number inc by 2
let exceeds_min_float = format!("-{exceeds_max_float}");

assert!(convert_float("abc").is_err());
assert!(convert_float("1.23.4").is_err());
assert!(convert_float(exceeds_max_float).is_err());
assert!(convert_float(exceeds_min_float).is_err());
assert!(convert_float("0.0").is_err());
}
Loading