Repository: pola-rs/polars
File: crates/polars-ops/src/chunked_array/binary/namespace.rs (Rust)

Code before:
                        ca.chunks().iter().map(|chunk| {
                            cast_binview_to_primitive_dyn::<$T>(
                                &**chunk,
                                &arrow_data_type,
                                is_little_endian,
                            )
                        }).collect()
                    }
                })
            },
            #[cfg(feature = "dtype-array")]
            PhysicalType::FixedSizeList => {
                if matches!(dtype.inner_dtype(), Some(DataType::Array(..))) {
                    polars_bail!(
                        InvalidOperation:
                        "to cast to a nested Array, first cast to a linear Array, and then use reshape()"
                    );
                }
                let arrow_data_type = dtype.to_arrow(CompatLevel::newest());

                let leaf_physical_type = dtype
                    .leaf_dtype()
                    .to_arrow(CompatLevel::newest())
                    .to_physical_type();
                polars_ensure!(
                    dtype.inner_dtype().map(|dt| dt.to_physical().is_primitive_numeric()) == Some(true),
                    InvalidOperation:
                    "cannot reinterpret from binary dtype to {:?}. When casting to arrays, the inner type must be physically represented by a numeric type.",
                    dtype,
                );
                let PhysicalType::Primitive(primitive_type) = leaf_physical_type else {
                    panic!("Shouldn't ever be reached.")
                };

                let result: Vec<ArrayRef> = with_match_primitive_type!(primitive_type, |$T| {
                    unsafe {
                        ca.chunks().iter().map(|chunk| {
                            cast_binview_to_array_primitive_dyn::<$T>(
                                &**chunk,
                                &arrow_data_type,
                                is_little_endian
                            )
                        }).collect::<Result<Vec<ArrayRef>, _>>()
                    }
                })?;
                Ok(result)
            },
            _ => Err(
                polars_err!(InvalidOperation:"unsupported data type in from_buffer. Only numerical types are allowed."),
            ),
        }

Review comment (suggestion):
There is a lot of logic here continuously checking against the original polars `DataType` in a context where we've already converted to physical types. This is undesirable - we know that `inner_dtype()` is always `Some()` and yet we've lost that invariant guarantee across the layers of code. All of these checks should have been done in an earlier context.



Code after:
                            binview_to_primitive_dyn::<<$T as PolarsNumericType>::Native>(
                                &**chunk,
                                &arrow_data_type,
                                is_little_endian,
                            )
                        }).collect()
                    }
                })
            },
            #[cfg(feature = "dtype-array")]
            DataType::Array(inner_dtype, array_width)
                if inner_dtype.is_primitive_numeric() || inner_dtype.is_temporal() =>
            {
                let inner_dtype = inner_dtype.to_physical();
                let result: Vec<ArrayRef> = with_match_physical_numeric_polars_type!(inner_dtype, |$T| {
                    unsafe {
                        ca.chunks().iter().map(|chunk| {
                            binview_to_fixed_size_list_dyn::<<$T as PolarsNumericType>::Native>(
                                &**chunk,
                                *array_width,
                                is_little_endian
                            )
                        }).collect::<Result<Vec<ArrayRef>, _>>()
                    }
                })?;
                Ok(result)
            },
            _ => Err(
                polars_err!(InvalidOperation: "unsupported data type {:?} in reinterpret. Only numeric or temporal types, or Arrays of those, are allowed.", dtype),
            ),
        }
    }
}

impl BinaryNameSpaceImpl for BinaryChunked {}