forked from jorgecarleitao/arrow2
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MapScalar (jorgecarleitao#1428)
- Loading branch information
Showing
5 changed files
with
147 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::any::Any; | ||
|
||
use crate::{array::*, datatypes::DataType}; | ||
|
||
use super::Scalar; | ||
|
||
/// The scalar equivalent of [`MapArray`]. Like [`MapArray`], this struct holds a dynamically-typed | ||
/// [`Array`]. The only difference is that this has only one element. | ||
#[derive(Debug, Clone)] | ||
pub struct MapScalar { | ||
values: Box<dyn Array>, | ||
is_valid: bool, | ||
data_type: DataType, | ||
} | ||
|
||
impl PartialEq for MapScalar { | ||
fn eq(&self, other: &Self) -> bool { | ||
(self.data_type == other.data_type) | ||
&& (self.is_valid == other.is_valid) | ||
&& ((!self.is_valid) | (self.values.as_ref() == other.values.as_ref())) | ||
} | ||
} | ||
|
||
impl MapScalar { | ||
/// returns a new [`MapScalar`] | ||
/// # Panics | ||
/// iff | ||
/// * the `data_type` is not `Map` | ||
/// * the child of the `data_type` is not equal to the `values` | ||
#[inline] | ||
pub fn new(data_type: DataType, values: Option<Box<dyn Array>>) -> Self { | ||
let inner_field = MapArray::try_get_field(&data_type).unwrap(); | ||
let inner_data_type = inner_field.data_type(); | ||
let (is_valid, values) = match values { | ||
Some(values) => { | ||
assert_eq!(inner_data_type, values.data_type()); | ||
(true, values) | ||
} | ||
None => (false, new_empty_array(inner_data_type.clone())), | ||
}; | ||
Self { | ||
values, | ||
is_valid, | ||
data_type, | ||
} | ||
} | ||
|
||
/// The values of the [`MapScalar`] | ||
pub fn values(&self) -> &Box<dyn Array> { | ||
&self.values | ||
} | ||
} | ||
|
||
impl Scalar for MapScalar { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn is_valid(&self) -> bool { | ||
self.is_valid | ||
} | ||
|
||
fn data_type(&self) -> &DataType { | ||
&self.data_type | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use arrow2::{ | ||
array::{BooleanArray, StructArray, Utf8Array}, | ||
datatypes::{DataType, Field}, | ||
scalar::{MapScalar, Scalar}, | ||
}; | ||
|
||
#[allow(clippy::eq_op)] | ||
#[test] | ||
fn equal() { | ||
let kv_dt = DataType::Struct(vec![ | ||
Field::new("key", DataType::Utf8, false), | ||
Field::new("value", DataType::Boolean, true), | ||
]); | ||
let kv_array1 = StructArray::try_new( | ||
kv_dt.clone(), | ||
vec![ | ||
Utf8Array::<i32>::from([Some("k1"), Some("k2")]).boxed(), | ||
BooleanArray::from_slice([true, false]).boxed(), | ||
], | ||
None, | ||
) | ||
.unwrap(); | ||
let kv_array2 = StructArray::try_new( | ||
kv_dt.clone(), | ||
vec![ | ||
Utf8Array::<i32>::from([Some("k1"), Some("k3")]).boxed(), | ||
BooleanArray::from_slice([true, true]).boxed(), | ||
], | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let dt = DataType::Map(Box::new(Field::new("entries", kv_dt, true)), false); | ||
let a = MapScalar::new(dt.clone(), Some(Box::new(kv_array1))); | ||
let b = MapScalar::new(dt.clone(), None); | ||
assert_eq!(a, a); | ||
assert_eq!(b, b); | ||
assert!(a != b); | ||
let b = MapScalar::new(dt, Some(Box::new(kv_array2))); | ||
assert!(a != b); | ||
assert_eq!(b, b); | ||
} | ||
|
||
#[test] | ||
fn basics() { | ||
let kv_dt = DataType::Struct(vec![ | ||
Field::new("key", DataType::Utf8, false), | ||
Field::new("value", DataType::Boolean, true), | ||
]); | ||
let kv_array = StructArray::try_new( | ||
kv_dt.clone(), | ||
vec![ | ||
Utf8Array::<i32>::from([Some("k1"), Some("k2")]).boxed(), | ||
BooleanArray::from_slice([true, false]).boxed(), | ||
], | ||
None, | ||
) | ||
.unwrap(); | ||
|
||
let dt = DataType::Map(Box::new(Field::new("entries", kv_dt, true)), false); | ||
let a = MapScalar::new(dt.clone(), Some(Box::new(kv_array.clone()))); | ||
|
||
assert_eq!(kv_array, a.values().as_ref()); | ||
assert_eq!(a.data_type(), &dt); | ||
assert!(a.is_valid()); | ||
|
||
let _: &dyn std::any::Any = a.as_any(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters