Skip to content

Commit

Permalink
Rename Typed to Composite (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Mar 19, 2024
1 parent 8402ccc commit 69701eb
Show file tree
Hide file tree
Showing 14 changed files with 158 additions and 130 deletions.
4 changes: 2 additions & 2 deletions bench-vortex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ use vortex::array::bool::BoolEncoding;
use vortex::array::chunked::{ChunkedArray, ChunkedEncoding};
use vortex::array::constant::ConstantEncoding;

use vortex::array::composite::CompositeEncoding;
use vortex::array::downcast::DowncastArrayBuiltin;
use vortex::array::primitive::PrimitiveEncoding;
use vortex::array::sparse::SparseEncoding;
use vortex::array::struct_::StructEncoding;
use vortex::array::typed::TypedEncoding;
use vortex::array::varbin::VarBinEncoding;
use vortex::array::varbinview::VarBinViewEncoding;
use vortex::array::{Array, ArrayRef, Encoding};
Expand All @@ -34,11 +34,11 @@ pub fn enumerate_arrays() -> Vec<&'static dyn Encoding> {
// Builtins
&BoolEncoding,
&ChunkedEncoding,
&CompositeEncoding,
&ConstantEncoding,
&PrimitiveEncoding,
&SparseEncoding,
&StructEncoding,
&TypedEncoding,
&VarBinEncoding,
&VarBinViewEncoding,
// Encodings
Expand Down
12 changes: 6 additions & 6 deletions pyvortex/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use pyo3::prelude::*;

use vortex::array::bool::BoolArray;
use vortex::array::chunked::ChunkedArray;
use vortex::array::composite::CompositeArray;
use vortex::array::constant::ConstantArray;
use vortex::array::primitive::PrimitiveArray;
use vortex::array::sparse::SparseArray;
use vortex::array::struct_::StructArray;
use vortex::array::typed::TypedArray;
use vortex::array::varbin::VarBinArray;
use vortex::array::varbinview::VarBinViewArray;
use vortex::array::{Array, ArrayKind, ArrayRef};
Expand Down Expand Up @@ -53,11 +53,11 @@ macro_rules! pyarray {

pyarray!(BoolArray, "BoolArray");
pyarray!(ChunkedArray, "ChunkedArray");
pyarray!(CompositeArray, "CompositeArray");
pyarray!(ConstantArray, "ConstantArray");
pyarray!(PrimitiveArray, "PrimitiveArray");
pyarray!(SparseArray, "SparseArray");
pyarray!(StructArray, "StructArray");
pyarray!(TypedArray, "TypedArray");
pyarray!(VarBinArray, "VarBinArray");
pyarray!(VarBinViewArray, "VarBinViewArray");

Expand All @@ -82,6 +82,10 @@ impl PyArray {
PyChunkedArray::wrap(py, inner.into_any().downcast::<ChunkedArray>().unwrap())?
.extract(py)
}
ArrayKind::Composite(_) => {
PyCompositeArray::wrap(py, inner.into_any().downcast::<CompositeArray>().unwrap())?
.extract(py)
}
ArrayKind::Constant(_) => {
PyConstantArray::wrap(py, inner.into_any().downcast::<ConstantArray>().unwrap())?
.extract(py)
Expand All @@ -98,10 +102,6 @@ impl PyArray {
PyStructArray::wrap(py, inner.into_any().downcast::<StructArray>().unwrap())?
.extract(py)
}
ArrayKind::Typed(_) => {
PyTypedArray::wrap(py, inner.into_any().downcast::<TypedArray>().unwrap())?
.extract(py)
}
ArrayKind::VarBin(_) => {
PyVarBinArray::wrap(py, inner.into_any().downcast::<VarBinArray>().unwrap())?
.extract(py)
Expand Down
2 changes: 1 addition & 1 deletion pyvortex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ fn _lib(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<PyBoolArray>()?;
m.add_class::<PyBitPackedArray>()?;
m.add_class::<PyChunkedArray>()?;
m.add_class::<PyCompositeArray>()?;
m.add_class::<PyConstantArray>()?;
m.add_class::<PyFoRArray>()?;
m.add_class::<PyPrimitiveArray>()?;
m.add_class::<PyREEArray>()?;
m.add_class::<PySparseArray>()?;
m.add_class::<PyStructArray>()?;
m.add_class::<PyTypedArray>()?;
m.add_class::<PyVarBinArray>()?;
m.add_class::<PyVarBinViewArray>()?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::array::typed::TypedArray;
use crate::array::composite::CompositeArray;
use crate::array::Array;
use crate::arrow::wrappers::as_nulls;
use crate::composite_dtypes::{TimeUnit, TimeUnitSerializer};
Expand All @@ -17,12 +17,12 @@ use arrow_array::{
use arrow_buffer::NullBuffer;
use std::sync::Arc;

impl AsArrowArray for TypedArray {
impl AsArrowArray for CompositeArray {
fn as_arrow(&self) -> VortexResult<ArrowArrayRef> {
// Decide based on the DType if we know how to do this or not...
match self.dtype() {
DType::Composite(id, _dtype, metadata) => match id.as_str() {
"zoneddatetime" => hacky_zoneddatetime_as_arrow(self.untyped_array(), metadata),
"zoneddatetime" => hacky_zoneddatetime_as_arrow(self.underlying(), metadata),
&_ => Err(VortexError::InvalidArgument(
format!("Cannot convert composite DType {} to arrow", id).into(),
)),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::array::composite::{CompositeArray, CompositeEncoding};
use crate::array::downcast::DowncastArrayBuiltin;
use crate::array::typed::{TypedArray, TypedEncoding};
use crate::array::{Array, ArrayRef};
use crate::compress::{CompressConfig, CompressCtx, EncodingCompression};
use crate::error::VortexResult;

impl EncodingCompression for TypedEncoding {
impl EncodingCompression for CompositeEncoding {
fn cost(&self) -> u8 {
0
}
Expand All @@ -23,15 +23,16 @@ impl EncodingCompression for TypedEncoding {
like: Option<&dyn Array>,
ctx: CompressCtx,
) -> VortexResult<ArrayRef> {
let typed_array = array.as_typed();
let typed_like = like.map(|like_array| like_array.as_typed());
let composite_array = array.as_composite();
let composite_like = like.map(|like_array| like_array.as_composite());

Ok(TypedArray::new(
Ok(CompositeArray::new(
composite_array.id(),
composite_array.metadata().clone(),
ctx.compress(
typed_array.untyped_array(),
typed_like.map(|typed_arr| typed_arr.untyped_array()),
composite_array.underlying(),
composite_like.map(|c| c.underlying()),
)?,
array.dtype().clone(),
)
.boxed())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use itertools::Itertools;

use crate::array::composite::CompositeArray;
use crate::array::downcast::DowncastArrayBuiltin;
use crate::array::typed::TypedArray;
use crate::array::{Array, ArrayRef};
use crate::compute::as_arrow::AsArrowArray;
use crate::compute::as_contiguous::{as_contiguous, AsContiguousFn};
Expand All @@ -11,7 +11,7 @@ use crate::compute::ArrayCompute;
use crate::error::VortexResult;
use crate::scalar::Scalar;

impl ArrayCompute for TypedArray {
impl ArrayCompute for CompositeArray {
fn as_arrow(&self) -> Option<&dyn AsArrowArray> {
Some(self)
}
Expand All @@ -29,30 +29,31 @@ impl ArrayCompute for TypedArray {
}
}

impl AsContiguousFn for TypedArray {
impl AsContiguousFn for CompositeArray {
fn as_contiguous(&self, arrays: Vec<ArrayRef>) -> VortexResult<ArrayRef> {
Ok(TypedArray::new(
Ok(CompositeArray::new(
self.id(),
self.metadata().clone(),
as_contiguous(
arrays
.into_iter()
.map(|array| dyn_clone::clone_box(array.as_typed().untyped_array()))
.map(|array| dyn_clone::clone_box(array.as_composite().underlying()))
.collect_vec(),
)?,
self.dtype().clone(),
)
.boxed())
}
}

impl FlattenFn for TypedArray {
impl FlattenFn for CompositeArray {
fn flatten(&self) -> VortexResult<FlattenedArray> {
Ok(FlattenedArray::Typed(self.clone()))
Ok(FlattenedArray::Composite(self.clone()))
}
}

impl ScalarAtFn for TypedArray {
impl ScalarAtFn for CompositeArray {
fn scalar_at(&self, index: usize) -> VortexResult<Scalar> {
let underlying = scalar_at(self.array.as_ref(), index)?;
let underlying = scalar_at(self.underlying(), index)?;
underlying.cast(self.dtype())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use linkme::distributed_slice;

use crate::array::{Array, ArrayRef, Encoding, EncodingId, EncodingRef, ENCODINGS};
use crate::compress::EncodingCompression;
use crate::dtype::DType;
use crate::dtype::{DType, Metadata};
use crate::error::VortexResult;
use crate::formatter::{ArrayDisplay, ArrayFormatter};
use crate::serde::{ArraySerde, EncodingSerde};
Expand All @@ -17,28 +17,43 @@ mod compute;
mod serde;

#[derive(Debug, Clone)]
pub struct TypedArray {
array: ArrayRef,
pub struct CompositeArray {
underlying: ArrayRef,
dtype: DType,
stats: Arc<RwLock<StatsSet>>,
}

impl TypedArray {
pub fn new(array: ArrayRef, dtype: DType) -> Self {
impl CompositeArray {
pub fn new(id: Arc<String>, metadata: Metadata, underlying: ArrayRef) -> Self {
let dtype = DType::Composite(id, Box::new(underlying.dtype().clone()), metadata);
Self {
array,
underlying,
dtype,
stats: Arc::new(RwLock::new(StatsSet::new())),
}
}

pub fn id(&self) -> Arc<String> {
let DType::Composite(id, _, _) = &self.dtype else {
unreachable!()
};
id.clone()
}

pub fn metadata(&self) -> &Metadata {
let DType::Composite(_, _, metadata) = &self.dtype else {
unreachable!()
};
metadata
}

#[inline]
pub fn untyped_array(&self) -> &dyn Array {
self.array.as_ref()
pub fn underlying(&self) -> &dyn Array {
self.underlying.as_ref()
}
}

impl Array for TypedArray {
impl Array for CompositeArray {
#[inline]
fn as_any(&self) -> &dyn Any {
self
Expand All @@ -56,12 +71,12 @@ impl Array for TypedArray {

#[inline]
fn len(&self) -> usize {
self.array.len()
self.underlying.len()
}

#[inline]
fn is_empty(&self) -> bool {
self.array.is_empty()
self.underlying.is_empty()
}

#[inline]
Expand All @@ -75,43 +90,48 @@ impl Array for TypedArray {
}

fn slice(&self, start: usize, stop: usize) -> VortexResult<ArrayRef> {
Ok(Self::new(self.array.slice(start, stop)?, self.dtype.clone()).boxed())
Ok(Self::new(
self.id().clone(),
self.metadata().clone(),
self.underlying.slice(start, stop)?,
)
.boxed())
}

#[inline]
fn encoding(&self) -> EncodingRef {
&TypedEncoding
&CompositeEncoding
}

#[inline]
fn nbytes(&self) -> usize {
self.array.nbytes()
self.underlying.nbytes()
}

fn serde(&self) -> Option<&dyn ArraySerde> {
Some(self)
}
}

impl StatsCompute for TypedArray {}
impl StatsCompute for CompositeArray {}

impl<'arr> AsRef<(dyn Array + 'arr)> for TypedArray {
impl<'arr> AsRef<(dyn Array + 'arr)> for CompositeArray {
fn as_ref(&self) -> &(dyn Array + 'arr) {
self
}
}

#[derive(Debug)]
pub struct TypedEncoding;
pub struct CompositeEncoding;

impl TypedEncoding {
pub const ID: EncodingId = EncodingId::new("vortex.typed");
impl CompositeEncoding {
pub const ID: EncodingId = EncodingId::new("vortex.composite");
}

#[distributed_slice(ENCODINGS)]
static ENCODINGS_TYPED: EncodingRef = &TypedEncoding;
static ENCODINGS_COMPOSITE: EncodingRef = &CompositeEncoding;

impl Encoding for TypedEncoding {
impl Encoding for CompositeEncoding {
fn id(&self) -> &EncodingId {
&Self::ID
}
Expand All @@ -125,40 +145,41 @@ impl Encoding for TypedEncoding {
}
}

impl ArrayDisplay for TypedArray {
impl ArrayDisplay for CompositeArray {
fn fmt(&self, f: &mut ArrayFormatter) -> std::fmt::Result {
f.child("untyped", self.untyped_array())
f.child("composite", self.underlying())
}
}

#[cfg(test)]
mod test {
use crate::array::typed::TypedArray;
use crate::composite_dtypes::{localtime, TimeUnit};
use super::*;
use crate::composite_dtypes::{localtime, TimeUnit, TimeUnitSerializer};
use crate::compute::scalar_at::scalar_at;
use crate::dtype::{IntWidth, Nullability};
use crate::scalar::{CompositeScalar, PScalar, PrimitiveScalar};

#[test]
pub fn scalar() {
let dtype = localtime(TimeUnit::Us, IntWidth::_64, Nullability::NonNullable);
let arr = TypedArray::new(
vec![64_799_000_000_u64, 43_000_000_000].into(),
dtype.clone(),
let arr = CompositeArray::new(
Arc::new("localtime".into()),
TimeUnitSerializer::serialize(TimeUnit::Us),
vec![64_799_000_000_i64, 43_000_000_000].into(),
);
assert_eq!(
scalar_at(arr.as_ref(), 0).unwrap(),
CompositeScalar::new(
dtype.clone(),
Box::new(PrimitiveScalar::some(PScalar::U64(64_799_000_000)).into()),
Box::new(PrimitiveScalar::some(PScalar::I64(64_799_000_000)).into()),
)
.into()
);
assert_eq!(
scalar_at(arr.as_ref(), 1).unwrap(),
CompositeScalar::new(
dtype.clone(),
Box::new(PrimitiveScalar::some(PScalar::U64(43_000_000_000)).into()),
Box::new(PrimitiveScalar::some(PScalar::I64(43_000_000_000)).into()),
)
.into()
);
Expand Down
Loading

0 comments on commit 69701eb

Please sign in to comment.