Skip to content

Commit 0159b45

Browse files
dancsiclaude
andauthored
refactor: Hold the category mapping in the categorical is_in lookup (#29747)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 29995b3 commit 0159b45

1 file changed

Lines changed: 27 additions & 4 deletions

File tree

  • crates/polars-ops/src/series/ops/is_in

‎crates/polars-ops/src/series/ops/is_in/mod.rs‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ enum Lookup {
6767
/// Only the null count of the haystack matters.
6868
NullNeedle,
6969
Primitive(Box<dyn PrimitiveProbe>),
70+
#[cfg(feature = "dtype-categorical")]
71+
Categorical {
72+
mapping: Arc<CategoricalMapping>,
73+
lookup: Box<dyn PrimitiveProbe>,
74+
},
7075
Binary(BinaryLookup),
7176
RowEncoded(RowEncodedLookup),
7277
Boolean {
@@ -119,11 +124,15 @@ impl IsInHaystack {
119124

120125
let lookup = match needle_dtype {
121126
#[cfg(feature = "dtype-categorical")]
122-
dt @ (DataType::Categorical(_, _) | DataType::Enum(_, _)) => {
123-
with_match_categorical_physical_type!(dt.cat_physical().unwrap(), |$C| {
127+
dt @ (DataType::Categorical(_, mapping) | DataType::Enum(_, mapping)) => {
128+
let lookup = with_match_categorical_physical_type!(dt.cat_physical().unwrap(), |$C| {
124129
let phys = categorical_haystack::<$C>(dt, haystack.dtype(), &flat)?;
125-
Lookup::Primitive(Box::new(PrimitiveLookup::<<$C as PolarsCategoricalType>::PolarsPhysical>::new(&phys)))
126-
})
130+
Box::new(PrimitiveLookup::<<$C as PolarsCategoricalType>::PolarsPhysical>::new(&phys)) as Box<dyn PrimitiveProbe>
131+
});
132+
Lookup::Categorical {
133+
mapping: mapping.clone(),
134+
lookup,
135+
}
127136
},
128137
DataType::String => {
129138
let flat = match flat.dtype() {
@@ -222,6 +231,20 @@ impl IsInHaystack {
222231
Lookup::Primitive(lookup) => {
223232
lookup.probe_series(&needle.to_physical_repr(), nulls_equal, has_null)
224233
},
234+
#[cfg(feature = "dtype-categorical")]
235+
Lookup::Categorical { mapping, lookup } => {
236+
let (DataType::Categorical(_, needle_mapping) | DataType::Enum(_, needle_mapping)) =
237+
needle.dtype()
238+
else {
239+
unreachable!()
240+
};
241+
polars_ensure!(
242+
Arc::ptr_eq(mapping, needle_mapping),
243+
SchemaMismatch: "is_in: needle of dtype {} uses another category mapping",
244+
needle.dtype()
245+
);
246+
lookup.probe_series(&needle.to_physical_repr(), nulls_equal, has_null)
247+
},
225248
Lookup::Binary(lookup) => match needle.dtype() {
226249
DataType::String => {
227250
lookup.probe(&needle.str().unwrap().as_binary(), nulls_equal, has_null)

0 commit comments

Comments
 (0)