1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
use std::hash::Hash;
use crate::prelude::*;
use crate::utils::{try_get_supertype, CustomIterTools};
unsafe fn is_in_helper<T, P>(ca: &ChunkedArray<T>, other: &Series) -> PolarsResult<BooleanChunked>
where
T: PolarsNumericType,
P: Eq + Hash + Copy,
{
let mut set = PlHashSet::with_capacity(other.len());
let other = ca.unpack_series_matching_type(other)?;
other.downcast_iter().for_each(|iter| {
iter.into_iter().for_each(|opt_val| {
// Safety
// bit sizes are/ should be equal
let ptr = &opt_val.copied() as *const Option<T::Native> as *const Option<P>;
let opt_val = *ptr;
set.insert(opt_val);
})
});
let name = ca.name();
let mut ca: BooleanChunked = ca
.into_iter()
.map(|opt_val| {
// Safety
// bit sizes are/ should be equal
let ptr = &opt_val as *const Option<T::Native> as *const Option<P>;
let opt_val = *ptr;
set.contains(&opt_val)
})
.collect_trusted();
ca.rename(name);
Ok(ca)
}
impl<T> IsIn for ChunkedArray<T>
where
T: PolarsNumericType,
{
fn is_in(&self, other: &Series) -> PolarsResult<BooleanChunked> {
// We check implicitly cast to supertype here
match other.dtype() {
DataType::List(dt) => {
let st = try_get_supertype(self.dtype(), dt)?;
if &st != self.dtype() {
let left = self.cast(&st)?;
let right = other.cast(&DataType::List(Box::new(st)))?;
return left.is_in(&right);
}
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().unpack::<T>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.collect_trusted()
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<T>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
_ => {
// first make sure that the types are equal
let st = try_get_supertype(self.dtype(), other.dtype())?;
if self.dtype() != other.dtype() {
let left = self.cast(&st)?;
let right = other.cast(&st)?;
return left.is_in(&right);
}
// now that the types are equal, we coerce every 32 bit array to u32
// and every 64 bit array to u64 (including floats)
// this allows hashing them and greatly reduces the number of code paths.
match self.dtype() {
DataType::UInt64 | DataType::Int64 | DataType::Float64 => unsafe {
is_in_helper::<T, u64>(self, other)
},
DataType::UInt32 | DataType::Int32 | DataType::Float32 => unsafe {
is_in_helper::<T, u32>(self, other)
},
DataType::UInt8 | DataType::Int8 => unsafe {
is_in_helper::<T, u8>(self, other)
},
DataType::UInt16 | DataType::Int16 => unsafe {
is_in_helper::<T, u16>(self, other)
},
dt => polars_bail!(opq = is_in, dt),
}
}
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
impl IsIn for Utf8Chunked {
fn is_in(&self, other: &Series) -> PolarsResult<BooleanChunked> {
match other.dtype() {
#[cfg(feature = "dtype-categorical")]
DataType::List(dt) if matches!(&**dt, DataType::Categorical(_)) => {
if let DataType::Categorical(Some(rev_map)) = &**dt {
let opt_val = self.get(0);
let other = other.list()?;
match opt_val {
None => {
let mut ca: BooleanChunked = other
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| s.as_ref().null_count() > 0) == Some(true)
})
.collect_trusted();
ca.rename(self.name());
Ok(ca)
}
Some(value) => {
match rev_map.find(value) {
// all false
None => Ok(BooleanChunked::full(self.name(), false, other.len())),
Some(idx) => {
let mut ca: BooleanChunked = other
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let s = s.as_ref().to_physical_repr();
let ca = s.as_ref().u32().unwrap();
if ca.null_count() == 0 {
ca.into_no_null_iter().any(|a| a == idx)
} else {
ca.into_iter().any(|a| a == Some(idx))
}
}) == Some(true)
})
.collect_trusted();
ca.rename(self.name());
Ok(ca)
}
}
}
}
} else {
unreachable!()
}
}
DataType::List(dt) if DataType::Utf8 == **dt => self.as_binary().is_in(
&other
.cast(&DataType::List(Box::new(DataType::Binary)))
.unwrap(),
),
DataType::Utf8 => self
.as_binary()
.is_in(&other.cast(&DataType::Binary).unwrap()),
_ => polars_bail!(opq = is_in, self.dtype(), other.dtype()),
}
}
}
impl IsIn for BinaryChunked {
fn is_in(&self, other: &Series) -> PolarsResult<BooleanChunked> {
match other.dtype() {
DataType::List(dt) if DataType::Binary == **dt => {
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
other
.list()?
.amortized_iter()
.map(|opt_b| {
opt_b.map(|s| {
let ca = s.as_ref().unpack::<BinaryType>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.collect_trusted()
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<BinaryType>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
DataType::Binary => {
let mut set = PlHashSet::with_capacity(other.len());
let other = other.binary()?;
other.downcast_iter().for_each(|iter| {
iter.into_iter().for_each(|opt_val| {
set.insert(opt_val);
})
});
let mut ca: BooleanChunked = self
.into_iter()
.map(|opt_val| set.contains(&opt_val))
.collect_trusted();
ca.rename(self.name());
Ok(ca)
}
_ => polars_bail!(opq = is_in, self.dtype(), other.dtype()),
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
impl IsIn for BooleanChunked {
fn is_in(&self, other: &Series) -> PolarsResult<BooleanChunked> {
match other.dtype() {
DataType::List(dt) if self.dtype() == &**dt => {
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let value = self.get(0);
// safety: we know the iterators len
unsafe {
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().unpack::<BooleanType>().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.trust_my_length(other.len())
.collect_trusted()
}
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().unpack::<BooleanType>().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect_trusted()
};
ca.rename(self.name());
Ok(ca)
}
DataType::Boolean => {
let other = other.bool().unwrap();
let has_true = other.any();
let has_false = !other.all();
Ok(self.apply(|v| if v { has_true } else { has_false }))
}
_ => polars_bail!(opq = is_in, self.dtype(), other.dtype()),
}
.map(|mut ca| {
ca.rename(self.name());
ca
})
}
}
#[cfg(feature = "dtype-struct")]
impl IsIn for StructChunked {
fn is_in(&self, other: &Series) -> PolarsResult<BooleanChunked> {
match other.dtype() {
DataType::List(_) => {
let mut ca: BooleanChunked = if self.len() == 1 && other.len() != 1 {
let mut value = vec![];
let left = self.clone().into_series();
let av = left.get(0).unwrap();
if let AnyValue::Struct(_, _, _) = av {
av._materialize_struct_av(&mut value);
}
other
.list()?
.amortized_iter()
.map(|opt_s| {
opt_s.map(|s| {
let ca = s.as_ref().struct_().unwrap();
ca.into_iter().any(|a| a == value)
}) == Some(true)
})
.collect()
} else {
self.into_iter()
.zip(other.list()?.amortized_iter())
.map(|(value, series)| match (value, series) {
(val, Some(series)) => {
let ca = series.as_ref().struct_().unwrap();
ca.into_iter().any(|a| a == val)
}
_ => false,
})
.collect()
};
ca.rename(self.name());
Ok(ca)
}
_ => {
let other = other.cast(&other.dtype().to_physical()).unwrap();
let other = other.struct_()?;
polars_ensure!(
self.fields().len() == other.fields().len(),
ComputeError: "`is_in`: mismatch in the number of struct fields: {} and {}",
self.fields().len(), other.fields().len()
);
let mut anyvalues = Vec::with_capacity(other.len() * other.fields().len());
// Safety:
// the iterator is unsafe as the lifetime is tied to the iterator
// so we copy to an owned buffer first
other.into_iter().for_each(|val| {
anyvalues.extend_from_slice(val);
});
// then we fill the set
let mut set = PlHashSet::with_capacity(other.len());
for key in anyvalues.chunks_exact(other.fields().len()) {
set.insert(key);
}
// physical self
let self_ca = self.cast(&self.dtype().to_physical()).unwrap();
let self_ca = self_ca.struct_().unwrap();
// and then we check for membership
let mut ca: BooleanChunked = self_ca
.into_iter()
.map(|vals| set.contains(&vals))
.collect();
ca.rename(self.name());
Ok(ca)
}
}
}
}
#[cfg(test)]
mod test {
use crate::prelude::*;
#[test]
fn test_is_in() -> PolarsResult<()> {
let a = Int32Chunked::new("a", &[1, 2, 3, 4]);
let b = Int64Chunked::new("b", &[4, 5, 1]);
let out = a.is_in(&b.into_series())?;
assert_eq!(
Vec::from(&out),
[Some(true), Some(false), Some(false), Some(true)]
);
let a = Utf8Chunked::new("a", &["a", "b", "c", "d"]);
let b = Utf8Chunked::new("b", &["d", "e", "c"]);
let out = a.is_in(&b.into_series())?;
assert_eq!(
Vec::from(&out),
[Some(false), Some(false), Some(true), Some(true)]
);
Ok(())
}
}