Skip to content

Commit baf0d39

Browse files
authored
chore: explicit error if struct is different from schema (#3589)
Spent 10 mins trying to debug an error which could have been prevented with a better error. We'll raise earlier if target schema has a different size from the num of fields in the struct --------- Signed-off-by: blaginin <[email protected]>
1 parent eeb1ffe commit baf0d39

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

vortex-array/src/arrow/compute/to_arrow/canonical.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ fn to_arrow_decimal256(array: DecimalArray) -> VortexResult<ArrowArrayRef> {
258258
}
259259

260260
fn to_arrow_struct(array: StructArray, fields: &[FieldRef]) -> VortexResult<ArrowArrayRef> {
261+
if array.fields().len() != fields.len() {
262+
vortex_bail!(
263+
"StructArray has {} fields, but target Arrow type has {} fields",
264+
array.fields().len(),
265+
fields.len()
266+
);
267+
}
268+
261269
let field_arrays = fields
262270
.iter()
263271
.zip_eq(array.fields())
@@ -438,6 +446,31 @@ mod tests {
438446
assert!(struct_a.into_array().into_arrow(&arrow_dt).is_err());
439447
}
440448

449+
#[test]
450+
fn struct_to_arrow_with_schema_missmatch() {
451+
let xs = PrimitiveArray::new(buffer![0i64, 1, 2, 3, 4], Validity::AllValid);
452+
453+
let struct_a = StructArray::try_new(
454+
FieldNames::from(["xs".into()]),
455+
vec![xs.into_array()],
456+
5,
457+
Validity::AllValid,
458+
)
459+
.unwrap();
460+
461+
let fields = vec![
462+
Field::new("xs", DataType::Int8, false),
463+
Field::new("ys", DataType::Int64, false),
464+
];
465+
let arrow_dt = DataType::Struct(fields.into());
466+
467+
let err = struct_a.into_array().into_arrow(&arrow_dt).err().unwrap();
468+
assert!(
469+
err.to_string()
470+
.contains("StructArray has 1 fields, but target Arrow type has 2 fields")
471+
);
472+
}
473+
441474
#[rstest]
442475
#[case(0i8)]
443476
#[case(0i16)]

0 commit comments

Comments
 (0)