Skip to content

Commit 129c9f7

Browse files
committed
refactor: Consolidate struct casting logic into nested_struct module
Remove duplicate struct_cast.rs module and use the existing nested_struct::cast_struct_column implementation instead. This eliminates code duplication and provides a single source of truth for struct field-by-name casting logic. Changes: - Add public cast_struct_array_by_name wrapper in nested_struct.rs - Update columnar_value.rs to use nested_struct::cast_struct_array_by_name - Update scalar/mod.rs to use nested_struct::cast_struct_array_by_name - Remove struct_cast module from lib.rs - Delete datafusion/common/src/struct_cast.rs Benefits: - Single implementation to maintain and test - Consistent behavior across all struct casting operations - Reduced maintenance burden for future bug fixes - Better code cohesion in nested_struct module
1 parent 67d2659 commit 129c9f7

File tree

5 files changed

+28
-130
lines changed

5 files changed

+28
-130
lines changed

datafusion/common/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ pub mod rounding;
5757
pub mod scalar;
5858
pub mod spans;
5959
pub mod stats;
60-
pub mod struct_cast;
6160
pub mod test_util;
6261
pub mod tree_node;
6362
pub mod types;

datafusion/common/src/nested_struct.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ pub fn cast_column(
165165
}
166166
}
167167

168+
/// Cast a struct array to another struct type by aligning child arrays using
169+
/// field names instead of their physical order.
170+
///
171+
/// This is a convenience wrapper around [`cast_struct_column`] that accepts
172+
/// `Fields` directly instead of requiring a `Field` wrapper.
173+
///
174+
/// See [`cast_column`] for detailed documentation on the casting behavior.
175+
///
176+
/// # Arguments
177+
/// * `array` - The source array to cast (must be a struct array)
178+
/// * `target_fields` - The target struct field definitions
179+
/// * `cast_options` - Options controlling cast behavior (strictness, formatting)
180+
///
181+
/// # Returns
182+
/// A `Result<ArrayRef>` containing the cast struct array
183+
///
184+
/// # Errors
185+
/// Returns an error if the source is not a struct array or if field casting fails
186+
pub fn cast_struct_array_by_name(
187+
array: &ArrayRef,
188+
target_fields: &arrow::datatypes::Fields,
189+
cast_options: &CastOptions,
190+
) -> Result<ArrayRef> {
191+
cast_struct_column(array, target_fields.as_ref(), cast_options)
192+
}
193+
168194
/// Validates compatibility between source and target struct fields for casting operations.
169195
///
170196
/// This function implements comprehensive struct compatibility checking by examining:

datafusion/common/src/scalar/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3708,7 +3708,7 @@ impl ScalarValue {
37083708
// Use name-based struct casting for struct types
37093709
let cast_arr = match (scalar_array.data_type(), target_type) {
37103710
(DataType::Struct(_), DataType::Struct(target_fields)) => {
3711-
crate::struct_cast::cast_struct_array_by_name(
3711+
crate::nested_struct::cast_struct_array_by_name(
37123712
&scalar_array,
37133713
target_fields,
37143714
cast_options,

datafusion/common/src/struct_cast.rs

Lines changed: 0 additions & 127 deletions
This file was deleted.

datafusion/expr-common/src/columnar_value.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn cast_array_by_name(
325325

326326
match (array.data_type(), cast_type) {
327327
(DataType::Struct(_source_fields), DataType::Struct(target_fields)) => {
328-
datafusion_common::struct_cast::cast_struct_array_by_name(
328+
datafusion_common::nested_struct::cast_struct_array_by_name(
329329
array,
330330
target_fields,
331331
cast_options,

0 commit comments

Comments
 (0)