Skip to content

Commit f8a7b41

Browse files
committed
Add arithmetic buffer compute
Signed-off-by: Nicholas Gates <[email protected]>
1 parent 87bfc99 commit f8a7b41

File tree

1 file changed

+71
-16
lines changed

1 file changed

+71
-16
lines changed

vortex-array/src/compute/arrays/arithmetic.rs

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -289,34 +289,89 @@ where
289289

290290
#[cfg(test)]
291291
mod tests {
292-
use vortex_buffer::bitbuffer;
292+
use vortex_buffer::buffer;
293+
use vortex_dtype::PTypeDowncastExt;
293294

294-
use crate::compute::arrays::logical::ArithmeticOperator;
295+
use crate::arrays::PrimitiveArray;
296+
use crate::compute::arrays::arithmetic::{ArithmeticArray, ArithmeticOperator};
295297
use crate::{ArrayOperator, ArrayRef, IntoArray};
296298

297-
fn and_(lhs: ArrayRef, rhs: ArrayRef) -> ArrayRef {
298-
ArithmeticArray::new(lhs, rhs, ArithmeticOperator::And).into_array()
299+
fn add(lhs: ArrayRef, rhs: ArrayRef) -> ArrayRef {
300+
ArithmeticArray::new(lhs, rhs, ArithmeticOperator::Add).into_array()
301+
}
302+
303+
fn sub(lhs: ArrayRef, rhs: ArrayRef) -> ArrayRef {
304+
ArithmeticArray::new(lhs, rhs, ArithmeticOperator::Sub).into_array()
305+
}
306+
307+
fn mul(lhs: ArrayRef, rhs: ArrayRef) -> ArrayRef {
308+
ArithmeticArray::new(lhs, rhs, ArithmeticOperator::Mul).into_array()
309+
}
310+
311+
fn div(lhs: ArrayRef, rhs: ArrayRef) -> ArrayRef {
312+
ArithmeticArray::new(lhs, rhs, ArithmeticOperator::Div).into_array()
313+
}
314+
315+
#[test]
316+
fn test_add() {
317+
let lhs = PrimitiveArray::from_iter([1u32, 2, 3]).into_array();
318+
let rhs = PrimitiveArray::from_iter([10u32, 20, 30]).into_array();
319+
let result = add(lhs, rhs)
320+
.execute()
321+
.unwrap()
322+
.into_primitive()
323+
.downcast::<u32>();
324+
assert_eq!(result.elements(), &buffer![11u32, 22, 33]);
299325
}
300326

301327
#[test]
302-
fn test_and() {
303-
let lhs = bitbuffer![0 1 0].into_array();
304-
let rhs = bitbuffer![0 1 1].into_array();
305-
let result = and_(lhs, rhs).execute().unwrap().into_bool();
306-
assert_eq!(result.bits(), &bitbuffer![0 1 0]);
328+
fn test_sub() {
329+
let lhs = PrimitiveArray::from_iter([10u32, 20, 30]).into_array();
330+
let rhs = PrimitiveArray::from_iter([1u32, 2, 3]).into_array();
331+
let result = sub(lhs, rhs)
332+
.execute()
333+
.unwrap()
334+
.into_primitive()
335+
.downcast::<u32>();
336+
assert_eq!(result.elements(), &buffer![9u32, 18, 27]);
337+
}
338+
339+
#[test]
340+
fn test_mul() {
341+
let lhs = PrimitiveArray::from_iter([2u32, 3, 4]).into_array();
342+
let rhs = PrimitiveArray::from_iter([10u32, 20, 30]).into_array();
343+
let result = mul(lhs, rhs)
344+
.execute()
345+
.unwrap()
346+
.into_primitive()
347+
.downcast::<u32>();
348+
assert_eq!(result.elements(), &buffer![20u32, 60, 120]);
349+
}
350+
351+
#[test]
352+
fn test_div() {
353+
let lhs = PrimitiveArray::from_iter([100u32, 200, 300]).into_array();
354+
let rhs = PrimitiveArray::from_iter([10u32, 20, 30]).into_array();
355+
let result = div(lhs, rhs)
356+
.execute()
357+
.unwrap()
358+
.into_primitive()
359+
.downcast::<u32>();
360+
assert_eq!(result.elements(), &buffer![10u32, 10, 10]);
307361
}
308362

309363
#[test]
310-
fn test_and_selected() {
311-
let lhs = bitbuffer![0 1 0].into_array();
312-
let rhs = bitbuffer![0 1 1].into_array();
364+
fn test_add_with_selection() {
365+
let lhs = PrimitiveArray::from_iter([1u32, 2, 3]).into_array();
366+
let rhs = PrimitiveArray::from_iter([10u32, 20, 30]).into_array();
313367

314-
let selection = bitbuffer![0 1 1].into_array();
368+
let selection = PrimitiveArray::from_iter([0u64, 2]).into_array();
315369

316-
let result = and_(lhs, rhs)
370+
let result = add(lhs, rhs)
317371
.execute_with_selection(Some(&selection))
318372
.unwrap()
319-
.into_bool();
320-
assert_eq!(result.bits(), &bitbuffer![1 0]);
373+
.into_primitive()
374+
.downcast::<u32>();
375+
assert_eq!(result.elements(), &buffer![11u32, 33]);
321376
}
322377
}

0 commit comments

Comments
 (0)