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 223
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
8992dac
commit ae0e9a9
Showing
11 changed files
with
803 additions
and
221 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate::{array::*, buffer::Buffer, datatypes::DataType}; | ||
|
||
use super::Scalar; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct BinaryScalar<O: Offset> { | ||
value: Buffer<u8>, | ||
is_valid: bool, | ||
phantom: std::marker::PhantomData<O>, | ||
} | ||
|
||
impl<O: Offset> PartialEq for BinaryScalar<O> { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.is_valid == other.is_valid && ((!self.is_valid) | (self.value == other.value)) | ||
} | ||
} | ||
|
||
impl<O: Offset> BinaryScalar<O> { | ||
#[inline] | ||
pub fn new<P: AsRef<[u8]>>(v: Option<P>) -> Self { | ||
let is_valid = v.is_some(); | ||
O::from_usize(v.as_ref().map(|x| x.as_ref().len()).unwrap_or_default()).expect("Too large"); | ||
let value = Buffer::from(v.as_ref().map(|x| x.as_ref()).unwrap_or(&[])); | ||
Self { | ||
value, | ||
is_valid, | ||
phantom: std::marker::PhantomData, | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn value(&self) -> &[u8] { | ||
self.value.as_slice() | ||
} | ||
} | ||
|
||
impl<O: Offset, P: AsRef<[u8]>> From<Option<P>> for BinaryScalar<O> { | ||
#[inline] | ||
fn from(v: Option<P>) -> Self { | ||
Self::new(v) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[allow(clippy::eq_op)] | ||
#[test] | ||
fn equal() { | ||
let a = BinaryScalar::<i32>::from(Some("a")); | ||
let b = BinaryScalar::<i32>::from(None::<&str>); | ||
assert_eq!(a, a); | ||
assert_eq!(b, b); | ||
assert!(a != b); | ||
let b = BinaryScalar::<i32>::from(Some("b")); | ||
assert!(a != b); | ||
assert_eq!(b, b); | ||
} | ||
|
||
#[test] | ||
fn basics() { | ||
let a = BinaryScalar::<i32>::from(Some("a")); | ||
|
||
assert_eq!(a.value(), b"a"); | ||
assert_eq!(a.data_type(), &DataType::Binary); | ||
assert!(a.is_valid()); | ||
|
||
let a = BinaryScalar::<i64>::from(None::<&str>); | ||
|
||
assert_eq!(a.data_type(), &DataType::LargeBinary); | ||
assert!(!a.is_valid()); | ||
} | ||
} |
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,82 @@ | ||
use crate::datatypes::DataType; | ||
|
||
use super::Scalar; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct BooleanScalar { | ||
value: bool, | ||
is_valid: bool, | ||
} | ||
|
||
impl PartialEq for BooleanScalar { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.is_valid == other.is_valid && ((!self.is_valid) | (self.value == other.value)) | ||
} | ||
} | ||
|
||
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 | ||
} | ||
} | ||
|
||
impl From<Option<bool>> for BooleanScalar { | ||
#[inline] | ||
fn from(v: Option<bool>) -> Self { | ||
Self::new(v) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[allow(clippy::eq_op)] | ||
#[test] | ||
fn equal() { | ||
let a = BooleanScalar::from(Some(true)); | ||
let b = BooleanScalar::from(None); | ||
assert_eq!(a, a); | ||
assert_eq!(b, b); | ||
assert!(a != b); | ||
let b = BooleanScalar::from(Some(false)); | ||
assert!(a != b); | ||
assert_eq!(b, b); | ||
} | ||
|
||
#[test] | ||
fn basics() { | ||
let a = BooleanScalar::new(Some(true)); | ||
|
||
assert!(a.value()); | ||
assert_eq!(a.data_type(), &DataType::Boolean); | ||
assert!(a.is_valid()); | ||
} | ||
} |
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!(), | ||
} | ||
} |
Oops, something went wrong.