From f7a60ffb64eb7cd61a503de1369767a8bc989a1e Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 2 Dec 2024 06:23:07 -0500 Subject: [PATCH 1/2] fix: Materialize smallest dyn ints to use feature gate for i8/i16 --- crates/polars-core/src/utils/supertype.rs | 52 ++++++++++++----------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/crates/polars-core/src/utils/supertype.rs b/crates/polars-core/src/utils/supertype.rs index 4e30a8da7904..ce0d5fbb63b8 100644 --- a/crates/polars-core/src/utils/supertype.rs +++ b/crates/polars-core/src/utils/supertype.rs @@ -465,35 +465,39 @@ pub fn materialize_dyn_int(v: i128) -> AnyValue<'static> { fn materialize_dyn_int_pos(v: i128) -> AnyValue<'static> { // Try to get the "smallest" fitting value. // TODO! next breaking go to true smallest. - match u8::try_from(v).ok() { - Some(v) => AnyValue::UInt8(v), - None => match u16::try_from(v).ok() { - Some(v) => AnyValue::UInt16(v), - None => match u32::try_from(v).ok() { - Some(v) => AnyValue::UInt32(v), - None => match u64::try_from(v).ok() { - Some(v) => AnyValue::UInt64(v), - None => AnyValue::Null, - }, - }, + #[cfg(feature = "dtype-u8")] + if let Some(v) = u8::try_from(v).ok() { + return AnyValue::UInt8(v); + } + #[cfg(feature = "dtype-u16")] + if let Some(v) = u16::try_from(v).ok() { + return AnyValue::UInt16(v); + } + match u32::try_from(v).ok() { + Some(v) => AnyValue::UInt32(v), + None => match u64::try_from(v).ok() { + Some(v) => AnyValue::UInt64(v), + None => AnyValue::Null, }, } } fn materialize_smallest_dyn_int(v: i128) -> AnyValue<'static> { - match i8::try_from(v).ok() { - Some(v) => AnyValue::Int8(v), - None => match i16::try_from(v).ok() { - Some(v) => AnyValue::Int16(v), - None => match i32::try_from(v).ok() { - Some(v) => AnyValue::Int32(v), - None => match i64::try_from(v).ok() { - Some(v) => AnyValue::Int64(v), - None => match u64::try_from(v).ok() { - Some(v) => AnyValue::UInt64(v), - None => AnyValue::Null, - }, - }, + #[cfg(feature = "dtype-i8")] + if let Some(v) = i8::try_from(v).ok() { + return AnyValue::Int8(v); + } + #[cfg(feature = "dtype-i16")] + if let Some(v) = i16::try_from(v).ok() { + return AnyValue::Int16(v); + } + match i32::try_from(v).ok() { + Some(v) => AnyValue::Int32(v), + None => match i64::try_from(v).ok() { + Some(v) => AnyValue::Int64(v), + None => match u64::try_from(v).ok() { + Some(v) => AnyValue::UInt64(v), + None => AnyValue::Null, }, }, } From edcd377b4ec768f28ab93048c757fe8bf4b819c2 Mon Sep 17 00:00:00 2001 From: Luke Manley Date: Mon, 2 Dec 2024 06:33:02 -0500 Subject: [PATCH 2/2] fix --- crates/polars-core/src/utils/supertype.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/polars-core/src/utils/supertype.rs b/crates/polars-core/src/utils/supertype.rs index ce0d5fbb63b8..fad3299d5891 100644 --- a/crates/polars-core/src/utils/supertype.rs +++ b/crates/polars-core/src/utils/supertype.rs @@ -466,11 +466,11 @@ fn materialize_dyn_int_pos(v: i128) -> AnyValue<'static> { // Try to get the "smallest" fitting value. // TODO! next breaking go to true smallest. #[cfg(feature = "dtype-u8")] - if let Some(v) = u8::try_from(v).ok() { + if let Ok(v) = u8::try_from(v) { return AnyValue::UInt8(v); } #[cfg(feature = "dtype-u16")] - if let Some(v) = u16::try_from(v).ok() { + if let Ok(v) = u16::try_from(v) { return AnyValue::UInt16(v); } match u32::try_from(v).ok() { @@ -484,11 +484,11 @@ fn materialize_dyn_int_pos(v: i128) -> AnyValue<'static> { fn materialize_smallest_dyn_int(v: i128) -> AnyValue<'static> { #[cfg(feature = "dtype-i8")] - if let Some(v) = i8::try_from(v).ok() { + if let Ok(v) = i8::try_from(v) { return AnyValue::Int8(v); } #[cfg(feature = "dtype-i16")] - if let Some(v) = i16::try_from(v).ok() { + if let Ok(v) = i16::try_from(v) { return AnyValue::Int16(v); } match i32::try_from(v).ok() {