Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ConstantArray#_to_canonical supports ListDType #2521

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
101 changes: 90 additions & 11 deletions vortex-array/src/arrays/constant/canonical.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
use arrow_array::builder::make_view;
use arrow_buffer::BooleanBuffer;
use vortex_buffer::{Buffer, BufferMut, buffer};
use vortex_dtype::{DType, Nullability, match_each_native_ptype};
use vortex_dtype::{DType, Nullability, PType, match_each_native_ptype};
use vortex_error::{VortexExpect, VortexResult, vortex_bail};
use vortex_scalar::{BinaryScalar, BoolScalar, ExtScalar, Utf8Scalar};
use vortex_scalar::{
BinaryScalar, BoolScalar, ExtScalar, ListScalar, Scalar, ScalarValue, Utf8Scalar,
};

use crate::array::ArrayCanonicalImpl;
use crate::arrays::constant::ConstantArray;
use crate::arrays::primitive::PrimitiveArray;
use crate::arrays::{BinaryView, BoolArray, ExtensionArray, NullArray, VarBinViewArray};
use crate::arrays::{BinaryView, BoolArray, ExtensionArray, ListArray, NullArray, VarBinViewArray};
use crate::builders::{ArrayBuilderExt, builder_with_capacity};
use crate::validity::Validity;
use crate::{Array, Canonical};
use crate::{Array, Canonical, IntoArray};

impl ArrayCanonicalImpl for ConstantArray {
fn _to_canonical(&self) -> VortexResult<Canonical> {
Expand Down Expand Up @@ -61,7 +64,15 @@ impl ArrayCanonicalImpl for ConstantArray {
Canonical::VarBinView(canonical_byte_view(const_value, self.dtype(), self.len())?)
}
DType::Struct(..) => vortex_bail!("Unsupported scalar type {}", self.dtype()),
DType::List(..) => vortex_bail!("Unsupported scalar type {}", self.dtype()),
DType::List(..) => {
let value = ListScalar::try_from(scalar)?;
Canonical::List(canonical_list_array(
value.elements(),
value.element_dtype(),
value.dtype().nullability(),
self.len(),
)?)
}
DType::Extension(ext_dtype) => {
let s = ExtScalar::try_from(scalar)?;

Expand Down Expand Up @@ -101,19 +112,60 @@ fn canonical_byte_view(
views.push(view);
}

let validity = if dtype.nullability() == Nullability::NonNullable {
Validity::NonNullable
} else {
Validity::AllValid
};
VarBinViewArray::try_new(
views.freeze(),
buffers,
dtype.clone(),
Validity::from(dtype.nullability()),
)
}
}
}

fn canonical_list_array(
values: Option<Vec<Scalar>>,
element_dtype: &DType,
list_nullability: Nullability,
len: usize,
) -> VortexResult<ListArray> {
match values {
None => ListArray::try_new(
ConstantArray::new(Scalar::null(element_dtype.clone()), 1).into_array(),
ConstantArray::new(
Scalar::new(
DType::Primitive(PType::U64, Nullability::NonNullable),
ScalarValue::from(0),
),
len,
)
.into_array(),
Validity::AllInvalid,
),
Some(vs) => {
let mut elements_builder = builder_with_capacity(element_dtype, len * vs.len());
for _ in 0..len {
for v in &vs {
elements_builder.append_scalar(v)?;
}
}
let offsets = (0..=len * vs.len())
.step_by(vs.len())
.map(|i| i as u64)
.collect::<Buffer<_>>();

VarBinViewArray::try_new(views.freeze(), buffers, dtype.clone(), validity)
ListArray::try_new(
elements_builder.finish(),
offsets.into_array(),
Validity::from(list_nullability),
)
}
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;

use enum_iterator::all;
use vortex_dtype::half::f16;
use vortex_dtype::{DType, Nullability, PType};
Expand Down Expand Up @@ -181,4 +233,31 @@ mod tests {
assert_eq!(scalar_at(&canonical_const, 0).unwrap(), scalar);
assert_eq!(scalar_at(&canonical_const, 0).unwrap(), f16_scalar);
}

#[test]
fn test_canonicalize_lists() {
let list_scalar = Scalar::list(
Arc::new(DType::Primitive(PType::U64, Nullability::NonNullable)),
vec![1u64.into(), 2u64.into()],
Nullability::NonNullable,
);
let const_array = ConstantArray::new(list_scalar, 2).into_array();
let canonical_const = const_array.to_list().unwrap();
assert_eq!(
canonical_const
.elements()
.to_primitive()
.unwrap()
.as_slice::<u64>(),
[1u64, 2, 1, 2]
);
assert_eq!(
canonical_const
.offsets()
.to_primitive()
.unwrap()
.as_slice::<u64>(),
[0u64, 2, 4]
);
}
}
8 changes: 4 additions & 4 deletions vortex-scalar/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,19 @@ impl<'a> ListScalar<'a> {
self.elements.is_none()
}

pub fn element_dtype(&self) -> DType {
pub fn element_dtype(&self) -> &DType {
let DType::List(element_type, _) = self.dtype() else {
unreachable!();
};
(*element_type).deref().clone()
(*element_type).deref()
}

pub fn element(&self, idx: usize) -> Option<Scalar> {
self.elements
.as_ref()
.and_then(|l| l.get(idx))
.map(|value| Scalar {
dtype: self.element_dtype(),
dtype: self.element_dtype().clone(),
value: value.clone(),
})
}
Expand All @@ -89,7 +89,7 @@ impl<'a> ListScalar<'a> {
self.elements.as_ref().map(|elems| {
elems
.iter()
.map(|e| Scalar::new(self.element_dtype(), e.clone()))
.map(|e| Scalar::new(self.element_dtype().clone(), e.clone()))
.collect_vec()
})
}
Expand Down
Loading