use itertools::Itertools;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use crate::ArrayRef;
use crate::ExecutionCtx;
use crate::IntoArray;
use crate::array::ArrayView;
use crate::arrays::ConstantArray;
use crate::arrays::Struct;
use crate::arrays::StructArray;
use crate::arrays::struct_::StructArrayExt;
use crate::builtins::ArrayBuiltins;
use crate::dtype::DType;
use crate::scalar::Scalar;
use crate::scalar_fn::fns::cast::CastKernel;
impl CastKernel for Struct {
fn cast(
array: ArrayView<'_, Struct>,
dtype: &DType,
_ctx: &mut ExecutionCtx,
) -> VortexResult<Option<ArrayRef>> {
let Some(target_sdtype) = dtype.as_struct_fields_opt() else {
return Ok(None);
};
let source_sdtype = array.struct_fields();
let fields_match_order = target_sdtype.nfields() == source_sdtype.nfields()
&& target_sdtype
.names()
.iter()
.zip(source_sdtype.names().iter())
.all(|(f1, f2)| f1 == f2);
let mut cast_fields = Vec::with_capacity(target_sdtype.nfields());
if fields_match_order {
for (field, target_type) in array.iter_unmasked_fields().zip_eq(target_sdtype.fields())
{
let cast_field = field.cast(target_type)?;
cast_fields.push(cast_field);
}
} else {
for (target_name, target_type) in
target_sdtype.names().iter().zip_eq(target_sdtype.fields())
{
match source_sdtype.find(target_name) {
None => {
vortex_ensure!(
target_type.is_nullable(),
"CAST for struct only supports added nullable fields"
);
cast_fields.push(
ConstantArray::new(Scalar::null(target_type), array.len()).into_array(),
);
}
Some(src_field_idx) => {
let cast_field = array.unmasked_field(src_field_idx).cast(target_type)?;
cast_fields.push(cast_field);
}
}
}
}
let validity = array
.validity()?
.cast_nullability(dtype.nullability(), array.len())?;
StructArray::try_new(
target_sdtype.names().clone(),
cast_fields,
array.len(),
validity,
)
.map(|a| Some(a.into_array()))
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use vortex_buffer::buffer;
use crate::IntoArray;
use crate::ToCanonical;
use crate::arrays::PrimitiveArray;
use crate::arrays::StructArray;
use crate::arrays::VarBinArray;
use crate::arrays::struct_::StructArrayExt;
use crate::builtins::ArrayBuiltins;
use crate::compute::conformance::cast::test_cast_conformance;
use crate::dtype::DType;
use crate::dtype::DecimalDType;
use crate::dtype::FieldNames;
use crate::dtype::Nullability;
use crate::dtype::PType;
use crate::validity::Validity;
#[rstest]
#[case(create_test_struct(false))]
#[case(create_test_struct(true))]
#[case(create_nested_struct())]
#[case(create_simple_struct())]
fn test_cast_struct_conformance(#[case] array: StructArray) {
test_cast_conformance(&array.into_array());
}
fn create_test_struct(nullable: bool) -> StructArray {
let names = FieldNames::from(["a", "b"]);
let a = buffer![1i32, 2, 3].into_array();
let b = VarBinArray::from_iter(
vec![Some("x"), None, Some("z")],
DType::Utf8(Nullability::Nullable),
)
.into_array();
StructArray::try_new(
names,
vec![a, b],
3,
if nullable {
Validity::AllValid
} else {
Validity::NonNullable
},
)
.unwrap()
}
fn create_nested_struct() -> StructArray {
let inner_names = FieldNames::from(["x", "y"]);
let x = buffer![1.0f32, 2.0, 3.0].into_array();
let y = buffer![4.0f32, 5.0, 6.0].into_array();
let inner_struct = StructArray::try_new(inner_names, vec![x, y], 3, Validity::NonNullable)
.unwrap()
.into_array();
let outer_names: FieldNames = ["id", "point"].into();
let ids = buffer![100i64, 200, 300].into_array();
StructArray::try_new(
outer_names,
vec![ids, inner_struct],
3,
Validity::NonNullable,
)
.unwrap()
}
fn create_simple_struct() -> StructArray {
let names = FieldNames::from(["value"]);
let values = buffer![42u8].into_array();
StructArray::try_new(names, vec![values], 1, Validity::NonNullable).unwrap()
}
#[test]
fn cast_nullable_all_invalid() {
let empty_struct = StructArray::try_new(
FieldNames::from(["a"]),
vec![PrimitiveArray::new::<i32>(buffer![], Validity::AllInvalid).into_array()],
0,
Validity::AllInvalid,
)
.unwrap()
.into_array();
let target_dtype = DType::struct_(
[("a", DType::Primitive(PType::I32, Nullability::NonNullable))],
Nullability::NonNullable,
);
let result = empty_struct.cast(target_dtype.clone()).unwrap();
assert_eq!(result.dtype(), &target_dtype);
assert_eq!(result.len(), 0);
}
#[test]
fn cast_duplicate_field_names_to_nullable() {
let names = FieldNames::from(["a", "a"]);
let field1 = buffer![1i32, 2, 3].into_array();
let field2 = buffer![10i64, 20, 30].into_array();
let struct_array =
StructArray::try_new(names, vec![field1, field2], 3, Validity::NonNullable).unwrap();
let target_dtype = struct_array.dtype().as_nullable();
let result = struct_array
.into_array()
.cast(target_dtype.clone())
.unwrap();
assert_eq!(result.dtype(), &target_dtype);
assert_eq!(result.len(), 3);
assert_eq!(result.to_struct().struct_fields().nfields(), 2);
}
#[test]
fn cast_add_fields() {
let names = FieldNames::from(["a", "b"]);
let field1 = buffer![1i32, 2, 3].into_array();
let field2 = buffer![10i64, 20, 30].into_array();
let target_dtype = DType::struct_(
[
("a", field1.dtype().clone()),
("b", field2.dtype().clone()),
(
"c",
DType::Decimal(DecimalDType::new(38, 10), Nullability::Nullable),
),
],
Nullability::NonNullable,
);
let struct_array =
StructArray::try_new(names, vec![field1, field2], 3, Validity::NonNullable).unwrap();
let result = struct_array
.into_array()
.cast(target_dtype.clone())
.unwrap();
assert_eq!(result.dtype(), &target_dtype);
assert_eq!(result.len(), 3);
assert_eq!(result.to_struct().struct_fields().nfields(), 3);
}
}