Skip to content

Commit 9a9174e

Browse files
committed
Add support for V2 schema evolution
1 parent 5fa9ef8 commit 9a9174e

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

crates/iceberg/src/spec/values.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,12 +403,26 @@ impl Datum {
403403
}
404404
}
405405
PrimitiveType::Int => PrimitiveLiteral::Int(i32::from_le_bytes(bytes.try_into()?)),
406-
PrimitiveType::Long => PrimitiveLiteral::Long(i64::from_le_bytes(bytes.try_into()?)),
406+
PrimitiveType::Long => {
407+
if bytes.len() == 4 {
408+
// In the case of an evolved field
409+
PrimitiveLiteral::Long(i32::from_le_bytes(bytes.try_into()?) as i64)
410+
} else {
411+
PrimitiveLiteral::Long(i64::from_le_bytes(bytes.try_into()?))
412+
}
413+
}
407414
PrimitiveType::Float => {
408415
PrimitiveLiteral::Float(OrderedFloat(f32::from_le_bytes(bytes.try_into()?)))
409416
}
410417
PrimitiveType::Double => {
411-
PrimitiveLiteral::Double(OrderedFloat(f64::from_le_bytes(bytes.try_into()?)))
418+
if bytes.len() == 4 {
419+
// In the case of an evolved field
420+
PrimitiveLiteral::Double(OrderedFloat(
421+
f32::from_le_bytes(bytes.try_into()?) as f64
422+
))
423+
} else {
424+
PrimitiveLiteral::Double(OrderedFloat(f64::from_le_bytes(bytes.try_into()?)))
425+
}
412426
}
413427
PrimitiveType::Date => PrimitiveLiteral::Int(i32::from_le_bytes(bytes.try_into()?)),
414428
PrimitiveType::Time => PrimitiveLiteral::Long(i64::from_le_bytes(bytes.try_into()?)),
@@ -3169,6 +3183,13 @@ mod tests {
31693183
check_avro_bytes_serde(bytes, Datum::long(32), &PrimitiveType::Long);
31703184
}
31713185

3186+
#[test]
3187+
fn avro_bytes_long_from_int() {
3188+
let bytes = vec![32u8, 0u8, 0u8, 0u8];
3189+
3190+
check_avro_bytes_serde(bytes, Datum::long(32), &PrimitiveType::Long);
3191+
}
3192+
31723193
#[test]
31733194
fn avro_bytes_float() {
31743195
let bytes = vec![0u8, 0u8, 128u8, 63u8];
@@ -3183,6 +3204,13 @@ mod tests {
31833204
check_avro_bytes_serde(bytes, Datum::double(1.0), &PrimitiveType::Double);
31843205
}
31853206

3207+
#[test]
3208+
fn avro_bytes_double_from_float() {
3209+
let bytes = vec![0u8, 0u8, 128u8, 63u8];
3210+
3211+
check_avro_bytes_serde(bytes, Datum::double(1.0), &PrimitiveType::Double);
3212+
}
3213+
31863214
#[test]
31873215
fn avro_bytes_string() {
31883216
let bytes = vec![105u8, 99u8, 101u8, 98u8, 101u8, 114u8, 103u8];

0 commit comments

Comments
 (0)