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

Added Array::with_validity #399

Merged
merged 3 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion src/array/binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType};
use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::DataType,
error::{ArrowError, Result},
};

use super::{
display_fmt, display_helper, specification::check_offsets, specification::Offset, Array,
Expand Down Expand Up @@ -159,6 +164,16 @@ impl<O: Offset> Array for BinaryArray<O> {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl<O: Offset> std::fmt::Display for BinaryArray<O> {
Expand Down
11 changes: 11 additions & 0 deletions src/array/boolean/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
bitmap::Bitmap,
datatypes::{DataType, PhysicalType},
error::{ArrowError, Result},
};

use super::{display_fmt, Array};
Expand Down Expand Up @@ -118,6 +119,16 @@ impl Array for BooleanArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
ritchie46 marked this conversation as resolved.
Show resolved Hide resolved
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl std::fmt::Display for BooleanArray {
Expand Down
4 changes: 4 additions & 0 deletions src/array/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use crate::{
bitmap::Bitmap,
datatypes::DataType,
error::Result,
scalar::{new_scalar, Scalar},
types::{NativeType, NaturalDataType},
};
Expand Down Expand Up @@ -137,6 +138,9 @@ impl<K: DictionaryKey> Array for DictionaryArray<K> {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
self.values.with_validity(validity)
}
}

impl<K: DictionaryKey> std::fmt::Display for DictionaryArray<K>
Expand Down
17 changes: 16 additions & 1 deletion src/array/fixed_size_binary/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType, error::Result};
use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::DataType,
error::{ArrowError, Result},
};

use super::{display_fmt, display_helper, ffi::ToFfi, Array};

Expand Down Expand Up @@ -128,6 +133,16 @@ impl Array for FixedSizeBinaryArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl std::fmt::Display for FixedSizeBinaryArray {
Expand Down
11 changes: 11 additions & 0 deletions src/array/fixed_size_list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use crate::{
bitmap::Bitmap,
datatypes::{DataType, Field},
error::{ArrowError, Result},
};

use super::{display_fmt, ffi::ToFfi, new_empty_array, new_null_array, Array};
Expand Down Expand Up @@ -131,6 +132,16 @@ impl Array for FixedSizeListArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl std::fmt::Display for FixedSizeListArray {
Expand Down
11 changes: 11 additions & 0 deletions src/array/list/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::{DataType, Field},
error::{ArrowError, Result},
};

use super::{
Expand Down Expand Up @@ -174,6 +175,16 @@ impl<O: Offset> Array for ListArray<O> {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl<O: Offset> std::fmt::Display for ListArray<O> {
Expand Down
3 changes: 3 additions & 0 deletions src/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ pub trait Array: std::fmt::Debug + Send + Sync {
/// # Panic
/// This function panics iff `offset + length >= self.len()`.
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array>;

/// Sets the validity bitmap on this [`Array`].
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>>;
}

/// A trait describing a mutable array; i.e. an array whose values can be changed.
Expand Down
11 changes: 10 additions & 1 deletion src/array/null.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use crate::{bitmap::Bitmap, datatypes::DataType};
use crate::{
bitmap::Bitmap,
datatypes::DataType,
error::{ArrowError, Result},
};

use super::{ffi::ToFfi, Array};

Expand Down Expand Up @@ -63,6 +67,11 @@ impl Array for NullArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, _: Option<Bitmap>) -> Result<Box<dyn Array>> {
Err(ArrowError::Other(
"cannot set validity of a null array".into(),
))
}
}

impl std::fmt::Display for NullArray {
Expand Down
12 changes: 11 additions & 1 deletion src/array/primitive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::*,
error::ArrowError,
error::{ArrowError, Result},
types::{days_ms, months_days_ns, NativeType},
};

Expand Down Expand Up @@ -162,6 +162,16 @@ impl<T: NativeType> Array for PrimitiveArray<T> {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

/// A type definition [`PrimitiveArray`] for `i8`
Expand Down
12 changes: 11 additions & 1 deletion src/array/struct_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Arc;
use crate::{
bitmap::Bitmap,
datatypes::{DataType, Field},
error::Result,
error::{ArrowError, Result},
ffi,
};

Expand Down Expand Up @@ -163,6 +163,16 @@ impl Array for StructArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl std::fmt::Display for StructArray {
Expand Down
6 changes: 6 additions & 0 deletions src/array/union/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::{DataType, Field},
error::{ArrowError, Result},
scalar::{new_scalar, Scalar},
};

Expand Down Expand Up @@ -212,6 +213,11 @@ impl Array for UnionArray {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, _: Option<Bitmap>) -> Result<Box<dyn Array>> {
Err(ArrowError::Other(
"cannot set validity of a union array".into(),
))
}
}

impl UnionArray {
Expand Down
17 changes: 16 additions & 1 deletion src/array/utf8/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use crate::{bitmap::Bitmap, buffer::Buffer, datatypes::DataType};
use crate::{
bitmap::Bitmap,
buffer::Buffer,
datatypes::DataType,
error::{ArrowError, Result},
};

use super::{
display_fmt,
Expand Down Expand Up @@ -199,6 +204,16 @@ impl<O: Offset> Array for Utf8Array<O> {
fn slice(&self, offset: usize, length: usize) -> Box<dyn Array> {
Box::new(self.slice(offset, length))
}
fn with_validity(&self, validity: Option<Bitmap>) -> Result<Box<dyn Array>> {
if matches!(&validity, Some(bitmap) if bitmap.len() < self.len()) {
return Err(ArrowError::InvalidArgumentError(
"validity should be as least as large as the array".into(),
));
}
let mut arr = self.clone();
arr.validity = validity;
Ok(Box::new(arr))
}
}

impl<O: Offset> std::fmt::Display for Utf8Array<O> {
Expand Down
15 changes: 14 additions & 1 deletion tests/it/array/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ mod primitive;
mod union;
mod utf8;

use arrow2::array::{clone, new_empty_array, new_null_array};
use arrow2::array::{clone, new_empty_array, new_null_array, Array, PrimitiveArray};
use arrow2::bitmap::Bitmap;
use arrow2::datatypes::PhysicalType::Primitive;
use arrow2::datatypes::{DataType, Field};

#[test]
Expand Down Expand Up @@ -68,3 +70,14 @@ fn test_clone() {
.all(|x| clone(new_null_array(x.clone(), 10).as_ref()) == new_null_array(x, 10));
assert!(a);
}

#[test]
fn test_with_validity() {
let arr = PrimitiveArray::from_slice(&[1i32, 2, 3]);
let validity = Bitmap::from(&[true, false, true]);
let arr = arr.with_validity(Some(validity)).unwrap();
let arr_ref = arr.as_any().downcast_ref::<PrimitiveArray<i32>>().unwrap();

let expected = PrimitiveArray::from(&[Some(1i32), None, Some(3)]);
assert_eq!(arr_ref, &expected);
}