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

Prevent precision=0 for decimal type #3162

Merged
merged 2 commits into from
Nov 22, 2022
Merged
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
27 changes: 21 additions & 6 deletions arrow-array/src/array/primitive_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -993,12 +993,13 @@ impl<T: ArrowPrimitiveType> From<ArrayData> for PrimitiveArray<T> {

impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
/// Returns a Decimal array with the same data as self, with the
/// specified precision.
/// specified precision and scale.
///
/// Returns an Error if:
/// 1. `precision` is larger than `T:MAX_PRECISION`
/// 2. `scale` is larger than `T::MAX_SCALE`
/// 3. `scale` is > `precision`
/// - `precision` is zero
/// - `precision` is larger than `T:MAX_PRECISION`
/// - `scale` is larger than `T::MAX_SCALE`
/// - `scale` is > `precision`
pub fn with_precision_and_scale(
self,
precision: u8,
Expand All @@ -1025,18 +1026,24 @@ impl<T: DecimalType + ArrowPrimitiveType> PrimitiveArray<T> {
precision: u8,
scale: u8,
) -> Result<(), ArrowError> {
if precision == 0 {
return Err(ArrowError::InvalidArgumentError(format!(
"precision cannot be 0, has to be between [1, {}]",
T::MAX_PRECISION
)));
}
if precision > T::MAX_PRECISION {
return Err(ArrowError::InvalidArgumentError(format!(
"precision {} is greater than max {}",
precision,
Decimal128Type::MAX_PRECISION
T::MAX_PRECISION
)));
}
if scale > T::MAX_SCALE {
return Err(ArrowError::InvalidArgumentError(format!(
"scale {} is greater than max {}",
scale,
Decimal128Type::MAX_SCALE
T::MAX_SCALE
)));
}
if scale > precision {
Expand Down Expand Up @@ -1934,6 +1941,14 @@ mod tests {
arr.validate_decimal_precision(5).unwrap();
}

#[test]
#[should_panic(expected = "precision cannot be 0, has to be between [1, 38]")]
fn test_decimal_array_with_precision_zero() {
Decimal128Array::from_iter_values([12345, 456])
.with_precision_and_scale(0, 2)
.unwrap();
}

#[test]
#[should_panic(expected = "precision 40 is greater than max 38")]
fn test_decimal_array_with_precision_and_scale_invalid_precision() {
Expand Down