Skip to content

Commit 4130e8d

Browse files
fix[array]: dict value nullable values (#6387)
Can have non-accessible value which cannot be casted to non-nullable Signed-off-by: Joe Isaacs <[email protected]>
1 parent f5bd84f commit 4130e8d

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

  • vortex-array/src/arrays/dict/compute

‎vortex-array/src/arrays/dict/compute/cast.rs‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ use crate::register_kernel;
1616

1717
impl CastKernel for DictVTable {
1818
fn cast(&self, array: &DictArray, dtype: &DType) -> VortexResult<Option<ArrayRef>> {
19+
// Can have un-reference null values making the cast of values fail without a possible mask.
20+
// TODO(joe): optimize this, could look at accessible values and fill_null not those?
21+
if !dtype.is_nullable()
22+
&& array.values().dtype().is_nullable()
23+
&& !array.values().all_valid()?
24+
{
25+
let canonical = array.to_canonical()?.into_array();
26+
return Ok(Some(cast(canonical.as_ref(), dtype)?));
27+
}
28+
1929
// Cast the dictionary values to the target type
2030
let casted_values = cast(array.values(), dtype)?;
2131

@@ -188,4 +198,46 @@ mod tests {
188198
fn test_cast_dict_conformance(#[case] array: crate::ArrayRef) {
189199
test_cast_conformance(array.as_ref());
190200
}
201+
202+
#[test]
203+
fn test_cast_dict_with_unreferenced_null_values_to_nonnullable() {
204+
use crate::arrays::dict::DictArray;
205+
use crate::validity::Validity;
206+
207+
// Create a dict with nullable values that have unreferenced null entries.
208+
// Values: [1.0, null, 3.0] (index 1 is null but no code points to it)
209+
// Codes: [0, 2, 0] (only reference indices 0 and 2, never 1)
210+
let values = PrimitiveArray::new(
211+
buffer![1.0f64, 0.0f64, 3.0f64],
212+
Validity::from(vortex_buffer::BitBuffer::from(vec![true, false, true])),
213+
)
214+
.into_array();
215+
let codes = buffer![0u32, 2, 0].into_array();
216+
let dict = DictArray::try_new(codes, values).unwrap();
217+
218+
// The dict is Nullable (because values are nullable), but all codes point to valid values.
219+
assert_eq!(
220+
dict.dtype(),
221+
&DType::Primitive(PType::F64, Nullability::Nullable)
222+
);
223+
224+
// Casting to NonNullable should succeed since all logical values are non-null.
225+
let result = cast(
226+
dict.as_ref(),
227+
&DType::Primitive(PType::F64, Nullability::NonNullable),
228+
);
229+
assert!(
230+
result.is_ok(),
231+
"cast to NonNullable should succeed for dict with only unreferenced null values"
232+
);
233+
let casted = result.unwrap();
234+
assert_eq!(
235+
casted.dtype(),
236+
&DType::Primitive(PType::F64, Nullability::NonNullable)
237+
);
238+
assert_arrays_eq!(
239+
casted.to_primitive(),
240+
PrimitiveArray::from_iter([1.0f64, 3.0, 1.0])
241+
);
242+
}
191243
}

0 commit comments

Comments
 (0)