Skip to content

Commit c46f419

Browse files
authored
fix(cast): Trying to fix cast losting schema problem (#10005)
# Which issue does this PR close? - Closes #10004 . # Rationale for this change Previously, just `data_type` is considered. Now the field is taking into account. # What changes are included in this PR? Previously, just `data_type` is considered. Now the field is taking into account. # Are these changes tested? Yes # Are there any user-facing changes? Maybe cast would be a bit more strict
1 parent f790721 commit c46f419

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

arrow-cast/src/cast/list.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn cast_fixed_size_list_to_list_inner<OffsetSize: OffsetSizeTrait, const IS_LIST
102102
let DataType::FixedSizeList(inner_field, size) = array.data_type() else {
103103
unreachable!()
104104
};
105-
let array = if to.data_type() != inner_field.data_type() {
105+
let array = if to != inner_field {
106106
// To transform inner type, can first cast to FSL with new inner type.
107107
let fsl_to = DataType::FixedSizeList(to.clone(), *size);
108108
let array = cast_with_options(array, &fsl_to, cast_options)?;

arrow-cast/src/cast/mod.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9038,6 +9038,41 @@ mod tests {
90389038
}
90399039
}
90409040

9041+
#[test]
9042+
fn test_cast_fixed_size_list_to_list_preserves_field_metadata() {
9043+
use std::collections::HashMap;
9044+
9045+
let metadata: HashMap<String, String> =
9046+
HashMap::from([("PARQUET:field_id".to_string(), "89".to_string())]);
9047+
9048+
let src = Arc::new(
9049+
FixedSizeListArray::from_iter_primitive::<Float32Type, _, _>(
9050+
[[1.0_f32, 2.0].map(Some), [3.0, 4.0].map(Some)].map(Some),
9051+
2,
9052+
),
9053+
) as ArrayRef;
9054+
9055+
let target_field = Arc::new(
9056+
Field::new("element", DataType::Float32, true).with_metadata(metadata.clone()),
9057+
);
9058+
9059+
let target_types = [
9060+
DataType::List(target_field.clone()),
9061+
DataType::LargeList(target_field.clone()),
9062+
DataType::ListView(target_field.clone()),
9063+
DataType::LargeListView(target_field.clone()),
9064+
];
9065+
9066+
for target_type in &target_types {
9067+
let result = cast(&src, target_type).unwrap();
9068+
assert_eq!(
9069+
result.data_type(),
9070+
target_type,
9071+
"Cast to {target_type:?} should preserve field metadata"
9072+
);
9073+
}
9074+
}
9075+
90419076
#[test]
90429077
fn test_cast_utf8_to_list() {
90439078
// DataType::List

0 commit comments

Comments
 (0)