Skip to content

Commit c63a159

Browse files
author
Artem Novikov
committed
feat(Datum): allow to cast from Double to Float
1 parent 8d65084 commit c63a159

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

crates/iceberg/src/spec/values/datum.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,9 @@ impl Datum {
11461146
(PrimitiveLiteral::String(val), _, PrimitiveType::Timestamptz) => {
11471147
Datum::timestamptz_from_str(val)
11481148
}
1149+
(PrimitiveLiteral::Double(val), _, PrimitiveType::Float) => {
1150+
Ok(Datum::float(**val as f32))
1151+
}
11491152

11501153
// TODO: implement more type conversions
11511154
(_, self_type, target_type) if self_type == target_type => Ok(self),

crates/iceberg/src/spec/values/tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,3 +1352,25 @@ fn test_date_from_json_as_number() {
13521352

13531353
// Both formats should produce the same Literal value
13541354
}
1355+
1356+
#[test]
1357+
fn test_iceberg_double_convert_to_float() {
1358+
let test_data = [
1359+
(Datum::double(-f64::NAN), Datum::float(-f32::NAN)),
1360+
(Datum::double(-f64::INFINITY), Datum::float(-f32::INFINITY)),
1361+
(Datum::double(f64::MIN), Datum::float(-f32::INFINITY)),
1362+
(Datum::double(f32::MIN as f64), Datum::float(f32::MIN)),
1363+
(Datum::double(-1.0), Datum::float(-1.0)),
1364+
(Datum::double(-0.0), Datum::float(-0.0)),
1365+
(Datum::double(0.0), Datum::float(0.0)),
1366+
(Datum::double(1.0), Datum::float(1.0)),
1367+
(Datum::double(f32::MAX as f64), Datum::float(f32::MAX)),
1368+
(Datum::double(f64::MAX), Datum::float(f32::INFINITY)),
1369+
(Datum::double(f64::INFINITY), Datum::float(f32::INFINITY)),
1370+
(Datum::double(f64::NAN), Datum::float(f32::NAN)),
1371+
];
1372+
for (datum, expected) in test_data {
1373+
let result = datum.to(&Primitive(PrimitiveType::Float)).unwrap();
1374+
assert_eq!(result, expected);
1375+
}
1376+
}

0 commit comments

Comments
 (0)