This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added remaining scalars and improved API.
- Loading branch information
1 parent
d086e7d
commit adeeb29
Showing
12 changed files
with
618 additions
and
220 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
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,50 @@ | ||
use crate::{array::*, buffer::Buffer, datatypes::DataType}; | ||
|
||
use super::Scalar; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct BinaryScalar<O: Offset> { | ||
value: Buffer<u8>, | ||
is_valid: bool, | ||
phantom: std::marker::PhantomData<O>, | ||
} | ||
|
||
impl<O: Offset> BinaryScalar<O> { | ||
#[inline] | ||
pub fn new(v: Option<&[u8]>) -> Self { | ||
let is_valid = v.is_some(); | ||
O::from_usize(v.map(|x| x.len()).unwrap_or_default()).expect("Too large"); | ||
let value = Buffer::from(v.unwrap_or(&[])); | ||
Self { | ||
value, | ||
is_valid, | ||
phantom: std::marker::PhantomData, | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn value(&self) -> &[u8] { | ||
self.value.as_slice() | ||
} | ||
} | ||
|
||
impl<O: Offset> Scalar for BinaryScalar<O> { | ||
#[inline] | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
#[inline] | ||
fn is_valid(&self) -> bool { | ||
self.is_valid | ||
} | ||
|
||
#[inline] | ||
fn data_type(&self) -> &DataType { | ||
if O::is_large() { | ||
&DataType::LargeBinary | ||
} else { | ||
&DataType::Binary | ||
} | ||
} | ||
} |
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,42 @@ | ||
use crate::datatypes::DataType; | ||
|
||
use super::Scalar; | ||
|
||
#[derive(Debug, Clone, PartialEq)] | ||
pub struct BooleanScalar { | ||
value: bool, | ||
is_valid: bool, | ||
} | ||
|
||
impl BooleanScalar { | ||
#[inline] | ||
pub fn new(v: Option<bool>) -> Self { | ||
let is_valid = v.is_some(); | ||
Self { | ||
value: v.unwrap_or_default(), | ||
is_valid, | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn value(&self) -> bool { | ||
self.value | ||
} | ||
} | ||
|
||
impl Scalar for BooleanScalar { | ||
#[inline] | ||
fn as_any(&self) -> &dyn std::any::Any { | ||
self | ||
} | ||
|
||
#[inline] | ||
fn is_valid(&self) -> bool { | ||
self.is_valid | ||
} | ||
|
||
#[inline] | ||
fn data_type(&self) -> &DataType { | ||
&DataType::Boolean | ||
} | ||
} |
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,115 @@ | ||
use super::*; | ||
|
||
impl PartialEq for dyn Scalar { | ||
fn eq(&self, other: &Self) -> bool { | ||
equal(self, other) | ||
} | ||
} | ||
|
||
macro_rules! dyn_eq { | ||
($ty:ty, $lhs:expr, $rhs:expr) => {{ | ||
let lhs = $lhs | ||
.as_any() | ||
.downcast_ref::<PrimitiveScalar<$ty>>() | ||
.unwrap(); | ||
let rhs = $rhs | ||
.as_any() | ||
.downcast_ref::<PrimitiveScalar<$ty>>() | ||
.unwrap(); | ||
lhs == rhs | ||
}}; | ||
} | ||
|
||
fn equal(lhs: &dyn Scalar, rhs: &dyn Scalar) -> bool { | ||
if lhs.data_type() != rhs.data_type() { | ||
return false; | ||
} | ||
|
||
match lhs.data_type() { | ||
DataType::Null => { | ||
let lhs = lhs.as_any().downcast_ref::<NullScalar>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<NullScalar>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::Boolean => { | ||
let lhs = lhs.as_any().downcast_ref::<BooleanScalar>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<BooleanScalar>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::UInt8 => { | ||
dyn_eq!(u8, lhs, rhs) | ||
} | ||
DataType::UInt16 => { | ||
dyn_eq!(u16, lhs, rhs) | ||
} | ||
DataType::UInt32 => { | ||
dyn_eq!(u32, lhs, rhs) | ||
} | ||
DataType::UInt64 => { | ||
dyn_eq!(u64, lhs, rhs) | ||
} | ||
DataType::Int8 => { | ||
dyn_eq!(i8, lhs, rhs) | ||
} | ||
DataType::Int16 => { | ||
dyn_eq!(i16, lhs, rhs) | ||
} | ||
DataType::Int32 | ||
| DataType::Date32 | ||
| DataType::Time32(_) | ||
| DataType::Interval(IntervalUnit::YearMonth) => { | ||
dyn_eq!(i32, lhs, rhs) | ||
} | ||
DataType::Int64 | ||
| DataType::Date64 | ||
| DataType::Time64(_) | ||
| DataType::Timestamp(_, _) | ||
| DataType::Duration(_) => { | ||
dyn_eq!(i64, lhs, rhs) | ||
} | ||
DataType::Decimal(_, _) => { | ||
dyn_eq!(i128, lhs, rhs) | ||
} | ||
DataType::Interval(IntervalUnit::DayTime) => { | ||
dyn_eq!(days_ms, lhs, rhs) | ||
} | ||
DataType::Float16 => unreachable!(), | ||
DataType::Float32 => { | ||
dyn_eq!(f32, lhs, rhs) | ||
} | ||
DataType::Float64 => { | ||
dyn_eq!(f64, lhs, rhs) | ||
} | ||
DataType::Utf8 => { | ||
let lhs = lhs.as_any().downcast_ref::<Utf8Scalar<i32>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<Utf8Scalar<i32>>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::LargeUtf8 => { | ||
let lhs = lhs.as_any().downcast_ref::<Utf8Scalar<i64>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<Utf8Scalar<i64>>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::Binary => { | ||
let lhs = lhs.as_any().downcast_ref::<BinaryScalar<i32>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<BinaryScalar<i32>>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::LargeBinary => { | ||
let lhs = lhs.as_any().downcast_ref::<BinaryScalar<i64>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<BinaryScalar<i64>>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::List(_) => { | ||
let lhs = lhs.as_any().downcast_ref::<ListScalar<i32>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<ListScalar<i32>>().unwrap(); | ||
lhs == rhs | ||
} | ||
DataType::LargeList(_) => { | ||
let lhs = lhs.as_any().downcast_ref::<ListScalar<i64>>().unwrap(); | ||
let rhs = rhs.as_any().downcast_ref::<ListScalar<i64>>().unwrap(); | ||
lhs == rhs | ||
} | ||
_ => unimplemented!(), | ||
} | ||
} |
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,62 @@ | ||
use std::any::Any; | ||
use std::sync::Arc; | ||
|
||
use crate::{array::*, datatypes::DataType}; | ||
|
||
use super::Scalar; | ||
|
||
/// The scalar equivalent of [`ListArray`]. Like [`ListArray`], this struct holds a dynamically-typed | ||
/// [`Array`]. The only difference is that this has only one element. | ||
#[derive(Debug, Clone)] | ||
pub struct ListScalar<O: Offset> { | ||
values: Arc<dyn Array>, | ||
is_valid: bool, | ||
phantom: std::marker::PhantomData<O>, | ||
data_type: DataType, | ||
} | ||
|
||
impl<O: Offset> PartialEq for ListScalar<O> { | ||
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())) | ||
} | ||
} | ||
|
||
pub enum ListScalarNew { | ||
Array(Arc<dyn Array>), | ||
DataType(DataType), | ||
} | ||
|
||
impl<O: Offset> ListScalar<O> { | ||
#[inline] | ||
pub fn new(data_type: DataType, values: Option<Arc<dyn Array>>) -> Self { | ||
let (is_valid, values) = match values { | ||
Some(values) => (true, values), | ||
None => { | ||
let data_type = ListArray::<O>::get_child_type(&data_type).clone(); | ||
(false, new_empty_array(data_type).into()) | ||
} | ||
}; | ||
Self { | ||
values, | ||
is_valid, | ||
phantom: std::marker::PhantomData, | ||
data_type, | ||
} | ||
} | ||
} | ||
|
||
impl<O: Offset> Scalar for ListScalar<O> { | ||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
|
||
fn is_valid(&self) -> bool { | ||
self.is_valid | ||
} | ||
|
||
fn data_type(&self) -> &DataType { | ||
&self.data_type | ||
} | ||
} |
Oops, something went wrong.