Skip to content

Commit f42d242

Browse files
authored
Add ArrowError::ArithmeticError (#6130)
1 parent b06ffce commit f42d242

File tree

5 files changed

+38
-24
lines changed

5 files changed

+38
-24
lines changed

arrow-arith/src/arithmetic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub fn multiply_fixed_point_checked(
122122
let mut mul = a.wrapping_mul(b);
123123
mul = divide_and_round::<Decimal256Type>(mul, divisor);
124124
mul.to_i128().ok_or_else(|| {
125-
ArrowError::ComputeError(format!("Overflow happened on: {:?} * {:?}", a, b))
125+
ArrowError::ArithmeticOverflow(format!("Overflow happened on: {:?} * {:?}", a, b))
126126
})
127127
})
128128
.and_then(|a| a.with_precision_and_scale(precision, required_scale))
@@ -323,7 +323,7 @@ mod tests {
323323

324324
// `multiply` overflows on this case.
325325
let err = mul(&a, &b).unwrap_err();
326-
assert_eq!(err.to_string(), "Compute error: Overflow happened on: 123456789000000000000000000 * 10000000000000000000");
326+
assert_eq!(err.to_string(), "Arithmetic overflow: Overflow happened on: 123456789000000000000000000 * 10000000000000000000");
327327

328328
// Avoid overflow by reducing the scale.
329329
let result = multiply_fixed_point(&a, &b, 28).unwrap();

arrow-arith/src/numeric.rs

+19-13
Original file line numberDiff line numberDiff line change
@@ -888,15 +888,15 @@ mod tests {
888888

889889
test_neg_primitive::<Int32Type>(
890890
&[i32::MIN],
891-
Err("Compute error: Overflow happened on: - -2147483648"),
891+
Err("Arithmetic overflow: Overflow happened on: - -2147483648"),
892892
);
893893
test_neg_primitive::<Int64Type>(
894894
&[i64::MIN],
895-
Err("Compute error: Overflow happened on: - -9223372036854775808"),
895+
Err("Arithmetic overflow: Overflow happened on: - -9223372036854775808"),
896896
);
897897
test_neg_primitive::<DurationSecondType>(
898898
&[i64::MIN],
899-
Err("Compute error: Overflow happened on: - -9223372036854775808"),
899+
Err("Arithmetic overflow: Overflow happened on: - -9223372036854775808"),
900900
);
901901

902902
let r = neg_wrapping(&Int32Array::from(vec![i32::MIN])).unwrap();
@@ -911,7 +911,7 @@ mod tests {
911911

912912
assert_eq!(
913913
err,
914-
"Compute error: Overflow happened on: - -9223372036854775808"
914+
"Arithmetic overflow: Overflow happened on: - -9223372036854775808"
915915
);
916916

917917
let a = Decimal128Array::from(vec![1, 3, -44, 2, 4])
@@ -1016,28 +1016,31 @@ mod tests {
10161016
let a = UInt8Array::from(vec![56, 5, 3]);
10171017
let b = UInt8Array::from(vec![200, 2, 5]);
10181018
let err = add(&a, &b).unwrap_err().to_string();
1019-
assert_eq!(err, "Compute error: Overflow happened on: 56 + 200");
1019+
assert_eq!(err, "Arithmetic overflow: Overflow happened on: 56 + 200");
10201020
let result = add_wrapping(&a, &b).unwrap();
10211021
assert_eq!(result.as_ref(), &UInt8Array::from(vec![0, 7, 8]));
10221022

10231023
let a = UInt8Array::from(vec![34, 5, 3]);
10241024
let b = UInt8Array::from(vec![200, 2, 5]);
10251025
let err = sub(&a, &b).unwrap_err().to_string();
1026-
assert_eq!(err, "Compute error: Overflow happened on: 34 - 200");
1026+
assert_eq!(err, "Arithmetic overflow: Overflow happened on: 34 - 200");
10271027
let result = sub_wrapping(&a, &b).unwrap();
10281028
assert_eq!(result.as_ref(), &UInt8Array::from(vec![90, 3, 254]));
10291029

10301030
let a = UInt8Array::from(vec![34, 5, 3]);
10311031
let b = UInt8Array::from(vec![200, 2, 5]);
10321032
let err = mul(&a, &b).unwrap_err().to_string();
1033-
assert_eq!(err, "Compute error: Overflow happened on: 34 * 200");
1033+
assert_eq!(err, "Arithmetic overflow: Overflow happened on: 34 * 200");
10341034
let result = mul_wrapping(&a, &b).unwrap();
10351035
assert_eq!(result.as_ref(), &UInt8Array::from(vec![144, 10, 15]));
10361036

10371037
let a = Int16Array::from(vec![i16::MIN]);
10381038
let b = Int16Array::from(vec![-1]);
10391039
let err = div(&a, &b).unwrap_err().to_string();
1040-
assert_eq!(err, "Compute error: Overflow happened on: -32768 / -1");
1040+
assert_eq!(
1041+
err,
1042+
"Arithmetic overflow: Overflow happened on: -32768 / -1"
1043+
);
10411044

10421045
let a = Int16Array::from(vec![21]);
10431046
let b = Int16Array::from(vec![0]);
@@ -1146,15 +1149,15 @@ mod tests {
11461149
.with_precision_and_scale(3, -2)
11471150
.unwrap();
11481151
let err = add(&a, &b).unwrap_err().to_string();
1149-
assert_eq!(err, "Compute error: Overflow happened on: 10 ^ 39");
1152+
assert_eq!(err, "Arithmetic overflow: Overflow happened on: 10 ^ 39");
11501153

11511154
let a = Decimal128Array::from(vec![10])
11521155
.with_precision_and_scale(3, -1)
11531156
.unwrap();
11541157
let err = add(&a, &b).unwrap_err().to_string();
11551158
assert_eq!(
11561159
err,
1157-
"Compute error: Overflow happened on: 10 * 100000000000000000000000000000000000000"
1160+
"Arithmetic overflow: Overflow happened on: 10 * 100000000000000000000000000000000000000"
11581161
);
11591162

11601163
let b = Decimal128Array::from(vec![0])
@@ -1349,7 +1352,10 @@ mod tests {
13491352
let a = IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNano::MAX]);
13501353
let b = IntervalMonthDayNanoArray::from(vec![IntervalMonthDayNano::ONE]);
13511354
let err = add(&a, &b).unwrap_err().to_string();
1352-
assert_eq!(err, "Compute error: Overflow happened on: 2147483647 + 1");
1355+
assert_eq!(
1356+
err,
1357+
"Arithmetic overflow: Overflow happened on: 2147483647 + 1"
1358+
);
13531359
}
13541360

13551361
fn test_duration_impl<T: ArrowPrimitiveType<Native = i64>>() {
@@ -1384,7 +1390,7 @@ mod tests {
13841390
let err = add(&a, &b).unwrap_err().to_string();
13851391
assert_eq!(
13861392
err,
1387-
"Compute error: Overflow happened on: 9223372036854775807 + 1"
1393+
"Arithmetic overflow: Overflow happened on: 9223372036854775807 + 1"
13881394
);
13891395
}
13901396

@@ -1511,7 +1517,7 @@ mod tests {
15111517
let err = sub(&a, &b).unwrap_err().to_string();
15121518
assert_eq!(
15131519
err,
1514-
"Compute error: Overflow happened on: 9223372036854775807 - -1"
1520+
"Arithmetic overflow: Overflow happened on: 9223372036854775807 - -1"
15151521
);
15161522
}
15171523
}

arrow-array/src/arithmetic.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ macro_rules! native_type_op {
154154
#[inline]
155155
fn add_checked(self, rhs: Self) -> Result<Self, ArrowError> {
156156
self.checked_add(rhs).ok_or_else(|| {
157-
ArrowError::ComputeError(format!(
157+
ArrowError::ArithmeticOverflow(format!(
158158
"Overflow happened on: {:?} + {:?}",
159159
self, rhs
160160
))
@@ -169,7 +169,7 @@ macro_rules! native_type_op {
169169
#[inline]
170170
fn sub_checked(self, rhs: Self) -> Result<Self, ArrowError> {
171171
self.checked_sub(rhs).ok_or_else(|| {
172-
ArrowError::ComputeError(format!(
172+
ArrowError::ArithmeticOverflow(format!(
173173
"Overflow happened on: {:?} - {:?}",
174174
self, rhs
175175
))
@@ -184,7 +184,7 @@ macro_rules! native_type_op {
184184
#[inline]
185185
fn mul_checked(self, rhs: Self) -> Result<Self, ArrowError> {
186186
self.checked_mul(rhs).ok_or_else(|| {
187-
ArrowError::ComputeError(format!(
187+
ArrowError::ArithmeticOverflow(format!(
188188
"Overflow happened on: {:?} * {:?}",
189189
self, rhs
190190
))
@@ -202,7 +202,7 @@ macro_rules! native_type_op {
202202
Err(ArrowError::DivideByZero)
203203
} else {
204204
self.checked_div(rhs).ok_or_else(|| {
205-
ArrowError::ComputeError(format!(
205+
ArrowError::ArithmeticOverflow(format!(
206206
"Overflow happened on: {:?} / {:?}",
207207
self, rhs
208208
))
@@ -221,7 +221,7 @@ macro_rules! native_type_op {
221221
Err(ArrowError::DivideByZero)
222222
} else {
223223
self.checked_rem(rhs).ok_or_else(|| {
224-
ArrowError::ComputeError(format!(
224+
ArrowError::ArithmeticOverflow(format!(
225225
"Overflow happened on: {:?} % {:?}",
226226
self, rhs
227227
))
@@ -237,14 +237,17 @@ macro_rules! native_type_op {
237237
#[inline]
238238
fn neg_checked(self) -> Result<Self, ArrowError> {
239239
self.checked_neg().ok_or_else(|| {
240-
ArrowError::ComputeError(format!("Overflow happened on: - {:?}", self))
240+
ArrowError::ArithmeticOverflow(format!("Overflow happened on: - {:?}", self))
241241
})
242242
}
243243

244244
#[inline]
245245
fn pow_checked(self, exp: u32) -> Result<Self, ArrowError> {
246246
self.checked_pow(exp).ok_or_else(|| {
247-
ArrowError::ComputeError(format!("Overflow happened on: {:?} ^ {exp:?}", self))
247+
ArrowError::ArithmeticOverflow(format!(
248+
"Overflow happened on: {:?} ^ {exp:?}",
249+
self
250+
))
248251
})
249252
}
250253

arrow-cast/src/cast/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -4531,7 +4531,7 @@ mod tests {
45314531
))],
45324532
IntervalUnit::DayTime,
45334533
format!(
4534-
"Compute error: Overflow happened on: {} * 100",
4534+
"Arithmetic overflow: Overflow happened on: {} * 100",
45354535
i64::MAX - 2
45364536
)
45374537
);
@@ -4543,7 +4543,10 @@ mod tests {
45434543
i64::MAX - 2
45444544
))],
45454545
IntervalUnit::MonthDayNano,
4546-
format!("Compute error: Overflow happened on: {} * 12", i64::MAX - 2)
4546+
format!(
4547+
"Arithmetic overflow: Overflow happened on: {} * 12",
4548+
i64::MAX - 2
4549+
)
45474550
);
45484551
}
45494552

arrow-schema/src/error.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub enum ArrowError {
3333
SchemaError(String),
3434
ComputeError(String),
3535
DivideByZero,
36+
ArithmeticOverflow(String),
3637
CsvError(String),
3738
JsonError(String),
3839
IoError(String, std::io::Error),
@@ -88,6 +89,7 @@ impl Display for ArrowError {
8889
ArrowError::ParseError(desc) => write!(f, "Parser error: {desc}"),
8990
ArrowError::SchemaError(desc) => write!(f, "Schema error: {desc}"),
9091
ArrowError::ComputeError(desc) => write!(f, "Compute error: {desc}"),
92+
ArrowError::ArithmeticOverflow(desc) => write!(f, "Arithmetic overflow: {desc}"),
9193
ArrowError::DivideByZero => write!(f, "Divide by zero error"),
9294
ArrowError::CsvError(desc) => write!(f, "Csv error: {desc}"),
9395
ArrowError::JsonError(desc) => write!(f, "Json error: {desc}"),

0 commit comments

Comments
 (0)