Skip to content

Commit

Permalink
fix(iceberg): fix decimal overflow for iceberg arrow (#18511)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenzl25 authored Sep 12, 2024
1 parent 6c3b68f commit b9ac1ac
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/common/src/array/arrow/arrow_iceberg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,8 @@ impl ToArrow for IcebergArrowConvert {
let scale = e.scale() as i8;
let diff_scale = abs(max_scale - scale);
let value = match scale {
_ if scale < max_scale => {
value.mul(10_i32.pow(diff_scale as u32) as i128)
}
_ if scale > max_scale => {
value.div(10_i32.pow(diff_scale as u32) as i128)
}
_ if scale < max_scale => value.mul(10_i128.pow(diff_scale as u32)),
_ if scale > max_scale => value.div(10_i128.pow(diff_scale as u32)),
_ => value,
};
Some(value)
Expand Down Expand Up @@ -297,4 +293,30 @@ mod test {
) as ArrayRef;
assert_eq!(&arrow_array, &expect_array);
}

#[test]
fn decimal_with_large_scale() {
let array = DecimalArray::from_iter([
None,
Some(Decimal::NaN),
Some(Decimal::PositiveInf),
Some(Decimal::NegativeInf),
Some(Decimal::Normalized("123.4".parse().unwrap())),
Some(Decimal::Normalized("123.456".parse().unwrap())),
]);
let ty = DataType::Decimal128(28, 10);
let arrow_array = IcebergArrowConvert.decimal_to_arrow(&ty, &array).unwrap();
let expect_array = Arc::new(
Decimal128Array::from(vec![
None,
None,
Some(9999999999999999999999999999),
Some(-9999999999999999999999999999),
Some(1234000000000),
Some(1234560000000),
])
.with_data_type(ty),
) as ArrayRef;
assert_eq!(&arrow_array, &expect_array);
}
}

0 comments on commit b9ac1ac

Please sign in to comment.