Skip to content
This repository has been archived by the owner on Feb 18, 2024. It is now read-only.

Commit

Permalink
Migrated tests and benches.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Nov 15, 2021
1 parent a367ac5 commit 34e0eba
Show file tree
Hide file tree
Showing 28 changed files with 216 additions and 250 deletions.
2 changes: 1 addition & 1 deletion benches/arithmetic_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn bench_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>)
where
T: NativeType + Add<Output = T> + NumCast,
{
criterion::black_box(add(lhs, rhs)).unwrap();
criterion::black_box(add(lhs, rhs));
}

fn add_benchmark(c: &mut Criterion) {
Expand Down
28 changes: 11 additions & 17 deletions benches/comparison_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
use arrow2::array::*;
use arrow2::scalar::*;
use arrow2::util::bench_util::*;
use arrow2::{compute::comparison::*, datatypes::DataType};
use arrow2::{compute::comparison::eq, datatypes::DataType};

fn bench_op(arr_a: &dyn Array, arr_b: &dyn Array, op: Operator) {
compare(black_box(arr_a), black_box(arr_b), op).unwrap();
fn bench_op(arr_a: &dyn Array, arr_b: &dyn Array) {
eq(black_box(arr_a), black_box(arr_b));
}

fn bench_op_scalar(arr_a: &dyn Array, value_b: &dyn Scalar, op: Operator) {
compare_scalar(black_box(arr_a), black_box(value_b), op).unwrap();
fn bench_op_scalar(arr_a: &dyn Array, value_b: &dyn Scalar) {
eq_scalar(black_box(arr_a), black_box(value_b));
}

fn add_benchmark(c: &mut Criterion) {
Expand All @@ -21,36 +21,30 @@ fn add_benchmark(c: &mut Criterion) {
let arr_b = create_primitive_array_with_seed::<f32>(size, DataType::Float32, 0.0, 43);

c.bench_function(&format!("f32 2^{}", log2_size), |b| {
b.iter(|| bench_op(&arr_a, &arr_b, Operator::Eq))
b.iter(|| bench_op(&arr_a, &arr_b))
});
c.bench_function(&format!("f32 scalar 2^{}", log2_size), |b| {
b.iter(|| {
bench_op_scalar(
&arr_a,
&PrimitiveScalar::<f32>::from(Some(0.5)),
Operator::Eq,
)
})
b.iter(|| bench_op_scalar(&arr_a, &PrimitiveScalar::<f32>::from(Some(0.5))))
});

let arr_a = create_boolean_array(size, 0.0, 0.1);
let arr_b = create_boolean_array(size, 0.0, 0.2);

c.bench_function(&format!("bool 2^{}", log2_size), |b| {
b.iter(|| bench_op(&arr_a, &arr_b, Operator::Eq))
b.iter(|| bench_op(&arr_a, &arr_b))
});
c.bench_function(&format!("bool scalar 2^{}", log2_size), |b| {
b.iter(|| bench_op_scalar(&arr_a, &BooleanScalar::from(Some(false)), Operator::Eq))
b.iter(|| bench_op_scalar(&arr_a, &BooleanScalar::from(Some(false))))
});

let arr_a = create_string_array::<i32>(size, 4, 0.1, 42);
let arr_b = create_string_array::<i32>(size, 4, 0.1, 43);
c.bench_function(&format!("utf8 2^{}", log2_size), |b| {
b.iter(|| bench_op(&arr_a, &arr_b, Operator::Eq))
b.iter(|| bench_op(&arr_a, &arr_b))
});

c.bench_function(&format!("utf8 2^{}", log2_size), |b| {
b.iter(|| bench_op_scalar(&arr_a, &Utf8Scalar::<i32>::from(Some("abc")), Operator::Eq))
b.iter(|| bench_op_scalar(&arr_a, &Utf8Scalar::<i32>::from(Some("abc"))))
});
})
}
Expand Down
2 changes: 1 addition & 1 deletion benches/write_ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn write(array: &dyn Array) -> Result<()> {
let batch = RecordBatch::try_new(Arc::new(schema.clone()), vec![clone(array).into()])?;

let writer = Cursor::new(vec![]);
let mut writer = FileWriter::try_new(writer, &schema)?;
let mut writer = FileWriter::try_new(writer, &schema, Default::default())?;

writer.write(&batch)
}
Expand Down
28 changes: 11 additions & 17 deletions examples/arithmetics.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
use arrow2::array::{Array, PrimitiveArray};
use arrow2::compute::arithmetics::*;
use arrow2::compute::arithmetics::basic::*;
use arrow2::compute::arithmetics::{add as dyn_add, can_add};
use arrow2::compute::arity::{binary, unary};
use arrow2::datatypes::DataType;
use arrow2::error::Result;

fn main() -> Result<()> {
fn main() {
// say we have two arrays
let array0 = PrimitiveArray::<i64>::from(&[Some(1), Some(2), Some(3)]);
let array1 = PrimitiveArray::<i64>::from(&[Some(4), None, Some(6)]);

// we can add them as follows:
let added = arithmetic_primitive(&array0, Operator::Add, &array1)?;
let added = add(&array0, &array1);
assert_eq!(
added,
PrimitiveArray::<i64>::from(&[Some(5), None, Some(9)])
);

// subtract:
let subtracted = arithmetic_primitive(&array0, Operator::Subtract, &array1)?;
let subtracted = sub(&array0, &array1);
assert_eq!(
subtracted,
PrimitiveArray::<i64>::from(&[Some(-3), None, Some(-3)])
);

// add a scalar:
let plus10 = arithmetic_primitive_scalar(&array0, Operator::Add, &10)?;
let plus10 = add_scalar(&array0, &10);
assert_eq!(
plus10,
PrimitiveArray::<i64>::from(&[Some(11), Some(12), Some(13)])
);

// when the array is a trait object, there is a similar API
// a similar API for trait objects:
let array0 = &array0 as &dyn Array;
let array1 = &array1 as &dyn Array;

// check whether the logical types support addition (they could be any `Array`).
assert!(can_arithmetic(
array0.data_type(),
Operator::Add,
array1.data_type()
));
// check whether the logical types support addition.
assert!(can_add(array0.data_type(), array1.data_type()));

// add them
let added = arithmetic(array0, Operator::Add, array1).unwrap();
let added = dyn_add(array0, array1);
assert_eq!(
PrimitiveArray::<i64>::from(&[Some(5), None, Some(9)]),
added.as_ref(),
Expand All @@ -54,7 +50,7 @@ fn main() -> Result<()> {
let array1 = PrimitiveArray::<i64>::from(&[Some(4), None, Some(6)]);

let op = |x: i64, y: i64| x.pow(2) + y.pow(2);
let r = binary(&array0, &array1, DataType::Int64, op)?;
let r = binary(&array0, &array1, DataType::Int64, op);
assert_eq!(
r,
PrimitiveArray::<i64>::from(&[Some(1 + 16), None, Some(9 + 36)])
Expand All @@ -79,6 +75,4 @@ fn main() -> Result<()> {
rounded,
PrimitiveArray::<i64>::from(&[Some(4), None, Some(5)])
);

Ok(())
}
2 changes: 0 additions & 2 deletions examples/csv_read_async.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use std::sync::Arc;

use futures::io::Cursor;
use tokio::fs::File;
use tokio_util::compat::*;

use arrow2::array::*;
use arrow2::error::Result;
use arrow2::io::csv::read_async::*;

Expand Down
2 changes: 1 addition & 1 deletion examples/growable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use arrow2::array::growable::{Growable, GrowablePrimitive};
use arrow2::array::{Array, PrimitiveArray};
use arrow2::array::PrimitiveArray;

fn main() {
// say we have two sorted arrays
Expand Down
16 changes: 7 additions & 9 deletions src/compute/arithmetics/basic/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use crate::{
bitmap::Bitmap,
compute::{
arithmetics::{
basic::check_same_type, ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd,
ArraySaturatingAdd, ArrayWrappingAdd, NativeArithmetics,
ArrayAdd, ArrayCheckedAdd, ArrayOverflowingAdd, ArraySaturatingAdd, ArrayWrappingAdd,
NativeArithmetics,
},
arity::{
binary, binary_checked, binary_with_bitmap, unary, unary_checked, unary_with_bitmap,
Expand All @@ -28,7 +28,7 @@ use crate::{
///
/// let a = PrimitiveArray::from([None, Some(6), None, Some(6)]);
/// let b = PrimitiveArray::from([Some(5), None, None, Some(6)]);
/// let result = add(&a, &b).unwrap();
/// let result = add(&a, &b);
/// let expected = PrimitiveArray::from([None, None, None, Some(12)]);
/// assert_eq!(result, expected)
/// ```
Expand All @@ -49,7 +49,7 @@ where
///
/// let a = PrimitiveArray::from([Some(-100i8), Some(100i8), Some(100i8)]);
/// let b = PrimitiveArray::from([Some(0i8), Some(100i8), Some(0i8)]);
/// let result = wrapping_add(&a, &b).unwrap();
/// let result = wrapping_add(&a, &b);
/// let expected = PrimitiveArray::from([Some(-100i8), Some(-56i8), Some(100i8)]);
/// assert_eq!(result, expected);
/// ```
Expand All @@ -72,16 +72,14 @@ where
///
/// let a = PrimitiveArray::from([Some(100i8), Some(100i8), Some(100i8)]);
/// let b = PrimitiveArray::from([Some(0i8), Some(100i8), Some(0i8)]);
/// let result = checked_add(&a, &b).unwrap();
/// let result = checked_add(&a, &b);
/// let expected = PrimitiveArray::from([Some(100i8), None, Some(100i8)]);
/// assert_eq!(result, expected);
/// ```
pub fn checked_add<T>(lhs: &PrimitiveArray<T>, rhs: &PrimitiveArray<T>) -> PrimitiveArray<T>
where
T: NativeType + CheckedAdd<Output = T>,
{
check_same_type(lhs, rhs).unwrap();

let op = move |a: T, b: T| a.checked_add(&b);

binary_checked(lhs, rhs, lhs.data_type().clone(), op)
Expand All @@ -98,7 +96,7 @@ where
///
/// let a = PrimitiveArray::from([Some(100i8)]);
/// let b = PrimitiveArray::from([Some(100i8)]);
/// let result = saturating_add(&a, &b).unwrap();
/// let result = saturating_add(&a, &b);
/// let expected = PrimitiveArray::from([Some(127)]);
/// assert_eq!(result, expected);
/// ```
Expand All @@ -123,7 +121,7 @@ where
///
/// let a = PrimitiveArray::from([Some(1i8), Some(100i8)]);
/// let b = PrimitiveArray::from([Some(1i8), Some(100i8)]);
/// let (result, overflow) = overflowing_add(&a, &b).unwrap();
/// let (result, overflow) = overflowing_add(&a, &b);
/// let expected = PrimitiveArray::from([Some(2i8), Some(-56i8)]);
/// assert_eq!(result, expected);
/// ```
Expand Down
6 changes: 3 additions & 3 deletions src/compute/arithmetics/basic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use super::super::arity::{unary, unary_checked};
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::negate;
/// use arrow2::compute::arithmetics::basic::negate;
/// use arrow2::array::PrimitiveArray;
///
/// let a = PrimitiveArray::from([None, Some(6), None, Some(7)]);
Expand All @@ -55,7 +55,7 @@ where
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::checked_negate;
/// use arrow2::compute::arithmetics::basic::checked_negate;
/// use arrow2::array::{Array, PrimitiveArray};
///
/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]);
Expand All @@ -75,7 +75,7 @@ where
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::wrapping_negate;
/// use arrow2::compute::arithmetics::basic::wrapping_negate;
/// use arrow2::array::{Array, PrimitiveArray};
///
/// let a = PrimitiveArray::from([None, Some(6), Some(i8::MIN), Some(7)]);
Expand Down
2 changes: 1 addition & 1 deletion src/compute/arithmetics/basic/rem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ where
///
/// let a = Int8Array::from(&[Some(-100i8), Some(10i8)]);
/// let b = Int8Array::from(&[Some(100i8), Some(0i8)]);
/// let result = checked_rem(&a, &b).unwrap();
/// let result = checked_rem(&a, &b);
/// let expected = Int8Array::from(&[Some(-0i8), None]);
/// assert_eq!(result, expected);
/// ```
Expand Down
2 changes: 1 addition & 1 deletion src/compute/arithmetics/basic/sub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where
///
/// let a = Int8Array::from(&[Some(100i8), Some(-100i8), Some(100i8)]);
/// let b = Int8Array::from(&[Some(1i8), Some(100i8), Some(0i8)]);
/// let result = checked_sub(&a, &b).unwrap();
/// let result = checked_sub(&a, &b);
/// let expected = Int8Array::from(&[Some(99i8), None, Some(100i8)]);
/// assert_eq!(result, expected);
/// ```
Expand Down
12 changes: 6 additions & 6 deletions src/compute/arithmetics/decimal/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits};
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::add::add;
/// use arrow2::compute::arithmetics::decimal::add;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
Expand Down Expand Up @@ -64,14 +64,14 @@ pub fn add(lhs: &PrimitiveArray<i128>, rhs: &PrimitiveArray<i128>) -> PrimitiveA
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::add::saturating_add;
/// use arrow2::compute::arithmetics::decimal::saturating_add;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
/// let a = PrimitiveArray::from([Some(99000i128), Some(11100i128), None, Some(22200i128)]).to(DataType::Decimal(5, 2));
/// let b = PrimitiveArray::from([Some(01000i128), Some(22200i128), None, Some(11100i128)]).to(DataType::Decimal(5, 2));
///
/// let result = saturating_add(&a, &b).unwrap();
/// let result = saturating_add(&a, &b);
/// let expected = PrimitiveArray::from([Some(99999i128), Some(33300i128), None, Some(33300i128)]).to(DataType::Decimal(5, 2));
///
/// assert_eq!(result, expected);
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn saturating_add(
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::add::checked_add;
/// use arrow2::compute::arithmetics::decimal::checked_add;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
Expand Down Expand Up @@ -172,13 +172,13 @@ impl ArraySaturatingAdd<PrimitiveArray<i128>> for PrimitiveArray<i128> {
/// ```
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::add::adaptive_add;
/// use arrow2::compute::arithmetics::decimal::adaptive_add;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
/// let a = PrimitiveArray::from([Some(11111_11i128)]).to(DataType::Decimal(7, 2));
/// let b = PrimitiveArray::from([Some(11111_111i128)]).to(DataType::Decimal(8, 3));
/// let result = adaptive_add(&a, &b);
/// let result = adaptive_add(&a, &b).unwrap();
/// let expected = PrimitiveArray::from([Some(22222_221i128)]).to(DataType::Decimal(8, 3));
///
/// assert_eq!(result, expected);
Expand Down
10 changes: 5 additions & 5 deletions src/compute/arithmetics/decimal/div.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use super::{adjusted_precision_scale, get_parameters, max_value, number_digits};
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::div::div;
/// use arrow2::compute::arithmetics::decimal::div;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn div(lhs: &PrimitiveArray<i128>, rhs: &PrimitiveArray<i128>) -> PrimitiveA
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::div::saturating_div;
/// use arrow2::compute::arithmetics::decimal::saturating_div;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
Expand Down Expand Up @@ -126,7 +126,7 @@ pub fn saturating_div(
///
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::div::checked_div;
/// use arrow2::compute::arithmetics::decimal::checked_div;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
Expand Down Expand Up @@ -188,13 +188,13 @@ impl ArrayCheckedDiv<PrimitiveArray<i128>> for PrimitiveArray<i128> {
/// ```
/// # Examples
/// ```
/// use arrow2::compute::arithmetics::decimal::div::adaptive_div;
/// use arrow2::compute::arithmetics::decimal::adaptive_div;
/// use arrow2::array::PrimitiveArray;
/// use arrow2::datatypes::DataType;
///
/// let a = PrimitiveArray::from([Some(1000_00i128)]).to(DataType::Decimal(7, 2));
/// let b = PrimitiveArray::from([Some(10_0000i128)]).to(DataType::Decimal(6, 4));
/// let result = adaptive_div(&a, &b);
/// let result = adaptive_div(&a, &b).unwrap();
/// let expected = PrimitiveArray::from([Some(100_0000i128)]).to(DataType::Decimal(9, 4));
///
/// assert_eq!(result, expected);
Expand Down
Loading

0 comments on commit 34e0eba

Please sign in to comment.