Skip to content

Apache DataFusion SQL Query Engine

ARROW-3880: [Rust] Implement simple math operations for numeric arrays

Andy Grove12150c665043
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 39de50c8a3..aa23815f74 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -42,6 +42,7 @@ serde_derive = "1.0.80"
serde_json = "1.0.13"
rand = "0.5"
csv = "1.0.0"
+num = "0.2"
[dev-dependencies]
criterion = "0.2"
diff --git a/rust/src/array.rs b/rust/src/array.rs
index 51bc8d993c..11e732a126 100644
--- a/rust/src/array.rs
+++ b/rust/src/array.rs
@@ -200,39 +200,6 @@ impl<T: ArrowNumericType> PrimitiveArray<T> {
&raw[offset..offset + len]
}
- /// Returns the minimum value in the array, according to the natural order.
- pub fn min(&self) -> Option<T::Native> {
- self.min_max_helper(|a, b| a < b)
- }
-
- /// Returns the maximum value in the array, according to the natural order.
- pub fn max(&self) -> Option<T::Native> {
- self.min_max_helper(|a, b| a > b)
- }
-
- fn min_max_helper<F>(&self, cmp: F) -> Option<T::Native>
- where
- F: Fn(T::Native, T::Native) -> bool,
- {
- let mut n: Option<T::Native> = None;
- let data = self.data();
- for i in 0..data.len() {
- if data.is_null(i) {
- continue;
- }
- let m = self.value(i);
- match n {
- None => n = Some(m),
- Some(nn) => {
- if cmp(m, nn) {
- n = Some(m)
- }
- }
- }
- }
- n
- }
-
// Returns a new primitive array builder
pub fn builder(capacity: usize) -> PrimitiveArrayBuilder<T> {
PrimitiveArrayBuilder::<T>::new(capacity)
@@ -1218,20 +1185,6 @@ mod tests {
BinaryArray::from(array_data);
}
- #[test]
- fn test_buffer_array_min_max() {
- let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
- assert_eq!(5, a.min().unwrap());
- assert_eq!(9, a.max().unwrap());
- }
-
- #[test]
- fn test_buffer_array_min_max_with_nulls() {
- let a = Int32Array::from(vec![Some(5), None, None, Some(8), Some(9)]);
- assert_eq!(5, a.min().unwrap());
- assert_eq!(9, a.max().unwrap());
- }
-
#[test]
fn test_access_array_concurrently() {
let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
diff --git a/rust/src/array_ops.rs b/rust/src/array_ops.rs
new file mode 100644
index 0000000000..e73a858e95
--- /dev/null
+++ b/rust/src/array_ops.rs
@@ -0,0 +1,418 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use std::ops::{Add, Div, Mul, Sub};
+
+use num::Zero;
+
+use crate::array::{Array, BooleanArray, PrimitiveArray};
+use crate::builder::{ArrayBuilder, PrimitiveArrayBuilder};
+use crate::datatypes;
+use crate::datatypes::ArrowNumericType;
+use crate::error::{ArrowError, Result};
+
+pub fn add<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
+where
+ T: datatypes::ArrowNumericType,
+ T::Native: Add<Output = T::Native>
+ + Sub<Output = T::Native>
+ + Mul<Output = T::Native>
+ + Div<Output = T::Native>
+ + Zero,
+{
+ math_op(left, right, |a, b| Ok(a + b))
+}
+
+pub fn subtract<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
+where
+ T: datatypes::ArrowNumericType,
+ T::Native: Add<Output = T::Native>
+ + Sub<Output = T::Native>
+ + Mul<Output = T::Native>
+ + Div<Output = T::Native>
+ + Zero,
+{
+ math_op(left, right, |a, b| Ok(a - b))
+}
+
+pub fn multiply<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
+where
+ T: datatypes::ArrowNumericType,
+ T::Native: Add<Output = T::Native>
+ + Sub<Output = T::Native>
+ + Mul<Output = T::Native>
+ + Div<Output = T::Native>
+ + Zero,
+{
+ math_op(left, right, |a, b| Ok(a * b))
+}
+
+pub fn divide<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<PrimitiveArray<T>>
+where
+ T: datatypes::ArrowNumericType,
+ T::Native: Add<Output = T::Native>
+ + Sub<Output = T::Native>
+ + Mul<Output = T::Native>
+ + Div<Output = T::Native>
+ + Zero,
+{
+ math_op(left, right, |a, b| {
+ if b.is_zero() {
+ Err(ArrowError::DivideByZero)
+ } else {
+ Ok(a / b)
+ }
+ })
+}
+
+fn math_op<T, F>(
+ left: &PrimitiveArray<T>,
+ right: &PrimitiveArray<T>,
+ op: F,
+) -> Result<PrimitiveArray<T>>
+where
+ T: datatypes::ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> Result<T::Native>,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform math operation on two batches of different length".to_string(),
+ ));
+ }
+ let mut b = PrimitiveArrayBuilder::<T>::new(left.len());
+ for i in 0..left.len() {
+ let index = i;
+ if left.is_null(i) || right.is_null(i) {
+ b.push_null().unwrap();
+ } else {
+ b.push(op(left.value(index), right.value(index))?).unwrap();
+ }
+ }
+ Ok(b.finish())
+}
+
+/// Returns the minimum value in the array, according to the natural order.
+pub fn min<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
+where
+ T: ArrowNumericType,
+{
+ min_max_helper(array, |a, b| a < b)
+}
+
+/// Returns the maximum value in the array, according to the natural order.
+pub fn max<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
+where
+ T: ArrowNumericType,
+{
+ min_max_helper(array, |a, b| a > b)
+}
+
+fn min_max_helper<T, F>(array: &PrimitiveArray<T>, cmp: F) -> Option<T::Native>
+where
+ T: ArrowNumericType,
+ F: Fn(T::Native, T::Native) -> bool,
+{
+ let mut n: Option<T::Native> = None;
+ let data = array.data();
+ for i in 0..data.len() {
+ if data.is_null(i) {
+ continue;
+ }
+ let m = array.value(i);
+ match n {
+ None => n = Some(m),
+ Some(nn) => {
+ if cmp(m, nn) {
+ n = Some(m)
+ }
+ }
+ }
+ }
+ n
+}
+
+pub fn eq<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| a == b)
+}
+
+pub fn neq<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| a != b)
+}
+
+pub fn lt<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| match (a, b) {
+ (None, _) => true,
+ (_, None) => false,
+ (Some(aa), Some(bb)) => aa < bb,
+ })
+}
+
+pub fn lt_eq<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| match (a, b) {
+ (None, _) => true,
+ (_, None) => false,
+ (Some(aa), Some(bb)) => aa <= bb,
+ })
+}
+
+pub fn gt<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| match (a, b) {
+ (None, _) => false,
+ (_, None) => true,
+ (Some(aa), Some(bb)) => aa > bb,
+ })
+}
+
+pub fn gt_eq<T>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+{
+ bool_op(left, right, |a, b| match (a, b) {
+ (None, _) => false,
+ (_, None) => true,
+ (Some(aa), Some(bb)) => aa >= bb,
+ })
+}
+
+fn bool_op<T, F>(left: &PrimitiveArray<T>, right: &PrimitiveArray<T>, op: F) -> Result<BooleanArray>
+where
+ T: ArrowNumericType,
+ F: Fn(Option<T::Native>, Option<T::Native>) -> bool,
+{
+ if left.len() != right.len() {
+ return Err(ArrowError::ComputeError(
+ "Cannot perform math operation on two batches of different length".to_string(),
+ ));
+ }
+ let mut b = BooleanArray::builder(left.len());
+ for i in 0..left.len() {
+ let index = i;
+ let l = if left.is_null(i) {
+ None
+ } else {
+ Some(left.value(index))
+ };
+ let r = if right.is_null(i) {
+ None
+ } else {
+ Some(right.value(index))
+ };
+ b.push(op(l, r)).unwrap();
+ }
+ Ok(b.finish())
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::array::{Float64Array, Int32Array};
+
+ #[test]
+ fn test_primitive_array_add() {
+ let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 8]);
+ let c = add(&a, &b).unwrap();
+ assert_eq!(11, c.value(0));
+ assert_eq!(13, c.value(1));
+ assert_eq!(15, c.value(2));
+ assert_eq!(17, c.value(3));
+ assert_eq!(17, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_add_mismatched_length() {
+ let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
+ let b = Int32Array::from(vec![6, 7, 8]);
+ let e = add(&a, &b)
+ .err()
+ .expect("should have failed due to different lengths");
+ assert_eq!(
+ "ComputeError(\"Cannot perform math operation on two batches of different length\")",
+ format!("{:?}", e)
+ );
+ }
+
+ #[test]
+ fn test_primitive_array_subtract() {
+ let a = Int32Array::from(vec![1, 2, 3, 4, 5]);
+ let b = Int32Array::from(vec![5, 4, 3, 2, 1]);
+ let c = subtract(&a, &b).unwrap();
+ assert_eq!(-4, c.value(0));
+ assert_eq!(-2, c.value(1));
+ assert_eq!(0, c.value(2));
+ assert_eq!(2, c.value(3));
+ assert_eq!(4, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_multiply() {
+ let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 8]);
+ let c = multiply(&a, &b).unwrap();
+ assert_eq!(30, c.value(0));
+ assert_eq!(42, c.value(1));
+ assert_eq!(56, c.value(2));
+ assert_eq!(72, c.value(3));
+ assert_eq!(72, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_divide() {
+ let a = Int32Array::from(vec![15, 15, 8, 1, 9]);
+ let b = Int32Array::from(vec![5, 6, 8, 9, 1]);
+ let c = divide(&a, &b).unwrap();
+ assert_eq!(3, c.value(0));
+ assert_eq!(2, c.value(1));
+ assert_eq!(1, c.value(2));
+ assert_eq!(0, c.value(3));
+ assert_eq!(9, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_divide_by_zero() {
+ let a = Int32Array::from(vec![15]);
+ let b = Int32Array::from(vec![0]);
+ assert_eq!(
+ ArrowError::DivideByZero,
+ divide(&a, &b).err().expect("divide by zero should fail")
+ );
+ }
+
+ #[test]
+ fn test_primitive_array_divide_f64() {
+ let a = Float64Array::from(vec![15.0, 15.0, 8.0]);
+ let b = Float64Array::from(vec![5.0, 6.0, 8.0]);
+ let c = divide(&a, &b).unwrap();
+ assert_eq!(3.0, c.value(0));
+ assert_eq!(2.5, c.value(1));
+ assert_eq!(1.0, c.value(2));
+ }
+
+ #[test]
+ fn test_primitive_array_add_with_nulls() {
+ let a = Int32Array::from(vec![Some(5), None, Some(7), None]);
+ let b = Int32Array::from(vec![None, None, Some(6), Some(7)]);
+ let c = add(&a, &b).unwrap();
+ assert_eq!(true, c.is_null(0));
+ assert_eq!(true, c.is_null(1));
+ assert_eq!(false, c.is_null(2));
+ assert_eq!(true, c.is_null(3));
+ assert_eq!(13, c.value(2));
+ }
+
+ #[test]
+ fn test_primitive_array_eq() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = eq(&a, &b).unwrap();
+ assert_eq!(false, c.value(0));
+ assert_eq!(false, c.value(1));
+ assert_eq!(true, c.value(2));
+ assert_eq!(false, c.value(3));
+ assert_eq!(false, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_neq() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = neq(&a, &b).unwrap();
+ assert_eq!(true, c.value(0));
+ assert_eq!(true, c.value(1));
+ assert_eq!(false, c.value(2));
+ assert_eq!(true, c.value(3));
+ assert_eq!(true, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_lt() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = lt(&a, &b).unwrap();
+ assert_eq!(false, c.value(0));
+ assert_eq!(false, c.value(1));
+ assert_eq!(false, c.value(2));
+ assert_eq!(true, c.value(3));
+ assert_eq!(true, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_lt_eq() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = lt_eq(&a, &b).unwrap();
+ assert_eq!(false, c.value(0));
+ assert_eq!(false, c.value(1));
+ assert_eq!(true, c.value(2));
+ assert_eq!(true, c.value(3));
+ assert_eq!(true, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_gt() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = gt(&a, &b).unwrap();
+ assert_eq!(true, c.value(0));
+ assert_eq!(true, c.value(1));
+ assert_eq!(false, c.value(2));
+ assert_eq!(false, c.value(3));
+ assert_eq!(false, c.value(4));
+ }
+
+ #[test]
+ fn test_primitive_array_gt_eq() {
+ let a = Int32Array::from(vec![8, 8, 8, 8, 8]);
+ let b = Int32Array::from(vec![6, 7, 8, 9, 10]);
+ let c = gt_eq(&a, &b).unwrap();
+ assert_eq!(true, c.value(0));
+ assert_eq!(true, c.value(1));
+ assert_eq!(true, c.value(2));
+ assert_eq!(false, c.value(3));
+ assert_eq!(false, c.value(4));
+ }
+
+ #[test]
+ fn test_buffer_array_min_max() {
+ let a = Int32Array::from(vec![5, 6, 7, 8, 9]);
+ assert_eq!(5, min(&a).unwrap());
+ assert_eq!(9, max(&a).unwrap());
+ }
+
+ #[test]
+ fn test_buffer_array_min_max_with_nulls() {
+ let a = Int32Array::from(vec![Some(5), None, None, Some(8), Some(9)]);
+ assert_eq!(5, min(&a).unwrap());
+ assert_eq!(9, max(&a).unwrap());
+ }
+
+}
diff --git a/rust/src/error.rs b/rust/src/error.rs
index d82ee1190a..559b2d7205 100644
--- a/rust/src/error.rs
+++ b/rust/src/error.rs
@@ -19,6 +19,8 @@
pub enum ArrowError {
MemoryError(String),
ParseError(String),
+ ComputeError(String),
+ DivideByZero,
}
pub type Result<T> = ::std::result::Result<T, ArrowError>;
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index e1670ff055..b661c21279 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -27,6 +27,7 @@ extern crate serde_json;
pub mod array;
pub mod array_data;
+pub mod array_ops;
pub mod bitmap;
pub mod buffer;
pub mod builder;