Repository: pola-rs/polars
File: crates/polars-expr/src/reduce/first_last.rs (Rust)

Code before:

    fn take_evictions(&mut self) -> Box<dyn GroupedReduction> {
        Box::new(Self {
            in_dtype: self.in_dtype.clone(),
            policy: self.policy,
            values: core::mem::take(&mut self.evicted_values),
            seqs: core::mem::take(&mut self.evicted_seqs),
            counts: core::mem::take(&mut self.evicted_counts),
            evicted_values: Vec::new(),
            evicted_seqs: Vec::new(),
            evicted_counts: Vec::new(),
        })
    }

    fn finalize(&mut self) -> PolarsResult<Series> {
        self.seqs.clear();
        if let Some(allow_empty) = self.policy.item_policy() {
            for count in self.counts.iter() {
                polars_ensure!(
                    (allow_empty && *count == 0) || *count == 1,
                    item_agg_count_not_one = *count,
                    allow_empty = allow_empty
                );
            }
        }
        let s = if self.in_dtype.is_nested() {
            let mut buf = AnyValueBuffer::new(&self.in_dtype, self.values.len());
            for v in core::mem::take(&mut self.values) {
                buf.add(v);
            }
            buf.into_series()
        } else {
            let mut buf = AnyValueBufferTrusted::new(&self.in_dtype, self.values.len());
            for v in core::mem::take(&mut self.values) {
                // SAFETY: v is not nested.
                unsafe { buf.add_unchecked_owned_physical(&v) };
            }
            buf.into_series()
        };
        Ok(s)
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

fn check_item_count_is_one<T: Clone>(v: &[Value<T>], allow_empty: bool) -> PolarsResult<()> {
    if let Some(Value { count: n, .. }) = v
        .iter()
        .find(|v| !(allow_empty && v.count == 0) && v.count != 1)

Review comment (suggestion):
I don't understand why this branch is necessary. `add_unchecked_owned_physical` already has a path matching for structs, so why does it not work? I'd rather you fix the underlying issue than hide the symptoms here.

Code after:

    fn take_evictions(&mut self) -> Box<dyn GroupedReduction> {
        Box::new(Self {
            in_dtype: self.in_dtype.clone(),
            policy: self.policy,
            values: core::mem::take(&mut self.evicted_values),
            seqs: core::mem::take(&mut self.evicted_seqs),
            counts: core::mem::take(&mut self.evicted_counts),
            evicted_values: Vec::new(),
            evicted_seqs: Vec::new(),
            evicted_counts: Vec::new(),
        })
    }

    fn finalize(&mut self) -> PolarsResult<Series> {
        self.seqs.clear();
        if let Some(allow_empty) = self.policy.item_policy() {
            for count in self.counts.iter() {
                polars_ensure!(
                    (allow_empty && *count == 0) || *count == 1,
                    item_agg_count_not_one = *count,
                    allow_empty = allow_empty
                );
            }
        }
        let phys_type = self.in_dtype.to_physical();
        let mut buf = AnyValueBufferTrusted::new(&phys_type, self.values.len());
        for v in core::mem::take(&mut self.values) {
            // SAFETY: v is cast to physical.
            unsafe { buf.add_unchecked_owned_physical(&v.to_physical()) };
        }
        // SAFETY: dtype is valid for series.
        unsafe { buf.into_series().from_physical_unchecked(&self.in_dtype) }
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

fn check_item_count_is_one<T: Clone>(v: &[Value<T>], allow_empty: bool) -> PolarsResult<()> {
    if let Some(Value { count: n, .. }) = v
        .iter()
        .find(|v| !(allow_empty && v.count == 0) && v.count != 1)
    {
        polars_bail!(item_agg_count_not_one = *n, allow_empty = allow_empty);
    }
    Ok(())
}