diff --git a/crates/polars-expr/src/expressions/cast.rs b/crates/polars-expr/src/expressions/cast.rs index 43301aff7189..ebfd50311918 100644 --- a/crates/polars-expr/src/expressions/cast.rs +++ b/crates/polars-expr/src/expressions/cast.rs @@ -13,14 +13,7 @@ pub struct CastExpr { impl CastExpr { fn finish(&self, input: &Series) -> PolarsResult { - let dtype = if input.dtype().is_list() && !self.dtype.is_nested() { - // Necessary for expressions that e.g. add UInt8 to List[Int64] to - // work. - &input.dtype().cast_leaf(self.dtype.clone()) - } else { - &self.dtype - }; - input.cast_with_options(dtype, self.options) + input.cast_with_options(&self.dtype, self.options) } } diff --git a/crates/polars-plan/src/plans/conversion/type_coercion/binary.rs b/crates/polars-plan/src/plans/conversion/type_coercion/binary.rs index 7ee2282b0da9..37d58e004ab1 100644 --- a/crates/polars-plan/src/plans/conversion/type_coercion/binary.rs +++ b/crates/polars-plan/src/plans/conversion/type_coercion/binary.rs @@ -56,11 +56,12 @@ fn process_list_arithmetic( expr_arena: &mut Arena, ) -> PolarsResult> { match (&type_left, &type_right) { - (DataType::List(inner), _) => { - if type_right != **inner { + (DataType::List(_), _) => { + let leaf = type_left.leaf_dtype(); + if type_right != *leaf { let new_node_right = expr_arena.add(AExpr::Cast { expr: node_right, - dtype: *inner.clone(), + dtype: type_left.cast_leaf(leaf.clone()), options: CastOptions::NonStrict, }); @@ -73,11 +74,12 @@ fn process_list_arithmetic( Ok(None) } }, - (_, DataType::List(inner)) => { - if type_left != **inner { + (_, DataType::List(_)) => { + let leaf = type_right.leaf_dtype(); + if type_left != *leaf { let new_node_left = expr_arena.add(AExpr::Cast { expr: node_left, - dtype: *inner.clone(), + dtype: type_right.cast_leaf(leaf.clone()), options: CastOptions::NonStrict, });