cast_struct_column

Function cast_struct_column 

Source
fn cast_struct_column(
    source_col: &ArrayRef,
    target_fields: &[Arc<Field>],
    cast_options: &CastOptions<'_>,
) -> Result<ArrayRef>
Expand description

Cast a struct column to match target struct fields, handling nested structs recursively.

This function implements struct-to-struct casting with the assumption that structs should always be allowed to cast to other structs. However, the source column must already be a struct type - non-struct sources will result in an error.

§Field Matching Strategy

  • By Name: Source struct fields are matched to target fields by name (case-sensitive)
  • Type Adaptation: When a matching field is found, it is recursively cast to the target field’s type
  • Missing Fields: Target fields not present in the source are filled with null values
  • Extra Fields: Source fields not present in the target are ignored

§Nested Struct Handling

  • Nested structs are handled recursively using the same casting rules
  • Each level of nesting follows the same field matching and null-filling strategy
  • This allows for complex struct transformations while maintaining data integrity

§Arguments

  • source_col - The source array to cast (must be a struct array)
  • target_fields - The target struct field definitions to cast to

§Returns

A Result<ArrayRef> containing the cast struct array

§Errors

Returns a DataFusionError::Plan if the source column is not a struct type